-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathlineChartWidget.stories.tsx
265 lines (238 loc) · 7.28 KB
/
lineChartWidget.stories.tsx
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import {Fragment} from 'react';
import {useTheme} from '@emotion/react';
import styled from '@emotion/styled';
import moment from 'moment-timezone';
import JSXNode from 'sentry/components/stories/jsxNode';
import SideBySide from 'sentry/components/stories/sideBySide';
import SizingWindow from 'sentry/components/stories/sizingWindow';
import storyBook from 'sentry/stories/storyBook';
import type {DateString} from 'sentry/types/core';
import usePageFilters from 'sentry/utils/usePageFilters';
import type {Release, TimeseriesData} from '../common/types';
import {LineChartWidget} from './lineChartWidget';
import sampleDurationTimeSeries from './sampleDurationTimeSeries.json';
import sampleThroughputTimeSeries from './sampleThroughputTimeSeries.json';
import {shiftTimeserieToNow} from './shiftTimeserieToNow';
const sampleDurationTimeSeries2 = {
...sampleDurationTimeSeries,
field: 'p50(span.duration)',
data: sampleDurationTimeSeries.data.map(datum => {
return {
...datum,
value: datum.value * 0.3 + 30 * Math.random(),
};
}),
meta: {
fields: {
'p50(span.duration)': 'duration',
},
units: {
'p50(span.duration)': 'millisecond',
},
},
};
export default storyBook(LineChartWidget, story => {
story('Getting Started', () => {
return (
<Fragment>
<p>
<JSXNode name="LineChartWidget" /> is a Dashboard Widget Component. It displays
a timeseries chart with one or more timeseries. Used to visualize data that
changes over time in Project Details, Dashboards, Performance, and other UIs.
</p>
</Fragment>
);
});
story('Visualization', () => {
const {selection} = usePageFilters();
const {datetime} = selection;
const {start, end} = datetime;
const throughputTimeSeries = toTimeSeriesSelection(
sampleThroughputTimeSeries as unknown as TimeseriesData,
start,
end
);
const durationTimeSeries1 = toTimeSeriesSelection(
sampleDurationTimeSeries as unknown as TimeseriesData,
start,
end
);
const durationTimeSeries2 = toTimeSeriesSelection(
sampleDurationTimeSeries2,
start,
end
);
return (
<Fragment>
<p>
The visualization of <JSXNode name="LineChartWidget" /> a line chart. It has
some bells and whistles including automatic axes labels, and a hover tooltip.
Like other widgets, it automatically fills the parent element.
</p>
<SmallSizingWindow>
<LineChartWidget
title="eps()"
description="Number of events per second"
timeseries={[throughputTimeSeries]}
/>
</SmallSizingWindow>
<p>
The <code>dataCompletenessDelay</code> prop indicates that this data is live,
and the last few buckets might not have complete data. The delay is a number in
seconds. Any data bucket that happens in that delay window will be plotted with
a dotted line. By default the delay is <code>0</code>.
</p>
<SideBySide>
<MediumWidget>
<LineChartWidget
title="span.duration"
dataCompletenessDelay={60 * 60 * 3}
timeseries={[
shiftTimeserieToNow(durationTimeSeries1),
shiftTimeserieToNow(durationTimeSeries2),
]}
/>
</MediumWidget>
</SideBySide>
</Fragment>
);
});
story('State', () => {
return (
<Fragment>
<p>
<JSXNode name="LineChartWidget" /> supports the usual loading and error states.
The loading state shows a spinner. The error state shows a message, and an
optional "Retry" button.
</p>
<SideBySide>
<SmallWidget>
<LineChartWidget title="Loading Count" isLoading />
</SmallWidget>
<SmallWidget>
<LineChartWidget title="Missing Count" />
</SmallWidget>
<SmallWidget>
<LineChartWidget
title="Count Error"
error={new Error('Something went wrong!')}
/>
</SmallWidget>
<SmallWidget>
<LineChartWidget
title="Data Error"
error={new Error('Something went wrong!')}
onRetry={() => {}}
/>
</SmallWidget>
</SideBySide>
</Fragment>
);
});
story('Colors', () => {
const theme = useTheme();
return (
<Fragment>
<p>
You can control the color of each timeseries by setting the <code>color</code>{' '}
attribute to a string that contains a valid hex color code.
</p>
<MediumWidget>
<LineChartWidget
title="error_rate()"
description="Rate of Errors"
timeseries={[
{
...sampleThroughputTimeSeries,
field: 'error_rate()',
meta: {
fields: {
'error_rate()': 'rate',
},
units: {
'error_rate()': '1/second',
},
},
color: theme.error,
} as unknown as TimeseriesData,
]}
/>
</MediumWidget>
</Fragment>
);
});
story('Releases', () => {
const releases = [
{
version: '[email protected]',
timestamp: sampleThroughputTimeSeries.data.at(2)?.timestamp,
},
{
version: '[email protected]',
timestamp: sampleThroughputTimeSeries.data.at(20)?.timestamp,
},
].filter(hasTimestamp);
return (
<Fragment>
<p>
<JSXNode name="LineChartWidget" /> supports the <code>releases</code> prop. If
passed in, the widget will plot every release as a vertical line that overlays
the chart data. Clicking on a release line will open the release details page.
</p>
<MediumWidget>
<LineChartWidget
title="error_rate()"
timeseries={[
{
...sampleThroughputTimeSeries,
field: 'error_rate()',
meta: {
fields: {
'error_rate()': 'rate',
},
units: {
'error_rate()': '1/second',
},
},
} as unknown as TimeseriesData,
]}
releases={releases}
/>
</MediumWidget>
</Fragment>
);
});
});
const MediumWidget = styled('div')`
width: 420px;
height: 250px;
`;
const SmallWidget = styled('div')`
width: 360px;
height: 160px;
`;
const SmallSizingWindow = styled(SizingWindow)`
width: 50%;
height: 300px;
`;
function toTimeSeriesSelection(
timeSeries: TimeseriesData,
start: DateString | null,
end: DateString | null
): TimeseriesData {
return {
...timeSeries,
data: timeSeries.data.filter(datum => {
if (start && moment(datum.timestamp).isBefore(moment.utc(start))) {
return false;
}
if (end && moment(datum.timestamp).isAfter(moment.utc(end))) {
return false;
}
return true;
}),
};
}
function hasTimestamp(release: Partial<Release>): release is Release {
return Boolean(release?.timestamp);
}