Skip to content

Commit b105433

Browse files
authored
automatically detect dates in strings (#25)
1 parent da411ca commit b105433

8 files changed

Lines changed: 60 additions & 13 deletions

File tree

dist/module.js

Lines changed: 20 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/module.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
],
2222
"screenshots": [],
2323
"version": "1.1.2",
24-
"updated": "2020-06-30"
24+
"updated": "2020-07-01"
2525
},
2626

2727
"dependencies": {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"@grafana/data": "7.0.3",
1515
"@grafana/runtime": "7.0.3",
1616
"@grafana/toolkit": "7.0.3",
17-
"@grafana/ui": "7.0.3"
17+
"@grafana/ui": "7.0.3",
18+
"moment":"*"
1819
},
1920
"dependencies": {
2021
"@types/lodash": "^4.14.144"

src/DataSource.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { MyQuery, MyDataSourceOptions, defaultQuery } from './types';
1515
import { dateTime, MutableDataFrame, FieldType, DataFrame } from '@grafana/data';
1616
import { getTemplateSrv } from '@grafana/runtime';
1717
import _ from 'lodash';
18-
import { flatten } from './util';
18+
import { flatten, isRFC3339_ISO6801 } from './util';
1919

2020
export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
2121
basicAuth: string | undefined;
@@ -168,7 +168,7 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
168168
}
169169
for (const fieldName in doc) {
170170
let t: FieldType = FieldType.string;
171-
if (fieldName === 'Time') {
171+
if (fieldName === 'Time' || isRFC3339_ISO6801(doc[fieldName])) {
172172
t = FieldType.time;
173173
} else if (_.isNumber(doc[fieldName])) {
174174
t = FieldType.number;

src/util.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { flatten } from './util';
1+
import { flatten, isRFC3339_ISO6801 } from './util';
22

33
test('flatten function test', () => {
44
let obj = {
@@ -35,3 +35,15 @@ test('flatten function test', () => {
3535

3636
expect(flatten(obj)).toEqual(flattenObj);
3737
});
38+
39+
test('RFC3339 and ISO8601 valid string test', () => {
40+
expect(isRFC3339_ISO6801('I am not a date but a string')).toBe(false);
41+
expect(isRFC3339_ISO6801('1234')).toBe(false);
42+
expect(isRFC3339_ISO6801(8)).toBe(false);
43+
expect(isRFC3339_ISO6801(null)).toBe(false);
44+
expect(isRFC3339_ISO6801('2020-06-01T00:00:00.000Z')).toBe(true);
45+
expect(isRFC3339_ISO6801('2020-06-01T00:00:00Z')).toBe(true);
46+
expect(isRFC3339_ISO6801(true)).toBe(false);
47+
expect(isRFC3339_ISO6801(0)).toBe(false);
48+
expect(isRFC3339_ISO6801(0.111111)).toBe(false);
49+
});

src/util.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
1+
import { dateTime, ISO_8601 } from '@grafana/data';
2+
13
export function flatten<T extends Record<string, any>>(object: T, path: string | null = null, separator = '.'): T {
24
return Object.keys(object).reduce((acc: T, key: string): T => {
35
const isObject = typeof object[key] === 'object' && object[key] != null;
46
const newPath = [path, key].filter(Boolean).join(separator);
57
return isObject ? { ...acc, ...flatten(object[key], newPath, separator) } : { ...acc, [newPath]: object[key] };
68
}, {} as T);
79
}
10+
11+
export function isRFC3339_ISO6801(str: any): boolean {
12+
let date = dateTime(str, ISO_8601);
13+
if (date.isValid()) {
14+
let iso = date.toISOString();
15+
if (iso === str) {
16+
return true;
17+
} else {
18+
// some RFC3339 dates don't include fractions of a second to same resolution, but still valid.
19+
return iso.substring(0, 19) === str.substring(0, 19);
20+
}
21+
}
22+
return false;
23+
}

yarn.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7648,16 +7648,16 @@ mkdirp@0.x, "mkdirp@>=0.5 0", mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@^0.5.3, mkdir
76487648
dependencies:
76497649
minimist "^1.2.5"
76507650

7651+
moment@*, moment@2.x:
7652+
version "2.27.0"
7653+
resolved "https://registry.yarnpkg.com/moment/-/moment-2.27.0.tgz#8bff4e3e26a236220dfe3e36de756b6ebaa0105d"
7654+
integrity sha512-al0MUK7cpIcglMv3YF13qSgdAIqxHTO7brRtaz3DlSULbqfazqkc5kEjNrLDOM7fsjshoFIihnU8snrP7zUvhQ==
7655+
76517656
moment@2.24.0:
76527657
version "2.24.0"
76537658
resolved "https://registry.yarnpkg.com/moment/-/moment-2.24.0.tgz#0d055d53f5052aa653c9f6eb68bb5d12bf5c2b5b"
76547659
integrity sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg==
76557660

7656-
moment@2.x:
7657-
version "2.27.0"
7658-
resolved "https://registry.yarnpkg.com/moment/-/moment-2.27.0.tgz#8bff4e3e26a236220dfe3e36de756b6ebaa0105d"
7659-
integrity sha512-al0MUK7cpIcglMv3YF13qSgdAIqxHTO7brRtaz3DlSULbqfazqkc5kEjNrLDOM7fsjshoFIihnU8snrP7zUvhQ==
7660-
76617661
move-concurrently@^1.0.1:
76627662
version "1.0.1"
76637663
resolved "https://registry.yarnpkg.com/move-concurrently/-/move-concurrently-1.0.1.tgz#be2c005fda32e0b29af1f05d7c4b33214c701f92"

0 commit comments

Comments
 (0)