Skip to content

Commit fb4c658

Browse files
authored
Replace old Latency Graph with TimeSeries component (#70)
1 parent 9b3d8e1 commit fb4c658

File tree

5 files changed

+76
-99
lines changed

5 files changed

+76
-99
lines changed

src/redis-cli-panel/components/redis-cli-panel/redis-cli-panel.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { map as map$, switchMap as switchMap$ } from 'rxjs/operators';
44
import { css, cx } from '@emotion/css';
55
import { DataFrame, DataQueryRequest, DataQueryResponse, PanelProps } from '@grafana/data';
66
import { getDataSourceSrv } from '@grafana/runtime';
7-
import { Button, LegacyForms } from '@grafana/ui';
7+
import { Button, LegacyForms, useTheme2 } from '@grafana/ui';
88
import { Help } from '../../constants';
99
import { Styles } from '../../styles';
1010
import { HelpCommand, PanelOptions, RedisQuery } from '../../types';
@@ -27,7 +27,7 @@ export const RedisCLIPanel: React.FC<PanelProps<PanelOptions>> = ({
2727
replaceVariables,
2828
}) => {
2929
const { query, raw, output, help } = options;
30-
const styles = Styles();
30+
const styles = Styles(useTheme2());
3131

3232
/**
3333
* Run Query

src/redis-cli-panel/styles.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
import { css } from '@emotion/css';
2-
import { stylesFactory, useTheme2 } from '@grafana/ui';
2+
import { GrafanaTheme2 } from '@grafana/data';
33

44
/**
55
* Styles
66
*/
7-
export const Styles = stylesFactory(() => {
8-
const theme = useTheme2();
9-
10-
/**
11-
* Return
12-
*/
7+
export const Styles = (theme: GrafanaTheme2) => {
138
return {
149
wrapper: css`
1510
position: relative;
@@ -51,4 +46,4 @@ export const Styles = stylesFactory(() => {
5146
margin-right: 12px;
5247
`,
5348
};
54-
});
49+
};

src/redis-latency-panel/components/redis-latency-panel-graph/redis-latency-panel-graph.test.tsx

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
import { shallow } from 'enzyme';
22
import React from 'react';
3-
import { dateTime, dateTimeParse, GraphSeriesXY } from '@grafana/data';
4-
import { DefaultColorModeId } from '../../constants';
3+
import { DataFrame, dateTime, dateTimeParse } from '@grafana/data';
54
import { RedisLatencyPanelGraph } from './redis-latency-panel-graph';
65

7-
/**
8-
* Mock @grafana/runtime
9-
*/
10-
jest.mock('@grafana/runtime', () => ({
11-
config: { theme2: {} },
12-
}));
13-
146
/**
157
* Latency Panel Table
168
*/
@@ -42,9 +34,10 @@ describe('RedisLatencyPanelGraph', () => {
4234
},
4335
],
4436
};
45-
const result: GraphSeriesXY[] = RedisLatencyPanelGraph.getGraphSeries(seriesMap, false);
46-
expect(result[0].seriesIndex).toEqual(0);
47-
expect(result[1].seriesIndex).toEqual(1);
37+
const result: DataFrame[] = RedisLatencyPanelGraph.getGraphDataFrame(seriesMap, false);
38+
expect(result[0].length).toEqual(2);
39+
expect(result[0].fields[0].values.length).toEqual(2);
40+
expect(result[1].length).toEqual(2);
4841
});
4942

5043
it('Should remove zero series if hideZero=true', () => {
@@ -70,40 +63,14 @@ describe('RedisLatencyPanelGraph', () => {
7063
},
7164
],
7265
};
73-
const result: GraphSeriesXY[] = RedisLatencyPanelGraph.getGraphSeries(seriesMap, true);
66+
const result: DataFrame[] = RedisLatencyPanelGraph.getGraphDataFrame(seriesMap, true);
7467
expect(result.length).toEqual(1);
7568

7669
/**
7770
* SeriesIndex should be numerated by visible items not by all items
7871
*/
79-
expect(result[0].seriesIndex).toEqual(0);
80-
});
81-
82-
it('Should define color mode Id', () => {
83-
const seriesMap = {
84-
get: [
85-
{
86-
time: dateTime(),
87-
value: 0,
88-
},
89-
{
90-
time: dateTime(),
91-
value: 0,
92-
},
93-
],
94-
info: [
95-
{
96-
time: dateTime(),
97-
value: 10,
98-
},
99-
{
100-
time: dateTime().add(10, 'seconds'),
101-
value: 20,
102-
},
103-
],
104-
};
105-
const result: GraphSeriesXY[] = RedisLatencyPanelGraph.getGraphSeries(seriesMap, true);
106-
expect(result[0].valueField.config.color?.mode).toEqual(DefaultColorModeId);
72+
expect(result[0].length).toEqual(2);
73+
expect(result[0].fields[0].values.length).toEqual(2);
10774
});
10875
});
10976

@@ -134,13 +101,43 @@ describe('RedisLatencyPanelGraph', () => {
134101

135102
it('Should update timeRange when gets a new seriesMap or timeRange', () => {
136103
const wrapper = shallow<RedisLatencyPanelGraph>(
137-
getComponent({ seriesMap: { get: [{ time: dateTime(), value: 1 }] }, timeRange: { raw: { from: dateTime() } } })
104+
getComponent({
105+
seriesMap: { get: [{ time: dateTime(), value: 1 }] },
106+
timeRange: { raw: { from: dateTime() } },
107+
options: { hideZero: true },
108+
})
138109
);
139110
const currentTimeRange = wrapper.state().timeRange;
140111
wrapper.setProps({
141112
seriesMap: { get: [{ time: dateTime(), value: 2 }] },
142113
});
143114
expect(currentTimeRange !== wrapper.state().timeRange).toBeTruthy();
144115
});
116+
117+
it('Should return gathering results div if data frame is empty', () => {
118+
const wrapper = shallow<RedisLatencyPanelGraph>(
119+
getComponent({
120+
seriesMap: {},
121+
timeRange: { raw: { from: dateTime() } },
122+
options: { hideZero: true },
123+
})
124+
);
125+
126+
const div = wrapper.findWhere((node) => node.name() === 'div');
127+
expect(div.exists()).toBeTruthy();
128+
});
129+
130+
it('Should return Time Series if data frame has data', () => {
131+
const wrapper = shallow<RedisLatencyPanelGraph>(
132+
getComponent({
133+
seriesMap: { get: [{ time: dateTime(), value: 1 }] },
134+
timeRange: { raw: { from: dateTime() } },
135+
options: { hideZero: true },
136+
})
137+
);
138+
139+
const timeSeries = wrapper.findWhere((node) => node.name() === 'TimeSeries');
140+
expect(timeSeries.exists()).toBeTruthy();
141+
});
145142
});
146143
});

src/redis-latency-panel/components/redis-latency-panel-graph/redis-latency-panel-graph.tsx

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
import React, { PureComponent } from 'react';
22
import {
3+
DataFrame,
34
DateTime,
45
dateTimeParse,
56
FieldColorModeId,
67
FieldType,
78
getDisplayProcessor,
8-
getSeriesTimeStep,
99
GraphSeriesValue,
10-
GraphSeriesXY,
1110
PanelProps,
1211
TimeRange,
1312
TimeZone,
1413
toDataFrame,
1514
} from '@grafana/data';
1615
import { config } from '@grafana/runtime';
17-
import { colors, GraphWithLegend, LegendDisplayMode } from '@grafana/ui';
18-
import { DefaultColorModeId } from '../../constants';
16+
import { LegendDisplayMode, TimeSeries, TooltipDisplayMode, TooltipPlugin } from '@grafana/ui';
1917
import { PanelOptions, SeriesMap, SeriesValue } from '../../types';
2018

2119
/**
@@ -47,13 +45,13 @@ interface State {
4745
*/
4846
export class RedisLatencyPanelGraph extends PureComponent<Props, State> {
4947
/**
50-
* Convert seriesMap to GraphSeriesXY
48+
* Convert seriesMap to Data Frames
5149
* @param seriesMap
5250
* @param hideZero
5351
*/
54-
static getGraphSeries(seriesMap: SeriesMap, hideZero: boolean): GraphSeriesXY[] {
52+
static getGraphDataFrame(seriesMap: SeriesMap, hideZero: boolean): DataFrame[] {
5553
return Object.entries(seriesMap).reduce(
56-
(acc: GraphSeriesXY[], [command, seriesValues]: [string, SeriesValue[]], index) => {
54+
(acc: DataFrame[], [command, seriesValues]: [string, SeriesValue[]], index) => {
5755
const { times, values, shouldBeHidden } = seriesValues.reduce(
5856
(acc: { times: DateTime[]; values: number[]; shouldBeHidden: boolean }, { time, value }) => {
5957
let shouldBeHidden = acc.shouldBeHidden;
@@ -81,11 +79,6 @@ export class RedisLatencyPanelGraph extends PureComponent<Props, State> {
8179
return acc;
8280
}
8381

84-
/**
85-
* Color
86-
*/
87-
const color = colors[index % colors.length];
88-
8982
/**
9083
* Data Frame
9184
*/
@@ -99,13 +92,12 @@ export class RedisLatencyPanelGraph extends PureComponent<Props, State> {
9992
},
10093
{
10194
type: FieldType.number,
102-
name: command,
95+
name: ' ',
10396
values,
10497
config: {
10598
unit: 'µs',
10699
color: {
107-
fixedColor: color,
108-
mode: FieldColorModeId ? FieldColorModeId.Fixed : DefaultColorModeId,
100+
mode: FieldColorModeId.PaletteClassic,
109101
},
110102
},
111103
},
@@ -128,19 +120,7 @@ export class RedisLatencyPanelGraph extends PureComponent<Props, State> {
128120
data.push([times[i].valueOf(), values[i]]);
129121
}
130122

131-
return acc.concat([
132-
{
133-
seriesIndex: acc.length,
134-
yAxis: { index: 1 },
135-
label: command,
136-
isVisible: true,
137-
data,
138-
timeField: seriesDataFrame.fields[0],
139-
valueField: seriesDataFrame.fields[1],
140-
timeStep: getSeriesTimeStep(seriesDataFrame.fields[0]),
141-
color,
142-
},
143-
]);
123+
return acc.concat(seriesDataFrame);
144124
},
145125
[]
146126
);
@@ -183,11 +163,6 @@ export class RedisLatencyPanelGraph extends PureComponent<Props, State> {
183163
};
184164
}
185165

186-
/**
187-
* onToggleSort
188-
*/
189-
onToggleSort() {}
190-
191166
/**
192167
* Render
193168
*/
@@ -196,21 +171,36 @@ export class RedisLatencyPanelGraph extends PureComponent<Props, State> {
196171
const { timeRange } = this.state;
197172

198173
/**
199-
* Return GraphWithLegend
174+
* Convert to Data Frames
175+
*/
176+
const dataFrames = RedisLatencyPanelGraph.getGraphDataFrame(seriesMap, options.hideZero);
177+
if (!dataFrames.length) {
178+
return <div>Gathering latency data...</div>;
179+
}
180+
181+
/**
182+
* Return Time Series
200183
*/
201184
return (
202-
<GraphWithLegend
185+
<TimeSeries
186+
frames={dataFrames}
203187
width={width}
204188
height={height}
205-
series={RedisLatencyPanelGraph.getGraphSeries(seriesMap, options?.hideZero)}
206189
timeRange={timeRange}
190+
legend={{ displayMode: LegendDisplayMode.List, placement: 'bottom', calcs: [] }}
207191
timeZone={this.props.timeZone}
208-
legendDisplayMode={LegendDisplayMode.List}
209-
placement="bottom"
210-
onToggleSort={this.onToggleSort}
211-
hideZero={options?.hideZero}
212-
showLines
213-
></GraphWithLegend>
192+
>
193+
{(config, alignedDataFrame) => {
194+
return (
195+
<TooltipPlugin
196+
config={config}
197+
data={alignedDataFrame}
198+
mode={TooltipDisplayMode.Multi}
199+
timeZone={this.props.timeZone}
200+
/>
201+
);
202+
}}
203+
</TimeSeries>
214204
);
215205
}
216206
}

src/redis-latency-panel/constants.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ export const DefaultInterval = 1000;
88
*/
99
export const MaxItemsPerSeries = 300;
1010

11-
/**
12-
* Default color Mode id for 7.2.X
13-
*/
14-
export const DefaultColorModeId = 'fixed';
15-
1611
/**
1712
* Fields
1813
*/

0 commit comments

Comments
 (0)