@zhoujinandrew/te-cli 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,217 @@
1
+ // src/commands/analysis/list-reports.ts
2
+ var listReports = {
3
+ service: "analysis",
4
+ command: "+list-reports",
5
+ description: "List all analysis reports for a project",
6
+ flags: [
7
+ { name: "project-id", type: "number", required: true, alias: "p", desc: "Project ID" }
8
+ ],
9
+ risk: "read",
10
+ execute: async (ctx) => {
11
+ return ctx.api("POST", "/v1/ta/event/listAll", {
12
+ projectId: ctx.num("project-id")
13
+ });
14
+ }
15
+ };
16
+
17
+ // src/commands/analysis/get-report.ts
18
+ var getReport = {
19
+ service: "analysis",
20
+ command: "+get-report",
21
+ description: "Get full report definition by ID",
22
+ flags: [
23
+ { name: "project-id", type: "number", required: true, alias: "p", desc: "Project ID" },
24
+ { name: "report-id", type: "number", required: true, desc: "Report ID" }
25
+ ],
26
+ risk: "read",
27
+ execute: async (ctx) => {
28
+ return ctx.api("GET", "/v1/ta/event/reportsearch", {
29
+ projectId: ctx.num("project-id"),
30
+ reportId: ctx.num("report-id")
31
+ });
32
+ }
33
+ };
34
+
35
+ // src/commands/analysis/save-report.ts
36
+ var saveReport = {
37
+ service: "analysis",
38
+ command: "+save-report",
39
+ description: "Save (create/update) an analysis report",
40
+ flags: [
41
+ { name: "project-id", type: "number", required: true, alias: "p", desc: "Project ID" },
42
+ { name: "report-name", type: "string", required: true, desc: "Report name" },
43
+ { name: "report-model", type: "number", required: true, desc: "0=Event, 1=Retention, 2=Funnel, 10=Distribution" },
44
+ { name: "events", type: "json", required: true, desc: "Events configuration array" },
45
+ { name: "event-view", type: "json", required: true, desc: "Event view configuration" }
46
+ ],
47
+ risk: "write",
48
+ execute: async (ctx) => {
49
+ return ctx.api("POST", "/v1/ta/event/reportsave", {
50
+ projectId: ctx.num("project-id")
51
+ }, {
52
+ reportName: ctx.str("report-name"),
53
+ reportModel: ctx.num("report-model"),
54
+ events: ctx.json("events"),
55
+ eventView: ctx.json("event-view")
56
+ });
57
+ }
58
+ };
59
+
60
+ // src/commands/analysis/list-dashboards.ts
61
+ var listDashboards = {
62
+ service: "analysis",
63
+ command: "+list-dashboards",
64
+ description: "List all dashboards for a project",
65
+ flags: [
66
+ { name: "project-id", type: "number", required: true, alias: "p", desc: "Project ID" }
67
+ ],
68
+ risk: "read",
69
+ execute: async (ctx) => {
70
+ return ctx.api("GET", "/v1/ta/dashboard/all-dashboards", {
71
+ projectId: ctx.num("project-id")
72
+ });
73
+ }
74
+ };
75
+
76
+ // src/commands/analysis/get-dashboard.ts
77
+ var getDashboard = {
78
+ service: "analysis",
79
+ command: "+get-dashboard",
80
+ description: "Get dashboard details by ID",
81
+ flags: [
82
+ { name: "project-id", type: "number", required: true, alias: "p", desc: "Project ID" },
83
+ { name: "dashboard-id", type: "number", required: true, desc: "Dashboard ID" }
84
+ ],
85
+ risk: "read",
86
+ execute: async (ctx) => {
87
+ return ctx.api("POST", "/v1/ta/dashboard/search-dashboard", {
88
+ projectId: ctx.num("project-id"),
89
+ dashbordId: ctx.num("dashboard-id")
90
+ });
91
+ }
92
+ };
93
+
94
+ // src/commands/analysis/create-dashboard.ts
95
+ var createDashboard = {
96
+ service: "analysis",
97
+ command: "+create-dashboard",
98
+ description: "Create a new dashboard",
99
+ flags: [
100
+ { name: "project-id", type: "number", required: true, alias: "p", desc: "Project ID" },
101
+ { name: "dashboard-name", type: "string", required: true, desc: "Dashboard name" }
102
+ ],
103
+ risk: "write",
104
+ execute: async (ctx) => {
105
+ return ctx.api("POST", "/v1/ta/dashboard/create-dashboard", {
106
+ projectId: ctx.num("project-id")
107
+ }, {
108
+ dashboardName: ctx.str("dashboard-name")
109
+ });
110
+ }
111
+ };
112
+
113
+ // src/commands/analysis/update-dashboard.ts
114
+ var updateDashboard = {
115
+ service: "analysis",
116
+ command: "+update-dashboard",
117
+ description: "Update dashboard report layout",
118
+ flags: [
119
+ { name: "project-id", type: "number", required: true, alias: "p", desc: "Project ID" },
120
+ { name: "dashboard-id", type: "number", required: true, desc: "Dashboard ID" },
121
+ { name: "reports", type: "json", required: true, desc: "[{reportId, reportWidth?, indexOrder}]" }
122
+ ],
123
+ risk: "write",
124
+ execute: async (ctx) => {
125
+ return ctx.api("POST", "/v1/ta/dashboard/update-dashboard", {
126
+ projectId: ctx.num("project-id")
127
+ }, {
128
+ dashbordId: ctx.num("dashboard-id"),
129
+ reports: ctx.json("reports")
130
+ });
131
+ }
132
+ };
133
+
134
+ // src/commands/analysis/list-dashboard-reports.ts
135
+ var listDashboardReports = {
136
+ service: "analysis",
137
+ command: "+list-dashboard-reports",
138
+ description: "List reports within a dashboard",
139
+ flags: [
140
+ { name: "project-id", type: "number", required: true, alias: "p", desc: "Project ID" },
141
+ { name: "dashboard-id", type: "number", required: true, desc: "Dashboard ID" }
142
+ ],
143
+ risk: "read",
144
+ execute: async (ctx) => {
145
+ const result = await ctx.api("POST", "/v1/ta/dashboard/search-dashboard", {
146
+ projectId: ctx.num("project-id")
147
+ }, {
148
+ dashboardId: ctx.num("dashboard-id")
149
+ });
150
+ return result?.eventReportList ?? result;
151
+ }
152
+ };
153
+
154
+ // src/commands/analysis/query-report-data.ts
155
+ var queryReportData = {
156
+ service: "analysis",
157
+ command: "+query-report-data",
158
+ description: "Query report data by report ID",
159
+ flags: [
160
+ { name: "project-id", type: "number", required: true, alias: "p", desc: "Project ID" },
161
+ { name: "report-id", type: "number", required: true, desc: "Report ID" },
162
+ { name: "dashboard-id", type: "number", desc: "Dashboard ID (optional)" },
163
+ { name: "start-time", type: "string", desc: "Start time (YYYY-MM-DD HH:mm:ss)" },
164
+ { name: "end-time", type: "string", desc: "End time (YYYY-MM-DD HH:mm:ss)" }
165
+ ],
166
+ risk: "read",
167
+ execute: async (ctx) => {
168
+ const projectId = ctx.num("project-id");
169
+ const reportId = ctx.num("report-id");
170
+ const report = await ctx.api("GET", "/v1/ta/event/reportsearch", {
171
+ projectId,
172
+ reportId
173
+ });
174
+ const qp = {
175
+ events: report.events,
176
+ eventView: report.eventView
177
+ };
178
+ const startTime = ctx.str("start-time");
179
+ const endTime = ctx.str("end-time");
180
+ if (startTime) qp.eventView = { ...qp.eventView, startTime };
181
+ if (endTime) qp.eventView = { ...qp.eventView, endTime };
182
+ return ctx.queryReportData(projectId, reportId, qp, report.reportModel || 0);
183
+ }
184
+ };
185
+
186
+ // src/commands/analysis/query-sql.ts
187
+ var querySql = {
188
+ service: "analysis",
189
+ command: "+query-sql",
190
+ description: "Execute a SQL query against the project",
191
+ flags: [
192
+ { name: "project-id", type: "number", required: true, alias: "p", desc: "Project ID" },
193
+ { name: "sql", type: "string", required: true, desc: "SQL query string" }
194
+ ],
195
+ risk: "read",
196
+ execute: async (ctx) => {
197
+ return ctx.querySql(ctx.num("project-id"), ctx.str("sql"));
198
+ }
199
+ };
200
+
201
+ // src/commands/analysis/index.ts
202
+ var commands = [
203
+ listReports,
204
+ getReport,
205
+ saveReport,
206
+ listDashboards,
207
+ getDashboard,
208
+ createDashboard,
209
+ updateDashboard,
210
+ listDashboardReports,
211
+ queryReportData,
212
+ querySql
213
+ ];
214
+ var analysis_default = commands;
215
+ export {
216
+ analysis_default as default
217
+ };
@@ -0,0 +1,148 @@
1
+ import {
2
+ clearToken,
3
+ getToken,
4
+ resolveHost
5
+ } from "./chunk-P7NQZGSZ.js";
6
+
7
+ // src/core/client.ts
8
+ import WebSocket from "ws";
9
+ import { randomBytes } from "crypto";
10
+ function genRequestId(prefix) {
11
+ const rand = randomBytes(4).toString("base64url").slice(0, 8);
12
+ return `${prefix}@@${rand}`;
13
+ }
14
+ function buildUrl(baseUrl, modulePath, params = {}) {
15
+ const base = baseUrl.replace(/\/$/, "");
16
+ const p = modulePath.startsWith("/") ? modulePath : `/${modulePath}`;
17
+ const url = new URL(`${base}${p}`);
18
+ for (const [k, v] of Object.entries(params)) {
19
+ if (v !== void 0 && v !== null) url.searchParams.set(k, String(v));
20
+ }
21
+ return url.toString();
22
+ }
23
+ function buildWsUrl(baseUrl, token) {
24
+ const base = baseUrl.replace(/\/$/, "");
25
+ const wsBase = base.replace(/^https:\/\//, "wss://").replace(/^http:\/\//, "ws://");
26
+ return `${wsBase}/v1/ta-websocket/query/${token}`;
27
+ }
28
+ async function request(method, modulePath, params = {}, body = null, retry = true, hostUrl) {
29
+ const resolvedHost = resolveHost(hostUrl);
30
+ const token = await getToken(resolvedHost);
31
+ const url = buildUrl(resolvedHost, modulePath, params);
32
+ const headers = {
33
+ "Authorization": `bearer ${token}`,
34
+ "Content-Type": "application/json"
35
+ };
36
+ const options = { method, headers };
37
+ if (body && method !== "GET") options.body = JSON.stringify(body);
38
+ if (process.env.TE_DEBUG) {
39
+ console.error(`[DEBUG] ${method} ${url}`);
40
+ console.error(`[DEBUG] body: ${options.body ?? "(none)"}`);
41
+ }
42
+ const resp = await fetch(url, options);
43
+ if ((resp.status === 401 || resp.status === 403) && retry) {
44
+ clearToken(resolvedHost);
45
+ return request(method, modulePath, params, body, false, resolvedHost);
46
+ }
47
+ const data = await resp.json();
48
+ if (data.return_code === -1001 && retry) {
49
+ clearToken(resolvedHost);
50
+ return request(method, modulePath, params, body, false, resolvedHost);
51
+ }
52
+ if (data.return_code !== 0 && data.return_code !== void 0) {
53
+ throw new Error(`TE API error: ${data.return_message || "unknown"} (code: ${data.return_code})`);
54
+ }
55
+ return data.data !== void 0 ? data.data : data;
56
+ }
57
+ async function httpGet(modulePath, params = {}, hostUrl) {
58
+ return request("GET", modulePath, params, null, true, hostUrl);
59
+ }
60
+ async function httpPost(modulePath, params = {}, body, hostUrl) {
61
+ return request("POST", modulePath, params, body ?? {}, true, hostUrl);
62
+ }
63
+ async function wsQueryOnce(projectId, requestId, qp, eventModel, options = {}, token, hostUrl, timeoutMs = 3e4) {
64
+ const wsUrl = buildWsUrl(hostUrl, token);
65
+ return new Promise((resolve, reject) => {
66
+ const ws = new WebSocket(wsUrl);
67
+ const timer = setTimeout(() => {
68
+ ws.close();
69
+ reject(new Error(`WebSocket query timed out after ${timeoutMs}ms`));
70
+ }, timeoutMs);
71
+ ws.on("open", () => {
72
+ const payload = {
73
+ requestId,
74
+ projectId,
75
+ eventModel,
76
+ qp: typeof qp === "string" ? qp : JSON.stringify(qp),
77
+ searchSource: options.searchSource || "reportQuery",
78
+ querySource: options.querySource || "single_report",
79
+ contentTranslate: options.contentTranslate !== false,
80
+ ...options.extra || {}
81
+ };
82
+ ws.send(JSON.stringify(["data", payload, { channel: "ta" }]));
83
+ });
84
+ ws.on("message", (raw) => {
85
+ try {
86
+ const msg = JSON.parse(raw.toString());
87
+ if (Array.isArray(msg) && msg.length >= 2) {
88
+ const data = msg[1];
89
+ if (data.requestId === requestId && data.progress === 100) {
90
+ clearTimeout(timer);
91
+ ws.close();
92
+ resolve(data);
93
+ }
94
+ }
95
+ } catch {
96
+ }
97
+ });
98
+ ws.on("error", (err) => {
99
+ clearTimeout(timer);
100
+ reject(err);
101
+ });
102
+ ws.on("close", () => {
103
+ clearTimeout(timer);
104
+ });
105
+ });
106
+ }
107
+ async function wsQuery(projectId, requestId, qp, eventModel, options = {}, hostUrl) {
108
+ const resolvedHost = resolveHost(hostUrl);
109
+ const token = await getToken(resolvedHost);
110
+ try {
111
+ return await wsQueryOnce(projectId, requestId, qp, eventModel, options, token, resolvedHost);
112
+ } catch (err) {
113
+ const msg = err.message || "";
114
+ if (msg.includes("401") || msg.includes("403") || msg.includes("auth")) {
115
+ clearToken(resolvedHost);
116
+ const newToken = await getToken(resolvedHost);
117
+ return wsQueryOnce(projectId, requestId, qp, eventModel, options, newToken, resolvedHost);
118
+ }
119
+ throw err;
120
+ }
121
+ }
122
+ async function querySql(projectId, sql, hostUrl) {
123
+ const requestId = genRequestId("sqlIde");
124
+ const qp = {
125
+ events: { sql },
126
+ eventView: { sqlViewParams: [] }
127
+ };
128
+ return wsQuery(projectId, requestId, qp, 10, {
129
+ searchSource: "sqlIde",
130
+ querySource: "sqlIde"
131
+ }, hostUrl);
132
+ }
133
+ async function queryReportData(projectId, reportId, qp, eventModel, options = {}, hostUrl) {
134
+ const requestId = genRequestId("reportQuery");
135
+ return wsQuery(projectId, requestId, qp, eventModel, {
136
+ searchSource: "reportQuery",
137
+ querySource: "single_report",
138
+ ...options
139
+ }, hostUrl);
140
+ }
141
+
142
+ export {
143
+ httpGet,
144
+ httpPost,
145
+ wsQuery,
146
+ querySql,
147
+ queryReportData
148
+ };
@@ -0,0 +1,145 @@
1
+ import {
2
+ clearToken,
3
+ getToken,
4
+ resolveHost
5
+ } from "./chunk-P7NQZGSZ.js";
6
+
7
+ // src/core/client.ts
8
+ import WebSocket from "ws";
9
+ import { randomBytes } from "crypto";
10
+ function genRequestId(prefix) {
11
+ const rand = randomBytes(4).toString("base64url").slice(0, 8);
12
+ return `${prefix}@@${rand}`;
13
+ }
14
+ function buildUrl(baseUrl, modulePath, params = {}) {
15
+ const base = baseUrl.replace(/\/$/, "");
16
+ const p = modulePath.startsWith("/") ? modulePath : `/${modulePath}`;
17
+ const url = new URL(`${base}${p}`);
18
+ for (const [k, v] of Object.entries(params)) {
19
+ if (v !== void 0 && v !== null) url.searchParams.set(k, String(v));
20
+ }
21
+ return url.toString();
22
+ }
23
+ function buildWsUrl(baseUrl, token) {
24
+ const base = baseUrl.replace(/\/$/, "");
25
+ const wsBase = base.replace(/^https:\/\//, "wss://").replace(/^http:\/\//, "ws://");
26
+ return `${wsBase}/v1/ta-websocket/query/${token}`;
27
+ }
28
+ async function request(method, modulePath, params = {}, body = null, retry = true, hostUrl) {
29
+ const resolvedHost = resolveHost(hostUrl);
30
+ const token = await getToken(resolvedHost);
31
+ const url = buildUrl(resolvedHost, modulePath, params);
32
+ const headers = {
33
+ "Authorization": `bearer ${token}`,
34
+ "Content-Type": "application/json"
35
+ };
36
+ const options = { method, headers };
37
+ if (body && method !== "GET") options.body = JSON.stringify(body);
38
+ const resp = await fetch(url, options);
39
+ if ((resp.status === 401 || resp.status === 403) && retry) {
40
+ clearToken(resolvedHost);
41
+ return request(method, modulePath, params, body, false, resolvedHost);
42
+ }
43
+ const data = await resp.json();
44
+ if (data.return_code === -1001 && retry) {
45
+ clearToken(resolvedHost);
46
+ return request(method, modulePath, params, body, false, resolvedHost);
47
+ }
48
+ if (data.return_code !== 0 && data.return_code !== void 0) {
49
+ throw new Error(`TE API error: ${data.return_message || "unknown"} (code: ${data.return_code})`);
50
+ }
51
+ return data.data !== void 0 ? data.data : data;
52
+ }
53
+ async function httpGet(modulePath, params = {}, hostUrl) {
54
+ return request("GET", modulePath, params, null, true, hostUrl);
55
+ }
56
+ async function httpPost(modulePath, params = {}, body, hostUrl) {
57
+ return request("POST", modulePath, params, body ?? {}, true, hostUrl);
58
+ }
59
+ async function wsQueryOnce(projectId, requestId, qp, eventModel, options = {}, token, hostUrl, timeoutMs = 3e4) {
60
+ const wsUrl = buildWsUrl(hostUrl, token);
61
+ return new Promise((resolve, reject) => {
62
+ const ws = new WebSocket(wsUrl);
63
+ const timer = setTimeout(() => {
64
+ ws.close();
65
+ reject(new Error(`WebSocket query timed out after ${timeoutMs}ms`));
66
+ }, timeoutMs);
67
+ ws.on("open", () => {
68
+ const payload = {
69
+ requestId,
70
+ projectId,
71
+ eventModel,
72
+ qp: typeof qp === "string" ? qp : JSON.stringify(qp),
73
+ searchSource: options.searchSource || "reportQuery",
74
+ querySource: options.querySource || "single_report",
75
+ contentTranslate: options.contentTranslate !== false,
76
+ ...options.extra || {}
77
+ };
78
+ ws.send(JSON.stringify(["data", payload, { channel: "ta" }]));
79
+ });
80
+ ws.on("message", (raw) => {
81
+ try {
82
+ const msg = JSON.parse(raw.toString());
83
+ if (Array.isArray(msg) && msg.length >= 2) {
84
+ const data = msg[1];
85
+ if (data.requestId === requestId && data.progress === 100) {
86
+ clearTimeout(timer);
87
+ ws.close();
88
+ resolve(data);
89
+ }
90
+ }
91
+ } catch {
92
+ }
93
+ });
94
+ ws.on("error", (err) => {
95
+ clearTimeout(timer);
96
+ reject(err);
97
+ });
98
+ ws.on("close", () => {
99
+ clearTimeout(timer);
100
+ });
101
+ });
102
+ }
103
+ async function wsQuery(projectId, requestId, qp, eventModel, options = {}, hostUrl) {
104
+ const resolvedHost = resolveHost(hostUrl);
105
+ const token = await getToken(resolvedHost);
106
+ try {
107
+ return await wsQueryOnce(projectId, requestId, qp, eventModel, options, token, resolvedHost);
108
+ } catch (err) {
109
+ const msg = err.message || "";
110
+ if (msg.includes("401") || msg.includes("403") || msg.includes("auth")) {
111
+ clearToken(resolvedHost);
112
+ const newToken = await getToken(resolvedHost);
113
+ return wsQueryOnce(projectId, requestId, qp, eventModel, options, newToken, resolvedHost);
114
+ }
115
+ throw err;
116
+ }
117
+ }
118
+ async function querySql(projectId, sql, hostUrl) {
119
+ const requestId = genRequestId("sqlIde");
120
+ const qp = {
121
+ events: { sql },
122
+ eventView: { sqlViewParams: [] }
123
+ };
124
+ return wsQuery(projectId, requestId, qp, 10, {
125
+ searchSource: "sqlIde",
126
+ querySource: "sqlIde"
127
+ }, hostUrl);
128
+ }
129
+ async function queryReportData(projectId, reportId, qp, eventModel, options = {}, hostUrl) {
130
+ const dashboardId = options.dashboardId || 0;
131
+ const requestId = genRequestId(`${projectId}_${dashboardId}_${reportId}`);
132
+ return wsQuery(projectId, requestId, qp, eventModel, {
133
+ searchSource: options.searchSource || "model_search",
134
+ querySource: options.querySource || "module",
135
+ ...options
136
+ }, hostUrl);
137
+ }
138
+
139
+ export {
140
+ httpGet,
141
+ httpPost,
142
+ wsQuery,
143
+ querySql,
144
+ queryReportData
145
+ };
@@ -0,0 +1,149 @@
1
+ import {
2
+ clearToken,
3
+ getToken,
4
+ resolveHost
5
+ } from "./chunk-P7NQZGSZ.js";
6
+
7
+ // src/core/client.ts
8
+ import WebSocket from "ws";
9
+ import { randomBytes } from "crypto";
10
+ function genRequestId(prefix) {
11
+ const rand = randomBytes(4).toString("base64url").slice(0, 8);
12
+ return `${prefix}@@${rand}`;
13
+ }
14
+ function buildUrl(baseUrl, modulePath, params = {}) {
15
+ const base = baseUrl.replace(/\/$/, "");
16
+ const p = modulePath.startsWith("/") ? modulePath : `/${modulePath}`;
17
+ const url = new URL(`${base}${p}`);
18
+ for (const [k, v] of Object.entries(params)) {
19
+ if (v !== void 0 && v !== null) url.searchParams.set(k, String(v));
20
+ }
21
+ return url.toString();
22
+ }
23
+ function buildWsUrl(baseUrl, token) {
24
+ const base = baseUrl.replace(/\/$/, "");
25
+ const wsBase = base.replace(/^https:\/\//, "wss://").replace(/^http:\/\//, "ws://");
26
+ return `${wsBase}/v1/ta-websocket/query/${token}`;
27
+ }
28
+ async function request(method, modulePath, params = {}, body = null, retry = true, hostUrl) {
29
+ const resolvedHost = resolveHost(hostUrl);
30
+ const token = await getToken(resolvedHost);
31
+ const url = buildUrl(resolvedHost, modulePath, params);
32
+ const headers = {
33
+ "Authorization": `bearer ${token}`,
34
+ "Content-Type": "application/json"
35
+ };
36
+ const options = { method, headers };
37
+ if (body && method !== "GET") options.body = JSON.stringify(body);
38
+ if (process.env.TE_DEBUG) {
39
+ console.error(`[DEBUG] ${method} ${url}`);
40
+ console.error(`[DEBUG] body: ${options.body ?? "(none)"}`);
41
+ }
42
+ const resp = await fetch(url, options);
43
+ if ((resp.status === 401 || resp.status === 403) && retry) {
44
+ clearToken(resolvedHost);
45
+ return request(method, modulePath, params, body, false, resolvedHost);
46
+ }
47
+ const data = await resp.json();
48
+ if (data.return_code === -1001 && retry) {
49
+ clearToken(resolvedHost);
50
+ return request(method, modulePath, params, body, false, resolvedHost);
51
+ }
52
+ if (data.return_code !== 0 && data.return_code !== void 0) {
53
+ throw new Error(`TE API error: ${data.return_message || "unknown"} (code: ${data.return_code})`);
54
+ }
55
+ return data.data !== void 0 ? data.data : data;
56
+ }
57
+ async function httpGet(modulePath, params = {}, hostUrl) {
58
+ return request("GET", modulePath, params, null, true, hostUrl);
59
+ }
60
+ async function httpPost(modulePath, params = {}, body, hostUrl) {
61
+ return request("POST", modulePath, params, body ?? {}, true, hostUrl);
62
+ }
63
+ async function wsQueryOnce(projectId, requestId, qp, eventModel, options = {}, token, hostUrl, timeoutMs = 3e4) {
64
+ const wsUrl = buildWsUrl(hostUrl, token);
65
+ return new Promise((resolve, reject) => {
66
+ const ws = new WebSocket(wsUrl);
67
+ const timer = setTimeout(() => {
68
+ ws.close();
69
+ reject(new Error(`WebSocket query timed out after ${timeoutMs}ms`));
70
+ }, timeoutMs);
71
+ ws.on("open", () => {
72
+ const payload = {
73
+ requestId,
74
+ projectId,
75
+ eventModel,
76
+ qp: typeof qp === "string" ? qp : JSON.stringify(qp),
77
+ searchSource: options.searchSource || "reportQuery",
78
+ querySource: options.querySource || "single_report",
79
+ contentTranslate: options.contentTranslate !== false,
80
+ ...options.extra || {}
81
+ };
82
+ ws.send(JSON.stringify(["data", payload, { channel: "ta" }]));
83
+ });
84
+ ws.on("message", (raw) => {
85
+ try {
86
+ const msg = JSON.parse(raw.toString());
87
+ if (Array.isArray(msg) && msg.length >= 2) {
88
+ const data = msg[1];
89
+ if (data.requestId === requestId && data.progress === 100) {
90
+ clearTimeout(timer);
91
+ ws.close();
92
+ resolve(data);
93
+ }
94
+ }
95
+ } catch {
96
+ }
97
+ });
98
+ ws.on("error", (err) => {
99
+ clearTimeout(timer);
100
+ reject(err);
101
+ });
102
+ ws.on("close", () => {
103
+ clearTimeout(timer);
104
+ });
105
+ });
106
+ }
107
+ async function wsQuery(projectId, requestId, qp, eventModel, options = {}, hostUrl) {
108
+ const resolvedHost = resolveHost(hostUrl);
109
+ const token = await getToken(resolvedHost);
110
+ try {
111
+ return await wsQueryOnce(projectId, requestId, qp, eventModel, options, token, resolvedHost);
112
+ } catch (err) {
113
+ const msg = err.message || "";
114
+ if (msg.includes("401") || msg.includes("403") || msg.includes("auth")) {
115
+ clearToken(resolvedHost);
116
+ const newToken = await getToken(resolvedHost);
117
+ return wsQueryOnce(projectId, requestId, qp, eventModel, options, newToken, resolvedHost);
118
+ }
119
+ throw err;
120
+ }
121
+ }
122
+ async function querySql(projectId, sql, hostUrl) {
123
+ const requestId = genRequestId("sqlIde");
124
+ const qp = {
125
+ events: { sql },
126
+ eventView: { sqlViewParams: [] }
127
+ };
128
+ return wsQuery(projectId, requestId, qp, 10, {
129
+ searchSource: "sqlIde",
130
+ querySource: "sqlIde"
131
+ }, hostUrl);
132
+ }
133
+ async function queryReportData(projectId, reportId, qp, eventModel, options = {}, hostUrl) {
134
+ const dashboardId = options.dashboardId || 0;
135
+ const requestId = genRequestId(`${projectId}_${dashboardId}_${reportId}`);
136
+ return wsQuery(projectId, requestId, qp, eventModel, {
137
+ searchSource: options.searchSource || "model_search",
138
+ querySource: options.querySource || "module",
139
+ ...options
140
+ }, hostUrl);
141
+ }
142
+
143
+ export {
144
+ httpGet,
145
+ httpPost,
146
+ wsQuery,
147
+ querySql,
148
+ queryReportData
149
+ };
@@ -0,0 +1,16 @@
1
+ import {
2
+ httpGet,
3
+ httpPost,
4
+ queryReportData,
5
+ querySql,
6
+ wsQuery
7
+ } from "./chunk-GZG7YDWV.js";
8
+ import "./chunk-P7NQZGSZ.js";
9
+ import "./chunk-CFCHSAMQ.js";
10
+ export {
11
+ httpGet,
12
+ httpPost,
13
+ queryReportData,
14
+ querySql,
15
+ wsQuery
16
+ };
@@ -0,0 +1,16 @@
1
+ import {
2
+ httpGet,
3
+ httpPost,
4
+ queryReportData,
5
+ querySql,
6
+ wsQuery
7
+ } from "./chunk-4OJI46I5.js";
8
+ import "./chunk-P7NQZGSZ.js";
9
+ import "./chunk-CFCHSAMQ.js";
10
+ export {
11
+ httpGet,
12
+ httpPost,
13
+ queryReportData,
14
+ querySql,
15
+ wsQuery
16
+ };