import { State } from "." import { Config, defaultNotifyInterval, config } from "./config" import { notify } from "./notify" import { formatTS } from "./utils" type EndpointState = State extends Map ? L : never export const handleDown = (endpointState: EndpointState, curTime: number, endpoint: Config["pollEndpoints"][number]) => { const prevDown = endpointState.isDown endpointState.isDown = true endpointState.attemptsFailed++ console.log(`Endpoint ${endpoint.name} has failed ${endpointState.attemptsFailed} times`) if (!prevDown) endpointState.downStart = curTime if (curTime - endpointState.lastDownAlert < (endpoint.notifyInterval) * 1000) return if (endpointState.attemptsFailed > (endpoint.retries ?? config.defaults.retries)) { const message = prevDown ? `[${formatTS(curTime)}] ${endpoint.name} is still down and initially went down at ${formatTS(endpointState.downStart)}` : `[${formatTS(curTime)}] ${endpoint.name} went down` notify(message, endpoint) endpointState.lastDownAlert = curTime } } export const handleUp = (endpointState: EndpointState, curTime: number, endpoint: Config["pollEndpoints"][number]) => { if (endpointState.isDown) { endpointState.isDown = false endpointState.attemptsFailed = 0 const message = `[${formatTS(curTime)}] ${endpoint.name} is back up` notify(message, endpoint) } }