Skip to content

Commit

Permalink
Add automatically recalculate the size of the axis for the bandwidth.
Browse files Browse the repository at this point in the history
Change filter for variable of the cdn-resource.
  • Loading branch information
Denis committed Jul 20, 2021
1 parent 7c3cbdf commit ae6a025
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 43 deletions.
8 changes: 3 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Changelog

## 1.0.4
## 1.0.5

- Support for groupings and filtering data by vhosts, regions, countries, datacenters ids
- Support for permanent token authentication
- Add text formatting for legend key values
- Add tests
- Support deleted cdn resource
- Support automatic chosen scale value for the bandwidth metric
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gcore-cdn-stats",
"version": "1.0.4",
"version": "1.0.5",
"description": "",
"scripts": {
"build": "grafana-toolkit plugin:build",
Expand Down
7 changes: 7 additions & 0 deletions src/bandwidth/bandwidth-description.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export enum BandwidthEnum {
Bits = "bit/s",
KBits = "kbit/s",
MBits = "Mbit/s",
GBits = "Gbit/s",
TBits = "Tbit/s",
}
17 changes: 17 additions & 0 deletions src/bandwidth/bandwidth.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* eslint-disable no-magic-numbers */
import { preferredBandwidth } from "./bandwidth";
import { BandwidthEnum } from "./bandwidth-description";

describe("function bandwidth", () => {
it("Should return 8 bit/s", () => {
expect(preferredBandwidth(1)).toEqual(BandwidthEnum.Bits);
});

it("Should return 0 bit/s", () => {
expect(preferredBandwidth(0)).toEqual(BandwidthEnum.Bits);
});

it("Should return 4 Gbit/s", () => {
expect(preferredBandwidth(500000000)).toEqual(BandwidthEnum.GBits);
});
});
43 changes: 43 additions & 0 deletions src/bandwidth/bandwidth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* eslint-disable no-magic-numbers */
import { BandwidthEnum } from "./bandwidth-description";

export const preferredBandwidth = (
bytes: number,
periodInSeconds = 1
): BandwidthEnum => {
const bits = (bytes * 8) / periodInSeconds;

if (bits > 1000000000000) {
return BandwidthEnum.TBits;
} else if (bits > 1000000000) {
return BandwidthEnum.GBits;
} else if (bits > 1000000) {
return BandwidthEnum.MBits;
} else if (bits > 1000) {
return BandwidthEnum.KBits;
} else {
return BandwidthEnum.Bits;
}
};

export const normalizedBandwidth = (
bytes: number,
periodInSeconds: number,
unit: BandwidthEnum,
precision = 2
): number => {
const bits = (bytes * 8) / periodInSeconds;

switch (unit) {
case BandwidthEnum.Bits:
return Number(bits.toFixed(precision));
case BandwidthEnum.KBits:
return Number((bits / 1000).toFixed(precision));
case BandwidthEnum.MBits:
return Number((bits / 1000000).toFixed(precision));
case BandwidthEnum.GBits:
return Number((bits / 1000000000).toFixed(precision));
case BandwidthEnum.TBits:
return Number((bits / 1000000000000).toFixed(precision));
}
};
2 changes: 2 additions & 0 deletions src/bandwidth/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./bandwidth-description";
export * from "./bandwidth";
17 changes: 8 additions & 9 deletions src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ import {
GCVariableQuery,
} from "./types";
import {
createGetter,
createGetterSample,
getLabelByMetric,
getOriginalMetric,
getUnitByMetric,
} from "metric";
import {
createLabelInfo,
Expand All @@ -37,11 +36,11 @@ import {
takeNumbers,
takeStrings,
} from "./utils";
import { createTransform } from "./transform";
import { defaultQuery } from "./defaults";
import { MetricFindValue } from "@grafana/data/types/datasource";
import { regions } from "./regions";
import { countries } from "countries";
import { getUnit } from "./unit";

export class DataSource extends DataSourceApi<GCQuery, GCDataSourceOptions> {
url?: string;
Expand Down Expand Up @@ -77,7 +76,7 @@ export class DataSource extends DataSourceApi<GCQuery, GCDataSourceOptions> {
url: `${this.url}/resources`,
responseType: "json",
showErrorAlert: true,
params: { fields: "id,cname,client", status: "active" },
params: { fields: "id,cname,client", deleted: true },
});

switch (selector) {
Expand Down Expand Up @@ -112,13 +111,12 @@ export class DataSource extends DataSourceApi<GCQuery, GCDataSourceOptions> {

const fields: Field[] = [];
const metric = query.metric.value!;
const getter = createGetter(metric);
const unit = getUnitByMetric(metric);
const getter = createGetterSample(metric);
const label = getLabelByMetric(metric);
const transform = createTransform(options, query);
const firstPoints = getter(data[0].metrics)!;
const sample = getter(data[0].metrics);
const [unit, transform] = getUnit(query, data);

fields.push(getTimeField(firstPoints));
fields.push(getTimeField(sample));

for (const row of data) {
const rawLabels: Labels = {
Expand All @@ -136,6 +134,7 @@ export class DataSource extends DataSourceApi<GCQuery, GCDataSourceOptions> {
unit,
labels,
transform,
decimals: 2,
data: metricsData,
displayNameFromDS: name,
});
Expand Down
10 changes: 8 additions & 2 deletions src/granularity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ const config: Record<GCGranularity, GCGranularityConfig> = {
label: "15m",
seconds: 15 * TimeInSeconds.MINUTE,
},
[GCGranularity.OneHour]: { label: "1h", seconds: TimeInSeconds.HOUR },
[GCGranularity.OneDay]: { label: "1d", seconds: TimeInSeconds.DAY },
[GCGranularity.OneHour]: {
label: "1h",
seconds: TimeInSeconds.HOUR,
},
[GCGranularity.OneDay]: {
label: "1d",
seconds: TimeInSeconds.DAY,
},
};

export const createOptions = () =>
Expand Down
23 changes: 20 additions & 3 deletions src/metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export interface GCReportsConfig {
unit: GCUnit;
}

export type GCGetterFn<T> = (
data: Partial<Record<GCServerMetric, GCPoint[]>>
) => T;

const config: Record<GCMetric, GCReportsConfig> = {
[GCClientMetric.Bandwidth]: {
originalMetric: GCServerMetric.TotalBytes,
Expand Down Expand Up @@ -94,9 +98,22 @@ export const getLabelByMetric = (metric: GCMetric): string =>
config[metric].label;
export const getUnitByMetric = (metric: GCMetric): GCUnit =>
config[metric].unit;
export const createGetter = (metric: GCMetric) => (
export const createGetterSample = (metric: GCMetric): GCGetterFn<GCPoint[]> => (
data: Partial<Record<GCServerMetric, GCPoint[]>>
): GCPoint[] => {
const originalMetric = getOriginalMetric(metric);
if (data[originalMetric]) {
return data[originalMetric] || [];
}
return [];
};
export const createGetterYValues = (metric: GCMetric): GCGetterFn<number[]> => (
data: Partial<Record<GCServerMetric, GCPoint[]>>
) => {
): number[] => {
const originalMetric = getOriginalMetric(metric);
return data[originalMetric];
if (data[originalMetric]) {
const points = data[originalMetric] || [];
return points.map((p) => p[1]);
}
return [];
};
21 changes: 0 additions & 21 deletions src/transform.ts

This file was deleted.

28 changes: 28 additions & 0 deletions src/unit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { GCClientMetric, GCQuery, GCResponseStats } from "./types";
import { preferredBandwidth, normalizedBandwidth } from "./bandwidth";
import { getSecondsByGranularity } from "./granularity";
import { createGetterYValues, getUnitByMetric } from "./metric";

type Transform = (value: number) => number;
const noopTransform = (v: number): number => v;

export const getUnit = (
query: GCQuery,
data: GCResponseStats[]
): [string, Transform] => {
const metric = query.metric.value!;
const granulation = query.granularity.value!;
const getter = createGetterYValues(metric);

switch (metric) {
case GCClientMetric.Bandwidth:
const times = data.map((p) => getter(p.metrics)).flat();
// @ts-ignore
const maxValue = Math.max(...times);
const period = getSecondsByGranularity(granulation);
const unit = preferredBandwidth(maxValue, period);
return [unit, (value) => normalizedBandwidth(value, period, unit)];
default:
return [getUnitByMetric(metric), noopTransform];
}
};
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
toDataFrame,
} from "@grafana/data";
import { getTemplateSrv } from "@grafana/runtime";
import { GCPoint, GCQuery, GCUnit } from "./types";
import { GCPoint, GCQuery } from "./types";
import { ScopedVars } from "@grafana/data/types/ScopedVars";
import { MetricFindValue } from "@grafana/data/types/datasource";
import { TimeInSeconds } from "./times";
Expand Down Expand Up @@ -72,7 +72,7 @@ type ValueFieldOptions = {
valueName?: string;
parseValue?: boolean;
labels?: Labels;
unit?: GCUnit;
unit?: string;
decimals?: number;
displayNameFromDS?: string;
transform?: (value: number) => number;
Expand Down

0 comments on commit ae6a025

Please sign in to comment.