-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanalysis-console.ts
More file actions
95 lines (83 loc) · 3.78 KB
/
Copy pathanalysis-console.ts
File metadata and controls
95 lines (83 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { Account, AnalysisInfo } from "@tago-io/sdk";
import { EventSource } from "eventsource";
import { getEnvironmentConfig, IEnvironment } from "../../lib/config-file.js";
import { errorHandler, highlightMSG, infoMSG, successMSG } from "../../lib/messages.js";
import { requireLocalScope } from "../../lib/resolve-scope.js";
import { searchName } from "../../lib/search-name.js";
import { pickAnalysisFromConfig } from "../../prompt/pick-analysis-from-config.js";
/**
* Creates a new SSE connection to the TagoIO Realtime API.
* @param profileToken - The user's profile token.
* @param analysisID - The ID of the analysis script to connect to.
* @param urlSSERealtime - The URL of the TagoIO SSE Realtime API.
* @returns An EventSource instance connected to the TagoIO Realtime API.
*/
function apiSSE(profileToken: string, analysisID: string, urlSSERealtime?: string) {
const url = urlSSERealtime || "https://sse.tago.io/events";
const sse = new EventSource(`${url}?channel=analysis_console.${analysisID}&token=${profileToken}`);
return sse;
}
/**
* Returns the script object from the analysis list based on the script name or prompts the user to select one.
* @param scriptName - The name of the script to search for.
* @param analysisList - The list of analysis objects to search through.
* @returns The script object that matches the script name or the one selected by the user.
*/
async function getScriptObj(scriptName: string | void, analysisList: NonNullable<IEnvironment["analysisList"]>) {
let scriptObj: NonNullable<IEnvironment["analysisList"]>[number] | undefined;
if (scriptName) {
scriptObj = searchName(
scriptName,
analysisList.map((x) => ({ names: [x.name, x.fileName], value: x })),
);
} else {
scriptObj = await pickAnalysisFromConfig(analysisList);
}
return scriptObj;
}
/**
* Sets up the SSE connection and event listeners for device live inspection.
* @param sse - The SSE connection to TagoIO.
* @param deviceIdOrToken - The ID or token of the device to inspect.
* @param deviceInfo - Information about the device being inspected.
*/
function setupSSE(sse: ReturnType<typeof apiSSE>, _script_id: string, analysis_info: AnalysisInfo) {
sse.onmessage = (event) => {
const scope = JSON.parse(event.data).payload;
console.log(`\x1b[35m${new Date(scope.timestamp).toISOString()} \x1b[0m ${scope.message}`);
};
sse.onerror = (error) => {
console.error(error);
errorHandler("Connection error");
};
sse.onopen = () => {
infoMSG("Connected to TagoIO, Getting analysis information...");
successMSG(`Analysis [${highlightMSG(analysis_info.name)}] found successfully.`);
successMSG(`Waiting for logs...`);
};
}
/**
* Connects to the analysis console for a given script name and environment.
* @param scriptName - The name of the script to connect to.
* @param options - The options object containing the environment to connect to.
* @returns void
*/
async function connectAnalysisConsole(scriptName: string | void, options: { environment: string }) {
requireLocalScope("analysis-console");
const config = getEnvironmentConfig(options.environment);
if (!config || !config.profileToken) {
errorHandler("Environment not found");
}
const scriptObj = await getScriptObj(scriptName, config.analysisList ?? []);
if (!scriptObj) {
errorHandler(`Analysis not found: ${scriptName}`);
}
const account = new Account({ token: config.profileToken, region: config.profileRegion });
const analysis_info = await account.analysis.info(scriptObj.id).catch(() => null);
if (!analysis_info) {
errorHandler(`Analysis with ID: ${scriptObj.id} couldn't be found.`);
}
const sse = apiSSE(config.profileToken, analysis_info.id, config?.tagoSSEURL);
setupSSE(sse, scriptObj.id, analysis_info);
}
export { connectAnalysisConsole };