From adcb4b106468f08d8a172900faaaeb4f72ddae72 Mon Sep 17 00:00:00 2001 From: Akshay Shekhawat Date: Sun, 11 Sep 2022 15:56:12 -0700 Subject: [PATCH] (feature) log aggregated stats --- backend/src/jobs.ts | 7 +++++++ backend/src/services/logging/index.ts | 30 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 backend/src/services/logging/index.ts diff --git a/backend/src/jobs.ts b/backend/src/jobs.ts index 0769e609..d0733840 100644 --- a/backend/src/jobs.ts +++ b/backend/src/jobs.ts @@ -2,6 +2,7 @@ import schedule from "node-schedule" import { AppDataSource } from "data-source" import { JobsService } from "services/jobs" import runAllTests from "services/testing/runAllTests" +import { logAggregatedStats } from "services/logging" const main = async () => { const datasource = await AppDataSource.initialize() @@ -36,6 +37,12 @@ const main = async () => { console.log("Finished clearing Api Trace data.") }) + schedule.scheduleJob("0 */6 * * *", async () => { + console.log("Logging Aggregated Stats...") + await logAggregatedStats() + console.log("Finished Logging Aggregated Stats.") + }) + process.on("SIGINT", () => { schedule.gracefulShutdown().then(() => process.exit(0)) }) diff --git a/backend/src/services/logging/index.ts b/backend/src/services/logging/index.ts new file mode 100644 index 00000000..d78bfae1 --- /dev/null +++ b/backend/src/services/logging/index.ts @@ -0,0 +1,30 @@ +import axios from "axios" +import { AppDataSource } from "data-source" +import { InstanceSettings } from "models" +import { getCounts } from "services/summary/usageStats" + +export const logAggregatedStats = async () => { + const settingRepository = AppDataSource.getRepository(InstanceSettings) + const settingsLs = await settingRepository.find() + if (settingsLs.length == 0) { + console.log("No instance settings found...") + return + } + const settings = settingsLs[0] + const counts = await getCounts() + await axios({ + url: "https://logger.metlo.com/log", + method: "POST", + data: { + instanceUUID: settings.uuid, + eventName: "instanceAggregatedStats", + data: { + numEndpoints: counts.endpointsTracked, + numHosts: counts.hostCount, + openAlerts: counts.newAlerts, + openHighRiskAlerts: counts.highRiskAlerts, + piiDataFields: counts.piiDataFields, + }, + }, + }) +}