File size: 3,243 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
 * @jest-environment jsdom
 */
import { renderHook } from '@testing-library/react';
import { useGroupByTime } from '../hooks/use-group-by-time';
import { PeriodData } from '../hooks/use-metrics-query';

const periods: PeriodData[] = [
	{
		timestamp: 1691664300, // Initial timestamp in seconds
		dimension: {
			'200': 0.2377777777777777,
			'301': 0.012222222222222223,
			'302': 0.0011111111111111111,
			'304': 0.024444444444444446,
			'404': 0.0033333333333333335,
		},
	},
	{
		timestamp: 1691664300 + 300,
		dimension: {
			'200': 0.2744444444444444,
			'404': 0.005555555555555556,
		},
	},
	{
		timestamp: 1691664300 + 600,
		dimension: {
			'200': 0.2744444444444444,
			'301': 0.0044444444444444444,
		},
	},
	{
		timestamp: 1691664300 + 800,
		dimension: {
			'200': 0.2744444444444444,
			'301': 0.0044444444444444444,
			'304': 0.027777777777777776,
			'404': 0.005555555555555556,
		},
	},
	{
		timestamp: 1691664300 + 900,
		dimension: {
			'200': 0.2744444444444444,
		},
	},
];

const data = {
	_meta: {
		start: 1691150400,
		end: 1691762400,
		resolution: 7200,
		metric: 'requests_persec',
		dimension: 'http_status',
		took: 604,
	},
	periods,
};

describe( 'useGroupByTime hook', () => {
	it( 'should group data by time windows correctly', () => {
		const { result } = renderHook( () => useGroupByTime( data, [ 200, 301, 302, 304, 404 ] ) );
		const { groupedData, dataGroupedByTime, labels } = result.current;

		expect( Object.keys( groupedData ).length ).toBe( 5 );
		expect( groupedData ).toStrictEqual( {
			'1691664300': {
				'200': 14.266666666666662,
				'301': 0.7333333333333334,
				'302': 0.06666666666666667,
				'304': 1.4666666666666668,
				'404': 0.2,
			},
			'1691664600': { '200': 16.46666666666666, '404': 0.33333333333333337 },
			'1691664900': { '200': 16.46666666666666, '301': 0.26666666666666666 },
			'1691665100': {
				'200': 16.46666666666666,
				'301': 0.26666666666666666,
				'304': 1.6666666666666665,
				'404': 0.33333333333333337,
			},
			'1691665200': { '200': 16.46666666666666 },
		} );
		expect( dataGroupedByTime ).toEqual( [
			[
				14.266666666666662, 16.46666666666666, 16.46666666666666, 16.46666666666666,
				16.46666666666666,
			],
			[ 0.7333333333333334, 0, 0.26666666666666666, 0.26666666666666666, 0 ],
			[ 0.06666666666666667, 0, 0, 0, 0 ],
			[ 1.4666666666666668, 0, 0, 1.6666666666666665, 0 ],
			[ 0.2, 0.33333333333333337, 0, 0.33333333333333337, 0 ],
		] );
		expect( labels.length ).toEqual( 5 );
		expect( labels ).toEqual( [
			1691664300,
			1691664300 + 300,
			1691664300 + 600,
			1691664300 + 800,
			1691664300 + 900,
		] );
	} );

	it( 'should correctly handle missing status codes', () => {
		const { result } = renderHook( () => useGroupByTime( data, [ 444 ] ) );
		const { dataGroupedByTime } = result.current;

		expect( dataGroupedByTime ).toEqual( [ [ 0, 0, 0, 0, 0 ] ] );
	} );

	it( 'should return empty arrays if no periods given', () => {
		const { result } = renderHook( () => useGroupByTime( undefined, [ 200, 301 ] ) );
		const { groupedData, dataGroupedByTime, labels } = result.current;

		expect( groupedData ).toEqual( {} );
		expect( dataGroupedByTime ).toEqual( [ [], [] ] );
		expect( labels ).toEqual( [] );
	} );
} );