From dfd0c57781ff41ae1623baa2d833ee6874d8d908 Mon Sep 17 00:00:00 2001 From: beckerinj Date: Wed, 11 May 2022 11:04:32 -0400 Subject: [PATCH] daily interval --- core/src/plugins/slackNotifier.ts | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/core/src/plugins/slackNotifier.ts b/core/src/plugins/slackNotifier.ts index e3a1e1be1..33cff0111 100644 --- a/core/src/plugins/slackNotifier.ts +++ b/core/src/plugins/slackNotifier.ts @@ -12,6 +12,12 @@ import { getPeripherySystemStats } from "../util/periphery/server"; import { serverStatusPeriphery } from "../util/periphery/status"; import { notifySlack } from "../util/slack"; +declare module "fastify" { + interface FastifyInstance { + dailyInterval: () => Promise; + } +} + const slackNotifier = fp((app: FastifyInstance, _: {}, done: () => void) => { const getAllServerStats = async () => { const servers = await app.servers.find({}); @@ -34,7 +40,6 @@ const slackNotifier = fp((app: FastifyInstance, _: {}, done: () => void) => { ).filter((server) => server.stats); return serversWithStatus; }; - const interval = async () => { const servers = await getAllServerStats(); servers.forEach((server) => { @@ -67,9 +72,24 @@ const slackNotifier = fp((app: FastifyInstance, _: {}, done: () => void) => { }); }; + const dailyInterval = async () => { + const servers = await getAllServerStats(); + const statsLog = servers.reduce((prev, curr) => { + const stats = curr.stats!; + return ( + prev + + `name: ${curr.name} | CPU: ${stats.cpu}% | MEM: ${stats.mem.usedMemPercentage}% | DISK: ${stats.disk.usedPercentage}%\n\n` + ); + }, ""); + const message = "INFO | daily update\n\n" + statsLog; + notifySlack(message); + }; + + app.decorate("dailyInterval", dailyInterval); + if (SECRETS.SLACK_TOKEN) { - // only do this if slack token is provided setInterval(interval, SERVER_STATS_INTERVAL); + setInterval(dailyInterval, 24 * 60 * 60 * 1000); } done();