-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't detect time fields from Unix epoch
Fixes #82
- Loading branch information
1 parent
c5975d7
commit a35ebfe
Showing
5 changed files
with
102 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { isValid, parseISO } from 'date-fns'; | ||
import { FieldType } from '@grafana/data'; | ||
|
||
/** | ||
* Detects the field type from an array of values. | ||
*/ | ||
export const detectFieldType = (values: any[]): FieldType => { | ||
// If all values are null, default to strings. | ||
if (values.every((_) => _ === null)) { | ||
return FieldType.string; | ||
} | ||
|
||
// If all values are valid ISO 8601, then assume that it's a time field. | ||
const isValidISO = values | ||
.filter((value) => value !== null) | ||
.every((value) => value.length >= 10 && isValid(parseISO(value))); | ||
if (isValidISO) { | ||
return FieldType.time; | ||
} | ||
|
||
if (values.every((value) => typeof value === 'number')) { | ||
return FieldType.number; | ||
} | ||
|
||
if (values.every((value) => typeof value === 'boolean')) { | ||
return FieldType.boolean; | ||
} | ||
|
||
return FieldType.string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { parseISO } from 'date-fns'; | ||
import { FieldType } from '@grafana/data'; | ||
|
||
/** | ||
* parseValues converts values to the given field type. | ||
*/ | ||
export const parseValues = (values: any[], type: FieldType): any[] => { | ||
switch (type) { | ||
case FieldType.time: | ||
// For time field, values are expected to be numbers representing a Unix | ||
// epoch in milliseconds. | ||
|
||
if (values.filter((_) => _).every((value) => typeof value === 'string')) { | ||
return values.map((_) => (_ !== null ? parseISO(_).valueOf() : _)); | ||
} | ||
|
||
if (values.filter((_) => _).every((value) => typeof value === 'number')) { | ||
const ms = 1_000_000_000_000; | ||
|
||
// If there are no "big" numbers, assume seconds. | ||
if (values.filter((_) => _).every((_) => _ < ms)) { | ||
return values.map((_) => (_ !== null ? _ * 1000.0 : _)); | ||
} | ||
|
||
// ... otherwise assume milliseconds. | ||
return values; | ||
} | ||
|
||
throw new Error('Unsupported time property'); | ||
case FieldType.string: | ||
return values.every((_) => typeof _ === 'string') ? values : values.map((_) => (_ !== null ? _.toString() : _)); | ||
case FieldType.number: | ||
return values.every((_) => typeof _ === 'number') ? values : values.map((_) => (_ !== null ? parseFloat(_) : _)); | ||
case FieldType.boolean: | ||
return values.every((_) => typeof _ === 'boolean') | ||
? values | ||
: values.map((_) => { | ||
if (_ === null) { | ||
return _; | ||
} | ||
|
||
switch (_.toString()) { | ||
case '0': | ||
case 'false': | ||
case 'FALSE': | ||
case 'False': | ||
return false; | ||
case '1': | ||
case 'true': | ||
case 'TRUE': | ||
case 'True': | ||
return true; | ||
default: | ||
throw new Error('Found non-boolean values in a field of type boolean: ' + _.toString()); | ||
} | ||
}); | ||
default: | ||
throw new Error('Unsupported field type'); | ||
} | ||
}; |