forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArrayDataFrame.ts
69 lines (62 loc) · 1.7 KB
/
ArrayDataFrame.ts
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { QueryResultMeta } from '../types';
import { Field, FieldType, DataFrame, TIME_SERIES_VALUE_FIELD_NAME } from '../types/dataFrame';
import { guessFieldTypeForField } from './processDataFrame';
/**
* The ArrayDataFrame takes an array of objects and presents it as a DataFrame
*
* @deprecated use arrayToDataFrame
*/
export class ArrayDataFrame<T = any> implements DataFrame {
fields: Field[] = [];
length = 0;
name?: string;
refId?: string;
meta?: QueryResultMeta;
constructor(source: T[], names?: string[]) {
return arrayToDataFrame(source, names) as ArrayDataFrame<T>; // returns a standard DataFrame
}
}
/**
* arrayToDataFrame will convert any array into a DataFrame
*
* @public
*/
export function arrayToDataFrame(source: any[], names?: string[]): DataFrame {
const df: DataFrame = {
fields: [],
length: source.length,
};
if (!source?.length) {
return df;
}
if (names) {
for (const name of names) {
df.fields.push(
makeFieldFromValues(
name,
source.map((v) => v[name])
)
);
}
return df;
}
const first = source.find((v) => v != null); // first not null|undefined
if (first != null) {
if (typeof first === 'object') {
df.fields = Object.keys(first).map((name) => {
return makeFieldFromValues(
name,
source.map((v) => v[name])
);
});
} else {
df.fields.push(makeFieldFromValues(TIME_SERIES_VALUE_FIELD_NAME, source));
}
}
return df;
}
function makeFieldFromValues(name: string, values: unknown[]): Field {
const f = { name, config: {}, values, type: FieldType.other };
f.type = guessFieldTypeForField(f) ?? FieldType.other;
return f;
}