Skip to content

Commit 23d4eee

Browse files
committed
time data fixes and validation
1 parent 855678a commit 23d4eee

File tree

3 files changed

+55
-9
lines changed

3 files changed

+55
-9
lines changed

src/components/ReactTimeSeries/index.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import MainLayout from "../MainLayout"
1111
import fetchAudioData from "../../utils/fetch-audio-data"
1212
import fetchCSVData from "../../utils/fetch-csv-data"
1313

14-
import validateTimeData from "../../utils/validate-time-data"
14+
import fixTimeData from "../../utils/fix-time-data"
1515

1616
const emptyAr = []
1717

@@ -52,13 +52,18 @@ export const ReactTimeSeriesWithoutContext = ({
5252

5353
const timeDataAvailable = [sampleTimeData, audioUrl, csvUrl].some(Boolean)
5454

55+
const [error, setError] = useState(null)
5556
const timeData = useAsyncMemo(
5657
async () => {
57-
if (sampleTimeData) return validateTimeData(sampleTimeData)
58-
if (audioUrl) return validateTimeData(await fetchAudioData(audioUrl))
59-
if (csvUrl) return validateTimeData(await fetchCSVData(csvUrl))
60-
return []
61-
// TODO load csvUrl
58+
try {
59+
if (sampleTimeData) return fixTimeData(sampleTimeData, graphs)
60+
if (audioUrl) return fixTimeData(await fetchAudioData(audioUrl), graphs)
61+
if (csvUrl) return fixTimeData(await fetchCSVData(csvUrl), graphs)
62+
return []
63+
} catch (e) {
64+
setError(e)
65+
return []
66+
}
6267
},
6368
[sampleTimeData, audioUrl, csvUrl],
6469
null
@@ -200,6 +205,10 @@ export const ReactTimeSeriesWithoutContext = ({
200205
throw new Error(`For some reason, no curves are able to be displayed.`)
201206
}
202207

208+
if (error) {
209+
throw error
210+
}
211+
203212
return (
204213
<Measure bounds onResize={onResize}>
205214
{({ measureRef }) => (

src/utils/fix-time-data.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
export default (timeData, graphs) => {
2+
// Make sure that all points have time, and time is always a number
3+
for (const sample of timeData) {
4+
if (sample.time === undefined || sample.time === null)
5+
throw new Error(`Undefined time for sample\n\n${JSON.stringify(sample)}`)
6+
if (typeof sample.time === "string") {
7+
const ogTime = sample.time
8+
if (isNaN(ogTime)) {
9+
sample.time = new Date(sample.time).valueOf()
10+
} else {
11+
sample.time = parseFloat(sample.time)
12+
}
13+
if (isNaN(sample.time)) {
14+
throw new Error(`Couldn't parse time "${sample.time}"`)
15+
}
16+
}
17+
if (isNaN(sample.time)) throw new Error("Time must be a number")
18+
}
19+
20+
// Make sure that timeData has a value in each datapoint for
21+
// each graph (or explicitly doesn't define it w/ undefined)
22+
// No NaNs allowed
23+
for (const graph of graphs) {
24+
let pointsDefined = 0
25+
26+
for (const sample of timeData) {
27+
const v = sample[graph.keyName]
28+
if (v !== undefined && v !== null && isNaN(v))
29+
throw new Error(`Bad value for "${graph.keyName}": "${v}"`)
30+
pointsDefined++
31+
}
32+
33+
if (pointsDefined < 2)
34+
throw new Error(
35+
`Less than two points defined for the "${graph.keyName}" graph`
36+
)
37+
}
38+
39+
return timeData
40+
}

src/utils/validate-time-data.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)