Skip to content

Commit 5dde009

Browse files
[Observability] Fixing dynamic return type based on the appName (#69894) (#69960)
* fixing generic return type * addressing pr comments
1 parent cdd6914 commit 5dde009

File tree

4 files changed

+385
-20
lines changed

4 files changed

+385
-20
lines changed
Lines changed: 365 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,365 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
import { registerDataHandler, getDataHandler } from './data_handler';
7+
8+
const params = {
9+
startTime: '0',
10+
endTime: '1',
11+
bucketSize: '10s',
12+
};
13+
14+
describe('registerDataHandler', () => {
15+
describe('APM', () => {
16+
registerDataHandler({
17+
appName: 'apm',
18+
fetchData: async () => {
19+
return {
20+
title: 'apm',
21+
appLink: '/apm',
22+
stats: {
23+
services: {
24+
label: 'services',
25+
type: 'number',
26+
value: 1,
27+
},
28+
transactions: {
29+
label: 'transactions',
30+
type: 'number',
31+
value: 1,
32+
},
33+
},
34+
series: {
35+
transactions: {
36+
label: 'transactions',
37+
coordinates: [{ x: 1 }],
38+
},
39+
},
40+
};
41+
},
42+
hasData: async () => true,
43+
});
44+
45+
it('registered data handler', () => {
46+
const dataHandler = getDataHandler('apm');
47+
expect(dataHandler?.fetchData).toBeDefined();
48+
expect(dataHandler?.hasData).toBeDefined();
49+
});
50+
51+
it('returns data when fetchData is called', async () => {
52+
const dataHandler = getDataHandler('apm');
53+
const response = await dataHandler?.fetchData(params);
54+
expect(response).toEqual({
55+
title: 'apm',
56+
appLink: '/apm',
57+
stats: {
58+
services: {
59+
label: 'services',
60+
type: 'number',
61+
value: 1,
62+
},
63+
transactions: {
64+
label: 'transactions',
65+
type: 'number',
66+
value: 1,
67+
},
68+
},
69+
series: {
70+
transactions: {
71+
label: 'transactions',
72+
coordinates: [{ x: 1 }],
73+
},
74+
},
75+
});
76+
});
77+
78+
it('returns true when hasData is called', async () => {
79+
const dataHandler = getDataHandler('apm');
80+
const hasData = await dataHandler?.hasData();
81+
expect(hasData).toBeTruthy();
82+
});
83+
});
84+
describe('Logs', () => {
85+
registerDataHandler({
86+
appName: 'infra_logs',
87+
fetchData: async () => {
88+
return {
89+
title: 'logs',
90+
appLink: '/logs',
91+
stats: {
92+
foo: {
93+
label: 'Foo',
94+
type: 'number',
95+
value: 1,
96+
},
97+
bar: {
98+
label: 'bar',
99+
type: 'number',
100+
value: 1,
101+
},
102+
},
103+
series: {
104+
foo: {
105+
label: 'Foo',
106+
coordinates: [{ x: 1 }],
107+
},
108+
bar: {
109+
label: 'Bar',
110+
coordinates: [{ x: 1 }],
111+
},
112+
},
113+
};
114+
},
115+
hasData: async () => true,
116+
});
117+
118+
it('registered data handler', () => {
119+
const dataHandler = getDataHandler('infra_logs');
120+
expect(dataHandler?.fetchData).toBeDefined();
121+
expect(dataHandler?.hasData).toBeDefined();
122+
});
123+
124+
it('returns data when fetchData is called', async () => {
125+
const dataHandler = getDataHandler('infra_logs');
126+
const response = await dataHandler?.fetchData(params);
127+
expect(response).toEqual({
128+
title: 'logs',
129+
appLink: '/logs',
130+
stats: {
131+
foo: {
132+
label: 'Foo',
133+
type: 'number',
134+
value: 1,
135+
},
136+
bar: {
137+
label: 'bar',
138+
type: 'number',
139+
value: 1,
140+
},
141+
},
142+
series: {
143+
foo: {
144+
label: 'Foo',
145+
coordinates: [{ x: 1 }],
146+
},
147+
bar: {
148+
label: 'Bar',
149+
coordinates: [{ x: 1 }],
150+
},
151+
},
152+
});
153+
});
154+
155+
it('returns true when hasData is called', async () => {
156+
const dataHandler = getDataHandler('apm');
157+
const hasData = await dataHandler?.hasData();
158+
expect(hasData).toBeTruthy();
159+
});
160+
});
161+
describe('Uptime', () => {
162+
registerDataHandler({
163+
appName: 'uptime',
164+
fetchData: async () => {
165+
return {
166+
title: 'uptime',
167+
appLink: '/uptime',
168+
stats: {
169+
monitors: {
170+
label: 'Monitors',
171+
type: 'number',
172+
value: 1,
173+
},
174+
up: {
175+
label: 'Up',
176+
type: 'number',
177+
value: 1,
178+
},
179+
down: {
180+
label: 'Down',
181+
type: 'number',
182+
value: 1,
183+
},
184+
},
185+
series: {
186+
down: {
187+
label: 'Down',
188+
coordinates: [{ x: 1 }],
189+
},
190+
up: {
191+
label: 'Up',
192+
coordinates: [{ x: 1 }],
193+
},
194+
},
195+
};
196+
},
197+
hasData: async () => true,
198+
});
199+
200+
it('registered data handler', () => {
201+
const dataHandler = getDataHandler('uptime');
202+
expect(dataHandler?.fetchData).toBeDefined();
203+
expect(dataHandler?.hasData).toBeDefined();
204+
});
205+
206+
it('returns data when fetchData is called', async () => {
207+
const dataHandler = getDataHandler('uptime');
208+
const response = await dataHandler?.fetchData(params);
209+
expect(response).toEqual({
210+
title: 'uptime',
211+
appLink: '/uptime',
212+
stats: {
213+
monitors: {
214+
label: 'Monitors',
215+
type: 'number',
216+
value: 1,
217+
},
218+
up: {
219+
label: 'Up',
220+
type: 'number',
221+
value: 1,
222+
},
223+
down: {
224+
label: 'Down',
225+
type: 'number',
226+
value: 1,
227+
},
228+
},
229+
series: {
230+
down: {
231+
label: 'Down',
232+
coordinates: [{ x: 1 }],
233+
},
234+
up: {
235+
label: 'Up',
236+
coordinates: [{ x: 1 }],
237+
},
238+
},
239+
});
240+
});
241+
242+
it('returns true when hasData is called', async () => {
243+
const dataHandler = getDataHandler('apm');
244+
const hasData = await dataHandler?.hasData();
245+
expect(hasData).toBeTruthy();
246+
});
247+
});
248+
describe('Metrics', () => {
249+
registerDataHandler({
250+
appName: 'infra_metrics',
251+
fetchData: async () => {
252+
return {
253+
title: 'metrics',
254+
appLink: '/metrics',
255+
stats: {
256+
hosts: {
257+
label: 'hosts',
258+
type: 'number',
259+
value: 1,
260+
},
261+
cpu: {
262+
label: 'cpu',
263+
type: 'number',
264+
value: 1,
265+
},
266+
memory: {
267+
label: 'memory',
268+
type: 'number',
269+
value: 1,
270+
},
271+
disk: {
272+
label: 'disk',
273+
type: 'number',
274+
value: 1,
275+
},
276+
inboundTraffic: {
277+
label: 'inboundTraffic',
278+
type: 'number',
279+
value: 1,
280+
},
281+
outboundTraffic: {
282+
label: 'outboundTraffic',
283+
type: 'number',
284+
value: 1,
285+
},
286+
},
287+
series: {
288+
inboundTraffic: {
289+
label: 'inbound Traffic',
290+
coordinates: [{ x: 1 }],
291+
},
292+
outboundTraffic: {
293+
label: 'outbound Traffic',
294+
coordinates: [{ x: 1 }],
295+
},
296+
},
297+
};
298+
},
299+
hasData: async () => true,
300+
});
301+
302+
it('registered data handler', () => {
303+
const dataHandler = getDataHandler('infra_metrics');
304+
expect(dataHandler?.fetchData).toBeDefined();
305+
expect(dataHandler?.hasData).toBeDefined();
306+
});
307+
308+
it('returns data when fetchData is called', async () => {
309+
const dataHandler = getDataHandler('infra_metrics');
310+
const response = await dataHandler?.fetchData(params);
311+
expect(response).toEqual({
312+
title: 'metrics',
313+
appLink: '/metrics',
314+
stats: {
315+
hosts: {
316+
label: 'hosts',
317+
type: 'number',
318+
value: 1,
319+
},
320+
cpu: {
321+
label: 'cpu',
322+
type: 'number',
323+
value: 1,
324+
},
325+
memory: {
326+
label: 'memory',
327+
type: 'number',
328+
value: 1,
329+
},
330+
disk: {
331+
label: 'disk',
332+
type: 'number',
333+
value: 1,
334+
},
335+
inboundTraffic: {
336+
label: 'inboundTraffic',
337+
type: 'number',
338+
value: 1,
339+
},
340+
outboundTraffic: {
341+
label: 'outboundTraffic',
342+
type: 'number',
343+
value: 1,
344+
},
345+
},
346+
series: {
347+
inboundTraffic: {
348+
label: 'inbound Traffic',
349+
coordinates: [{ x: 1 }],
350+
},
351+
outboundTraffic: {
352+
label: 'outbound Traffic',
353+
coordinates: [{ x: 1 }],
354+
},
355+
},
356+
});
357+
});
358+
359+
it('returns true when hasData is called', async () => {
360+
const dataHandler = getDataHandler('apm');
361+
const hasData = await dataHandler?.hasData();
362+
expect(hasData).toBeTruthy();
363+
});
364+
});
365+
});

0 commit comments

Comments
 (0)