Skip to content

Commit 4f94266

Browse files
committed
feat: improve cls api, add types
1 parent 87b2ef2 commit 4f94266

File tree

5 files changed

+41
-288
lines changed

5 files changed

+41
-288
lines changed

__tests__/cls/cls.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ describe('Cls', () => {
9393
await sleep(5000);
9494
await client.remove(outputs);
9595

96-
const detail = await client.cls.getTopic({
96+
const detail = await client.clsClient.getTopic({
9797
topic_id: outputs.topicId,
9898
});
9999

__tests__/cls/dashboard.test.ts

Lines changed: 29 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,159 +1,96 @@
1-
import { DeployDashboardInputs } from '../../src/modules/cls/interface';
21
import { Cls } from '../../src';
2+
import {
3+
DashboardChartType,
4+
DeployDashboardInputs,
5+
DeployDashChartInputs,
6+
} from '../../src/modules/cls/dashboard';
37

48
// TODO: 添加更多的图形测试用例,目前 CLS 产品并未相关说明文档
59
describe('Cls dashboard', () => {
610
const credentials = {
711
SecretId: process.env.TENCENT_SECRET_ID,
812
SecretKey: process.env.TENCENT_SECRET_KEY,
913
};
10-
const clsConfig = {
14+
const logsetConfig = {
15+
region: process.env.REGION,
1116
logsetId: '5681feab-fae1-4a50-b41b-fb93d565d1fc',
1217
topicId: 'c429f9ca-8229-4cef-9d63-dd9ad6189f4c',
1318
};
1419
// * | select SCF_StartTime as time, max(SCF_MemUsage) / 1000000 as memory group by SCF_StartTime
15-
const chart1Config = {
16-
id: 'chart-d0a90626-0a73-466c-8a94-2f5bdc597abd',
20+
const chart1Config: DeployDashChartInputs = {
1721
title: 'Request URL Time',
18-
type: 'bar',
22+
type: 'bar' as DashboardChartType,
1923
query: '* | select url,request_time',
20-
yAxis: 'request_time',
21-
yAxisUnit: 'ms',
24+
yAxisKey: 'request_time',
2225
aggregateKey: 'url',
2326
};
24-
const chart2Config = {
25-
id: 'chart-ed7a85c7-5327-4763-93c0-b137be676258',
27+
const chart2Config: DeployDashChartInputs = {
2628
title: '4xx Code',
27-
type: 'bar',
29+
type: 'bar' as DashboardChartType,
2830
query:
2931
'* | select error_code, count(*) as count where error_code > 400 and error_code < 500 group by error_code',
30-
yAxis: 'count',
31-
yAxisUnit: '',
32+
yAxisKey: 'count',
3233
aggregateKey: 'error_code',
3334
};
34-
const client = new Cls(credentials, process.env.REGION);
35+
const cls = new Cls(credentials, process.env.REGION);
3536

3637
const inputs: DeployDashboardInputs = {
37-
name: 'serverless-unit-test',
38-
data: JSON.stringify({
39-
panels: [
40-
{
41-
id: chart1Config.id,
42-
title: chart1Config.title,
43-
description: null,
44-
gridPos: { x: 0, y: 0, w: 12, h: 8 },
45-
type: chart1Config.type,
46-
target: {
47-
RegionId: 1,
48-
LogsetId: clsConfig.logsetId,
49-
TopicId: clsConfig.topicId,
50-
Query: chart1Config.query,
51-
ChartAxis: {
52-
xAxisKey: '',
53-
yAxisKeys: [chart1Config.yAxis],
54-
aggregateKey: chart1Config.aggregateKey,
55-
},
56-
},
57-
chartConfig: {
58-
orientation: true,
59-
unit: chart1Config.yAxisUnit,
60-
options: { dataLinks: [] },
61-
legend: { show: false },
62-
xAxis: { position: 'bottom', axisLabel: {} },
63-
yAxis: { position: 'top' },
64-
type: 'basicBar',
65-
staticStyle: 'current',
66-
sort: -1,
67-
decimals: null,
68-
id: chart1Config.id,
69-
},
70-
fieldConfig: { defaults: {}, overrides: [] },
71-
},
72-
{
73-
id: chart2Config.id,
74-
title: chart2Config.title,
75-
description: null,
76-
gridPos: { x: 0, y: 0, w: 12, h: 8 },
77-
type: chart2Config.type,
78-
target: {
79-
RegionId: 1,
80-
LogsetId: clsConfig.logsetId,
81-
TopicId: clsConfig.topicId,
82-
Query: chart2Config.query,
83-
ChartAxis: {
84-
xAxisKey: '',
85-
yAxisKeys: [chart2Config.yAxis],
86-
aggregateKey: chart2Config.aggregateKey,
87-
},
88-
},
89-
chartConfig: {
90-
orientation: true,
91-
unit: chart2Config.yAxisUnit,
92-
options: { dataLinks: [] },
93-
legend: { show: false },
94-
xAxis: { position: 'bottom', axisLabel: {} },
95-
yAxis: { position: 'top' },
96-
type: 'basicBar',
97-
staticStyle: 'current',
98-
sort: -1,
99-
decimals: null,
100-
id: chart2Config.id,
101-
},
102-
fieldConfig: { defaults: {}, overrides: [] },
103-
},
104-
],
105-
}),
38+
name: 'serverless-unit-test-dashboard',
39+
chartList: [chart1Config, chart2Config],
10640
};
10741

10842
let dashboardId = '';
10943

11044
test('deploy dashboard', async () => {
111-
const res = await client.deployDashboard(inputs);
45+
const res = await cls.dashboard.deploy(inputs, logsetConfig);
11246
expect(res).toEqual({
11347
id: expect.stringContaining('dashboard-'),
11448
name: inputs.name,
115-
data: inputs.data,
49+
chartList: expect.any(Array),
11650
});
11751

11852
dashboardId = res.id;
53+
54+
console.log({ dashboardId });
11955
});
12056

12157
test('get dashboard list', async () => {
122-
const res = await client.getDashboardList();
58+
const res = await cls.dashboard.getList();
12359
expect(res[0]).toEqual({
12460
createTime: expect.any(String),
12561
id: expect.stringContaining('dashboard-'),
12662
name: expect.any(String),
127-
data: expect.any(String),
63+
chartList: expect.any(Array),
12864
});
12965
});
13066

13167
test('get dashboard detail by id', async () => {
132-
const res = await client.getDashboardDetail({
68+
console.log({ dashboardId });
69+
const res = await cls.dashboard.getDetail({
13370
id: dashboardId,
13471
});
13572
expect(res).toEqual({
136-
createTime: expect.any(String),
13773
id: expect.stringContaining('dashboard-'),
13874
name: expect.any(String),
139-
data: expect.any(String),
75+
createTime: expect.any(String),
76+
chartList: expect.any(Array),
14077
});
14178
});
14279

14380
test('get dashboard detail by name', async () => {
144-
const res = await client.getDashboardDetail({
81+
const res = await cls.dashboard.getDetail({
14582
name: inputs.name,
14683
});
14784
expect(res).toEqual({
14885
createTime: expect.any(String),
14986
id: expect.stringContaining('dashboard-'),
15087
name: expect.any(String),
151-
data: expect.any(String),
88+
chartList: expect.any(Array),
15289
});
15390
});
15491

15592
test('remove dashboard', async () => {
156-
const res = await client.removeDashboard(inputs);
93+
const res = await cls.dashboard.remove(inputs);
15794
expect(res).toEqual(true);
15895
});
15996
});

src/modules/cls/dashboard.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export class ClsDashboard {
195195
}
196196

197197
// 获取 dashboard 列表
198-
async getDashboardList(): Promise<Dashboard[]> {
198+
async getList(): Promise<Dashboard[]> {
199199
const res = await this.cls.clsClient.request({
200200
method: 'GET',
201201
path: '/dashboards',
@@ -224,13 +224,7 @@ export class ClsDashboard {
224224
}
225225

226226
// 获取 dashboard 详情
227-
async getDashboardDetail({
228-
name,
229-
id,
230-
}: {
231-
name?: string;
232-
id?: string;
233-
}): Promise<Dashboard | undefined> {
227+
async getDetail({ name, id }: { name?: string; id?: string }): Promise<Dashboard | undefined> {
234228
if (id) {
235229
const res = await this.cls.clsClient.request({
236230
method: 'GET',
@@ -263,7 +257,7 @@ export class ClsDashboard {
263257
};
264258
}
265259
if (name) {
266-
const list = await this.getDashboardList();
260+
const list = await this.getList();
267261
const exist = list.find((item) => item.name === name);
268262
if (exist) {
269263
return exist;
@@ -277,7 +271,7 @@ export class ClsDashboard {
277271
}
278272

279273
// 删除 dashboard
280-
async removeDashboard({ id, name }: RemoveDashboardInputs) {
274+
async remove({ id, name }: RemoveDashboardInputs) {
281275
if (!id && !name) {
282276
throw new ApiError({
283277
type: 'API_removeDashboard',
@@ -286,7 +280,7 @@ export class ClsDashboard {
286280
}
287281
if (!id) {
288282
// 通过名称查找ID
289-
const exist = await this.getDashboardDetail({ name });
283+
const exist = await this.getDetail({ name });
290284
if (!exist) {
291285
console.log(`Dashboard ${name} not exist`);
292286

@@ -314,7 +308,7 @@ export class ClsDashboard {
314308
}
315309

316310
// 创建 dashboard
317-
async deployDashboard(inputs: DeployDashboardInputs, logsetConfig: LogsetConfig) {
311+
async deploy(inputs: DeployDashboardInputs, logsetConfig: LogsetConfig) {
318312
const { name, chartList } = inputs;
319313
const data = JSON.stringify({
320314
panels: chartList.map((v) => {
@@ -341,7 +335,7 @@ export class ClsDashboard {
341335
});
342336

343337
// 1. 检查是否存在同名 dashboard
344-
const exist = await this.getDashboardDetail({ name });
338+
const exist = await this.getDetail({ name });
345339
let dashboardId = '';
346340
// 2. 如果不存在则创建,否则更新
347341
if (exist) {
@@ -382,7 +376,7 @@ export class ClsDashboard {
382376
return {
383377
id: dashboardId,
384378
name,
385-
outputs: inputs,
379+
chartList: inputs.chartList,
386380
};
387381
}
388382
}

0 commit comments

Comments
 (0)