From d60c2dff40dcec4d9452556151e4c1da4764e3ed Mon Sep 17 00:00:00 2001 From: Artur Wierzbicki Date: Wed, 9 Feb 2022 22:12:24 +0400 Subject: [PATCH] Chore: export `closestIdx` from `StreamingDataFrame` (#45162) * #41993: export closestIdx function * #41993: formatting * #41993: formatting v2 --- .../live/data/StreamingDataFrame.test.ts | 69 ++++++++++++++++++- .../features/live/data/StreamingDataFrame.ts | 33 ++++----- 2 files changed, 81 insertions(+), 21 deletions(-) diff --git a/public/app/features/live/data/StreamingDataFrame.test.ts b/public/app/features/live/data/StreamingDataFrame.test.ts index 1c59069e6e5..c9875d5e8d3 100644 --- a/public/app/features/live/data/StreamingDataFrame.test.ts +++ b/public/app/features/live/data/StreamingDataFrame.test.ts @@ -1,8 +1,71 @@ -import { reduceField, ReducerID, getFieldDisplayName, DataFrame, FieldType, DataFrameJSON } from '@grafana/data'; +import { DataFrame, DataFrameJSON, FieldType, getFieldDisplayName, reduceField, ReducerID } from '@grafana/data'; import { StreamingFrameAction, StreamingFrameOptions } from '@grafana/runtime'; -import { getStreamingFrameOptions, StreamingDataFrame } from './StreamingDataFrame'; +import { closestIdx, getStreamingFrameOptions, StreamingDataFrame } from './StreamingDataFrame'; describe('Streaming JSON', () => { + describe('closestIdx', function () { + [ + { + num: 10, + arr: [2, 3, 4, 5, 6], + expected: 4, + descr: 'bigger than all in array', + }, + { + num: 10, + arr: [2, 3, 4, 5, 11, 12, 13], + expected: 4, + descr: 'bigger than some in array #1 - smaller difference to bigger number', + }, + { + num: 10, + arr: [2, 3, 4, 5, 16, 17, 18], + expected: 3, + descr: 'bigger than some in array #2 - smaller difference to smaller number', + }, + { + num: 10, + arr: [2, 3, 4, 9, 11, 12, 13], + expected: 3, + descr: 'bigger than some in array #3 - same difference between smaller and bigger number - favors smaller', + }, + { + num: 10, + arr: [9, 10, 11, 12, 13, 14], + expected: 1, + descr: 'present in the array', + }, + { + num: 10, + arr: [10, 11, 12, 13, 14], + expected: 0, + descr: 'present in the array on first position', + }, + { + num: 10, + arr: [5, 6, 7, 8, 9, 10], + expected: 5, + descr: 'present in the array on last position', + }, + { + num: 10, + arr: [11, 12, 13, 14, 15], + expected: 0, + descr: 'smaller than all in array', + }, + { + num: 10, + arr: [], + expected: -1, + descr: 'empty array', + }, + ].forEach(({ num, arr, expected, descr }) => { + it(descr, () => { + expect(closestIdx(num, arr)).toEqual(expected); + }); + }); + }); + describe('when called with a DataFrame', () => { const json: DataFrameJSON = { schema: { @@ -506,7 +569,7 @@ describe('Streaming JSON', () => { expect(val).toEqual(200); expect(stream.length).toEqual(2); - const copy = ({ ...stream } as any) as DataFrame; + const copy = { ...stream } as any as DataFrame; expect(copy.length).toEqual(2); }); diff --git a/public/app/features/live/data/StreamingDataFrame.ts b/public/app/features/live/data/StreamingDataFrame.ts index b1f69db9183..71d22196d6a 100644 --- a/public/app/features/live/data/StreamingDataFrame.ts +++ b/public/app/features/live/data/StreamingDataFrame.ts @@ -1,23 +1,20 @@ import { + ArrayVector, DataFrame, + DataFrameJSON, + decodeFieldValueEntities, Field, FieldDTO, + FieldSchema, FieldType, + guessFieldTypeFromValue, Labels, + parseLabels, QueryResultMeta, - DataFrameJSON, - decodeFieldValueEntities, - FieldSchema, - guessFieldTypeFromValue, - ArrayVector, toFilteredDataFrameDTO, - parseLabels, } from '@grafana/data'; import { join } from '@grafana/data/src/transformations/transformers/joinDataFrames'; -import { - StreamingFrameAction, - StreamingFrameOptions, -} from '@grafana/runtime/src/services/live'; +import { StreamingFrameAction, StreamingFrameOptions } from '@grafana/runtime/src/services/live'; import { renderLegendFormat } from 'app/plugins/datasource/prometheus/legend'; import { AlignedData } from 'uplot'; @@ -209,7 +206,7 @@ export class StreamingDataFrame implements DataFrame { const firstField = schema.fields[0]; if ( this.timeFieldIndex === 1 && - firstField.type === FieldType.string && + firstField.type === FieldType.string && (firstField.name === 'labels' || firstField.name === 'Labels') ) { this.pushMode = PushMode.labels; @@ -230,10 +227,10 @@ export class StreamingDataFrame implements DataFrame { const sf = niceSchemaFields[idx % len]; f.config = sf.config ?? {}; f.labels = sf.labels; - }); + }); if (displayNameFormat) { this.fields.forEach((f) => { - const labels = {[PROM_STYLE_METRIC_LABEL]:f.name, ...f.labels}; + const labels = { [PROM_STYLE_METRIC_LABEL]: f.name, ...f.labels }; f.config.displayNameFromDS = renderLegendFormat(displayNameFormat, labels); }); } @@ -243,7 +240,7 @@ export class StreamingDataFrame implements DataFrame { this.fields = niceSchemaFields.map((f) => { const config = f.config ?? {}; if (displayNameFormat) { - const labels = {[PROM_STYLE_METRIC_LABEL]:f.name, ...f.labels}; + const labels = { [PROM_STYLE_METRIC_LABEL]: f.name, ...f.labels }; config.displayNameFromDS = renderLegendFormat(displayNameFormat, labels); } return { @@ -411,7 +408,7 @@ export class StreamingDataFrame implements DataFrame { if (i > 0) { f.labels = parsedLabels; if (displayNameFormat) { - const labels = {[PROM_STYLE_METRIC_LABEL]:f.name, ...parsedLabels}; + const labels = { [PROM_STYLE_METRIC_LABEL]: f.name, ...parsedLabels }; f.config.displayNameFromDS = renderLegendFormat(displayNameFormat, labels); } } @@ -421,7 +418,7 @@ export class StreamingDataFrame implements DataFrame { let proto = this.schemaFields[i] as Field; const config = proto.config ?? {}; if (displayNameFormat) { - const labels = {[PROM_STYLE_METRIC_LABEL]:proto.name, ...parsedLabels}; + const labels = { [PROM_STYLE_METRIC_LABEL]: proto.name, ...parsedLabels }; config.displayNameFromDS = renderLegendFormat(displayNameFormat, labels); } this.fields.push({ @@ -434,7 +431,7 @@ export class StreamingDataFrame implements DataFrame { } this.labels.add(label); - }; + } getOptions = (): Readonly => this.options; } @@ -473,7 +470,7 @@ export function transpose(vrecs: any[][]) { } // binary search for index of closest value -function closestIdx(num: number, arr: number[], lo?: number, hi?: number) { +export function closestIdx(num: number, arr: number[], lo?: number, hi?: number) { let mid; lo = lo || 0; hi = hi || arr.length - 1;