Chore: export closestIdx from StreamingDataFrame (#45162)

* #41993: export closestIdx function

* #41993: formatting

* #41993: formatting v2
This commit is contained in:
Artur Wierzbicki 2022-02-09 22:12:24 +04:00 committed by GitHub
parent 67423f42a5
commit d60c2dff40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 82 additions and 22 deletions

View File

@ -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);
});

View File

@ -1,23 +1,20 @@
import {
ArrayVector,
DataFrame,
Field,
FieldDTO,
FieldType,
Labels,
QueryResultMeta,
DataFrameJSON,
decodeFieldValueEntities,
Field,
FieldDTO,
FieldSchema,
FieldType,
guessFieldTypeFromValue,
ArrayVector,
toFilteredDataFrameDTO,
Labels,
parseLabels,
QueryResultMeta,
toFilteredDataFrameDTO,
} 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<StreamingFrameOptions> => 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;