chronograf/ui/spec/utils/timeSeriesToDygraphSpec.js

180 lines
3.7 KiB
JavaScript
Raw Normal View History

import timeSeriesToDygraph from 'src/utils/timeSeriesToDygraph';
2016-12-14 20:22:08 +00:00
import {STROKE_WIDTH} from 'src/shared/constants';
const {light: strokeWidth} = STROKE_WIDTH;
describe('timeSeriesToDygraph', () => {
it('parses a raw InfluxDB response into a dygraph friendly data format', () => {
const influxResponse = [
{
"response":
{
"results": [
{
"series": [
{
"name":"m1",
"columns": ["time","f1"],
"values": [[1000, 1],[2000, 2]],
},
]
},
{
"series": [
{
"name":"m1",
"columns": ["time","f2"],
"values": [[2000, 3],[4000, 4]],
},
]
},
],
},
}
];
const actual = timeSeriesToDygraph(influxResponse);
const expected = {
labels: [
'time',
`m1.f1`,
`m1.f2`,
],
timeSeries: [
[new Date(1000), 1, null],
[new Date(2000), 2, 3],
[new Date(4000), null, 4],
],
2016-11-28 20:53:03 +00:00
dygraphSeries: {
'm1.f1': {
axis: 'y',
2016-12-14 20:22:08 +00:00
strokeWidth,
2016-11-28 20:53:03 +00:00
},
'm1.f2': {
axis: 'y',
2016-12-14 20:22:08 +00:00
strokeWidth,
2016-11-28 20:53:03 +00:00
},
},
};
expect(actual).to.deep.equal(expected);
});
it('can sort numerical timestamps correctly', () => {
const influxResponse = [
{
"response":
{
"results": [
{
"series": [
{
"name":"m1",
"columns": ["time","f1"],
"values": [[100, 1],[3000, 3],[200, 2]],
},
]
},
],
},
}
];
const actual = timeSeriesToDygraph(influxResponse);
const expected = {
labels: [
'time',
'm1.f1',
],
timeSeries: [
[new Date(100), 1],
[new Date(200), 2],
[new Date(3000), 3],
],
};
2016-11-28 20:53:03 +00:00
expect(actual.timeSeries).to.deep.equal(expected.timeSeries);
});
it('can parse multiple responses into two axes', () => {
const influxResponse = [
{
"response":
{
"results": [
{
"series": [
{
"name":"m1",
"columns": ["time","f1"],
"values": [[1000, 1],[2000, 2]],
},
]
},
{
"series": [
{
"name":"m1",
"columns": ["time","f2"],
"values": [[2000, 3],[4000, 4]],
},
]
},
],
},
},
{
"response":
{
"results": [
{
"series": [
{
"name":"m3",
"columns": ["time","f3"],
"values": [[1000, 1],[2000, 2]],
},
]
},
],
},
},
];
const actual = timeSeriesToDygraph(influxResponse);
const expected = {
labels: [
2016-11-28 20:53:03 +00:00
'time',
`m1.f1`,
`m1.f2`,
`m3.f3`,
],
timeSeries: [
[new Date(1000), 1, null, 1],
[new Date(2000), 2, 3, 2],
[new Date(4000), null, 4, null],
],
dygraphSeries: {
'm1.f1': {
axis: 'y',
2016-12-14 20:22:08 +00:00
strokeWidth,
2016-11-28 20:53:03 +00:00
},
'm1.f2': {
axis: 'y',
2016-12-14 20:22:08 +00:00
strokeWidth,
2016-11-28 20:53:03 +00:00
},
'm3.f3': {
axis: 'y2',
2016-12-14 20:22:08 +00:00
strokeWidth,
2016-11-28 20:53:03 +00:00
},
},
};
expect(actual.dygraphSeries).to.deep.equal(expected.dygraphSeries);
});
});