https://github.com/gapitio/grafana-metric
Retrieves metric value from Grafana's data interface.
https://github.com/gapitio/grafana-metric
gapit-htmlgraphics-panel grafana metric retrieves-metric utility
Last synced: about 1 month ago
JSON representation
Retrieves metric value from Grafana's data interface.
- Host: GitHub
- URL: https://github.com/gapitio/grafana-metric
- Owner: gapitio
- Created: 2020-10-27T08:03:57.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2023-06-15T11:39:45.000Z (about 3 years ago)
- Last Synced: 2025-10-06T06:49:27.078Z (9 months ago)
- Topics: gapit-htmlgraphics-panel, grafana, metric, retrieves-metric, utility
- Language: TypeScript
- Homepage:
- Size: 929 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# HTMLGraphics metric
Retrieves metric value from Grafana's [data interface](https://grafana.com/docs/grafana/latest/packages_api/data/paneldata/).
## Usage
The main difference between MetricValue and MetricData is that MetricValue is a single value while MetricData is an object
```ts
type MetricValue = unknown;
interface TimeData {
first?: unknown;
last?: unknown;
}
interface MetricData {
calcs: { [key: string]: unknown };
time: TimeData;
hasData: boolean;
}
```
### getMetricValueFromExpression
Evaluates a metric expression
```ts
import { getMetricValueFromExpression } from "@gapit/grafana-metric";
// random-metric = 100
getMetricValueFromExpression("'random-metric' * 2"); // Returns 200
getMetricValueFromExpression("Math.sqrt('random-metric')"); // Returns 10
```
### getShowcaseMetricValue
Generate a random number
```ts
import { getShowcaseMetricValue } from "@gapit/grafana-metric";
getShowcaseMetricValue(); // Returns a value between 0 and 1000 with 2 decimals
getShowcaseMetricValue({ range: { min: x, max: y }, decimals: z }); // Return a random value between x and y with z decimals.
```
### getMetricValueFromName
Gets a metric value by name/alias
```ts
import { getMetricValueFromName } from "@gapit/grafana-metric";
// metric-name = 100
getMetricValueFromName("metric-name"); // Returns 100
getMetricValueFromName("non-existing-metric"); // Returns null
getMetricValueFromName("non-existing-metric", { noDataValue: "No data" }); // Returns "No data"
```
### getMetricValue
Function provides calculations for grafana queries.
```ts
import { getMetricValue } from "@gapit/grafana-metric";
// metric-name = 100
// From name
getMetricValue("metric-name"); // Returns 100
// No data
getMetricValue("non-existing-metric"); // Returns null
getMetricValue("non-existing-metric", { noDataValue: "No data" }); // Returns "No data"
// Evaluation string
getMetricValue("'metric-name' * 2"); // Returns 200
getMetricValue("Math.sqrt('metric-name')"); // Returns 10
// Showcase
getMetricValue("metric-name", { showcase: true }); // Returns a random value between 0 and 1000.
getMetricValue("metric-name", { showcase: true, range: { min: 0, max: 10 } }); // Returns random value between 1-10.
getMetricValue("metric-name", {
showcase: true,
range: { min: 0, max: 10 },
decimals: 4,
}); // Returns random value between 1-10 with 4 decimals.
```
### getMetricDataFromExpression
Evaluates a metric expression
### getShowcaseMetricData
Generates a random metric data object
### getMetricDataFromName
Gets metric data by name/alias
### getMetricData
Gets metric data