From 9c17482659ce42d638f3c88583d13cb769aabca6 Mon Sep 17 00:00:00 2001 From: Joachim Schirrmacher Date: Sun, 5 May 2024 02:41:43 +0200 Subject: [PATCH] feat: use logger from useful-typescript-functions --- gateway/Config.ts | 4 ++-- gateway/MQTTClient.ts | 4 ++-- gateway/MetricsGenerator.ts | 15 +++++++-------- gateway/index.ts | 31 +++++-------------------------- gateway/types.ts | 6 +++--- package-lock.json | 14 +++++++------- package.json | 2 +- 7 files changed, 27 insertions(+), 49 deletions(-) diff --git a/gateway/Config.ts b/gateway/Config.ts index ce48c7b..ee818fb 100644 --- a/gateway/Config.ts +++ b/gateway/Config.ts @@ -1,9 +1,9 @@ import { parse } from "yaml" import { resolve } from "path" import { readFile } from "fs/promises" -import { MetricsConfiguration } from "./types" +import { Configuration } from "./types" const defaultConfigFile = resolve(__dirname, "..", "config.yaml") export default async function readConfig(configFile = defaultConfigFile) { - return parse((await readFile(configFile)).toString()) as MetricsConfiguration + return parse((await readFile(configFile)).toString()) as Configuration } diff --git a/gateway/MQTTClient.ts b/gateway/MQTTClient.ts index 61ca063..76f86bf 100644 --- a/gateway/MQTTClient.ts +++ b/gateway/MQTTClient.ts @@ -23,7 +23,7 @@ const dateTimeRegEx = new RegExp(regExStr) const invalidDateTimeRegEx = new RegExp('(? metric.topic)) + const topics = unique(config.map((metric) => metric.topic)) client.on("connect", () => { logger.info(`Connection to mqtt://${brokerName}:${port} established`) diff --git a/gateway/MetricsGenerator.ts b/gateway/MetricsGenerator.ts index 60ed9d3..6af44ab 100644 --- a/gateway/MetricsGenerator.ts +++ b/gateway/MetricsGenerator.ts @@ -1,6 +1,5 @@ -import type { BaseType, LabelList, SingleConfiguration } from "./types" +import type { BaseType, LabelList, MetricsConfiguration } from "./types" import { metrics } from "./MetricsModel" -import { MetricsConfiguration } from "./types" function object2label(obj: LabelList) { return Object.entries(obj) @@ -9,7 +8,7 @@ function object2label(obj: LabelList) { } function createMetric( - config: SingleConfiguration, + config: MetricsConfiguration, value: string | number, labels: Record = {} ) { @@ -22,7 +21,7 @@ function createMetric( ) } -function getLabels(config: SingleConfiguration) { +function getLabels(config: MetricsConfiguration) { return Object.fromEntries( Object.entries(config.labels || {}).map(([key, path]) => [ key, @@ -31,17 +30,17 @@ function getLabels(config: SingleConfiguration) { ) as LabelList } -function getValue(config: SingleConfiguration) { +function getValue(config: MetricsConfiguration) { return metrics[config.topic][config.path as string] as BaseType } -export default function getMetrics(config: MetricsConfiguration) { - config.metrics +export default function getMetrics(config: MetricsConfiguration[]) { + config .map((config) => config.topic) .filter((topic) => !metrics[topic]) .forEach((topic) => console.warn(`path '${topic}' not found in metrics`)) - return config.metrics + return config .filter((config) => metrics[config.topic]) .map((config) => createMetric(config, getValue(config), getLabels(config))) .join("") diff --git a/gateway/index.ts b/gateway/index.ts index 55a55a0..7e99bcd 100644 --- a/gateway/index.ts +++ b/gateway/index.ts @@ -4,37 +4,16 @@ import * as actualMqtt from "mqtt" import readConfig from "./Config" import getMetrics from "./MetricsGenerator" import setupMQTTClient from "./MQTTClient" -import { - setupServer, - defineRouter, - LogLevel, -} from "useful-typescript-functions" - -const logPrintLevel = { - error: ["error"], - warn: ["error", "warn"], - info: ["error", "warn", "info"], - debug: ["error", "warn", "info", "debug"], -} - -const printLog = (level: LogLevel) => (msg: string | object) => - logPrintLevel[logger.logLevel].includes(level) && console.log(msg) - -const logger = { - logLevel: "info" as LogLevel, - debug: printLog("debug"), - info: printLog("info"), - warn: printLog("warn"), - error: printLog("error"), -} +import { setupServer, defineRouter, Logger } from "useful-typescript-functions" async function setup() { const config = await readConfig() - logger.logLevel = config.logLevel || "info" - setupMQTTClient(config, actualMqtt, logger) + const logger = Logger() + logger.setLogLevel(config.logLevel || "info") + setupMQTTClient(config.metrics, actualMqtt, logger) setupServer({ routers: [ - defineRouter("/metrics", "metrics").get("/", () => getMetrics(config)), + defineRouter("/metrics", "metrics").get("/", () => getMetrics(config.metrics)), ], }) } diff --git a/gateway/types.ts b/gateway/types.ts index c5dd241..5d1c9c1 100644 --- a/gateway/types.ts +++ b/gateway/types.ts @@ -7,7 +7,7 @@ export type BaseType = string | number export type LabelList = Record -export type SingleConfiguration = { +export type MetricsConfiguration = { name: string description: string type: MetricType @@ -16,7 +16,7 @@ export type SingleConfiguration = { labels: LabelList } -export type MetricsConfiguration = { +export type Configuration = { logLevel?: LogLevel - metrics: SingleConfiguration[] + metrics: MetricsConfiguration[] } diff --git a/package-lock.json b/package-lock.json index 8716314..2a226c2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,7 +15,7 @@ "mqtt": "^5.5.5", "ts-node": "^10.9.2", "typescript": "^5.4.5", - "useful-typescript-functions": "^4.1.4", + "useful-typescript-functions": "4.2.0", "yaml": "^2.4.2" }, "devDependencies": { @@ -4640,9 +4640,9 @@ } }, "node_modules/useful-typescript-functions": { - "version": "4.1.4", - "resolved": "https://registry.npmjs.org/useful-typescript-functions/-/useful-typescript-functions-4.1.4.tgz", - "integrity": "sha512-pETsFLOhKKa/J7gPMvd2vt9fUBPMaQk/IeJkbD8TUe2Bnlg2bHm3nhzWYIrs5my8c2ZxohPk0Em9G99zhjQelw==", + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/useful-typescript-functions/-/useful-typescript-functions-4.2.0.tgz", + "integrity": "sha512-AE2PsLgqESyBKaSDKqwSffc94Bwhy3p5OyeTbsdzmxXPUdDSw9Z5tHDD18od6WSrFInauuzDuCxQho6xa4B6gg==", "peerDependencies": { "express": "4.x", "express-fileupload": "1.x", @@ -8164,9 +8164,9 @@ "integrity": "sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ==" }, "useful-typescript-functions": { - "version": "4.1.4", - "resolved": "https://registry.npmjs.org/useful-typescript-functions/-/useful-typescript-functions-4.1.4.tgz", - "integrity": "sha512-pETsFLOhKKa/J7gPMvd2vt9fUBPMaQk/IeJkbD8TUe2Bnlg2bHm3nhzWYIrs5my8c2ZxohPk0Em9G99zhjQelw==", + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/useful-typescript-functions/-/useful-typescript-functions-4.2.0.tgz", + "integrity": "sha512-AE2PsLgqESyBKaSDKqwSffc94Bwhy3p5OyeTbsdzmxXPUdDSw9Z5tHDD18od6WSrFInauuzDuCxQho6xa4B6gg==", "requires": {} }, "util-deprecate": { diff --git a/package.json b/package.json index 68b5a92..6c79c1a 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "mqtt": "^5.5.5", "ts-node": "^10.9.2", "typescript": "^5.4.5", - "useful-typescript-functions": "^4.1.4", + "useful-typescript-functions": "4.2.0", "yaml": "^2.4.2" }, "devDependencies": {