Skip to content

Commit

Permalink
feat: use logger from useful-typescript-functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jschirrmacher committed May 5, 2024
1 parent b0cb8ad commit 9c17482
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 49 deletions.
4 changes: 2 additions & 2 deletions gateway/Config.ts
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions gateway/MQTTClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const dateTimeRegEx = new RegExp(regExStr)
const invalidDateTimeRegEx = new RegExp('(?<!")(' + regExStr + ')(?!")', "g")

export default function (
config: MetricsConfiguration,
config: MetricsConfiguration[],
mqtt: MQTT = actualMqtt,
logger: Logger = console
) {
Expand All @@ -34,7 +34,7 @@ export default function (
const auth = user && pwd ? `${user}:${pwd}@` : ""

const client = mqtt.connect(`mqtt://${auth}${brokerName}:${port}`)
const topics = unique(config.metrics.map((metric) => metric.topic))
const topics = unique(config.map((metric) => metric.topic))

Check failure on line 37 in gateway/MQTTClient.ts

View workflow job for this annotation

GitHub Actions / unit-tests

gateway/MQTTClient.test.ts > MQTTClient > should connect to the configured MQTT Server

TypeError: config.map is not a function ❯ Module.__vite_ssr_exports__.default gateway/MQTTClient.ts:37:32 ❯ gateway/MQTTClient.test.ts:54:5

Check failure on line 37 in gateway/MQTTClient.ts

View workflow job for this annotation

GitHub Actions / unit-tests

gateway/MQTTClient.test.ts > MQTTClient > should subscribe to all configured MQTT topics

TypeError: config.map is not a function ❯ Module.__vite_ssr_exports__.default gateway/MQTTClient.ts:37:32 ❯ gateway/MQTTClient.test.ts:61:5

Check failure on line 37 in gateway/MQTTClient.ts

View workflow job for this annotation

GitHub Actions / unit-tests

gateway/MQTTClient.test.ts > MQTTClient > should set the metrics model with incoming messages

TypeError: config.map is not a function ❯ Module.__vite_ssr_exports__.default gateway/MQTTClient.ts:37:32 ❯ gateway/MQTTClient.test.ts:68:5

Check failure on line 37 in gateway/MQTTClient.ts

View workflow job for this annotation

GitHub Actions / unit-tests

gateway/MQTTClient.test.ts > MQTTClient > should log > successful subscriptions

TypeError: config.map is not a function ❯ Module.__vite_ssr_exports__.default gateway/MQTTClient.ts:37:32 ❯ gateway/MQTTClient.test.ts:78:7

Check failure on line 37 in gateway/MQTTClient.ts

View workflow job for this annotation

GitHub Actions / unit-tests

gateway/MQTTClient.test.ts > MQTTClient > should log > subscription errors

TypeError: config.map is not a function ❯ Module.__vite_ssr_exports__.default gateway/MQTTClient.ts:37:32 ❯ gateway/MQTTClient.test.ts:85:7

Check failure on line 37 in gateway/MQTTClient.ts

View workflow job for this annotation

GitHub Actions / unit-tests

gateway/MQTTClient.test.ts > MQTTClient > should log > other errors

TypeError: config.map is not a function ❯ Module.__vite_ssr_exports__.default gateway/MQTTClient.ts:37:32 ❯ gateway/MQTTClient.test.ts:94:7

Check failure on line 37 in gateway/MQTTClient.ts

View workflow job for this annotation

GitHub Actions / unit-tests

gateway/MQTTClient.test.ts > MQTTClient > should log > connections

TypeError: config.map is not a function ❯ Module.__vite_ssr_exports__.default gateway/MQTTClient.ts:37:32 ❯ gateway/MQTTClient.test.ts:102:7

Check failure on line 37 in gateway/MQTTClient.ts

View workflow job for this annotation

GitHub Actions / unit-tests

gateway/MQTTClient.test.ts > MQTTClient > should log > disconnects

TypeError: config.map is not a function ❯ Module.__vite_ssr_exports__.default gateway/MQTTClient.ts:37:32 ❯ gateway/MQTTClient.test.ts:110:7

Check failure on line 37 in gateway/MQTTClient.ts

View workflow job for this annotation

GitHub Actions / unit-tests

gateway/MQTTClient.test.ts > MQTTClient > should log > messages in debug mode

TypeError: config.map is not a function ❯ Module.__vite_ssr_exports__.default gateway/MQTTClient.ts:37:32 ❯ gateway/MQTTClient.test.ts:118:7

client.on("connect", () => {
logger.info(`Connection to mqtt://${brokerName}:${port} established`)
Expand Down
15 changes: 7 additions & 8 deletions gateway/MetricsGenerator.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -9,7 +8,7 @@ function object2label(obj: LabelList) {
}

function createMetric(
config: SingleConfiguration,
config: MetricsConfiguration,
value: string | number,
labels: Record<string, string | number> = {}
) {
Expand All @@ -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,
Expand All @@ -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)

Check failure on line 39 in gateway/MetricsGenerator.ts

View workflow job for this annotation

GitHub Actions / unit-tests

gateway/MetricsGenerator.test.ts

TypeError: config.map is not a function ❯ Module.getMetrics [as default] gateway/MetricsGenerator.ts:39:6 ❯ gateway/MetricsGenerator.test.ts:44:18
.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("")
Expand Down
31 changes: 5 additions & 26 deletions gateway/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
],
})
}
Expand Down
6 changes: 3 additions & 3 deletions gateway/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type BaseType = string | number

export type LabelList = Record<string, BaseType>

export type SingleConfiguration = {
export type MetricsConfiguration = {
name: string
description: string
type: MetricType
Expand All @@ -16,7 +16,7 @@ export type SingleConfiguration = {
labels: LabelList
}

export type MetricsConfiguration = {
export type Configuration = {
logLevel?: LogLevel
metrics: SingleConfiguration[]
metrics: MetricsConfiguration[]
}
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 9c17482

Please sign in to comment.