FieldValues: Implement array accessors for deprecated Vector types (#66807)

This commit is contained in:
Ryan McKinley
2023-04-19 00:07:27 +03:00
committed by GitHub
parent 9452c0d718
commit 987eff82a3
18 changed files with 137 additions and 144 deletions
@@ -8,6 +8,9 @@ describe('Check Appending Vector', () => {
appended.append(new ArrayVector([4, 5, 6]));
appended.append(new ArrayVector([7, 8, 9]));
expect(appended.length).toEqual(9);
expect(appended[0]).toEqual(1);
expect(appended[1]).toEqual(2);
expect(appended[100]).toEqual(undefined);
appended.setLength(5);
expect(appended.length).toEqual(5);
@@ -1,4 +1,4 @@
import { Vector } from '../types/vector';
import { Vector, makeArrayIndexableVector } from '../types/vector';
import { FunctionalVector } from './FunctionalVector';
import { vectorToArray } from './vectorToArray';
@@ -14,7 +14,7 @@ interface AppendedVectorInfo<T> {
* RAM -- rather than allocate a new array the size of all previous arrays, this just
* points the correct index to their original array values
*
* @deprecated use a simple Arrays
* @deprecated use a simple Arrays. NOTE this is not used in grafana core
*/
export class AppendedVectors<T = any> extends FunctionalVector<T> {
length = 0;
@@ -23,6 +23,7 @@ export class AppendedVectors<T = any> extends FunctionalVector<T> {
constructor(startAt = 0) {
super();
this.length = startAt;
return makeArrayIndexableVector(this);
}
/**
@@ -1,23 +1,14 @@
import { Vector } from '../types';
import { FunctionalVector } from './FunctionalVector';
/**
* This will force all values to be numbers
*
* @public
* @deprecated use a simple Arrays
* @deprecated use a simple Arrays. NOTE: Not used in grafana core
*/
export class AsNumberVector extends FunctionalVector<number> {
constructor(private field: Vector) {
export class AsNumberVector extends Array<number> {
constructor(field: Vector) {
super();
}
get length() {
return this.field.length;
}
get(index: number) {
return +this.field.get(index);
return field.map((v) => +v) as AsNumberVector;
}
}
@@ -11,9 +11,13 @@ describe('ScaledVector', () => {
const operation = binaryOperators.get(BinaryOperationID.Multiply).operation;
const v = new BinaryOperationVector(source, new ConstantVector(scale, source.length), operation);
expect(v.length).toEqual(source.length);
// expect(v.push(10)).toEqual(source.length); // not implemented
for (let i = 0; i < 10; i++) {
// Accessed with getters
for (let i = 0; i < 4; i++) {
expect(v.get(i)).toEqual(source.get(i) * scale);
}
// Accessed with array index
for (let i = 0; i < 4; i++) {
expect(v[i]).toEqual(source[i] * scale);
}
});
});
@@ -1,31 +1,18 @@
import { Vector } from '../types/vector';
import { BinaryOperation } from '../utils/binaryOperators';
import { FunctionalVector } from './FunctionalVector';
import { vectorToArray } from './vectorToArray';
/**
* @public
* @deprecated use a simple Arrays
* @deprecated use a simple Arrays. NOTE: Not used in grafana core
*/
export class BinaryOperationVector extends FunctionalVector<number> {
constructor(private left: Vector<number>, private right: Vector<number>, private operation: BinaryOperation) {
export class BinaryOperationVector extends Array<number> {
constructor(left: Vector<number>, right: Vector<number>, operation: BinaryOperation) {
super();
}
get length(): number {
return this.left.length;
}
get(index: number): number {
return this.operation(this.left.get(index), this.right.get(index));
}
toArray(): number[] {
return vectorToArray(this);
}
toJSON(): number[] {
return vectorToArray(this);
const arr = new Array(left.length);
for (let i = 0; i < arr.length; i++) {
arr[i] = operation(left[i], right[i]);
}
return arr as BinaryOperationVector;
}
}
@@ -5,6 +5,10 @@ describe('Check Circular Vector', () => {
const buffer = [1, 2, 3];
const v = new CircularVector({ buffer }); // tail is default option
expect(v.toArray()).toEqual([1, 2, 3]);
expect(v[0]).toEqual(1);
expect(v[1]).toEqual(2);
expect(v[2]).toEqual(3);
expect(v[3]).toEqual(1); // loops back to one
v.add(4);
expect(v.toArray()).toEqual([2, 3, 4]);
@@ -1,3 +1,5 @@
import { makeArrayIndexableVector } from '../types';
import { FunctionalVector } from './FunctionalVector';
interface CircularOptions<T> {
@@ -34,6 +36,7 @@ export class CircularVector<T = any> extends FunctionalVector<T> {
if (options.capacity) {
this.setCapacity(options.capacity);
}
return makeArrayIndexableVector(this);
}
/**
@@ -10,8 +10,9 @@ describe('ConstantVector', () => {
expect(v.get(1)).toEqual(value);
// Now check all of them
for (let i = 0; i < 10; i++) {
for (let i = 0; i < 7; i++) {
expect(v.get(i)).toEqual(value);
expect(v[i]).toEqual(value);
}
});
});
@@ -1,28 +1,10 @@
import { FunctionalVector } from './FunctionalVector';
/**
* @public
* @deprecated use a simple Arrays
* @deprecated use a simple Arrays. NOTE: Not used in grafana core.
*/
export class ConstantVector<T = any> extends FunctionalVector<T> {
constructor(private value: T, private len: number) {
export class ConstantVector<T = any> extends Array<T> {
constructor(value: T, len: number) {
super();
}
get length() {
return this.len;
}
get(index: number): T {
return this.value;
}
toArray(): T[] {
const arr = new Array<T>(this.length);
return arr.fill(this.value);
}
toJSON(): T[] {
return this.toArray();
return new Array<T>(len).fill(value) as ConstantVector<T>;
}
}
@@ -2,23 +2,13 @@ import { DisplayProcessor } from '../types';
import { Vector } from '../types/vector';
import { formattedValueToString } from '../valueFormats';
import { FunctionalVector } from './FunctionalVector';
/**
* @public
* @deprecated use a simple Arrays
* @deprecated use a simple Arrays. NOTE: not used in grafana core.
*/
export class FormattedVector<T = any> extends FunctionalVector<string> {
constructor(private source: Vector<T>, private formatter: DisplayProcessor) {
export class FormattedVector<T = any> extends Array<string> {
constructor(source: Vector<T>, formatter: DisplayProcessor) {
super();
}
get length() {
return this.source.length;
}
get(index: number): string {
const v = this.source.get(index);
return formattedValueToString(this.formatter(v));
return source.map((v) => formattedValueToString(formatter(v))) as FormattedVector<T>;
}
}
+10 -13
View File
@@ -1,29 +1,26 @@
import { Field, FieldType } from '../types';
import { FunctionalVector } from './FunctionalVector';
/**
* IndexVector is a simple vector implementation that returns the index value
* for each element in the vector. It is functionally equivolant a vector backed
* by an array with values: `[0,1,2,...,length-1]`
*
* @deprecated use a simple Arrays
* @deprecated use a simple Arrays. NOTE: not used in grafana core
*/
export class IndexVector extends FunctionalVector<number> {
constructor(private len: number) {
export class IndexVector extends Array<number> {
constructor(len: number) {
super();
}
get length() {
return this.len;
}
get(index: number): number {
return index;
const arr = new Array(len);
for (let i = 0; i < len; i++) {
arr[i] = i;
}
return arr as IndexVector;
}
/**
* Returns a field representing the range [0 ... length-1]
*
* @deprecated
*/
static newField(len: number): Field<number> {
return {
@@ -1,33 +0,0 @@
import { Vector } from '../types';
import { FunctionalVector } from './FunctionalVector';
import { vectorToArray } from './vectorToArray';
/**
* RowVector makes the row values look like a vector
* @internal
* @deprecated use a simple Arrays
*/
export class RowVector extends FunctionalVector<number> {
constructor(private columns: Vector[]) {
super();
}
rowIndex = 0;
get length(): number {
return this.columns.length;
}
get(index: number): number {
return this.columns[index].get(this.rowIndex);
}
toArray(): number[] {
return vectorToArray(this);
}
toJSON(): number[] {
return vectorToArray(this);
}
}
@@ -0,0 +1,13 @@
import { ArrayVector } from './ArrayVector';
import { SortedVector } from './SortedVector';
describe('SortedVector', () => {
it('Should support sorting', () => {
const values = new ArrayVector([1, 5, 2, 4]);
const sorted = new SortedVector(values, [0, 2, 3, 1]);
expect(sorted.toArray()).toEqual([1, 2, 4, 5]);
// The proxy should still be an instance of SortedVector (used in timeseries)
expect(sorted instanceof SortedVector).toBeTruthy();
});
});
@@ -1,4 +1,4 @@
import { Vector } from '../types/vector';
import { makeArrayIndexableVector, Vector } from '../types/vector';
import { FunctionalVector } from './FunctionalVector';
import { vectorToArray } from './vectorToArray';
@@ -11,6 +11,7 @@ import { vectorToArray } from './vectorToArray';
export class SortedVector<T = any> extends FunctionalVector<T> {
constructor(private source: Vector<T>, private order: number[]) {
super();
return makeArrayIndexableVector(this);
}
get length(): number {