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

graphty-org / graphty-element / 20514590651

26 Dec 2025 02:37AM UTC coverage: 70.559% (-0.3%) from 70.836%
20514590651

push

github

apowers313
ci: fix npm ci

9591 of 13363 branches covered (71.77%)

Branch coverage included in aggregate %.

25136 of 35854 relevant lines covered (70.11%)

6233.71 hits per line

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

87.64
/src/algorithms/DegreeAlgorithm.ts
1
import type {SuggestedStylesConfig} from "../config";
2
import {Algorithm} from "./Algorithm";
15✔
3
import {toAlgorithmGraph} from "./utils/graphConverter";
15✔
4

5
/**
6
 *
7
 */
8
export class DegreeAlgorithm extends Algorithm {
15✔
9
    static namespace = "graphty";
19✔
10
    static type = "degree";
120✔
11

12
    static suggestedStyles = (): SuggestedStylesConfig => ({
120✔
13
        layers: [
12✔
14
            {
12✔
15
                node: {
12✔
16
                    selector: "",
12✔
17
                    style: {
12✔
18
                        enabled: true,
12✔
19
                    },
12✔
20
                    calculatedStyle: {
12✔
21
                        inputs: ["algorithmResults.graphty.degree.degreePct"],
12✔
22
                        output: "style.texture.color",
12✔
23
                        expr: "{ return StyleHelpers.color.sequential.viridis(arguments[0]) }",
12✔
24
                    },
12✔
25
                },
12✔
26
                metadata: {
12✔
27
                    name: "Degree - Viridis Gradient",
12✔
28
                    description: "Purple (low) → Yellow (high) - colorblind-safe",
12✔
29
                },
12✔
30
            },
12✔
31
        ],
12✔
32
        description: "Visualizes node importance through color based on connection count",
12✔
33
        category: "node-metric",
12✔
34
    });
12✔
35

36
    /**
37
     * Executes the degree algorithm on the graph
38
     *
39
     * Computes in-degree, out-degree, and total degree for all nodes.
40
     */
41
    async run(): Promise<void> {
19✔
42
        const g = this.graph;
14✔
43
        const nodes = Array.from(g.getDataManager().nodes.keys());
14✔
44

45
        if (nodes.length === 0) {
14!
46
            return;
×
47
        }
×
48

49
        // Convert to @graphty/algorithms format - use directed mode to get accurate in/out degrees
50
        const graphData = toAlgorithmGraph(g, {directed: true, addReverseEdges: false});
14✔
51

52
        // Calculate degrees using the @graphty/algorithms Graph methods
53
        let maxInDegree = 0;
14✔
54
        let maxOutDegree = 0;
14✔
55
        let maxDegree = 0;
14✔
56

57
        const degreeResults = new Map<number | string, {inDegree: number, outDegree: number, degree: number}>();
14✔
58

59
        for (const nodeId of nodes) {
14✔
60
            const inDegree = graphData.inDegree(nodeId);
289✔
61
            const outDegree = graphData.outDegree(nodeId);
289✔
62
            const degree = inDegree + outDegree;
289✔
63

64
            degreeResults.set(nodeId, {inDegree, outDegree, degree});
289✔
65

66
            maxInDegree = Math.max(maxInDegree, inDegree);
289✔
67
            maxOutDegree = Math.max(maxOutDegree, outDegree);
289✔
68
            maxDegree = Math.max(maxDegree, degree);
289✔
69
        }
289✔
70

71
        // Store graph-level results
72
        this.addGraphResult("maxInDegree", maxInDegree);
14✔
73
        this.addGraphResult("maxOutDegree", maxOutDegree);
14✔
74
        this.addGraphResult("maxDegree", maxDegree);
14✔
75

76
        // Store node-level results
77
        for (const nodeId of nodes) {
14✔
78
            const result = degreeResults.get(nodeId);
289✔
79
            if (!result) {
289!
80
                continue;
×
81
            }
×
82

83
            const {inDegree, outDegree, degree} = result;
289✔
84

85
            this.addNodeResult(nodeId, "inDegree", inDegree);
289✔
86
            this.addNodeResult(nodeId, "outDegree", outDegree);
289✔
87
            this.addNodeResult(nodeId, "degree", degree);
289✔
88
            // Safe division: return 0 when max is 0 to avoid NaN
89
            this.addNodeResult(nodeId, "inDegreePct", maxInDegree > 0 ? inDegree / maxInDegree : 0);
289!
90
            this.addNodeResult(nodeId, "outDegreePct", maxOutDegree > 0 ? outDegree / maxOutDegree : 0);
289!
91
            this.addNodeResult(nodeId, "degreePct", maxDegree > 0 ? degree / maxDegree : 0);
289!
92
        }
289✔
93
    }
14✔
94
}
19✔
95

96
// Auto-register this algorithm when the module is imported
97
Algorithm.register(DegreeAlgorithm);
15✔
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