@@ -31,22 +31,25 @@ export class DataSource extends DataSourceApi<JsonApiQuery, JsonApiDataSourceOpt
31
31
. filter ( field => field . jsonPath )
32
32
. map ( field => {
33
33
const values = JSONPath ( { path : field . jsonPath , json : response } ) ;
34
+ const [ type , newvals ] = detectFieldType ( values ) ;
34
35
35
36
// Get the path for automatic setting of the field name.
36
37
//
37
38
// Casted to any due to typing issues with JSONPath-Plus
38
39
const paths = ( JSONPath as any ) . toPathArray ( field . jsonPath ) ;
39
-
40
- const [ type , newvals ] = detectFieldType ( values ) ;
40
+ const propertyName = paths [ paths . length - 1 ] ;
41
41
42
42
return {
43
- name : field . name || paths [ paths . length - 1 ] ,
43
+ name : field . name || propertyName ,
44
44
type : type ,
45
45
values : newvals ,
46
46
} ;
47
47
} ) ;
48
48
49
- if ( Array . from ( new Set ( fields . map ( field => field . values . length ) ) ) . length > 1 ) {
49
+ const fieldLengths = fields . map ( field => field . values . length ) ;
50
+
51
+ // All fields need to have the same length for the data frame to be valid.
52
+ if ( Array . from ( new Set ( fieldLengths ) ) . length > 1 ) {
50
53
throw new Error ( 'Fields have different lengths' ) ;
51
54
}
52
55
@@ -56,6 +59,7 @@ export class DataSource extends DataSourceApi<JsonApiQuery, JsonApiDataSourceOpt
56
59
} ) ;
57
60
} ) ;
58
61
62
+ // Wait for all queries to finish before returning the result.
59
63
return Promise . all ( promises ) . then ( data => ( { data } ) ) ;
60
64
}
61
65
@@ -68,9 +72,13 @@ export class DataSource extends DataSourceApi<JsonApiQuery, JsonApiDataSourceOpt
68
72
if ( ! query . jsonPath ) {
69
73
return [ ] ;
70
74
}
71
- return JSONPath ( { path : query . jsonPath , json : await this . api . get ( ) } ) . map ( ( _ : any ) => ( {
72
- text : _ ,
73
- } ) ) ;
75
+
76
+ const response = await this . api . get ( ) ;
77
+
78
+ return JSONPath ( {
79
+ path : query . jsonPath ,
80
+ json : response ,
81
+ } ) . map ( ( _ : any ) => ( { text : _ } ) ) ;
74
82
}
75
83
76
84
/**
@@ -81,6 +89,7 @@ export class DataSource extends DataSourceApi<JsonApiQuery, JsonApiDataSourceOpt
81
89
82
90
try {
83
91
const response = await this . api . test ( ) ;
92
+
84
93
if ( response . status === 200 ) {
85
94
return {
86
95
status : 'success' ,
@@ -118,8 +127,10 @@ export class DataSource extends DataSourceApi<JsonApiQuery, JsonApiDataSourceOpt
118
127
* Detects the type of the values, and converts values if necessary.
119
128
*
120
129
* @param values - The field values.
130
+ * @returns the detected field type and potentially converted values.
121
131
*/
122
132
const detectFieldType = ( values : any [ ] ) : [ FieldType , any [ ] ] => {
133
+ // If all values are valid ISO 8601, the assume that it's a time field.
123
134
const isValidISO = values . every ( value => isValid ( parseISO ( value ) ) ) ;
124
135
if ( isValidISO ) {
125
136
return [ FieldType . time , values . map ( _ => parseISO ( _ ) . valueOf ( ) ) ] ;
@@ -128,13 +139,20 @@ const detectFieldType = (values: any[]): [FieldType, any[]] => {
128
139
const isNumber = values . every ( value => typeof value === 'number' ) ;
129
140
if ( isNumber ) {
130
141
const uniqueLengths = Array . from ( new Set ( values . map ( value => value . toString ( ) . length ) ) ) ;
131
-
132
- if ( uniqueLengths . length === 1 && uniqueLengths [ 0 ] === 13 ) {
133
- return [ FieldType . time , values ] ;
134
- }
135
- if ( uniqueLengths . length === 1 && uniqueLengths [ 0 ] === 10 ) {
136
- return [ FieldType . time , values . map ( _ => _ * 1000.0 ) ] ;
142
+ const hasSameLength = uniqueLengths . length === 1 ;
143
+
144
+ // If all the values have the same length of either 10 (seconds) or 13
145
+ // (milliseconds), assume it's a time field. This is not always true, so we
146
+ // might need to add an option to disable detection of time fields.
147
+ if ( hasSameLength ) {
148
+ if ( uniqueLengths [ 0 ] === 13 ) {
149
+ return [ FieldType . time , values ] ;
150
+ }
151
+ if ( uniqueLengths [ 0 ] === 10 ) {
152
+ return [ FieldType . time , values . map ( _ => _ * 1000.0 ) ] ;
153
+ }
137
154
}
155
+
138
156
return [ FieldType . number , values ] ;
139
157
}
140
158
0 commit comments