From 6d08f47d1dddc14df6803fc7c15e4ec0f1c8d434 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Jani=C5=A1?= Date: Tue, 19 Jan 2021 00:18:32 +0100 Subject: [PATCH] feat: collect avg response time by default --- .github/workflows/deploy.yml | 2 + config.yaml | 2 +- src/cli/gcMonitors.js | 61 +++++++++++++++++------------ src/components/monitorCard.js | 4 +- src/components/monitorDayAverage.js | 17 ++++++++ src/components/monitorHistogram.js | 10 +++-- 6 files changed, 66 insertions(+), 30 deletions(-) create mode 100644 src/components/monitorDayAverage.js diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 898acd1..a6cbb8b 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -5,6 +5,8 @@ on: branches: - main repository_dispatch: + schedule: + - cron: '0 0 1 * *' jobs: deploy: diff --git a/config.yaml b/config.yaml index 2dd66a2..cb3e7b4 100644 --- a/config.yaml +++ b/config.yaml @@ -3,7 +3,7 @@ settings: url: 'https://status-page.eidam.dev' # used for Slack messages logo: logo-192x192.png # image in ./public/ folder daysInHistogram: 90 # number of days you want to display in histogram - collectResponseTimes: false # experimental feature, enable only for <5 monitors or on paid plans + collectResponseTimes: true # collects avg response times from CRON locations allmonitorsOperational: 'All Systems Operational' notAllmonitorsOperational: 'Not All Systems Operational' diff --git a/src/cli/gcMonitors.js b/src/cli/gcMonitors.js index 120fd3e..d04eb5a 100644 --- a/src/cli/gcMonitors.js +++ b/src/cli/gcMonitors.js @@ -6,7 +6,7 @@ const accountId = process.env.CF_ACCOUNT_ID const namespaceId = process.env.KV_NAMESPACE_ID const apiToken = process.env.CF_API_TOKEN -const kvPrefix = 's_' +const kvMonitorsKey = 'monitors_data_v1_1' if (!accountId || !namespaceId || !apiToken) { console.error( @@ -15,7 +15,7 @@ if (!accountId || !namespaceId || !apiToken) { process.exit(0) } -async function getKvMonitors(kvPrefix) { +async function getKvMonitors(kvMonitorsKey) { const init = { headers: { 'Content-Type': 'application/json', @@ -24,27 +24,29 @@ async function getKvMonitors(kvPrefix) { } const res = await fetch( - `https://api.cloudflare.com/client/v4/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/keys?limit=100&prefix=${kvPrefix}`, + `https://api.cloudflare.com/client/v4/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${kvMonitorsKey}`, init, ) const json = await res.json() - return json.result + return json } -async function deleteKvBulk(keys) { +async function saveKVMonitors(kvMonitorsKey, data) { const init = { + method: 'PUT', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${apiToken}`, }, - method: 'DELETE', - body: JSON.stringify(keys), + body: JSON.stringify(data), } - return await fetch( - `https://api.cloudflare.com/client/v4/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/bulk`, + const res = await fetch( + `https://api.cloudflare.com/client/v4/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${kvMonitorsKey}`, init, ) + + return res } function loadConfig() { @@ -53,26 +55,37 @@ function loadConfig() { return JSON.parse(config) } -getKvMonitors(kvPrefix) +getKvMonitors(kvMonitorsKey) .then(async (kvMonitors) => { + let stateMonitors = kvMonitors + const config = loadConfig() - const monitors = config.monitors.map((key) => { + const configMonitors = config.monitors.map((key) => { return key.id }) - const kvState = kvMonitors.map((key) => { - return key.name - }) - const keysForRemoval = kvState.filter( - (x) => !monitors.includes(x.replace(kvPrefix, '')), - ) - if (keysForRemoval.length > 0) { - console.log( - `Removing following keys from KV storage as they are no longer in the config: ${keysForRemoval.join( - ', ', - )}`, - ) - await deleteKvBulk(keysForRemoval) + Object.keys(stateMonitors.monitors).map((monitor) => { + // remove monitor data from state if missing in config + if (!configMonitors.includes(monitor)) { + delete stateMonitors.monitors[monitor] + } + + // delete dates older than config.settings.daysInHistogram + let date = new Date() + date.setDate(date.getDate() - config.settings.daysInHistogram) + date.toISOString().split('T')[0] + const cleanUpDate = date.toISOString().split('T')[0] + + Object.keys(stateMonitors.monitors[monitor].checks).map((checkDay) => { + if (checkDay < cleanUpDate) { + delete stateMonitors.monitors[monitor].checks[checkDay] + } + }) + }) + + // sanity check + if good save the KV + if (configMonitors.length === Object.keys(stateMonitors.monitors).length) { + await saveKVMonitors(kvMonitorsKey, stateMonitors) } }) .catch((e) => console.log(e)) diff --git a/src/components/monitorCard.js b/src/components/monitorCard.js index 690d5c8..01fc6e2 100644 --- a/src/components/monitorCard.js +++ b/src/components/monitorCard.js @@ -30,7 +30,9 @@ export default function MonitorCard({ key, monitor, data }) { )} -
{monitor.name}
+ +
{monitor.name}
+
diff --git a/src/components/monitorDayAverage.js b/src/components/monitorDayAverage.js new file mode 100644 index 0000000..8890440 --- /dev/null +++ b/src/components/monitorDayAverage.js @@ -0,0 +1,17 @@ +const locations = { + WAW: 'Warsaw', + SCL: 'Santiago de Chile', + MEL: 'Melbourne', + SIN: 'Singapore', +} + +export default function MonitorDayAverage({ location, avg }) { + return ( + <> +
+ + {locations[location] || location}: {avg}ms + + + ) +} diff --git a/src/components/monitorHistogram.js b/src/components/monitorHistogram.js index 8675c2c..398e343 100644 --- a/src/components/monitorHistogram.js +++ b/src/components/monitorHistogram.js @@ -1,4 +1,6 @@ +import React from 'react' import config from '../../config.yaml' +import MonitorDayAverage from './monitorDayAverage' export default function MonitorHistogram({ monitorId, kvMonitor }) { // create date and set date - daysInHistogram for the first day of the histogram @@ -43,10 +45,10 @@ export default function MonitorHistogram({ monitorId, kvMonitor }) { kvMonitor.checks.hasOwnProperty(dayInHistogram) && Object.keys(kvMonitor.checks[dayInHistogram].res).map((key) => { return ( - <> -
- {key}: {kvMonitor.checks[dayInHistogram].res[key].a}ms - + ) })}