Skip to content

Commit

Permalink
stable build
Browse files Browse the repository at this point in the history
  • Loading branch information
zoe-codez committed Apr 1, 2024
1 parent 35809da commit 151e970
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 21 deletions.
1 change: 1 addition & 0 deletions cspell.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ words:
- psevdo
- mplayer
- azbuka
- sigfig
- execa
- milli
- numlen
Expand Down
12 changes: 5 additions & 7 deletions src/app/extensions/fastify.extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import { collectDefaultMetrics } from "prom-client";

import {
HTTP_REQUEST_ERROR,
msOffset,
REQUEST_DURATION_HISTOGRAM,
} from "../helpers/metrics";

const LATE_CONFIG = -1;
const SECONDS_TO_MILLISECONDS = 1e3;
const NANOSECONDS_TO_MILLISECONDS = 1e-6;

export function FastifyPlugins({ logger, lifecycle, fastify }: TServiceParams) {
lifecycle.onPostConfig(() => {
Expand Down Expand Up @@ -57,17 +56,14 @@ export function FastifyPlugins({ logger, lifecycle, fastify }: TServiceParams) {
(request: FastifyRequest, _: FastifyReply, done) => {
const start = requests.get(request);
requests.delete(request);
const [seconds, nano] = hrtime(start);
const durationInMilliseconds =
seconds * SECONDS_TO_MILLISECONDS +
nano * NANOSECONDS_TO_MILLISECONDS;
const durationInMilliseconds = msOffset(start);

const path = request.routeOptions.url || request.raw.url;
logger.debug(
{ name: request.method },
`[%s] - {%s}ms`,
path,
durationInMilliseconds,
Math.floor(durationInMilliseconds * SIGFIG) / SIGFIG,
);
REQUEST_DURATION_HISTOGRAM.observe(
{ method: request.method, route: path },
Expand All @@ -82,3 +78,5 @@ export function FastifyPlugins({ logger, lifecycle, fastify }: TServiceParams) {
);
}, LATE_CONFIG);
}

const SIGFIG = 100;
10 changes: 7 additions & 3 deletions src/app/extensions/pixel.extension.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { TServiceParams } from "@digital-alchemy/core";
import { hrtime } from "process";
import { LedMatrixInstance } from "rpi-led-matrix";

import { SetPixelGrid } from "../..";
import { MATRIX_RENDER } from "../helpers/metrics";
import { msOffset, RENDER_DURATION_HISTOGRAM } from "../helpers/metrics";
const OFF = { b: 0, g: 0, r: 0 };

export function Pixel({ pi_matrix_app, pi_matrix }: TServiceParams) {
/**
* * `col` / `row` come in as a plain list of x/y coords
Expand All @@ -23,6 +23,7 @@ export function Pixel({ pi_matrix_app, pi_matrix }: TServiceParams) {
},
setGrid({ grid, palette, clear }: SetPixelGrid): void {
pi_matrix_app.render.renderMode = "pixel";
const start = hrtime();
if (clear !== false) {
pi_matrix_app.instance.instance.clear();
}
Expand All @@ -32,7 +33,10 @@ export function Pixel({ pi_matrix_app, pi_matrix }: TServiceParams) {
),
);
pi_matrix_app.instance.instance.sync();
MATRIX_RENDER.labels({ type: "pixel" }).inc();
const durationInMilliseconds = msOffset(start);
RENDER_DURATION_HISTOGRAM.labels({ type: "pixel" }).observe(
durationInMilliseconds,
);
},
};
}
22 changes: 20 additions & 2 deletions src/app/extensions/widget.extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { eachSeries, EMPTY, NONE, TServiceParams } from "@digital-alchemy/core";
import dayjs from "dayjs";
import { hrtime } from "process";
import {
HorizontalAlignment,
LayoutUtils,
Expand All @@ -20,7 +21,12 @@ import {
TextWidgetDTO,
UNLOAD_WIDGETS,
} from "../..";
import { MATRIX_RENDER, MATRIX_RENDER_WIDGET_COUNT } from "../helpers/metrics";
import {
MATRIX_RENDER_WIDGET_COUNT,
msOffset,
RENDER_DURATION_HISTOGRAM,
WIDGET_RENDER_PHASE,
} from "../helpers";

export function Widget({ event, pi_matrix_app, logger }: TServiceParams) {
function renderCircle({
Expand Down Expand Up @@ -176,13 +182,25 @@ export function Widget({ event, pi_matrix_app, logger }: TServiceParams) {
async render(): Promise<void> {
const list = [prerender(), widget.widgets, postrender()].flat();
try {
const start = hrtime();
pi_matrix_app.instance.instance.clear();
WIDGET_RENDER_PHASE.labels({ phase: "clear" }).observe(msOffset(start));
const assemble = hrtime();
await eachSeries(
list,
async widget => await pi_matrix_app.widget.renderWidget(widget),
);
MATRIX_RENDER.labels({ type: "widget" }).inc();
WIDGET_RENDER_PHASE.labels({ phase: "write" }).observe(
msOffset(assemble),
);
const syncStart = hrtime();
pi_matrix_app.instance.instance.sync();
WIDGET_RENDER_PHASE.labels({ phase: "sync" }).observe(
msOffset(syncStart),
);
RENDER_DURATION_HISTOGRAM.labels({ type: "widget" }).observe(
msOffset(start),
);
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
Expand Down
1 change: 1 addition & 0 deletions src/app/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./metrics";
36 changes: 27 additions & 9 deletions src/app/helpers/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
import { hrtime } from "process";
import { Counter, Gauge, Histogram } from "prom-client";

const SECONDS_TO_MILLISECONDS = 1e3;
const NANOSECONDS_TO_MILLISECONDS = 1e-6;

/**
* Counter for tracking rejected authentication requests.
* Widget counts the matrix is attempting to render
*/
export const MATRIX_RENDER = new Counter({
help: "Number of times the .sync method was used",
export const MATRIX_RENDER_WIDGET_COUNT = new Gauge({
help: "Widget counts the matrix is attempting to render",
labelNames: ["type"] as const,
name: "pi_matrix_render_sync",
name: "pi_matrix_widget_count",
});

/**
* Counter for tracking rejected authentication requests.
* Duration of each render call in milliseconds
*/
export const MATRIX_RENDER_WIDGET_COUNT = new Gauge({
help: "Widget counts the matrix is attempting to render",
export const RENDER_DURATION_HISTOGRAM = new Histogram({
help: "Duration of each render call in milliseconds",
labelNames: ["type"] as const,
name: "pi_matrix_widget_count",
name: "pi_matrix_render_duration_milliseconds",
});

/**
* Duration of each render call in milliseconds
*/
export const WIDGET_RENDER_PHASE = new Histogram({
help: "Duration of each phase of widget rendering in milliseconds",
labelNames: ["phase"] as const,
name: "pi_matrix_widget_render_phase",
});

/**
Expand All @@ -33,7 +47,6 @@ export const REQUEST_DURATION_HISTOGRAM = new Histogram({
labelNames: ["method", "route"] as const,
name: "http_request_duration_milliseconds",
});

/**
* Quantity of errors by route
*/
Expand All @@ -42,3 +55,8 @@ export const HTTP_REQUEST_ERROR = new Counter({
labelNames: ["method", "route"] as const,
name: "http_request_error",
});

export const msOffset = (start: ReturnType<typeof hrtime>) => {
const [seconds, nano] = hrtime(start);
return seconds * SECONDS_TO_MILLISECONDS + nano * NANOSECONDS_TO_MILLISECONDS;
};
1 change: 1 addition & 0 deletions src/app/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./controllers";
export * from "./helpers";

0 comments on commit 151e970

Please sign in to comment.