Skip to content

Commit

Permalink
fix: return back averages metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
madlabman committed Jan 16, 2023
1 parent f4b1121 commit a0b1141
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 31 deletions.
119 changes: 96 additions & 23 deletions grafana/dashboards/performance.json
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@
"type": "linear"
},
"showPoints": "never",
"spanNulls": false,
"spanNulls": true,
"stacking": {
"group": "A",
"mode": "none"
Expand Down Expand Up @@ -1351,6 +1351,13 @@
{
"id": "custom.fillOpacity",
"value": 15
},
{
"id": "color",
"value": {
"fixedColor": "red",
"mode": "fixed"
}
}
]
},
Expand Down Expand Up @@ -1393,30 +1400,34 @@
},
{
"matcher": {
"id": "byFrameRefID",
"options": "A"
"id": "byName",
"options": "TOP"
},
"properties": [
{
"id": "mappings",
"value": [
{
"options": {
"54": {
"index": 0,
"text": "Shard Labs"
}
},
"type": "value"
}
]
"id": "displayName",
"value": "AVG over top 10"
},
{
"id": "custom.lineStyle",
"value": {
"dash": [
10,
10
],
"fill": "dash"
}
},
{
"id": "custom.lineWidth",
"value": 2
}
]
}
]
},
"gridPos": {
"h": 8,
"h": 12,
"w": 24,
"x": 0,
"y": 16
Expand Down Expand Up @@ -1448,8 +1459,70 @@
"group": [],
"metricColumn": "label",
"rawQuery": true,
"rawSql": "SELECT\n \"blockTimestamp\" AS \"time\",\n labels ->> 'moniker' AS metric,\n value\nFROM metrics\nWHERE\n $__unixEpochFilter(\"blockTimestamp\") AND\n name = 'vals_perf_idx'\nORDER BY 1,2;",
"refId": "A",
"rawSql": "SELECT\n $__unixEpochGroup(\"blockTimestamp\", $__interval) as \"time\",\n labels ->> 'moniker' AS metric,\n value\nFROM metrics\nWHERE\n $__unixEpochFilter(\"blockTimestamp\") AND\n name = 'vals_perf_idx'\nORDER BY 1,2;",
"refId": "Tracked validators perf rates",
"select": [
[
{
"params": [
"value"
],
"type": "column"
}
]
],
"timeColumn": "time",
"where": [
{
"name": "$__timeFilter",
"params": [],
"type": "macro"
}
]
},
{
"datasource": {
"type": "postgres",
"uid": "LOn97bynk"
},
"format": "time_series",
"group": [],
"hide": false,
"metricColumn": "label",
"rawQuery": true,
"rawSql": "SELECT\n $__unixEpochGroup(\"blockTimestamp\", $__interval) as \"time\",\n 'PB' AS metric,\n value\nFROM metrics\nWHERE\n $__unixEpochFilter(\"blockTimestamp\") AND\n name = 'vals_perf_benchmark'\nORDER BY 1;",
"refId": "Performance benchmark",
"select": [
[
{
"params": [
"value"
],
"type": "column"
}
]
],
"timeColumn": "time",
"where": [
{
"name": "$__timeFilter",
"params": [],
"type": "macro"
}
]
},
{
"datasource": {
"type": "postgres",
"uid": "LOn97bynk"
},
"format": "time_series",
"group": [],
"hide": false,
"metricColumn": "label",
"rawQuery": true,
"rawSql": "SELECT\n $__unixEpochGroup(\"blockTimestamp\", $__interval) as \"time\",\n 'AVG' AS metric,\n value\nFROM metrics\nWHERE\n $__unixEpochFilter(\"blockTimestamp\") AND\n name = 'vals_perf_avg_rate'\nORDER BY 1;",
"refId": "Average percent",
"select": [
[
{
Expand Down Expand Up @@ -1479,8 +1552,8 @@
"hide": false,
"metricColumn": "label",
"rawQuery": true,
"rawSql": "SELECT\n \"blockTimestamp\" AS \"time\",\n 'PB' AS metric,\n value\nFROM metrics\nWHERE\n $__unixEpochFilter(\"blockTimestamp\") AND\n name = 'vals_perf_benchmark'\nORDER BY 1;",
"refId": "C",
"rawSql": "SELECT\n $__unixEpochGroup(\"blockTimestamp\", $__interval) as \"time\",\n 'TOP' AS metric,\n value\nFROM metrics\nWHERE\n $__unixEpochFilter(\"blockTimestamp\") AND\n name = 'vals_perf_top_rate'\nORDER BY 1;",
"refId": "Average over top 10",
"select": [
[
{
Expand Down Expand Up @@ -1561,7 +1634,7 @@
"h": 8,
"w": 18,
"x": 0,
"y": 24
"y": 28
},
"id": 22,
"maxDataPoints": 30,
Expand Down Expand Up @@ -1676,7 +1749,7 @@
"h": 8,
"w": 6,
"x": 18,
"y": 24
"y": 28
},
"id": 24,
"maxDataPoints": 30,
Expand Down Expand Up @@ -1798,7 +1871,7 @@
"h": 8,
"w": 18,
"x": 0,
"y": 32
"y": 36
},
"id": 23,
"maxDataPoints": 30,
Expand Down
8 changes: 4 additions & 4 deletions src/common/helpers/funcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export function median(numArray: number[]): number {
}
}

export function mean(...nums: number[]): number {
return nums.reduce((a, b) => a + b, 0) / nums.length;
}

function isEven(num: number): boolean {
return num % 2 == 0;
}
Expand All @@ -23,10 +27,6 @@ function toInt(num: number): number {
return num | 0;
}

function mean(...nums: number[]): number {
return nums.reduce((a, b) => a + b, 0) / nums.length;
}

// TODO: type for predicate p
export function takeWhile(iterable: Iterable<any>, p: any): any[] {
const c = [];
Expand Down
2 changes: 2 additions & 0 deletions src/metrics/metrics.consts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const VALIDATORS_PERF_BENCHMARK = 'vals_perf_benchmark';
export const VALIDATORS_PERF_AVG_RATE = 'vals_perf_avg_rate';
export const VALIDATORS_PERF_TOP_RATE = 'vals_perf_top_rate';
export const VALIDATORS_PERF_INDEX = 'vals_perf_idx';
export const VALIDATORS_PERF_CMP = 'vals_perf_cmp';

Expand Down
30 changes: 26 additions & 4 deletions src/metrics/metrics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import { Inject, Injectable, LoggerService } from '@nestjs/common';
import { DataSource } from 'typeorm';

import { CheckpointsService } from 'checkpoints';
import { median } from 'common/helpers';
import { mean, median } from 'common/helpers';
import { Checkpoint, Metric } from 'storage/entities';

import {
MONITORING_PERIOD,
VALIDATORS_PERF_AVG_RATE,
VALIDATORS_PERF_BENCHMARK,
VALIDATORS_PERF_CMP,
VALIDATORS_PERF_INDEX,
VALIDATORS_PERF_TOP_RATE,
} from './metrics.consts';
import {
AggrValsPerfRate,
Expand Down Expand Up @@ -73,11 +75,29 @@ export class MetricsService {

const perfBench = this.computePB(rows, checkpoint.number);

const perfBenchMetric = new Metric() as AggrValsPerfRate;
perfBenchMetric.name = VALIDATORS_PERF_BENCHMARK;
perfBenchMetric.blockTimestamp = checkpoint.blockTimestamp;
perfBenchMetric.labels = {};
perfBenchMetric.value = perfBench;

const avgPerfMetric = new Metric() as AggrValsPerfRate;
avgPerfMetric.name = VALIDATORS_PERF_BENCHMARK;
avgPerfMetric.name = VALIDATORS_PERF_AVG_RATE;
avgPerfMetric.blockTimestamp = checkpoint.blockTimestamp;
avgPerfMetric.labels = {};
avgPerfMetric.value = perfBench;
avgPerfMetric.value = mean(...rows.map((r) => this.perfPercent(r)));

const top10Ids = checkpoint.duties.filter((d) => d.isTop).map((d) => d.vId);

const topPerfMetric = new Metric() as AggrValsPerfRate;
topPerfMetric.name = VALIDATORS_PERF_TOP_RATE;
topPerfMetric.blockTimestamp = checkpoint.blockTimestamp;
topPerfMetric.labels = {};
topPerfMetric.value = mean(
...rows
.filter((r) => top10Ids.includes(r.vId))
.map((r) => this.perfPercent(r)),
);

const cmpPerfIdxs = ratesOfTracked.map((o) => {
const m = new Metric() as CmpValsPerfRate;
Expand All @@ -89,9 +109,11 @@ export class MetricsService {
});

await this.dataSource.manager.save([
perfBenchMetric,
avgPerfMetric,
topPerfMetric,
...ratesOfTracked,
...cmpPerfIdxs,
avgPerfMetric,
]);
}

Expand Down

0 comments on commit a0b1141

Please sign in to comment.