-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.tsx
More file actions
339 lines (314 loc) · 9.14 KB
/
App.tsx
File metadata and controls
339 lines (314 loc) · 9.14 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import React, { useMemo, useState } from 'react'
import { View, StyleSheet, TouchableOpacity, Text, Switch } from 'react-native'
import Chart, {
type Dataset,
type ChartProps,
type ErrorSegment,
} from '@mitigate-dev/react-native-d3-chart'
import { buildSlices } from './helpers/buildSlices'
import { generateTimeSeriesData } from './helpers/generateTimeSeriesData'
import { temperatureData, visits } from './mockData'
import type { HighlightPayload } from '../../src/types'
type TimeDomainType = 'hour' | 'day' | 'week' | 'month'
const TIME_DOMAIN_TYPES: TimeDomainType[] = ['hour', 'day', 'week', 'month']
const chartColors: ChartProps['colors'] = {
background: '#fff',
highlightLine: '#000',
border: '#555',
cursorStroke: '#0ff',
highlightLabel: '#000',
highlightTime: '#444',
}
enum Measurement {
Temperature = 'Temperature',
Blue = 'Blue',
Green = 'Green',
Pink = 'Pink',
Visits = 'Visits',
VisitRate = 'Visit Rate',
}
const measurementKeys = Object.values(Measurement)
const measurementsRecords: Record<Measurement, Dataset> = {
[Measurement.Temperature]: {
unit: '°C',
points: temperatureData,
decimals: 0,
areaColor: '#c4deff',
color: {
type: 'thresholds',
baseColor: '#3d91ff',
gradientBlur: 2,
thresholds: [
{ value: 32, color: '#bb2222' },
{ value: 24, color: '#ffc400' },
{ value: 16, color: '#089851' },
{ value: 10, color: '#9ceeff' },
{ value: 0, color: '#00d5ff' },
],
},
measurementName: Measurement.Temperature,
},
[Measurement.Blue]: {
unit: 'l',
points: generateTimeSeriesData({
startingValue: 160,
minimum: 50,
radomFactor: 6,
}),
decimals: 0,
color: '#66e',
measurementName: Measurement.Blue,
},
[Measurement.Green]: {
unit: 'kg',
points: generateTimeSeriesData(),
decimals: 0,
color: '#6e6',
measurementName: Measurement.Green,
},
[Measurement.Pink]: {
unit: 'm/s',
points: generateTimeSeriesData({
startingValue: 20,
minimum: 100,
}),
decimals: 1,
color: '#e0e',
measurementName: Measurement.Pink,
},
[Measurement.VisitRate]: {
unit: 'visits/h',
points: visits.movingAveregeData,
slices: buildSlices('horizontal', {
end: visits.latestTimestamp,
start: visits.oldestTimestamp,
yellowThreshold: visits.averageVisitRatePerHour,
redThreshold: visits.averageVisitRatePerHour * 1.1,
}),
decimals: 0,
color: '#000',
areaColor: null,
measurementName: Measurement.VisitRate,
},
[Measurement.Visits]: {
unit: 'pulses',
points: visits.culmulativeData,
decimals: 0,
color: '#000',
areaColor: null,
measurementName: 'Visits cumulative',
slices: buildSlices('axial', {
end: visits.latestTimestamp,
start: visits.oldestTimestamp,
yellowThreshold: visits.averageVisitRatePerHour,
redThreshold: visits.averageVisitRatePerHour * 1.1,
}),
},
}
export default function App() {
const [width, setWidth] = useState<number>(0)
const height = width * 1.1
const [timeDomainType, setTimeDomainType] = useState<TimeDomainType>('hour')
const timeDomain = useMemo(() => {
const now = new Date().valueOf()
let hours = 1
if (timeDomainType !== 'hour') {
hours *= 24
if (timeDomainType === 'week') {
hours *= 7
}
if (timeDomainType === 'month') {
hours *= 30
}
}
const start = now - hours * 60 * 60 * 1000
const end = now
return { start, end, type: timeDomainType }
}, [timeDomainType])
const [enabledMeasurements, setEnabledMeasurements] = useState<Measurement[]>(
[Measurement.Temperature]
)
const [highlightValuePosition, setHighlightValuePosition] = useState<
'top' | 'tooltip' | 'none'
>('tooltip')
const [currentHighlight, setCurrentHighlight] = useState<HighlightPayload>()
const errorSegments = useMemo<ErrorSegment[]>(() => {
const now = Date.now()
return [
{
message: 'Unknown error',
messageColor: '#f0f',
start: now - 50 * 60 * 1000,
end: now - 40 * 60 * 1000,
},
{
message: 'No data yet',
messageColor: '#b22',
start: now - 2 * 60 * 1000,
end: now + 15 * 60 * 1000,
},
]
}, [])
const datasets = useMemo<Dataset[]>(
() =>
enabledMeasurements.map((enabledMeasurement, index) => {
const data = measurementsRecords[enabledMeasurement]
if (index !== 0) return data
const firstErrorSegment = errorSegments[0]
if (!firstErrorSegment) return data
// invalidate points within first error segment for the first measurement for demo purposes
return {
...data,
decimalSeparator: ',',
decimals: 2,
points: data.points.map((point) => ({
timestamp: point.timestamp,
value:
point.timestamp - 60 * 1000 > firstErrorSegment.start &&
point.timestamp + 60 * 1000 < firstErrorSegment.end
? null
: point.value,
})),
}
}),
[enabledMeasurements, errorSegments]
)
return (
<View
style={styles.holder}
onLayout={(e) => setWidth(e.nativeEvent.layout.width)}
>
<View style={styles.optionsRow}>
{(['top', 'tooltip', 'none'] as const).map((type) => (
<TouchableOpacity
key={type}
style={[
styles.optionsItem,
type === highlightValuePosition &&
styles.highlightPositionItemActive,
]}
onPress={() => setHighlightValuePosition(type)}
>
<Text style={styles.optionsItemText}>{type}</Text>
</TouchableOpacity>
))}
</View>
<Chart
zoomEnabled
width={width}
height={height}
datasets={datasets}
colors={chartColors}
timeDomain={timeDomain}
marginHorizontal={PADDING}
errorSegments={errorSegments}
noDataString="No data available"
highlightValuePosition={highlightValuePosition}
xDividerConfig={{ type: 'segment', color: '#F2F2FF' }}
onHighlightChanged={setCurrentHighlight}
/>
<View style={styles.spacer} />
<View style={styles.optionsRow}>
{TIME_DOMAIN_TYPES.map((type) => (
<TouchableOpacity
key={type}
style={[
styles.optionsItem,
type === timeDomainType && styles.timeDomainItemActive,
]}
onPress={() => setTimeDomainType(type)}
>
<Text style={styles.optionsItemText}>{type}</Text>
</TouchableOpacity>
))}
</View>
<View style={styles.highlightContainer}>
<Text>Highlight listener:</Text>
<Text>
Exact timestamp:
{currentHighlight?.timestamp &&
new Date(currentHighlight.timestamp).toTimeString()}
</Text>
{currentHighlight?.values.map((value, index) => (
<Text key={index} style={{ color: value?.color || '#000' }}>
{`${value?.measurementName}: ${value?.errorMessage ?? value?.value}`}
</Text>
))}
</View>
{/* Measurement toggles */}
{measurementKeys.map((measurement) => (
<View key={measurement} style={styles.switchContainer}>
<Switch
value={enabledMeasurements.includes(measurement)}
onValueChange={() =>
setEnabledMeasurements((prev) => {
if (!prev.includes(measurement))
// enable
return prev.concat(measurement)
if (prev.length !== 1)
// disable
return prev.filter((m) => m !== measurement)
// only one measurement was enabled, switch to next one
const currentIndex = measurementKeys.findIndex(
(m) => m === measurement
)
const nextIndex = (currentIndex + 1) % measurementKeys.length
const nextMeasurement = measurementKeys[nextIndex]!
return [nextMeasurement]
})
}
/>
<Text style={styles.switchLabel}>{measurement}</Text>
</View>
))}
</View>
)
}
const PADDING = 20
const styles = StyleSheet.create({
holder: {
width: '100%',
flex: 1,
borderRadius: 10,
paddingTop: 30,
paddingBottom: PADDING,
backgroundColor: '#fff',
},
spacer: {
height: 10,
},
highlightContainer: {
right: 20,
bottom: 40,
opacity: 0.9,
position: 'absolute',
backgroundColor: '#dfe',
},
optionsRow: {
flexDirection: 'row',
paddingHorizontal: PADDING,
marginVertical: 10,
},
optionsItem: {
flex: 1,
borderRadius: 11,
paddingVertical: 2,
justifyContent: 'center',
alignItems: 'center',
},
optionsItemText: {
fontSize: 13,
},
timeDomainItemActive: {
backgroundColor: '#b22',
},
highlightPositionItemActive: {
backgroundColor: '#2b2',
},
switchContainer: {
marginVertical: 10,
flexDirection: 'row',
alignItems: 'center',
},
switchLabel: { marginLeft: 10 },
})