-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
52 lines (46 loc) · 1.12 KB
/
utils.ts
File metadata and controls
52 lines (46 loc) · 1.12 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
import CSV from "csv-string"
import { Point } from "@influxdata/influxdb-client"
const beginsWithFloat = (val: string) => {
return !isNaN(parseFloat(val))
}
// TODO: find type
export const parse_csv_points = (body: any) => {
return CSV.parse(body, { output: "objects" }).map((item) => {
return Object.keys(item).reduce((prev, key) => {
return {
...prev,
[key]:
key === "time" && beginsWithFloat(item[key])
? item[key]
: parseFloat(item[key]),
}
}, {})
})
}
// TODO: tidy up
export const create_single_point = ({
data,
measurement,
}: {
data: any
measurement: string
}) => {
const point = new Point(measurement)
for (const field in data) {
const value = data[field]
if (field === "time") {
// Add time if provided
point.timestamp(new Date(value))
} else if (typeof value === "number") {
if (!isNaN(value)) {
// float value
// TODO: typing
point.floatField(field, parseFloat(value as any))
}
} else {
// String value
point.stringField(field, value)
}
}
return point
}