Skip to content

Commit

Permalink
Add support for values (#28)
Browse files Browse the repository at this point in the history
* feat: add support for values
If calcs is missing it should use the values instead

* chore: update dependencies

* fix: fix failing tests
  • Loading branch information
ZuperZee authored Sep 16, 2021
1 parent 37b2d47 commit 72b9676
Show file tree
Hide file tree
Showing 8 changed files with 3,435 additions and 7,024 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ module.exports = {
},
coverageDirectory: "coverage",
collectCoverageFrom: ["src/**/*.{ts,tsx,js,jsx}", "!src/**/*.d.ts"],
testEnvironment: "jsdom",
};
10,327 changes: 3,322 additions & 7,005 deletions package-lock.json

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@
"watch": "rollup -cw"
},
"devDependencies": {
"@grafana/data": "~7.3.7",
"@rollup/plugin-commonjs": "^17.1.0",
"@rollup/plugin-node-resolve": "^11.2.0",
"@types/jest": "^26.0.20",
"@typescript-eslint/eslint-plugin": "^4.16.1",
"@typescript-eslint/parser": "^4.16.1",
"eslint": "^7.21.0",
"eslint-plugin-import": "^2.22.1",
"jest": "^26.6.3",
"jest-cli": "^26.6.3",
"prettier": "^2.2.1",
"@grafana/data": "~8.1.4",
"@rollup/plugin-commonjs": "^20.0.0",
"@rollup/plugin-node-resolve": "^13.0.4",
"@types/jest": "^27.0.1",
"@typescript-eslint/eslint-plugin": "^4.31.1",
"@typescript-eslint/parser": "^4.31.1",
"eslint": "^7.32.0",
"eslint-plugin-import": "^2.24.2",
"jest": "^27.2.0",
"jest-cli": "^27.2.0",
"prettier": "^2.4.1",
"rimraf": "^3.0.2",
"rollup": "^2.40.0",
"rollup": "^2.56.3",
"rollup-plugin-typescript2": "^0.30.0",
"ts-jest": "^26.5.3",
"typescript": "^4.2.3"
"ts-jest": "^27.0.5",
"typescript": "^4.4.3"
},
"pre-commit": [
"prepublishOnly"
Expand Down
2 changes: 1 addition & 1 deletion src/utils/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function field({
}: {
name: string;
type: FieldType;
calcs: FieldCalcs;
calcs?: FieldCalcs;
labels?: Labels;
values?: number[];
}): Field {
Expand Down
46 changes: 43 additions & 3 deletions src/utils/metricData/getMetricDataFromName.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ describe("getMetricDataFromName", () => {
delete window.data;
});

it("get correct time", () => {
it("gets correct time", () => {
expect(getMetricDataFromName("series-1")).toStrictEqual({
calcs: {
[ReducerID.last]: 1000,
Expand Down Expand Up @@ -414,7 +414,7 @@ describe("getMetricDataFromName", () => {
delete window.data;
});

it("get correct time", () => {
it("gets correct time", () => {
expect(getMetricDataFromName("series-1")).toStrictEqual({
calcs: {
[ReducerID.last]: 1000,
Expand Down Expand Up @@ -465,7 +465,7 @@ describe("getMetricDataFromName", () => {
delete window.data;
});

it("get correct time", () => {
it("gets correct time", () => {
expect(getMetricDataFromName("series-1")).toStrictEqual({
calcs: {
[ReducerID.last]: 1000,
Expand All @@ -481,3 +481,43 @@ describe("getMetricDataFromName", () => {
});
});
});

describe("missing calcs", () => {
beforeEach(() => {
window.data = {
state: LoadingState.Done,
series: [
{
fields: [
TIME_FIELD,
field({
name: "series-1",
type: FieldType.number,
values: [1000, 500, 300],
}),
],
length: 1,
},
],
timeRange: minimalTimeRange,
};
});

afterEach(() => {
delete window.data;
});

it("gets correct value", () => {
expect(getMetricDataFromName("series-1")).toStrictEqual({
calcs: {
[ReducerID.last]: 300,
[ReducerID.first]: 1000,
},
time: {
[ReducerID.first]: TIME_VALUES[0],
[ReducerID.last]: TIME_VALUES[TIME_VALUES.length - 1],
},
hasData: true,
});
});
});
12 changes: 12 additions & 0 deletions src/utils/metricData/getMetricDataFromName.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Field } from "@grafana/data";

import { getDataFieldsFromName } from "../getDataFieldsFromName";
import { getValue } from "../metricValue";

function getTime(timeField?: Field) {
const timeValues = timeField?.values;
Expand All @@ -22,6 +23,17 @@ function getCalcs({
reducerIDs?: string[];
}) {
const calcs = valueField?.state?.calcs ?? {};

if (calcs.last === undefined) {
const value = getValue(valueField, "last");
if (value) calcs.last = value;
}

if (calcs.first === undefined) {
const value = getValue(valueField, "first");
if (value) calcs.first = value;
}

const enquiredCalcs =
reducerIDs?.reduce(
(obj: { [key: string]: unknown }, key) => ((obj[key] = calcs[key]), obj),
Expand Down
22 changes: 22 additions & 0 deletions src/utils/metricValue/getMetricValueFromName.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ window.data = {
],
length: 1,
},
{
fields: [
TIME_FIELD,
field({
name: "random-value-field",
type: FieldType.number,
values: [1000, 500, 300],
}),
],
length: 1,
},
],
timeRange: {
from: dateTime(0),
Expand Down Expand Up @@ -95,4 +106,15 @@ describe("getMetricValueFromName", () => {
expect(getMetricValueFromName("label-1")).toEqual(300);
});
});

describe("missing calcs", () => {
it("gets correct value from label name", () => {
expect(getMetricValueFromName("random-value-field")).toEqual(300);
expect(
getMetricValueFromName("random-value-field", {
reducerID: ReducerID.first,
})
).toEqual(1000);
});
});
});
21 changes: 20 additions & 1 deletion src/utils/metricValue/getMetricValueFromName.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
import { Field, Vector } from "@grafana/data";

import { ReducerID } from "../field";
import { getDataFieldsFromName } from "../getDataFieldsFromName";

export function getValue(
valueField: Field<unknown, Vector<unknown>> | undefined,
reducerID: string
): unknown | null {
const length = valueField?.values.length;
if (!length) return null;

if (reducerID === "first" || reducerID === "last") {
const index = reducerID === "first" ? 0 : length - 1;
const value = valueField?.values.get(index);
return value;
}
}

export interface MetricValueFromNameOptions {
/**
* Return value when no data is found.
Expand Down Expand Up @@ -42,5 +58,8 @@ export function getMetricValueFromName(
}: MetricValueFromNameOptions = {}
): unknown {
const { valueField } = getDataFieldsFromName(metricName, { getTime: false });
return valueField?.state?.calcs?.[reducerID] ?? noDataValue;
const value =
valueField?.state?.calcs?.[reducerID] ?? getValue(valueField, reducerID);

return value ?? noDataValue;
}

0 comments on commit 72b9676

Please sign in to comment.