Skip to content
This repository was archived by the owner on Apr 19, 2024. It is now read-only.

Commit da25070

Browse files
authored
more precise seconds conversion (#407)
* tests for current time conversion for milliseconds * additional conversion for seconds * no decimal points when we handle days * 2.20.15 * unify time conversion for milliseconds and seconds * remove statsd menu hint relates to #405 * 2.25.5 * handle currentSecondsAsTimeSetting * 2.25.6
1 parent d8759d5 commit da25070

File tree

4 files changed

+420
-229
lines changed

4 files changed

+420
-229
lines changed

src/domains/chart/utils/formatters.ts

Lines changed: 161 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { identity } from "ramda"
2-
import {
3-
useCallback, useState, useMemo, useRef,
4-
} from "react"
2+
import { useCallback, useState, useMemo, useRef } from "react"
53

64
import { useSelector } from "store/redux-separate-context"
75
import { selectTemperatureSetting, selectSecondsAsTimeSetting } from "domains/global/selectors"
@@ -29,7 +27,8 @@ const fastNumberFormat = (min: number, max: number) => {
2927
}
3028

3129
return formattersFixed[key]
32-
} if (min === 0) {
30+
}
31+
if (min === 0) {
3332
if (typeof formattersZeroBased[key] === "undefined") {
3433
formattersZeroBased[key] = new Intl.NumberFormat(undefined, {
3534
useGrouping: true,
@@ -50,61 +49,65 @@ const fastNumberFormat = (min: number, max: number) => {
5049
})
5150
}
5251

53-
const getLegendFormatValue = (
54-
convertUnits: Converter, intlNumberFormat: Intl.NumberFormat | null, valueDecimalDetail: number,
55-
) => (value: number | string | null) => {
56-
if (typeof value !== "number") {
57-
return "-"
58-
}
52+
const getLegendFormatValue =
53+
(
54+
convertUnits: Converter,
55+
intlNumberFormat: Intl.NumberFormat | null,
56+
valueDecimalDetail: number
57+
) =>
58+
(value: number | string | null) => {
59+
if (typeof value !== "number") {
60+
return "-"
61+
}
5962

60-
const convertedValue = convertUnits(value)
61-
if (typeof convertedValue !== "number") {
62-
return convertedValue
63-
}
63+
const convertedValue = convertUnits(value)
64+
if (typeof convertedValue !== "number") {
65+
return convertedValue
66+
}
6467

65-
if (intlNumberFormat !== null) {
66-
return intlNumberFormat.format(convertedValue)
67-
}
68+
if (intlNumberFormat !== null) {
69+
return intlNumberFormat.format(convertedValue)
70+
}
6871

69-
let dmin
70-
let dmax
71-
if (valueDecimalDetail !== -1) {
72-
dmin = valueDecimalDetail
73-
dmax = valueDecimalDetail
74-
} else {
75-
dmin = 0
76-
const abs = (convertedValue < 0) ? -convertedValue : convertedValue
77-
if (abs > 1000) {
78-
dmax = 0
79-
} else if (abs > 10) {
80-
dmax = 1
81-
} else if (abs > 1) {
82-
dmax = 2
83-
} else if (abs > 0.1) {
84-
dmax = 2
85-
} else if (abs > 0.01) {
86-
dmax = 4
87-
} else if (abs > 0.001) {
88-
dmax = 5
89-
} else if (abs > 0.0001) {
90-
dmax = 6
72+
let dmin
73+
let dmax
74+
if (valueDecimalDetail !== -1) {
75+
dmin = valueDecimalDetail
76+
dmax = valueDecimalDetail
9177
} else {
92-
dmax = 7
78+
dmin = 0
79+
const abs = convertedValue < 0 ? -convertedValue : convertedValue
80+
if (abs > 1000) {
81+
dmax = 0
82+
} else if (abs > 10) {
83+
dmax = 1
84+
} else if (abs > 1) {
85+
dmax = 2
86+
} else if (abs > 0.1) {
87+
dmax = 2
88+
} else if (abs > 0.01) {
89+
dmax = 4
90+
} else if (abs > 0.001) {
91+
dmax = 5
92+
} else if (abs > 0.0001) {
93+
dmax = 6
94+
} else {
95+
dmax = 7
96+
}
9397
}
94-
}
9598

96-
return fastNumberFormat(dmin, dmax).format(convertedValue)
97-
}
99+
return fastNumberFormat(dmin, dmax).format(convertedValue)
100+
}
98101

99102
type LegendFormatValue = (value: string | number | null) => string | number
100103

101104
interface Arguments {
102-
attributes: Attributes,
103-
data: ChartData,
104-
units: string,
105-
unitsCommon: string | undefined,
106-
unitsDesired: string,
107-
uuid: string,
105+
attributes: Attributes
106+
data: ChartData
107+
units: string
108+
unitsCommon: string | undefined
109+
unitsDesired: string
110+
uuid: string
108111
}
109112
export const useFormatters = ({
110113
attributes,
@@ -135,113 +138,130 @@ export const useFormatters = ({
135138
decimalDigits = -1,
136139
} = attributes
137140

138-
139-
const legendFormatValue: LegendFormatValue = useMemo(() => (
140-
getLegendFormatValue(
141-
convertUnits, intlNumberFormat, decimalDigits,
142-
)
143-
), [convertUnits, decimalDigits, intlNumberFormat])
144-
141+
const legendFormatValue: LegendFormatValue = useMemo(
142+
() => getLegendFormatValue(convertUnits, intlNumberFormat, decimalDigits),
143+
[convertUnits, decimalDigits, intlNumberFormat]
144+
)
145145

146146
const legendFormatValueRef = useRef(legendFormatValue)
147147
const updateLegendFormatValueRef = (
148-
newConvertUnits: Converter, newIntlNumberFormat: any, newDecimalDigits: any,
148+
newConvertUnits: Converter,
149+
newIntlNumberFormat: any,
150+
newDecimalDigits: any
149151
) => {
150152
legendFormatValueRef.current = getLegendFormatValue(
151-
newConvertUnits, newIntlNumberFormat, newDecimalDigits,
153+
newConvertUnits,
154+
newIntlNumberFormat,
155+
newDecimalDigits
152156
)
153157
}
154158

155-
const legendFormatValueDecimalsFromMinMax = useCallback((newMin: number, newMax: number) => {
156-
if (safeEqualCheck(min, newMin) && safeEqualCheck(max, newMax)) {
157-
return legendFormatValueRef.current
158-
}
159-
// we should call the convertUnits-creation only when original app was doing this
160-
// so we don't get new updates in improper places
161-
setMin(newMin)
162-
setMax(newMax)
163-
164-
const newConvertUnits = unitsConversionCreator.get(
165-
uuid, newMin, newMax, units, unitsDesired, unitsCommon,
166-
(switchedUnits) => {
167-
setUnitsCurrent(switchedUnits)
168-
// that.legendSetUnitsString(that.units_current);
169-
// that.legendSetUnitsString just populates some DOM with unitsCurrent
170-
// on all occurences just take the unitsCurrent from this state
171-
}, temperatureSetting, secondsAsTimeSetting,
172-
)
173-
174-
// as function, so useState() interpretes it properly
175-
setConvertUnits(() => newConvertUnits)
176-
177-
const convertedMin = newConvertUnits(newMin)
178-
const convertedMax = newConvertUnits(newMax)
179-
180-
if (typeof convertedMin !== "number" || typeof convertedMax !== "number") {
181-
updateLegendFormatValueRef(newConvertUnits, intlNumberFormat, decimalDigits)
182-
return legendFormatValueRef.current
183-
}
184-
185-
let newDecimals
186-
187-
if (data.min === data.max) {
188-
// it is a fixed number, let the visualizer decide based on the value
189-
newDecimals = -1
190-
} else if (decimalDigits !== -1) {
191-
// there is an override
192-
newDecimals = decimalDigits
193-
} else {
194-
// ok, let's calculate the proper number of decimal points
195-
let delta
196-
197-
if (convertedMin === convertedMax) {
198-
delta = Math.abs(convertedMin)
199-
} else {
200-
delta = Math.abs(convertedMax - convertedMin)
159+
const legendFormatValueDecimalsFromMinMax = useCallback(
160+
(newMin: number, newMax: number) => {
161+
if (safeEqualCheck(min, newMin) && safeEqualCheck(max, newMax)) {
162+
return legendFormatValueRef.current
201163
}
202-
203-
if (delta > 1000) {
204-
newDecimals = 0
205-
} else if (delta > 10) {
206-
newDecimals = (1)
207-
} else if (delta > 1) {
208-
newDecimals = 2
209-
} else if (delta > 0.1) {
210-
newDecimals = 2
211-
} else if (delta > 0.01) {
212-
newDecimals = 4
213-
} else if (delta > 0.001) {
214-
newDecimals = 5
215-
} else if (delta > 0.0001) {
216-
newDecimals = 6
217-
} else {
218-
newDecimals = 7
164+
// we should call the convertUnits-creation only when original app was doing this
165+
// so we don't get new updates in improper places
166+
setMin(newMin)
167+
setMax(newMax)
168+
169+
const newConvertUnits = unitsConversionCreator.get(
170+
uuid,
171+
newMin,
172+
newMax,
173+
units,
174+
unitsDesired,
175+
unitsCommon,
176+
switchedUnits => {
177+
setUnitsCurrent(switchedUnits)
178+
// that.legendSetUnitsString(that.units_current);
179+
// that.legendSetUnitsString just populates some DOM with unitsCurrent
180+
// on all occurences just take the unitsCurrent from this state
181+
},
182+
temperatureSetting,
183+
secondsAsTimeSetting
184+
)
185+
186+
// as function, so useState() interpretes it properly
187+
setConvertUnits(() => newConvertUnits)
188+
189+
const convertedMin = newConvertUnits(newMin)
190+
const convertedMax = newConvertUnits(newMax)
191+
192+
// if number is returned, we format it!!!!
193+
if (typeof convertedMin !== "number" || typeof convertedMax !== "number") {
194+
updateLegendFormatValueRef(newConvertUnits, intlNumberFormat, decimalDigits)
195+
return legendFormatValueRef.current
219196
}
220-
}
221-
222197

223-
let newIntlNumberFormat = intlNumberFormat
198+
let newDecimals
224199

225-
if (newDecimals !== decimals) {
226-
if (newDecimals < 0) {
227-
newIntlNumberFormat = null
200+
if (data.min === data.max) {
201+
// it is a fixed number, let the visualizer decide based on the value
202+
newDecimals = -1
203+
} else if (decimalDigits !== -1) {
204+
// there is an override
205+
newDecimals = decimalDigits
228206
} else {
229-
newIntlNumberFormat = fastNumberFormat(
230-
newDecimals,
231-
newDecimals,
232-
)
207+
// ok, let's calculate the proper number of decimal points
208+
let delta
209+
210+
if (convertedMin === convertedMax) {
211+
delta = Math.abs(convertedMin)
212+
} else {
213+
delta = Math.abs(convertedMax - convertedMin)
214+
}
215+
216+
if (delta > 1000) {
217+
newDecimals = 0
218+
} else if (delta > 10) {
219+
newDecimals = 1
220+
} else if (delta > 1) {
221+
newDecimals = 2
222+
} else if (delta > 0.1) {
223+
newDecimals = 2
224+
} else if (delta > 0.01) {
225+
newDecimals = 4
226+
} else if (delta > 0.001) {
227+
newDecimals = 5
228+
} else if (delta > 0.0001) {
229+
newDecimals = 6
230+
} else {
231+
newDecimals = 7
232+
}
233233
}
234-
setIntlNumberFormat(() => newIntlNumberFormat)
235-
setDecimals(newDecimals)
236-
}
237-
updateLegendFormatValueRef(newConvertUnits, newIntlNumberFormat, newDecimals)
238-
return legendFormatValueRef.current
239-
}, [
240-
decimals, decimalDigits, min, max, uuid, temperatureSetting,
241-
units, unitsDesired, unitsCommon, secondsAsTimeSetting,
242-
data.min, data.max, intlNumberFormat,
243-
])
244234

235+
let newIntlNumberFormat = intlNumberFormat
236+
237+
if (newDecimals !== decimals) {
238+
if (newDecimals < 0) {
239+
newIntlNumberFormat = null
240+
} else {
241+
newIntlNumberFormat = fastNumberFormat(newDecimals, newDecimals)
242+
}
243+
setIntlNumberFormat(() => newIntlNumberFormat)
244+
setDecimals(newDecimals)
245+
}
246+
updateLegendFormatValueRef(newConvertUnits, newIntlNumberFormat, newDecimals)
247+
return legendFormatValueRef.current
248+
},
249+
[
250+
decimals,
251+
decimalDigits,
252+
min,
253+
max,
254+
uuid,
255+
temperatureSetting,
256+
units,
257+
unitsDesired,
258+
unitsCommon,
259+
secondsAsTimeSetting,
260+
data.min,
261+
data.max,
262+
intlNumberFormat,
263+
]
264+
)
245265

246266
return {
247267
legendFormatValue,

src/domains/charts/getChartMenu.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export default (
3737
case "ap":
3838
case "net":
3939
case "powersupply":
40-
case "statsd":
4140
return emit({ menu: part1 })
4241

4342
case "cpufreq":

0 commit comments

Comments
 (0)