@lark-apaas/miaoda-cli 0.1.2 → 0.1.3

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.
Files changed (55) hide show
  1. package/README.md +8 -7
  2. package/dist/api/app/api.js +25 -0
  3. package/dist/api/app/index.js +15 -0
  4. package/dist/api/app/schemas.js +79 -0
  5. package/dist/api/app/types.js +58 -0
  6. package/dist/api/deploy/api.js +60 -0
  7. package/dist/api/deploy/index.js +16 -0
  8. package/dist/api/deploy/schemas.js +105 -0
  9. package/dist/api/deploy/types.js +22 -0
  10. package/dist/api/index.js +7 -1
  11. package/dist/api/observability/api.js +52 -0
  12. package/dist/api/observability/index.js +16 -0
  13. package/dist/api/observability/schemas.js +60 -0
  14. package/dist/api/observability/types.js +27 -0
  15. package/dist/cli/commands/app/index.js +62 -0
  16. package/dist/cli/commands/db/index.js +6 -5
  17. package/dist/cli/commands/deploy/index.js +155 -0
  18. package/dist/cli/commands/file/index.js +6 -5
  19. package/dist/cli/commands/index.js +10 -6
  20. package/dist/cli/commands/observability/index.js +240 -0
  21. package/dist/cli/commands/shared.js +83 -7
  22. package/dist/cli/handlers/app/get.js +47 -0
  23. package/dist/cli/handlers/app/index.js +7 -0
  24. package/dist/cli/handlers/app/update.js +59 -0
  25. package/dist/cli/handlers/db/data.js +2 -3
  26. package/dist/cli/handlers/db/schema.js +2 -3
  27. package/dist/cli/handlers/db/sql.js +1 -2
  28. package/dist/cli/handlers/deploy/deploy.js +84 -0
  29. package/dist/cli/handlers/deploy/error-log.js +60 -0
  30. package/dist/cli/handlers/deploy/format.js +39 -0
  31. package/dist/cli/handlers/deploy/get.js +71 -0
  32. package/dist/cli/handlers/deploy/helpers.js +41 -0
  33. package/dist/cli/handlers/deploy/history.js +70 -0
  34. package/dist/cli/handlers/deploy/index.js +14 -0
  35. package/dist/cli/handlers/deploy/polling.js +162 -0
  36. package/dist/cli/handlers/file/cp.js +1 -2
  37. package/dist/cli/handlers/file/ls.js +1 -2
  38. package/dist/cli/handlers/file/rm.js +1 -2
  39. package/dist/cli/handlers/file/sign.js +1 -2
  40. package/dist/cli/handlers/file/stat.js +1 -2
  41. package/dist/cli/handlers/observability/analytics.js +212 -0
  42. package/dist/cli/handlers/observability/helpers.js +66 -0
  43. package/dist/cli/handlers/observability/index.js +12 -0
  44. package/dist/cli/handlers/observability/log.js +94 -0
  45. package/dist/cli/handlers/observability/metric.js +208 -0
  46. package/dist/cli/handlers/observability/trace.js +102 -0
  47. package/dist/main.js +6 -2
  48. package/dist/utils/args.js +8 -0
  49. package/dist/utils/devops-error.js +28 -0
  50. package/dist/utils/git.js +29 -0
  51. package/dist/utils/http.js +118 -0
  52. package/dist/utils/index.js +13 -1
  53. package/dist/utils/output.js +338 -7
  54. package/dist/utils/time.js +203 -0
  55. package/package.json +7 -5
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_POLL_INTERVAL_MS = exports.waitForPipeline = exports.handleDeployErrorLog = exports.handleDeployHistory = exports.handleDeployGet = exports.handleDeploy = void 0;
4
+ var deploy_1 = require("./deploy");
5
+ Object.defineProperty(exports, "handleDeploy", { enumerable: true, get: function () { return deploy_1.handleDeploy; } });
6
+ var get_1 = require("./get");
7
+ Object.defineProperty(exports, "handleDeployGet", { enumerable: true, get: function () { return get_1.handleDeployGet; } });
8
+ var history_1 = require("./history");
9
+ Object.defineProperty(exports, "handleDeployHistory", { enumerable: true, get: function () { return history_1.handleDeployHistory; } });
10
+ var error_log_1 = require("./error-log");
11
+ Object.defineProperty(exports, "handleDeployErrorLog", { enumerable: true, get: function () { return error_log_1.handleDeployErrorLog; } });
12
+ var polling_1 = require("./polling");
13
+ Object.defineProperty(exports, "waitForPipeline", { enumerable: true, get: function () { return polling_1.waitForPipeline; } });
14
+ Object.defineProperty(exports, "DEFAULT_POLL_INTERVAL_MS", { enumerable: true, get: function () { return polling_1.DEFAULT_POLL_INTERVAL_MS; } });
@@ -0,0 +1,162 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.DEFAULT_POLL_INTERVAL_MS = void 0;
40
+ exports.waitForPipeline = waitForPipeline;
41
+ const ora_1 = __importDefault(require("ora"));
42
+ const api = __importStar(require("../../../api/index"));
43
+ const error_1 = require("../../../utils/error");
44
+ const output_1 = require("../../../utils/output");
45
+ const index_1 = require("../../../api/deploy/index");
46
+ /** 默认轮询间隔(毫秒) */
47
+ exports.DEFAULT_POLL_INTERVAL_MS = 2_000;
48
+ const TERMINAL_STATUSES = [
49
+ index_1.NodeStatus.SUCCESS,
50
+ index_1.NodeStatus.FAILED,
51
+ index_1.NodeStatus.CANCELED,
52
+ ];
53
+ function defaultSleep(ms) {
54
+ return new Promise((res) => setTimeout(res, ms));
55
+ }
56
+ /**
57
+ * 轮询 pipeline 状态直到终态或超时。
58
+ *
59
+ * - JSON 模式:轮询期间静默;终态后由 caller 输出最终信封。
60
+ * - Pretty 非 TTY:同 JSON。
61
+ * - Pretty TTY:底部行用 braille spinner 显示当前正在跑的 stage(每 80ms 跳一帧),
62
+ * stage 转终态时把 ✓ stage(12.3s) 打到 spinner 上方然后清掉 spinner 行。
63
+ *
64
+ * 终态返回最终 PipelineInstance,调用方再决定怎么 emit。
65
+ */
66
+ async function waitForPipeline(opts) {
67
+ const interval = opts.intervalMs ?? exports.DEFAULT_POLL_INTERVAL_MS;
68
+ const timeoutMs = opts.timeoutSec * 1000;
69
+ const sleep = opts.sleep ?? defaultSleep;
70
+ const now = opts.now ?? Date.now;
71
+ const start = now();
72
+ // ora 自身在非 TTY 下会降级为不动画,但我们额外加 isJsonMode 闸门:JSON / 字段选择
73
+ // 模式下完全静默。
74
+ const showProgress = !(0, output_1.isJsonMode)() && process.stdout.isTTY;
75
+ const printedStages = new Set();
76
+ const spinner = showProgress
77
+ ? (0, ora_1.default)({ text: "等待调度", spinner: "dots", stream: process.stdout }).start()
78
+ : null;
79
+ try {
80
+ for (;;) {
81
+ const resp = await api.deploy.queryPipelineInstance({
82
+ appID: opts.appID,
83
+ instanceID: opts.pipelineTaskID,
84
+ });
85
+ const detail = normalizePipelineInstance(resp);
86
+ if (spinner)
87
+ advanceProgress(detail, printedStages, spinner);
88
+ if (TERMINAL_STATUSES.includes(detail.status)) {
89
+ return detail;
90
+ }
91
+ if (now() - start >= timeoutMs) {
92
+ throw new error_1.AppError("DEPLOY_TIMEOUT", `轮询发布状态超时(${String(opts.timeoutSec)}s)`, {
93
+ retryable: true,
94
+ next_actions: [
95
+ `Run \`miaoda deploy get <deploy-id>\` 查看最新状态`,
96
+ `加大 --timeout 后重试`,
97
+ ],
98
+ });
99
+ }
100
+ await sleep(interval);
101
+ }
102
+ }
103
+ finally {
104
+ spinner?.stop();
105
+ }
106
+ }
107
+ /**
108
+ * 每轮 poll 的进度推进:刚转终态的 stage 用 stopAndPersist 落定为永久行,
109
+ * 再把 spinner 文本切到当前 running stage;没有 running 时显示"等待调度"。
110
+ */
111
+ function advanceProgress(detail, printed, spinner) {
112
+ const stages = Array.isArray(detail.stages) ? detail.stages : [];
113
+ for (const stage of stages) {
114
+ if (printed.has(stage.id))
115
+ continue;
116
+ if (!TERMINAL_STATUSES.includes(stage.status))
117
+ continue;
118
+ spinner.stopAndPersist({
119
+ symbol: stageSymbol(stage),
120
+ text: formatStageBody(stage),
121
+ });
122
+ printed.add(stage.id);
123
+ }
124
+ const running = stages.find((s) => s.status === index_1.NodeStatus.RUNNING);
125
+ const text = running ? `${running.name} 执行中` : "等待调度";
126
+ if (spinner.isSpinning) {
127
+ spinner.text = text;
128
+ }
129
+ else {
130
+ spinner.start(text);
131
+ }
132
+ }
133
+ function normalizePipelineInstance(resp) {
134
+ const keyedDetail = readPipelineDetail(resp, "pipelineInstanceDetail") ?? readPipelineDetail(resp, "detail");
135
+ if (keyedDetail)
136
+ return keyedDetail;
137
+ if (isPipelineInstance(resp))
138
+ return resp;
139
+ throw new error_1.AppError("DEPLOY_RESPONSE_INVALID", "发布状态响应缺少 pipeline detail", {
140
+ next_actions: ["使用 --verbose 重试以查看请求 logid,并联系平台排查响应结构"],
141
+ });
142
+ }
143
+ function readPipelineDetail(resp, key) {
144
+ if (!(key in resp))
145
+ return undefined;
146
+ const detail = resp[key];
147
+ return isPipelineInstance(detail) ? detail : undefined;
148
+ }
149
+ function isPipelineInstance(value) {
150
+ if (typeof value !== "object" || value === null)
151
+ return false;
152
+ const item = value;
153
+ return typeof item.status === "number";
154
+ }
155
+ function stageSymbol(stage) {
156
+ return stage.status === index_1.NodeStatus.SUCCESS ? "✓" : stage.status === index_1.NodeStatus.FAILED ? "✗" : "•";
157
+ }
158
+ function formatStageBody(stage) {
159
+ const cost = stage.costTime >= 0 ? `(${(stage.costTime / 1000).toFixed(1)}s)` : "";
160
+ const status = (0, index_1.nodeStatusText)(stage.status) ?? String(stage.status);
161
+ return `${stage.name}${cost ? ` ${cost}` : ""} ${status}`.trim();
162
+ }
@@ -44,7 +44,6 @@ const api = __importStar(require("../../../api/index"));
44
44
  const output_1 = require("../../../utils/output");
45
45
  const render_1 = require("../../../utils/render");
46
46
  const error_1 = require("../../../utils/error");
47
- const shared_1 = require("../../../cli/commands/shared");
48
47
  const colors_1 = require("../../../utils/colors");
49
48
  const MAX_UPLOAD_BYTES = 100 * 1024 * 1024;
50
49
  /**
@@ -233,7 +232,7 @@ async function handleDownload(appId, remoteRaw, localRaw) {
233
232
  }
234
233
  }
235
234
  async function handleFileCp(src, dst, opts) {
236
- const appId = (0, shared_1.resolveAppId)(opts);
235
+ const appId = opts.appId;
237
236
  // src 本地存在 → upload;其他情况(file_ref / `/path` / 裸文件名)→ download
238
237
  if (isLocalSrc(src)) {
239
238
  return handleUpload(appId, src, dst, opts.rename);
@@ -38,7 +38,6 @@ const api = __importStar(require("../../../api/index"));
38
38
  const output_1 = require("../../../utils/output");
39
39
  const render_1 = require("../../../utils/render");
40
40
  const error_1 = require("../../../utils/error");
41
- const shared_1 = require("../../../cli/commands/shared");
42
41
  const index_1 = require("../../../api/file/index");
43
42
  /**
44
43
  * 把位置参数 `query` 路由到 `--path` 或 `--name`:
@@ -65,7 +64,7 @@ function resolveQueryRouting(opts) {
65
64
  return { name: opts.query };
66
65
  }
67
66
  async function handleFileLs(opts) {
68
- const appId = (0, shared_1.resolveAppId)(opts);
67
+ const appId = opts.appId;
69
68
  // commander 已经把 --limit 解析为 number;保留 ?? undefined 兼容老调用
70
69
  const limit = opts.limit;
71
70
  const sizeGt = opts.sizeGt ? (0, render_1.parseSize)(opts.sizeGt) : undefined;
@@ -40,7 +40,6 @@ exports.handleFileRm = handleFileRm;
40
40
  const api = __importStar(require("../../../api/index"));
41
41
  const output_1 = require("../../../utils/output");
42
42
  const error_1 = require("../../../utils/error");
43
- const shared_1 = require("../../../cli/commands/shared");
44
43
  const index_1 = require("../../../api/file/index");
45
44
  const render_1 = require("../../../utils/render");
46
45
  const colors_1 = require("../../../utils/colors");
@@ -133,7 +132,7 @@ async function handleFileRm(paths, opts) {
133
132
  if (totalCount > MAX_BATCH) {
134
133
  throw new error_1.AppError("FILE_BATCH_TOO_MANY", `Batch size ${String(totalCount)} exceeds the 100 limit`);
135
134
  }
136
- const appId = (0, shared_1.resolveAppId)(opts);
135
+ const appId = opts.appId;
137
136
  // destructive guardrail
138
137
  const tty = (0, render_1.isStdoutTty)();
139
138
  if (tty && !opts.yes) {
@@ -38,7 +38,6 @@ const api = __importStar(require("../../../api/index"));
38
38
  const output_1 = require("../../../utils/output");
39
39
  const render_1 = require("../../../utils/render");
40
40
  const error_1 = require("../../../utils/error");
41
- const shared_1 = require("../../../cli/commands/shared");
42
41
  const index_1 = require("../../../api/file/index");
43
42
  const MAX_EXPIRES_SECONDS = 30 * 86400;
44
43
  const DEFAULT_EXPIRES_SECONDS = 86400; // 1d(PRD 规定)
@@ -64,7 +63,7 @@ async function resolveFilePath(appId, input) {
64
63
  return resolved.file.path;
65
64
  }
66
65
  async function handleFileSign(file, opts) {
67
- const appId = (0, shared_1.resolveAppId)(opts);
66
+ const appId = opts.appId;
68
67
  let expiresSec = DEFAULT_EXPIRES_SECONDS;
69
68
  if (opts.expires) {
70
69
  expiresSec = (0, render_1.parseDuration)(opts.expires);
@@ -38,7 +38,6 @@ const api = __importStar(require("../../../api/index"));
38
38
  const output_1 = require("../../../utils/output");
39
39
  const render_1 = require("../../../utils/render");
40
40
  const error_1 = require("../../../utils/error");
41
- const shared_1 = require("../../../cli/commands/shared");
42
41
  const index_1 = require("../../../api/file/index");
43
42
  /**
44
43
  * 解析 `<file>` 为后端 head 可用的 filePath。
@@ -67,7 +66,7 @@ async function resolveFile(appId, input) {
67
66
  return head;
68
67
  }
69
68
  async function handleFileStat(file, opts) {
70
- const appId = (0, shared_1.resolveAppId)(opts);
69
+ const appId = opts.appId;
71
70
  const info = await resolveFile(appId, file);
72
71
  if ((0, output_1.isJsonMode)()) {
73
72
  (0, output_1.emitOk)(info);
@@ -0,0 +1,212 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.handleObservabilityAnalytics = handleObservabilityAnalytics;
37
+ const api = __importStar(require("../../../api/index"));
38
+ const output_1 = require("../../../utils/output");
39
+ const args_1 = require("../../../utils/args");
40
+ const helpers_1 = require("./helpers");
41
+ const GRANULARITY_TO_UNIT = {
42
+ day: "DAY",
43
+ daily: "DAY",
44
+ "1d": "DAY",
45
+ "1h": "HOUR",
46
+ hour: "HOUR",
47
+ week: "WEEK",
48
+ weekly: "WEEK",
49
+ "1w": "WEEK",
50
+ month: "MONTH",
51
+ monthly: "MONTH",
52
+ "1M": "MONTH",
53
+ };
54
+ /**
55
+ * <analytics-name> + --series → metricType → 表头列名(=对应 --series 取值)。
56
+ *
57
+ * - users 缺省返回 active/new/total 三条线(多线视图)。
58
+ * - page-view 当前 BAM 只有一个 PAGE_VIEW metricType;不同 series 通过 device_type 过滤区分;
59
+ * 表头 label 直接对应 --series 取值(all-view / desktop-view / mobile-view)。
60
+ */
61
+ const ANALYTICS_LABELS = {
62
+ users: {
63
+ ACTIVE_USER: "active-users",
64
+ NEW_USER: "new-users",
65
+ TOTAL_USER: "total-users",
66
+ },
67
+ };
68
+ /** miaoda observability analytics <analytics-name> */
69
+ async function handleObservabilityAnalytics(opts) {
70
+ if (!opts.analyticsName)
71
+ (0, args_1.failArgs)("<analytics-name> 必填");
72
+ const appID = opts.appId;
73
+ const { metricTypes, labelByMetric, extraFilters } = resolveAnalyticsSelection(opts.analyticsName, opts.series);
74
+ const timeAggregationUnit = opts.granularity
75
+ ? (GRANULARITY_TO_UNIT[opts.granularity] ?? opts.granularity.toUpperCase())
76
+ : "DAY";
77
+ const nowMs = Date.now();
78
+ const sinceMs = (0, helpers_1.parseToMs)(opts.since) ?? nowMs - 30 * 86_400_000;
79
+ const untilMs = (0, helpers_1.parseToMs)(opts.until) ?? nowMs;
80
+ const bucket = timeAggregationUnit;
81
+ const fieldFilters = (0, helpers_1.buildFieldFilters)([
82
+ { key: "path", value: opts.page ? (0, helpers_1.eqFilter)(opts.page) : undefined },
83
+ ...extraFilters,
84
+ ]);
85
+ const req = {
86
+ appID,
87
+ metricTypes,
88
+ startTimestampNs: (0, helpers_1.msToNs)(sinceMs),
89
+ endTimestampNs: (0, helpers_1.msToNs)((0, helpers_1.ceilMsToBucket)(untilMs, bucket)),
90
+ timeAggregationUnit,
91
+ fieldFilters,
92
+ };
93
+ const resp = await api.observability.queryAnalyticsData(req);
94
+ emitAnalyticsResponse(resp, labelByMetric);
95
+ }
96
+ // ── 渲染 ────────────
97
+ //
98
+ // BAM v1.0.123 起返回 points: [{timestampNs, dimensions, values: {metricType: v}}]——
99
+ // 服务端已把多 metricType 合并到同 timestampNs 的 row,下游直接平铺即可。
100
+ // JSON 模式:原样透传 points(保留 BAM 原 metricType 作为 values 的 key)。
101
+ // Pretty 模式:把 values 里的 metricType 重命名成 CLI label(active-users 等),
102
+ // 再走 buildAnalyticsPivotSchema 渲染。
103
+ function emitAnalyticsResponse(resp, labelByMetric) {
104
+ const points = resp.points ?? [];
105
+ // analytics 不分页,但补齐 has_more / next_cursor 与 log/trace 信封形状对齐
106
+ if ((0, output_1.isJsonMode)()) {
107
+ (0, output_1.emit)({ data: points, next_cursor: null, has_more: false });
108
+ return;
109
+ }
110
+ const { rows, seriesLabels } = renameAndSort(points, labelByMetric);
111
+ if (rows.length === 0) {
112
+ (0, output_1.emit)({ data: [], next_cursor: null, has_more: false });
113
+ return;
114
+ }
115
+ (0, output_1.emit)({ data: rows, next_cursor: null, has_more: false }, buildAnalyticsPivotSchema(seriesLabels));
116
+ }
117
+ function renameAndSort(points, labelByMetric) {
118
+ // 列顺序按 labelByMetric 声明顺序,避免 BAM 返回顺序波动影响表头
119
+ const seriesLabels = Object.values(labelByMetric);
120
+ const rows = points.map((point) => {
121
+ const row = { timestampNs: point.timestampNs };
122
+ for (const [metricType, value] of Object.entries(point.values)) {
123
+ const label = labelByMetric[metricType] ?? metricType;
124
+ row[label] = value;
125
+ }
126
+ return row;
127
+ });
128
+ rows.sort((a, b) => b.timestampNs - a.timestampNs);
129
+ return { rows, seriesLabels };
130
+ }
131
+ function buildAnalyticsPivotSchema(seriesLabels) {
132
+ return {
133
+ columns: [
134
+ { key: "timestampNs", label: "time", format: output_1.fmt.ns() },
135
+ ...seriesLabels.map((label) => ({ key: label, label })),
136
+ ],
137
+ strict: true,
138
+ };
139
+ }
140
+ /**
141
+ * <analytics-name> + --series → metricTypes[] + label 映射 + 附加过滤
142
+ *
143
+ * users:
144
+ * - 缺省 series → 三条线全返回(active-users/new-users/total-users)
145
+ * - 单值 series → 对应单条线
146
+ * page-view:
147
+ * - 缺省/all-view → 单条 PAGE_VIEW,无 device 过滤,label = "all-view"
148
+ * - desktop-view / mobile-view → 单条 PAGE_VIEW + device_type 过滤,label = --series 值
149
+ */
150
+ function resolveAnalyticsSelection(cliName, series) {
151
+ const extras = [];
152
+ const normalizedSeries = normalizeAnalyticsSeries(cliName, series);
153
+ if (cliName === "users") {
154
+ if (normalizedSeries === "active-users")
155
+ return single("ACTIVE_USER", "active-users", extras);
156
+ if (normalizedSeries === "new-users")
157
+ return single("NEW_USER", "new-users", extras);
158
+ if (normalizedSeries === "total-users")
159
+ return single("TOTAL_USER", "total-users", extras);
160
+ // 缺省:三条线
161
+ return all(ANALYTICS_LABELS.users, extras);
162
+ }
163
+ if (cliName === "page-view") {
164
+ if (normalizedSeries === "desktop-view") {
165
+ extras.push({ key: "device_type", value: (0, helpers_1.eqFilter)("desktop") });
166
+ return single("PAGE_VIEW", "desktop-view", extras);
167
+ }
168
+ if (normalizedSeries === "mobile-view") {
169
+ extras.push({ key: "device_type", value: (0, helpers_1.eqFilter)("mobile") });
170
+ return single("PAGE_VIEW", "mobile-view", extras);
171
+ }
172
+ // 缺省 / all-view → 不附加 device 过滤
173
+ return single("PAGE_VIEW", "all-view", extras);
174
+ }
175
+ // 兜底:CLI 名直接当 metricType + label
176
+ return single(cliName, cliName, extras);
177
+ }
178
+ function normalizeAnalyticsSeries(cliName, series) {
179
+ if (series === undefined)
180
+ return undefined;
181
+ if (cliName === "users") {
182
+ if (series === "active")
183
+ return "active-users";
184
+ if (series === "new")
185
+ return "new-users";
186
+ if (series === "total")
187
+ return "total-users";
188
+ }
189
+ if (cliName === "page-view") {
190
+ if (series === "all")
191
+ return "all-view";
192
+ if (series === "desktop")
193
+ return "desktop-view";
194
+ if (series === "mobile")
195
+ return "mobile-view";
196
+ }
197
+ return series;
198
+ }
199
+ function single(metricType, label, extras) {
200
+ return {
201
+ metricTypes: [metricType],
202
+ labelByMetric: { [metricType]: label },
203
+ extraFilters: extras,
204
+ };
205
+ }
206
+ function all(labelByMetric, extras) {
207
+ return {
208
+ metricTypes: Object.keys(labelByMetric),
209
+ labelByMetric,
210
+ extraFilters: extras,
211
+ };
212
+ }
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ceilMsToBucket = exports.floorMsToBucket = exports.msToSec = exports.msToNs = exports.parseToSec = exports.parseToNs = exports.parseToMs = exports.parseTimeToMs = void 0;
4
+ exports.eqFilter = eqFilter;
5
+ exports.rangeFilter = rangeFilter;
6
+ exports.fuzzyFilter = fuzzyFilter;
7
+ exports.buildFieldFilters = buildFieldFilters;
8
+ exports.validateLimit = validateLimit;
9
+ const args_1 = require("../../../utils/args");
10
+ const index_1 = require("../../../api/observability/index");
11
+ // 时间解析助手已抽到 utils/time.ts;这里 re-export 维持 observability handler 的旧 import 路径。
12
+ var time_1 = require("../../../utils/time");
13
+ Object.defineProperty(exports, "parseTimeToMs", { enumerable: true, get: function () { return time_1.parseTimeToMs; } });
14
+ Object.defineProperty(exports, "parseToMs", { enumerable: true, get: function () { return time_1.parseToMs; } });
15
+ Object.defineProperty(exports, "parseToNs", { enumerable: true, get: function () { return time_1.parseToNs; } });
16
+ Object.defineProperty(exports, "parseToSec", { enumerable: true, get: function () { return time_1.parseToSec; } });
17
+ Object.defineProperty(exports, "msToNs", { enumerable: true, get: function () { return time_1.msToNs; } });
18
+ Object.defineProperty(exports, "msToSec", { enumerable: true, get: function () { return time_1.msToSec; } });
19
+ Object.defineProperty(exports, "floorMsToBucket", { enumerable: true, get: function () { return time_1.floorMsToBucket; } });
20
+ Object.defineProperty(exports, "ceilMsToBucket", { enumerable: true, get: function () { return time_1.ceilMsToBucket; } });
21
+ // ── filter 构造 ──
22
+ //
23
+ // BAM FieldFilter 是「类型分桶 + 算子」结构(str/i64/double),不是扁平字段;
24
+ // 字符串字段无 server-side contains,模糊匹配走 fuzzyFilter。
25
+ /** 字符串字段等值过滤 → { str: { eq } } */
26
+ function eqFilter(value, type = "str") {
27
+ const v = type !== "str" ? Number(value) : value;
28
+ return { [type]: { eq: v } };
29
+ }
30
+ /** i64 数值范围过滤 → { i64: { gte, lte } };输入接受 string(CLI flag)或 number */
31
+ function rangeFilter(opts) {
32
+ const i64 = {};
33
+ if (opts.gte !== undefined)
34
+ i64.gte = Number(opts.gte);
35
+ if (opts.lte !== undefined)
36
+ i64.lte = Number(opts.lte);
37
+ return { i64 };
38
+ }
39
+ /** 模糊搜索过滤器;默认 AND 运算符 */
40
+ function fuzzyFilter(keyword, operator = index_1.WordOperator.AND) {
41
+ return { include: { words: [keyword], operator } };
42
+ }
43
+ /**
44
+ * 把若干条件按 key 装到 fieldFilters。值为 undefined 的条目跳过;
45
+ * 全部跳过时返回 undefined(避免传空 map)。
46
+ */
47
+ function buildFieldFilters(entries) {
48
+ const out = {};
49
+ let added = false;
50
+ for (const { key, value } of entries) {
51
+ if (value === undefined)
52
+ continue;
53
+ out[key] = value;
54
+ added = true;
55
+ }
56
+ return added ? out : undefined;
57
+ }
58
+ // ── 校验 ──
59
+ /** limit 校验(API 限制 1~100) */
60
+ function validateLimit(limit, defaultLimit = 50) {
61
+ const n = Number.isFinite(limit) ? limit : defaultLimit;
62
+ if (n <= 0 || n > 100) {
63
+ (0, args_1.failArgs)(`--limit 必须在 1~100 之间,收到 ${String(n)}`);
64
+ }
65
+ return n;
66
+ }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.handleObservabilityAnalytics = exports.handleObservabilityMetric = exports.handleObservabilityTraceGet = exports.handleObservabilityTraceList = exports.handleObservabilityLog = void 0;
4
+ var log_1 = require("./log");
5
+ Object.defineProperty(exports, "handleObservabilityLog", { enumerable: true, get: function () { return log_1.handleObservabilityLog; } });
6
+ var trace_1 = require("./trace");
7
+ Object.defineProperty(exports, "handleObservabilityTraceList", { enumerable: true, get: function () { return trace_1.handleObservabilityTraceList; } });
8
+ Object.defineProperty(exports, "handleObservabilityTraceGet", { enumerable: true, get: function () { return trace_1.handleObservabilityTraceGet; } });
9
+ var metric_1 = require("./metric");
10
+ Object.defineProperty(exports, "handleObservabilityMetric", { enumerable: true, get: function () { return metric_1.handleObservabilityMetric; } });
11
+ var analytics_1 = require("./analytics");
12
+ Object.defineProperty(exports, "handleObservabilityAnalytics", { enumerable: true, get: function () { return analytics_1.handleObservabilityAnalytics; } });
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.handleObservabilityLog = handleObservabilityLog;
37
+ const api = __importStar(require("../../../api/index"));
38
+ const output_1 = require("../../../utils/output");
39
+ const index_1 = require("../../../api/observability/index");
40
+ const helpers_1 = require("./helpers");
41
+ /** miaoda observability log */
42
+ async function handleObservabilityLog(opts) {
43
+ const appID = opts.appId;
44
+ const appEnv = "runtime";
45
+ const limit = (0, helpers_1.validateLimit)(opts.limit ?? 50);
46
+ // 过滤 key 对齐 BAM 查询字段名:
47
+ // - 顶层字段:severity_text / trace_id(输出仍映射成 severityText / traceID)
48
+ // - attributes 里的业务字段:module / user_id / page / api / ob_data_id(snake_case,
49
+ // 依据 BAM IDL 的 LogItem.attributes 描述:"包括 tenant_id, module, user_id, page, api 等")
50
+ // - duration_ms 暂未在 BAM 描述里明确列出,留 TODO 等 e2e 验证
51
+ const fieldFilters = (0, helpers_1.buildFieldFilters)([
52
+ { key: "severity_text", value: opts.level ? (0, helpers_1.eqFilter)(opts.level) : undefined },
53
+ { key: "attributes.ob_data_id", value: opts.logId ? (0, helpers_1.eqFilter)(opts.logId) : undefined },
54
+ { key: "trace_id", value: opts.traceId ? (0, helpers_1.eqFilter)(opts.traceId) : undefined },
55
+ { key: "module", value: opts.module ? (0, helpers_1.eqFilter)(opts.module) : undefined },
56
+ { key: "user_id", value: opts.userId ? (0, helpers_1.eqFilter)(opts.userId, "i64") : undefined },
57
+ { key: "page", value: opts.page ? (0, helpers_1.eqFilter)(opts.page) : undefined },
58
+ { key: "api", value: opts.api ? (0, helpers_1.eqFilter)(opts.api) : undefined },
59
+ {
60
+ // TODO: 确认 BAM 是否真用 duration_ms 作为 attribute key
61
+ key: "duration_ms",
62
+ value: opts.minDuration !== undefined || opts.maxDuration !== undefined
63
+ ? (0, helpers_1.rangeFilter)({ gte: opts.minDuration, lte: opts.maxDuration })
64
+ : undefined,
65
+ },
66
+ ]);
67
+ const fuzzyFilter = opts.grep
68
+ ? {
69
+ include: {
70
+ words: [opts.grep],
71
+ operator: 1, // AND
72
+ },
73
+ }
74
+ : undefined;
75
+ const req = {
76
+ appID,
77
+ appEnv,
78
+ startTimestampNs: (0, helpers_1.parseToNs)(opts.since),
79
+ endTimestampNs: (0, helpers_1.parseToNs)(opts.until),
80
+ limit,
81
+ pageToken: opts.cursor,
82
+ fieldFilters,
83
+ fuzzyFilter,
84
+ };
85
+ const resp = await api.observability.searchLogs(req);
86
+ emitSearchLogsResponse(resp);
87
+ }
88
+ function emitSearchLogsResponse(resp) {
89
+ (0, output_1.emit)({
90
+ data: resp.logItems ?? [],
91
+ next_cursor: resp.hasMore ? resp.nextPageToken : null,
92
+ has_more: resp.hasMore,
93
+ }, index_1.logItemSchema);
94
+ }