All Downloads are FREE. Search and download functionalities are using the official Maven repository.

graphql.nadel.engine.result.ResultComplexityAggregator Maven / Gradle / Ivy

Go to download

Nadel is a Java library that combines multiple GrahpQL services together into one API.

The newest version!
package graphql.nadel.engine.result;

import graphql.Internal;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;


@Internal
public class ResultComplexityAggregator {
    private AtomicInteger totalNodeCount = new AtomicInteger(0);
    private Map serviceNodeCounts = Collections.synchronizedMap(new LinkedHashMap<>());
    private AtomicInteger totalFieldRenameCount = new AtomicInteger(0);
    private AtomicInteger totalTypeRenameCount = new AtomicInteger(0);

    public int getTotalNodeCount() {
        return totalNodeCount.get();
    }
    public int getFieldRenamesCount() { return totalFieldRenameCount.get(); }
    public int getTypeRenamesCount() { return totalTypeRenameCount.get(); }

    public Map getServiceNodeCounts() {
        return serviceNodeCounts;
    }

    public int getNodeCountsForService(String serviceName) {
        return serviceNodeCounts.get(serviceName);
    }

    public void incrementServiceNodeCount(String serviceFieldName, int nodeCount) {
        serviceNodeCounts.compute(serviceFieldName, (k, v) -> (v == null) ? nodeCount : v + nodeCount);
        totalNodeCount.getAndAdd(nodeCount);
    }

    public void incrementFieldRenameCount(int fieldRenameCount) {
        totalFieldRenameCount.getAndAdd(fieldRenameCount);
    }

    public void incrementTypeRenameCount(int typeRenameCount) {
        totalTypeRenameCount.getAndAdd(typeRenameCount);
    }

    public Map snapshotResultComplexityData() {

        Map resultComplexityMap = new LinkedHashMap<>();
        resultComplexityMap.put("totalNodeCount", getTotalNodeCount());
        resultComplexityMap.put("serviceNodeCounts", getServiceNodeCounts());
        resultComplexityMap.put("fieldRenamesCount", getFieldRenamesCount());
        resultComplexityMap.put("typeRenamesCount", getTypeRenamesCount());

        return resultComplexityMap;
    }


    @Override
    public String toString() {
        return "ResultComplexityAggregator{" +
                "totalNodeCount=" + totalNodeCount +
                ", serviceNodeCountsMap=" + serviceNodeCounts +
                ", totalFieldRenameCount=" + totalFieldRenameCount +
                ", totalTypeRenameCount=" + totalTypeRenameCount +
                '}';
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy