Skip to content

Commit

Permalink
(feature) log aggregated stats
Browse files Browse the repository at this point in the history
  • Loading branch information
akshay288 committed Sep 11, 2022
1 parent a76d6f4 commit adcb4b1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions backend/src/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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))
})
Expand Down
30 changes: 30 additions & 0 deletions backend/src/services/logging/index.ts
Original file line number Diff line number Diff line change
@@ -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,
},
},
})
}

0 comments on commit adcb4b1

Please sign in to comment.