add mongo stats to diagnostic service

This commit is contained in:
Alex Holliday committed 2026-05-26 13:31:13 -07:00
1 parent 2764468e90
commit 2d432e0a3d
4 files changed
+127 -49

No files matched your search

+2 -1
View File
@@ -1,4 +1,5 @@
export interface IDb {
export interface IDb<TConnection = unknown> {
connect(): Promise<void>;
disconnect(): Promise<void>;
getConnection(): Promise<TConnection>;
}
+13 -2
View File
@@ -1,3 +1,4 @@
import { Mongoose } from "mongoose";
import mongoose from "mongoose";
import AppSettings from "@/db/models/AppSettings.js";
import { runMigrations } from "@/db/migration/index.js";
@@ -5,16 +6,19 @@ import { ILogger } from "@/utils/logger.js";
import { EnvConfig } from "@/service/system/settingsService.js";
const SERVICE_NAME = "MongoDB";
import { IDb } from "@/db/IDb.js";
import { AppError } from "@/utils/AppError.js";
class MongoDB implements IDb {
class MongoDB implements IDb<Mongoose> {
static SERVICE_NAME = SERVICE_NAME;
private logger: ILogger;
private envSettings: EnvConfig;
private db: Mongoose | null;
constructor(logger: ILogger, envSettings: EnvConfig) {
this.logger = logger;
this.envSettings = envSettings;
this.db = null;
}
get serviceName() {
@@ -24,7 +28,7 @@ class MongoDB implements IDb {
connect = async () => {
try {
const connectionString = this.envSettings.dbConnectionString || "mongodb://localhost:27017/uptime_db";
await mongoose.connect(connectionString);
this.db = await mongoose.connect(connectionString);
// If there are no AppSettings, create one // TODO why is this here?
await AppSettings.findOneAndUpdate(
{}, // empty filter to match any document
@@ -63,6 +67,13 @@ class MongoDB implements IDb {
}
};
getConnection = async (): Promise<Mongoose> => {
if (this.db === null) {
throw new AppError({ message: "Database not connected", service: SERVICE_NAME, method: "getConnection" });
}
return this.db;
};
disconnect = async () => {
try {
this.logger.info({ message: "Disconnecting from MongoDB", service: SERVICE_NAME, method: "disconnect" });