Skip to content

Commit

Permalink
changed grouping logic
Browse files Browse the repository at this point in the history
  • Loading branch information
vga91 committed Feb 2, 2024
1 parent 85411f4 commit 1e437a3
Show file tree
Hide file tree
Showing 2 changed files with 248 additions and 80 deletions.
63 changes: 25 additions & 38 deletions extended/src/main/java/apoc/agg/MultiStats.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
package apoc.agg;

import apoc.Extended;
import com.amazonaws.util.NumberUtils;
import org.HdrHistogram.HistogramUtil;
import org.neo4j.graphdb.Entity;
import org.neo4j.kernel.impl.util.ValueUtils;
import org.neo4j.procedure.Description;
import org.neo4j.procedure.Name;
import org.neo4j.procedure.UserAggregationFunction;
import org.neo4j.procedure.UserAggregationResult;
import org.neo4j.procedure.UserAggregationUpdate;
import org.neo4j.values.storable.LongValue;
import org.neo4j.values.AnyValue;
import org.neo4j.values.storable.NumberValue;
import org.neo4j.values.utils.ValueMath;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Extended
public class MultiStats {
Expand Down Expand Up @@ -59,7 +57,7 @@ public static class MultiStatsFunction {
// private DoubleHistogram doubles;
// private List<Double> percentiles = asList(0.5D, 0.75D, 0.9D, 0.95D, 0.9D, 0.99D);
// private Number minValue;
private final Map<String, Map<String, NumberValue>> result = new HashMap<>();
private final Map<String, Map<String, Map<String, NumberValue>>> result = new HashMap<>();

// --> TODO - sum must be similar to https://neo4j.com/docs/cypher-manual/current/functions/aggregating/#functions-sum

Expand All @@ -73,55 +71,44 @@ public void aggregate(
// todo - can be also a map, maybe?
Entity entity = (Entity) value;

// per ogni prop
// for each prop
keys.forEach(key -> {
if (entity.hasProperty(key)) {
Object property = entity.getProperty(key);
// todo - forse metterlo all'esterno

result.compute(key, (ignored, v) -> {
Map<String, NumberValue> map;
if (v == null) {
map = new HashMap<>();
} else {
map = v;
}

NumberValue count = map.compute("count", ((subKey, subVal) -> (NumberValue) ValueUtils.of(subVal == null ? 1 : subVal.longValue() + 1)) );
Map<String, Map<String, NumberValue>> map1 = Objects.requireNonNullElseGet(v, HashMap::new);

if (property instanceof Number propNum) {
// NumberValue
//
NumberValue of = (NumberValue) ValueUtils.of(property);

// ValueMath.overflowSafeAdd(

// ValueMath.overflowSafeAdd( )

// todo - it can be null?
map1.compute(property.toString(), (propKey, propVal) -> {

Map<String, NumberValue> map = Objects.requireNonNullElseGet(propVal, HashMap::new);

// todo - double and long must be different ?
NumberValue sum = map.compute("sum", ((subKey, subVal) -> subVal == null ? of : ValueMath.overflowSafeAdd(subVal, of)));
NumberValue count = map.compute("count", ((subKey, subVal) -> (NumberValue) ValueUtils.of(subVal == null ? 1 : subVal.longValue() + 1)) );

// NB: avg() return always a double
NumberValue avg = map.compute("avg", ((subKey, subVal) -> subVal == null ? of : sum.dividedBy(count.doubleValue()) ));
// NumberValue avg = map.compute("avg", ((subKey, subVal) -> subVal == null ? of : sum.divideBy (count) ));
}
AnyValue of1 = ValueUtils.of(property);

return map;
if (of1 instanceof NumberValue of) {
NumberValue sum = map.compute("sum", ((subKey, subVal) -> subVal == null ? of : ValueMath.overflowSafeAdd(subVal, of)));

// NB: avg() return always a double
NumberValue avg = map.compute("avg", ((subKey, subVal) -> subVal == null ? of : sum.dividedBy(count.doubleValue()) ));
}

return map;
});


return map1;
});

// Map<String, Number> orDefault = result.getOrDefault(key, new HashMap<>());
// orDefault.compute("count", ((k, v) -> v == null ? 1 : v.longValue() + 1));
// System.out.println("orDefault = " + orDefault);
}
});
// value.getProperty()
}

// --> Map<key, Map<key, Map<key, value>> >

@UserAggregationResult
// apoc.agg.multiStats([key1,key2,key3]) -> Map<Key,Map<agg="sum,count,avg", number>>
public Map<String, Map<String, NumberValue>> result() {
public Map<String, Map<String, Map<String, NumberValue>>> result() {
return result;
}
}
Expand Down
Loading

0 comments on commit 1e437a3

Please sign in to comment.