Skip to content

Commit

Permalink
Update Dependencies and Metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
danthonywalker committed Oct 28, 2023
1 parent 659b45a commit 252c88a
Show file tree
Hide file tree
Showing 15 changed files with 719 additions and 606 deletions.
1,169 changes: 655 additions & 514 deletions package-lock.json

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"author": "danthonywalker",
"dependencies": {
"@googleapis/youtube": "11.0.1",
"@googleapis/youtube": "13.0.0",
"canvas": "2.11.2",
"compress-tag": "3.0.0",
"cron": "3.1.0",
"cron": "3.1.5",
"csv": "6.3.5",
"discord.js": "14.13.0",
"express": "4.18.2",
Expand All @@ -13,23 +13,23 @@
"ioredis": "5.3.2",
"lunr": "2.3.9",
"pg": "8.11.3",
"pino": "8.16.0",
"pino": "8.16.1",
"pino-http": "8.5.0",
"prom-client": "15.0.0",
"puppeteer": "21.3.8",
"puppeteer": "21.4.1",
"puppeteer-extra": "3.3.6",
"puppeteer-extra-plugin-stealth": "2.11.2",
"rss-parser": "3.13.0",
"vega": "5.25.0"
},
"devDependencies": {
"@types/express": "4.17.18",
"@types/lunr": "2.3.5",
"@types/pg": "8.10.4",
"@typescript-eslint/eslint-plugin": "6.7.5",
"@typescript-eslint/parser": "6.7.5",
"eslint": "8.51.0",
"eslint-plugin-perfectionist": "2.1.0",
"@types/express": "4.17.20",
"@types/lunr": "2.3.6",
"@types/pg": "8.10.7",
"@typescript-eslint/eslint-plugin": "6.9.0",
"@typescript-eslint/parser": "6.9.0",
"eslint": "8.52.0",
"eslint-plugin-perfectionist": "2.2.0",
"eslint-plugin-sonarjs": "0.21.0",
"eslint-plugin-unicorn": "48.0.1",
"pino-pretty": "10.2.3",
Expand Down
9 changes: 4 additions & 5 deletions src/commands/carsized/carsized.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import assert from "node:assert";

import type { Car, CompareCars } from "./types";

import loggerFactory from "../../logger.factory";
import Environment from "../../shared/environment";
import loggerFactory from "../../shared/logger";
import { isNonNullable } from "../../shared/nullable";
import { usePage } from "../../shared/puppeteer";
import RedisKey, * as redis from "../../shared/redis";
Expand Down Expand Up @@ -112,10 +112,9 @@ const getCars = async () => {
};

const initializeCars = async () => {
const carsArray =
Environment.EnableCarsized === "true"
? await redis.computeIfAbsent(RedisKey.Cars, getCars, ExpireIn30Days)
: [];
const carsArray = Environment.EnableCarsized
? await redis.computeIfAbsent(RedisKey.Cars, getCars, ExpireIn30Days)
: [];

carsIndex = lunr((builder) => {
builder.ref("id");
Expand Down
2 changes: 1 addition & 1 deletion src/commands/carsized/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { AttachmentBuilder } from "discord.js";

import type { CompareCars } from "./types";

import loggerFactory from "../../logger.factory";
import Session from "../../session";
import loggerFactory from "../../shared/logger";
import * as carsized from "./carsized.manager";
import UI from "./ui";

Expand Down
2 changes: 1 addition & 1 deletion src/commands/carsized/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,5 @@ const onCommand = async (interaction: CommandInteraction) => {
return withContext.compareCarsUi(context, interaction);
};

if (Environment.EnableCarsized === "true")
if (Environment.EnableCarsized)
registerCommand(json, onCommand, onAutocomplete);
2 changes: 1 addition & 1 deletion src/commands/surveys/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
Survey,
} from "./types";

import loggerFactory from "../../logger.factory";
import loggerFactory from "../../shared/logger";
import { QuestionType } from "./constants";

const logger = loggerFactory(module);
Expand Down
2 changes: 1 addition & 1 deletion src/creators/post/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import type Nullable from "../../shared/nullable";
import type { CreatorType } from "../constants";
import type { CreatorSubscription } from "./database";

import loggerFactory from "../../logger.factory";
import { byDate, isUnique } from "../../shared/array";
import discord from "../../shared/discord";
import loggerFactory from "../../shared/logger";
import { isNullable } from "../../shared/nullable";
import * as creatorsDatabase from "../database";
import * as postDatabase from "./database";
Expand Down
7 changes: 3 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { glob } from "glob";
import path from "node:path";
import { collectDefaultMetrics } from "prom-client";

import loggerFactory from "./logger.factory";
import discord from "./shared/discord";
import Environment from "./shared/environment";
import express from "./shared/express";
import loggerFactory from "./shared/logger";

// region Logger and Metrics
const logger = loggerFactory(module);
Expand All @@ -22,9 +22,8 @@ const main = async () => {
.map((importPath) => import(`./${importPath}`));
await Promise.all(imports);

const expressPort = Number.parseInt(Environment.ExpressPort);
express.listen(expressPort, () => {
logger.info(`Express is listening on port ${expressPort}`);
express.listen(Environment.ExpressPort, () => {
logger.info(`Express is listening on port ${Environment.ExpressPort}`);
});

await discord.login();
Expand Down
25 changes: 5 additions & 20 deletions src/shared/caller.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
// Function type is safe since only name is accessed
/* eslint-disable @typescript-eslint/ban-types */
export class Caller {
private readonly caller: Function;
private readonly module: NodeModule;
export type Caller = `${string}#${string}`;

constructor(module: NodeModule, caller: Function) {
this.module = module;
this.caller = caller;
}

toString() {
const { filename: url } = this.module;
const { name: fragment } = this.caller;
return `${url}#${fragment}`;
}
}

export default (module: NodeModule, caller: Function) =>
new Caller(module, caller);
/* eslint-enable @typescript-eslint/ban-types */
export default (
{ filename }: NodeModule,
{ name }: Record<"name", string>,
): Caller => `${filename}#${name}`;
8 changes: 4 additions & 4 deletions src/shared/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Client, Events, Routes, User } from "discord.js";
import assert from "node:assert";
import { Gauge, Histogram } from "prom-client";

import loggerFactory from "../logger.factory";
import loggerFactory from "./logger";

// region Logger and Metrics
const logger = loggerFactory(module);
Expand All @@ -26,9 +26,9 @@ const interactionRequestDuration = new Histogram({
});

const shardPing = new Gauge({
help: "Shard ping in milliseconds",
help: "Shard ping in seconds",
labelNames: ["shard"],
name: "shard_ping_milliseconds",
name: "shard_ping_seconds",
});
// endregion

Expand All @@ -54,7 +54,7 @@ discord.on(Events.Debug, (debug) => {

for (const [shard, { ping }] of shards) {
const labels = { shard };
shardPing.set(labels, ping);
shardPing.set(labels, ping / 1000);
}
});

Expand Down
41 changes: 17 additions & 24 deletions src/shared/environment.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
import assert from "node:assert";

const { env: environmentVariables } = process;
const environmentVariable = (key: string, defaultValue?: string) => {
const value = environmentVariables[key];
if (value !== undefined) return value;
if (defaultValue !== undefined) return defaultValue;
assert.fail();
};

const { env } = process;
export default {
BotGuildId: environmentVariables.BOT_GUILD_ID,
DiscordToken: environmentVariable("DISCORD_TOKEN"),
EnableCarsized: environmentVariable("ENABLE_CARSIZED", "false"),
ExpressPort: environmentVariable("EXPRESS_PORT", "8080"),
PostgresqlDatabase: environmentVariable("POSTGRESQL_DATABASE", "db"),
PostgresqlHost: environmentVariable("POSTGRESQL_HOST", "postgres"),
PostgresqlPassword: environmentVariable("POSTGRESQL_PASSWORD", "password"),
PostgresqlPort: environmentVariable("POSTGRESQL_PORT", "5432"),
PostgresqlUser: environmentVariable("POSTGRESQL_USER", "user"),
ProjectName: environmentVariable("PROJECT_NAME", "Pedestrian"),
RedisCluster: environmentVariable("REDIS_CLUSTER", "false"),
RedisHost: environmentVariable("REDIS_HOST", "redis"),
RedisPassword: environmentVariables.REDIS_PASSWORD,
RedisPort: environmentVariable("REDIS_PORT", "6379"),
RedisUsername: environmentVariables.REDIS_USERNAME,
YoutubeApiKey: environmentVariable("YOUTUBE_API_KEY"),
BotGuildId: env.BOT_GUILD_ID,
DiscordToken: env.DISCORD_TOKEN ?? assert.fail(),
EnableCarsized: Boolean(env.ENABLE_CARSIZED),
ExpressPort: Number(env.EXPRESS_PORT ?? 8080),
PostgresqlDatabase: env.POSTGRESQL_DATABASE ?? "db",
PostgresqlHost: env.POSTGRESQL_HOST ?? "postgres",
PostgresqlPassword: env.POSTGRESQL_PASSWORD ?? "password",
PostgresqlPort: Number(env.POSTGRESQL_PORT ?? 5432),
PostgresqlUser: env.POSTGRESQL_USER ?? "user",
ProjectName: env.PROJECT_NAME ?? "Pedestrian",
RedisCluster: Boolean(env.REDIS_CLUSTER),
RedisHost: env.REDIS_HOST ?? "redis",
RedisPassword: env.REDIS_PASSWORD,
RedisPort: Number(env.REDIS_PORT ?? 6379),
RedisUsername: env.REDIS_USERNAME,
YoutubeApiKey: env.YOUTUBE_API_KEY ?? assert.fail(),
} as const;
2 changes: 1 addition & 1 deletion src/shared/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import express from "express";
import promBundle from "express-prom-bundle";
import pinoBundle from "pino-http";

import loggerFactory from "../logger.factory";
import loggerFactory from "./logger";

const server = express();

Expand Down
3 changes: 2 additions & 1 deletion src/logger.factory.ts → src/shared/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ export default ({ filename }: NodeModule) =>
bindings: () => ({
name: path.relative(__dirname, filename),
}),
level: (label) => ({
level: (label, value) => ({
level: label,
levelValue: value,
}),
},
level: "debug",
Expand Down
20 changes: 8 additions & 12 deletions src/shared/postgresql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { Histogram } from "prom-client";

import type { Caller } from "./caller";

import loggerFactory from "../logger.factory";
import Environment from "./environment";
import loggerFactory from "./logger";

// region Types
type Callback<T> = (client: PoolClient) => Promise<T>;
Expand All @@ -26,35 +26,31 @@ const postgresql = new Pool({
database: Environment.PostgresqlDatabase,
host: Environment.PostgresqlHost,
password: Environment.PostgresqlPassword,
port: Number.parseInt(Environment.PostgresqlPort),
port: Environment.PostgresqlPort,
user: Environment.PostgresqlUser,
});

export const useClient = async <T>(caller: Caller, callback: Callback<T>) => {
const observeRequestDuration = databaseRequestDuration.startTimer();
const onDatabase =
(status: "error" | "success", client?: PoolClient) => (result: unknown) => {
client?.release(status === "error");

const labels = {
caller: caller.toString(),
connected: `${client !== undefined}`,
status,
};
const connected = client !== undefined;
if (connected) client.release(status === "error");

const labels = { caller, connected: `${connected}`, status };
const requestDuration = observeRequestDuration(labels);
const childLogger = logger.child({ labels, requestDuration });

if (status === "error") {
childLogger.error(result, "ON_DATABASE_ERROR");
throw result;
} else if (requestDuration > 100) {
} else if (requestDuration >= 0.1) {
childLogger.warn(result, "ON_DATABASE_SUCCESS_SLOW");
return result as T;
} else {
childLogger.debug(result, "ON_DATABASE_SUCCESS");
return result as T;
}

return result as T;
};

return postgresql
Expand Down
11 changes: 5 additions & 6 deletions src/shared/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type { Callback, Result } from "ioredis";
import { Redis } from "ioredis";
import * as crypto from "node:crypto";

import loggerFactory from "../logger.factory";
import Environment from "./environment";
import loggerFactory from "./logger";
import { isNullable } from "./nullable";
import sleep from "./sleep";

Expand All @@ -13,7 +13,7 @@ const logger = loggerFactory(module);
// region redis
const node = {
host: Environment.RedisHost,
port: Number.parseInt(Environment.RedisPort),
port: Environment.RedisPort,
};

const user = {
Expand All @@ -34,10 +34,9 @@ const createRedisByClient = () =>
...user,
});

const redis =
Environment.RedisCluster === "true"
? createRedisByCluster()
: createRedisByClient();
const redis = Environment.RedisCluster
? createRedisByCluster()
: createRedisByClient();

redis.on("error", (error) => {
logger.error(error, "REDIS_ERROR");
Expand Down

0 comments on commit 252c88a

Please sign in to comment.