MCPcopy Create free account
hub / github.com/codemix/graph / computeSingleAggregate

Method computeSingleAggregate

packages/graph/src/Steps.ts:6958–7040  ·  view source on GitHub ↗
(
    paths: readonly TraversalPath<any, any, any>[],
    item: Extract<WithItemConfig, { type: "aggregate" }>,
  )

Source from the content-addressed store, hash-verified

6956 }
6957
6958 protected computeSingleAggregate(
6959 paths: readonly TraversalPath<any, any, any>[],
6960 item: Extract<WithItemConfig, { type: "aggregate" }>,
6961 ): any {
6962 switch (item.function) {
6963 case "COUNT":
6964 return paths.length;
6965
6966 case "COLLECT": {
6967 const collected: any[] = [];
6968 for (const path of paths) {
6969 const pathNode = path.get(item.sourceVariable);
6970 if (pathNode) {
6971 collected.push(pathNode.value);
6972 }
6973 }
6974 return collected;
6975 }
6976
6977 case "SUM": {
6978 if (!item.property) throw new Error("SUM requires a property");
6979 let sum = 0;
6980 for (const path of paths) {
6981 const pathNode = path.get(item.sourceVariable);
6982 if (pathNode) {
6983 const val = pathNode.property(item.property as never);
6984 if (typeof val === "number") sum += val;
6985 }
6986 }
6987 return sum;
6988 }
6989
6990 case "AVG": {
6991 if (!item.property) throw new Error("AVG requires a property");
6992 let sum = 0;
6993 let count = 0;
6994 for (const path of paths) {
6995 const pathNode = path.get(item.sourceVariable);
6996 if (pathNode) {
6997 const val = pathNode.property(item.property as never);
6998 if (typeof val === "number") {
6999 sum += val;
7000 count++;
7001 }
7002 }
7003 }
7004 return count > 0 ? sum / count : null;
7005 }
7006
7007 case "MIN": {
7008 if (!item.property) throw new Error("MIN requires a property");
7009 let min: number | null = null;
7010 for (const path of paths) {
7011 const pathNode = path.get(item.sourceVariable);
7012 if (pathNode) {
7013 const val = pathNode.property(item.property as never);
7014 if (typeof val === "number") {
7015 if (min === null || val < min) min = val;

Callers 1

computeAggregatesMethod · 0.95

Calls 3

getMethod · 0.65
pushMethod · 0.45
propertyMethod · 0.45

Tested by

no test coverage detected