• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

graphty-org / graphty-element / 16190621176

10 Jul 2025 08:52AM UTC coverage: 66.274% (-2.7%) from 68.949%
16190621176

push

github

apowers313
style: delint

441 of 681 branches covered (64.76%)

Branch coverage included in aggregate %.

3499 of 5264 relevant lines covered (66.47%)

1070.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.35
/src/algorithms/Algorithm.ts
1
import {set as deepSet} from "lodash";
2✔
2

3
import {AdHocData} from "../config";
4
import {Edge} from "../Edge";
5
import {Graph} from "../Graph";
6

7
type AlgorithmClass = new (g: Graph) => Algorithm;
8
const algorithmRegistry = new Map<string, AlgorithmClass>();
2✔
9

10
// algorithmResults layout:
11
// {
12
//     node: {
13
//         id: {
14
//             namespace: {
15
//                 algorithm: {
16
//                     result: unknown
17
//                 }
18
//             }
19
//         }
20
//     },
21
//     edge: {
22
//         id: {
23
//             namespace: {
24
//                 algorithm: {
25
//                     result: unknown
26
//                 }
27
//             }
28
//         }
29
//     },
30
//     graph: {
31
//         namespace: {
32
//             algorithm: {
33
//                 result: unknown
34
//             }
35
//         }
36
//     }
37
// }
38

39
export abstract class Algorithm {
2✔
40
    static type: string;
41
    static namespace: string;
42
    protected graph: Graph;
43

44
    constructor(g: Graph) {
2✔
45
        this.graph = g;
2✔
46
    }
2✔
47

48
    get type(): string {
2✔
49
        return (this.constructor as typeof Algorithm).type;
462✔
50
    }
462✔
51

52
    get namespace(): string {
2✔
53
        return (this.constructor as typeof Algorithm).namespace;
462✔
54
    }
462✔
55

56
    get results(): AdHocData {
2✔
57
        const algorithmResults = {} as AdHocData;
×
58
        for (const n of this.graph.nodes.values()) {
×
59
            deepSet(algorithmResults, `node.${n.id}`, n.algorithmResults);
×
60
        }
×
61

62
        // TODO: edge and graph
63

64
        return algorithmResults;
×
65
        // return structuredClone(algorithmResults) as AdHocData;
66
    }
×
67

68
    abstract run(g: Graph): Promise<void>;
69

70
    #createPath(resultName: string): string[] {
2✔
71
        const ret: string[] = [];
462✔
72

73
        ret.push("algorithmResults");
462✔
74
        ret.push(this.namespace);
462✔
75
        ret.push(this.type);
462✔
76
        ret.push(resultName);
462✔
77

78
        return ret;
462✔
79
    }
462✔
80

81
    addNodeResult(nodeId: number | string, resultName: string, result: unknown): void {
2✔
82
        const p = this.#createPath(resultName);
462✔
83
        const n = this.graph.nodes.get(nodeId);
462✔
84
        if (!n) {
462!
85
            throw new Error(`couldn't find nodeId '${nodeId}' while trying to run algorithm '${this.type}'`);
×
86
        }
×
87

88
        deepSet(n, p, result);
462✔
89
        // XXX: THIS IS WHERE I LEFT OFF
90
        // replace algorithmResults with graph.nodes; set result on each node.algorithmResult
91
    }
462✔
92

93
    addEdgeResult(e: Edge, result: unknown): void {
2✔
94
        console.log("adding edge result", e, result);
×
95
    }
×
96

97
    addGraphResult(g: Graph, result: unknown): void {
2✔
98
        console.log("adding graph result", g, result);
×
99
    }
×
100

101
    static register<T extends AlgorithmClass>(cls: T): T {
2✔
102
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
103
        const t: string = (cls as any).type;
9✔
104
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
105
        const ns: string = (cls as any).namespace;
9✔
106
        algorithmRegistry.set(`${ns}:${t}`, cls);
9✔
107
        return cls;
9✔
108
    }
9✔
109

110
    static get(g: Graph, namespace: string, type: string): Algorithm | null {
2✔
111
        const SourceClass = algorithmRegistry.get(`${namespace}:${type}`);
×
112
        if (SourceClass) {
×
113
            return new SourceClass(g);
×
114
        }
×
115

116
        return null;
×
117
    }
×
118
}
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc