Skip to content

Commit

Permalink
Merge branch 'implement-getmetricvalue'
Browse files Browse the repository at this point in the history
  • Loading branch information
flesa committed Nov 19, 2020
2 parents 26c4dd8 + a53fb81 commit 8ba7c39
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
28 changes: 27 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { LoadingState, PanelData, dateTime } from "@grafana/data";

import { createMinimalSeries, createSeries } from "./__mocks__/create-series";

import { getMetricValueByName } from "./index";
import {
getMetricValue,
getMetricValueByName,
getShowcaseMetricValue,
} from "./index";

declare global {
interface Window {
Expand Down Expand Up @@ -44,3 +48,25 @@ describe("getMetricValueByName", () => {
);
});
});

describe("getMetricValue", () => {
it("retrieves correct value", () => {
expect(getMetricValue("test", true)).toBeGreaterThanOrEqual(0);
expect(getMetricValue("test", true)).toBeLessThanOrEqual(1000);
expect(getMetricValue("test", true, [0, 10], 2)).toBeGreaterThanOrEqual(0);
expect(getMetricValue("test", true, [0, 10], 2)).toBeLessThanOrEqual(10);
});
});

describe("getShowcaseMetricValue", () => {
it("retrieves correct value", () => {
expect(getShowcaseMetricValue()).toBeGreaterThanOrEqual(0);
expect(getShowcaseMetricValue()).toBeLessThanOrEqual(1000);
expect(
getShowcaseMetricValue({ range: [0, 10], decimals: 2 })
).toBeGreaterThanOrEqual(0);
expect(
getShowcaseMetricValue({ range: [0, 10], decimals: 2 })
).toBeLessThanOrEqual(10);
});
});
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ function getShowcaseMetricValue({
decimals?: number;
} = {}): number {
let value = Math.random();
const fixedValue = (value * range[1]).toFixed(decimals);
const fixedValue = (value * (range[1] - range[0]) + range[0]).toFixed(
decimals
);
value = parseFloat(fixedValue);
return value;
}
Expand Down Expand Up @@ -60,7 +62,7 @@ const getMetricValueByName = (
*
* ```ts
* getMetricValue("queryAlias") // Returns null if query is not executed.
* getMetricValue("queryAlias", true); // Returns a random value between 0 and 2000000.
* getMetricValue("queryAlias", true); // Returns a random value between 0 and 1000.
* getMetricValue("queryAlias", true, [1,10]); // Returns random value between 1-10.
* getMetricValue("queryAlias", true, [1,10], true); // Returns random whole value between 1-10.
* ```
Expand Down

0 comments on commit 8ba7c39

Please sign in to comment.