Skip to content

Commit 96d78f1

Browse files
committed
feat(cls): support dashboard
1 parent f44a0cc commit 96d78f1

File tree

3 files changed

+361
-18
lines changed

3 files changed

+361
-18
lines changed

__tests__/cls/dashboard.test.ts

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
import { DeployDashboardInputs } from '../../src/modules/cls/interface';
2+
import { Cls } from '../../src';
3+
4+
// TODO: 添加更多的图形测试用例,目前 CLS 产品并未相关说明文档
5+
describe('Cls dashboard', () => {
6+
const credentials = {
7+
SecretId: process.env.TENCENT_SECRET_ID,
8+
SecretKey: process.env.TENCENT_SECRET_KEY,
9+
};
10+
const clsConfig = {
11+
logsetId: '5681feab-fae1-4a50-b41b-fb93d565d1fc',
12+
topicId: 'c429f9ca-8229-4cef-9d63-dd9ad6189f4c',
13+
};
14+
// * | 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',
17+
title: 'Request URL Time',
18+
type: 'bar',
19+
query: '* | select url,request_time',
20+
yAxis: 'request_time',
21+
yAxisUnit: 'ms',
22+
aggregateKey: 'url',
23+
};
24+
const chart2Config = {
25+
id: 'chart-ed7a85c7-5327-4763-93c0-b137be676258',
26+
title: '4xx Code',
27+
type: 'bar',
28+
query:
29+
'* | select error_code, count(*) as count where error_code > 400 and error_code < 500 group by error_code',
30+
yAxis: 'count',
31+
yAxisUnit: '',
32+
aggregateKey: 'error_code',
33+
};
34+
const client = new Cls(credentials, process.env.REGION);
35+
36+
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+
}),
106+
};
107+
108+
let dashboardId = '';
109+
110+
test('deploy dashboard', async () => {
111+
const res = await client.deployDashboard(inputs);
112+
expect(res).toEqual({
113+
id: expect.stringContaining('dashboard-'),
114+
name: inputs.name,
115+
data: inputs.data,
116+
});
117+
118+
dashboardId = res.id;
119+
});
120+
121+
test('get dashboard list', async () => {
122+
const res = await client.getDashboardList();
123+
expect(res[0]).toEqual({
124+
createTime: expect.any(String),
125+
id: expect.stringContaining('dashboard-'),
126+
name: expect.any(String),
127+
data: expect.any(String),
128+
});
129+
});
130+
131+
test('get dashboard detail by id', async () => {
132+
const res = await client.getDashboardDetail({
133+
id: dashboardId,
134+
});
135+
expect(res).toEqual({
136+
createTime: expect.any(String),
137+
id: expect.stringContaining('dashboard-'),
138+
name: expect.any(String),
139+
data: expect.any(String),
140+
});
141+
});
142+
143+
test('get dashboard detail by name', async () => {
144+
const res = await client.getDashboardDetail({
145+
name: inputs.name,
146+
});
147+
expect(res).toEqual({
148+
createTime: expect.any(String),
149+
id: expect.stringContaining('dashboard-'),
150+
name: expect.any(String),
151+
data: expect.any(String),
152+
});
153+
});
154+
155+
test('remove dashboard', async () => {
156+
const res = await client.removeDashboard(inputs);
157+
expect(res).toEqual(true);
158+
});
159+
});

0 commit comments

Comments
 (0)