• 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

85.04
/src/managers/AlgorithmManager.ts
1
import {Algorithm} from "../algorithms/Algorithm";
15✔
2
import type {Graph} from "../Graph";
3
import type {AlgorithmSpecificOptions} from "../utils/queue-migration";
4
import type {EventManager} from "./EventManager";
5
import type {Manager} from "./interfaces";
6

7
/**
8
 * Manages algorithm execution and coordination
9
 * Handles running algorithms from templates and individual algorithm execution
10
 */
11
export class AlgorithmManager implements Manager {
15✔
12
    /**
13
     * Creates an instance of AlgorithmManager
14
     * @param eventManager - Event manager for emitting algorithm events
15
     * @param graph - Graph instance to run algorithms on
16
     */
17
    constructor(
15✔
18
        private eventManager: EventManager,
1,011✔
19
        private graph: Graph,
1,011✔
20
    ) {}
1,011✔
21

22
    /**
23
     * Initializes the algorithm manager
24
     * @returns Promise that resolves when initialization is complete
25
     */
26
    async init(): Promise<void> {
15✔
27
        // AlgorithmManager doesn't need async initialization
28
        return Promise.resolve();
919✔
29
    }
919✔
30

31
    /**
32
     * Disposes of the algorithm manager and cleans up resources
33
     */
34
    dispose(): void {
15✔
35
        // No cleanup needed for algorithms
36
    }
779✔
37

38
    /**
39
     * Run algorithms specified in the template configuration
40
     * Called during initialization if runAlgorithmsOnLoad is true
41
     * @param algorithms - Array of algorithm names in "namespace:type" format
42
     */
43
    async runAlgorithmsFromTemplate(algorithms: string[]): Promise<void> {
15✔
44
        const errors: Error[] = [];
10✔
45

46
        for (const algName of algorithms) {
10✔
47
            try {
23✔
48
                const trimmedName = algName.trim();
23✔
49
                const [namespace, type] = trimmedName.split(":");
23✔
50
                if (!namespace || !type) {
23!
51
                    throw new Error(`invalid algorithm name format: ${trimmedName}. Expected format: namespace:type`);
1✔
52
                }
1✔
53

54
                await this.runAlgorithm(namespace.trim(), type.trim());
22✔
55
            } catch (error) {
23!
56
                const algorithmError = error instanceof Error ? error : new Error(String(error));
6!
57
                errors.push(algorithmError);
6✔
58
                // Individual error already emitted by runAlgorithm
59
            }
6✔
60
        }
23✔
61

62
        // If there were any errors, throw a summary error
63
        if (errors.length > 0) {
10!
64
            const summaryError = new Error(
4✔
65
                `${errors.length} algorithm(s) failed during template execution: ${
4✔
66
                    errors.map((e) => e.message).join(", ")
4✔
67
                }`,
4✔
68
            );
4✔
69

70
            this.eventManager.emitGraphError(
4✔
71
                this.graph,
4✔
72
                summaryError,
4✔
73
                "algorithm",
4✔
74
                {
4✔
75
                    errorCount: errors.length,
4✔
76
                    component: "AlgorithmManager",
4✔
77
                },
4✔
78
            );
4✔
79

80
            throw summaryError;
4✔
81
        }
4✔
82
    }
10✔
83

84
    /**
85
     * Run a specific algorithm by namespace and type
86
     * @param namespace - Algorithm namespace (e.g., "graphty")
87
     * @param type - Algorithm type (e.g., "dijkstra")
88
     * @param algorithmOptions - Optional algorithm-specific options (source, target, etc.)
89
     */
90
    async runAlgorithm(
15✔
91
        namespace: string,
92✔
92
        type: string,
92✔
93
        algorithmOptions?: AlgorithmSpecificOptions,
92✔
94
    ): Promise<void> {
92✔
95
        try {
92✔
96
            const alg = Algorithm.get(this.graph, namespace, type);
92✔
97
            if (!alg) {
92!
98
                throw new Error(`algorithm not found: ${namespace}:${type}`);
1✔
99
            }
1✔
100

101
            // Configure the algorithm if options are provided and configure method exists
102
            if (algorithmOptions && "configure" in alg && typeof alg.configure === "function") {
92!
103
                alg.configure(algorithmOptions);
×
104
            }
✔
105

106
            await alg.run(this.graph);
91✔
107

108
            // Re-apply styles to all nodes and edges after algorithm completes
109
            // This ensures algorithm results are used in style selector matching
110
            this.graph.getDataManager().applyStylesToExistingNodes();
82✔
111
            this.graph.getDataManager().applyStylesToExistingEdges();
82✔
112
        } catch (error) {
92!
113
            // Emit error event for any error (not found or execution)
114
            const algorithmError = error instanceof Error ? error : new Error(String(error));
10✔
115

116
            this.eventManager.emitGraphError(
10✔
117
                this.graph,
10✔
118
                algorithmError,
10✔
119
                "algorithm",
10✔
120
                {
10✔
121
                    algorithm: `${namespace}:${type}`,
10✔
122
                    component: "AlgorithmManager",
10✔
123
                },
10✔
124
            );
10✔
125

126
            throw algorithmError;
10✔
127
        }
10✔
128
    }
92✔
129

130
    /**
131
     * Check if an algorithm exists
132
     * @param namespace - Algorithm namespace
133
     * @param type - Algorithm type
134
     * @returns True if the algorithm exists, false otherwise
135
     */
136
    hasAlgorithm(namespace: string, type: string): boolean {
15✔
137
        try {
3✔
138
            const alg = Algorithm.get(this.graph, namespace, type);
3✔
139
            return alg !== null;
3✔
140
        } catch {
3✔
141
            return false;
1✔
142
        }
1✔
143
    }
3✔
144

145
    /**
146
     * Get list of available algorithms
147
     * TODO: This depends on the Algorithm registry implementation
148
     * @returns Array of available algorithm names
149
     */
150
    getAvailableAlgorithms(): string[] {
15✔
151
        // This would need to be implemented in the Algorithm class
152
        // For now, return empty array
153
        return [];
1✔
154
    }
1✔
155
}
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