123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- import {
- toDataFrame,
- ArrayVector,
- DataFrame,
- FieldType,
- toDataFrameDTO,
- DataFrameDTO,
- DataFrameType,
- getFrameDisplayName,
- } from '@grafana/data';
- import { prepareTimeSeriesTransformer, PrepareTimeSeriesOptions, timeSeriesFormat } from './prepareTimeSeries';
- describe('Prepare time series transformer', () => {
- it('should transform wide to many', () => {
- const source = [
- toDataFrame({
- name: 'wide',
- refId: 'A',
- fields: [
- { name: 'time', type: FieldType.time, values: [1, 2, 3, 4, 5, 6] },
- { name: 'count', type: FieldType.number, values: [10, 20, 30, 40, 50, 60] },
- { name: 'more', type: FieldType.number, values: [2, 3, 4, 5, 6, 7] },
- ],
- }),
- ];
- const config: PrepareTimeSeriesOptions = {
- format: timeSeriesFormat.TimeSeriesMany,
- };
- expect(prepareTimeSeriesTransformer.transformer(config)(source)).toEqual([
- toEquableDataFrame({
- name: 'wide',
- refId: 'A',
- fields: [
- { name: 'time', type: FieldType.time, values: [1, 2, 3, 4, 5, 6] },
- { name: 'count', type: FieldType.number, values: [10, 20, 30, 40, 50, 60] },
- ],
- meta: {
- type: DataFrameType.TimeSeriesMany,
- },
- length: 6,
- }),
- toEquableDataFrame({
- name: 'wide',
- refId: 'A',
- fields: [
- { name: 'time', type: FieldType.time, values: [1, 2, 3, 4, 5, 6] },
- { name: 'more', type: FieldType.number, values: [2, 3, 4, 5, 6, 7] },
- ],
- meta: {
- type: DataFrameType.TimeSeriesMany,
- },
- length: 6,
- }),
- ]);
- });
- it('should treat string fields as labels', () => {
- const source = [
- toDataFrame({
- name: 'wide',
- refId: 'A',
- fields: [
- { name: 'time', type: FieldType.time, values: [1, 1, 2, 2] },
- { name: 'region', type: FieldType.string, values: ['a', 'b', 'a', 'b'] },
- { name: 'count', type: FieldType.number, values: [10, 20, 30, 40] },
- { name: 'more', type: FieldType.number, values: [2, 3, 4, 5] },
- ],
- }),
- ];
- const config: PrepareTimeSeriesOptions = {
- format: timeSeriesFormat.TimeSeriesMany,
- };
- const frames = prepareTimeSeriesTransformer.transformer(config)(source);
- expect(frames.length).toEqual(4);
- expect(
- frames.map((f) => ({
- name: getFrameDisplayName(f),
- labels: f.fields[1].labels,
- time: f.fields[0].values.toArray(),
- values: f.fields[1].values.toArray(),
- }))
- ).toMatchInlineSnapshot(`
- Array [
- Object {
- "labels": Object {
- "region": "a",
- },
- "name": "wide",
- "time": Array [
- 1,
- 2,
- ],
- "values": Array [
- 10,
- 30,
- ],
- },
- Object {
- "labels": Object {
- "region": "b",
- },
- "name": "wide",
- "time": Array [
- 1,
- 2,
- ],
- "values": Array [
- 20,
- 40,
- ],
- },
- Object {
- "labels": Object {
- "region": "a",
- },
- "name": "wide",
- "time": Array [
- 1,
- 2,
- ],
- "values": Array [
- 2,
- 4,
- ],
- },
- Object {
- "labels": Object {
- "region": "b",
- },
- "name": "wide",
- "time": Array [
- 1,
- 2,
- ],
- "values": Array [
- 3,
- 5,
- ],
- },
- ]
- `);
- });
- it('should transform all wide to many when mixed', () => {
- const source = [
- toDataFrame({
- name: 'wide',
- refId: 'A',
- fields: [
- { name: 'time', type: FieldType.time, values: [0, 1, 2, 3, 4, 5] },
- { name: 'count', type: FieldType.number, values: [10, 20, 30, 40, 50, 60] },
- { name: 'another', type: FieldType.number, values: [2, 3, 4, 5, 6, 7] },
- ],
- }),
- toDataFrame({
- name: 'long',
- refId: 'B',
- fields: [
- { name: 'time', type: FieldType.time, values: [4, 5, 6, 7, 8, 9] },
- { name: 'value', type: FieldType.number, values: [2, 3, 4, 5, 6, 7] },
- ],
- }),
- ];
- const config: PrepareTimeSeriesOptions = {
- format: timeSeriesFormat.TimeSeriesMany,
- };
- expect(prepareTimeSeriesTransformer.transformer(config)(source)).toEqual([
- toEquableDataFrame({
- name: 'wide',
- refId: 'A',
- fields: [
- { name: 'time', type: FieldType.time, values: [0, 1, 2, 3, 4, 5] },
- { name: 'another', type: FieldType.number, values: [2, 3, 4, 5, 6, 7] },
- ],
- length: 6,
- meta: {
- type: DataFrameType.TimeSeriesMany,
- },
- }),
- toEquableDataFrame({
- name: 'wide',
- refId: 'A',
- fields: [
- { name: 'time', type: FieldType.time, values: [0, 1, 2, 3, 4, 5] },
- { name: 'count', type: FieldType.number, values: [10, 20, 30, 40, 50, 60] },
- ],
- length: 6,
- meta: {
- type: DataFrameType.TimeSeriesMany,
- },
- }),
- toEquableDataFrame({
- name: 'long',
- refId: 'B',
- fields: [
- { name: 'time', type: FieldType.time, values: [4, 5, 6, 7, 8, 9] },
- { name: 'value', type: FieldType.number, values: [2, 3, 4, 5, 6, 7] },
- ],
- length: 6,
- meta: {
- type: DataFrameType.TimeSeriesMany,
- },
- }),
- ]);
- });
- it('should transform none when source only has long frames', () => {
- const source = [
- toDataFrame({
- name: 'long',
- refId: 'A',
- fields: [
- { name: 'time', type: FieldType.time, values: [1, 2, 3, 4, 5, 6] },
- { name: 'count', type: FieldType.number, values: [10, 20, 30, 40, 50, 60] },
- ],
- }),
- toDataFrame({
- name: 'long',
- refId: 'B',
- fields: [
- { name: 'time', type: FieldType.time, values: [1, 2, 3, 4, 5, 6] },
- { name: 'count', type: FieldType.number, values: [10, 20, 30, 40, 50, 60] },
- ],
- }),
- ];
- const config: PrepareTimeSeriesOptions = {
- format: timeSeriesFormat.TimeSeriesMany,
- };
- expect(toEquableDataFrames(prepareTimeSeriesTransformer.transformer(config)(source))).toEqual(
- toEquableDataFrames(
- source.map((frame) => ({
- ...frame,
- meta: {
- type: DataFrameType.TimeSeriesMany,
- },
- }))
- )
- );
- });
- it('should return empty array when no timeseries exist', () => {
- const source = [
- toDataFrame({
- name: 'wide',
- refId: 'A',
- fields: [
- { name: 'text', type: FieldType.string, values: ['a', 'z', 'b', 'x', 'c', 'b'] },
- { name: 'text', type: FieldType.string, values: ['a', 'z', 'b', 'x', 'c', 'b'] },
- { name: 'text', type: FieldType.string, values: ['a', 'z', 'b', 'x', 'c', 'b'] },
- ],
- }),
- toDataFrame({
- name: 'wide',
- refId: 'B',
- fields: [
- { name: 'text', type: FieldType.string, values: ['a', 'z', 'b', 'x', 'c', 'b'] },
- { name: 'text', type: FieldType.string, values: ['a', 'z', 'b', 'x', 'c', 'b'] },
- { name: 'text', type: FieldType.string, values: ['a', 'z', 'b', 'x', 'c', 'b'] },
- ],
- }),
- ];
- const config: PrepareTimeSeriesOptions = {
- format: timeSeriesFormat.TimeSeriesMany,
- };
- expect(prepareTimeSeriesTransformer.transformer(config)(source)).toEqual([]);
- });
- it('should convert long to many', () => {
- const source = [
- toDataFrame({
- name: 'long',
- refId: 'X',
- fields: [
- { name: 'time', type: FieldType.time, values: [1, 1, 2, 2, 3, 3] },
- { name: 'value', type: FieldType.number, values: [10, 20, 30, 40, 50, 60] },
- { name: 'region', type: FieldType.string, values: ['a', 'b', 'a', 'b', 'a', 'b'] },
- ],
- }),
- ];
- const config: PrepareTimeSeriesOptions = {
- format: timeSeriesFormat.TimeSeriesMany,
- };
- const frames = prepareTimeSeriesTransformer.transformer(config)(source);
- expect(frames).toEqual([
- toEquableDataFrame({
- name: 'long',
- refId: 'X',
- fields: [
- { name: 'time', type: FieldType.time, values: [1, 2, 3] },
- { name: 'value', labels: { region: 'a' }, type: FieldType.number, values: [10, 30, 50] },
- ],
- length: 3,
- meta: {
- type: DataFrameType.TimeSeriesMany,
- },
- }),
- toEquableDataFrame({
- name: 'long',
- refId: 'X',
- fields: [
- { name: 'time', type: FieldType.time, values: [1, 2, 3] },
- { name: 'value', labels: { region: 'b' }, type: FieldType.number, values: [20, 40, 60] },
- ],
- length: 3,
- meta: {
- type: DataFrameType.TimeSeriesMany,
- },
- }),
- ]);
- });
- });
- function toEquableDataFrame(source: any): DataFrame {
- return toDataFrame({
- meta: undefined,
- ...source,
- fields: source.fields.map((field: any) => {
- return {
- ...field,
- values: new ArrayVector(field.values),
- config: {},
- };
- }),
- });
- }
- function toEquableDataFrames(data: DataFrame[]): DataFrameDTO[] {
- return data.map((frame) => toDataFrameDTO(frame));
- }
|