2020-05-06 15:25:35 +00:00
|
|
|
import {json, Router} from 'express';
|
2021-01-10 15:06:18 +00:00
|
|
|
import {CurrentStatus, State, UptimeStatus} from './app/_data/data';
|
2020-07-05 18:19:52 +00:00
|
|
|
import {existsSync, readFileSync, writeFileSync} from 'fs';
|
2020-07-05 20:09:28 +00:00
|
|
|
import {join} from 'path';
|
|
|
|
import {JSONPath} from 'jsonpath-plus';
|
2021-01-10 15:06:18 +00:00
|
|
|
import * as dayjs from 'dayjs';
|
|
|
|
import {Dayjs} from 'dayjs';
|
|
|
|
import * as utc from 'dayjs/plugin/utc';
|
|
|
|
import * as isBetween from 'dayjs/plugin/isBetween';
|
|
|
|
import {CronJob} from 'cron';
|
|
|
|
|
|
|
|
dayjs.extend(utc);
|
|
|
|
dayjs.extend(isBetween);
|
2020-05-06 15:25:35 +00:00
|
|
|
|
|
|
|
interface Cache {
|
2020-07-05 18:19:52 +00:00
|
|
|
[id: string]: State;
|
2020-05-06 15:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface Config {
|
|
|
|
authToken: string;
|
|
|
|
title: string;
|
|
|
|
description: string;
|
2021-01-10 15:06:18 +00:00
|
|
|
translations?: {
|
|
|
|
[lang: string]: {
|
|
|
|
title: string;
|
|
|
|
description: string;
|
|
|
|
}
|
|
|
|
},
|
2020-07-16 20:08:02 +00:00
|
|
|
servicesPath?: string;
|
|
|
|
idPath?: string;
|
|
|
|
statePath?: string;
|
2020-07-05 18:19:52 +00:00
|
|
|
stateValues: {
|
|
|
|
operational: string[];
|
|
|
|
maintenance: string[];
|
|
|
|
};
|
2020-05-06 15:25:35 +00:00
|
|
|
groups: {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
2020-07-16 20:08:02 +00:00
|
|
|
url?: string;
|
2020-05-06 15:25:35 +00:00
|
|
|
services: {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
2020-07-16 20:08:02 +00:00
|
|
|
url?: string;
|
2020-07-05 18:19:52 +00:00
|
|
|
statePath?: string;
|
2020-05-06 15:25:35 +00:00
|
|
|
}[];
|
|
|
|
}[];
|
|
|
|
}
|
|
|
|
|
|
|
|
const api = Router();
|
|
|
|
api.use(json());
|
|
|
|
|
2020-07-05 20:09:28 +00:00
|
|
|
const serviceStates = existsSync(join(process.cwd(), 'cache.json')) ? JSON.parse(readFileSync(join(process.cwd(), 'cache.json'), {encoding: 'utf-8'})) : {} as Cache;
|
|
|
|
const config = JSON.parse(readFileSync(join(process.cwd(), 'config.json'), {encoding: 'utf-8'})) as Config;
|
2020-07-16 20:08:02 +00:00
|
|
|
const serviceStatePaths: { [service: string]: string } = config.groups
|
2020-07-05 18:19:52 +00:00
|
|
|
.map(g => g.services).reduce((x, y) => x.concat(y), [])
|
2020-07-16 20:08:02 +00:00
|
|
|
.filter(s => s.statePath)
|
2020-07-05 18:19:52 +00:00
|
|
|
.reduce((services, service) => {
|
2020-07-16 20:08:02 +00:00
|
|
|
services[service.id] = service.statePath;
|
2020-07-05 18:19:52 +00:00
|
|
|
return services;
|
|
|
|
}, {});
|
2020-05-06 15:25:35 +00:00
|
|
|
|
|
|
|
let cache: CurrentStatus;
|
2021-01-10 19:49:09 +00:00
|
|
|
let uptimeStates = existsSync(join(process.cwd(), 'uptime.json')) ? JSON.parse(readFileSync(join(process.cwd(), 'uptime.json'), {encoding: 'utf-8'})) : {} as { [id: string]: UptimeStatus; };
|
|
|
|
// init serviceStates and uptimeStates
|
2021-01-10 18:10:12 +00:00
|
|
|
config.groups
|
|
|
|
.map(g => g.services).reduce((x, y) => x.concat(y), [])
|
|
|
|
.map(s => s.id).filter(id => !serviceStates[id])
|
|
|
|
.forEach(id => serviceStates[id] = 'operational');
|
2021-01-10 19:49:09 +00:00
|
|
|
for (let id in serviceStates) {
|
|
|
|
if (serviceStates.hasOwnProperty(id)) {
|
|
|
|
updateServiceState(id, serviceStates[id]);
|
2021-01-10 18:10:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-06 15:25:35 +00:00
|
|
|
updateCache();
|
|
|
|
|
|
|
|
api.post('/update/health', (req, res) => {
|
|
|
|
const token = req.query.token;
|
|
|
|
if (token !== config.authToken) {
|
|
|
|
return res.status(401).send('invalid token');
|
|
|
|
}
|
|
|
|
const serviceId = req.query.service as string;
|
2020-07-16 20:08:02 +00:00
|
|
|
let services: { id: string, state: string }[] = [];
|
|
|
|
if (serviceId) {
|
|
|
|
services = [{id: serviceId, state: JSONPath({path: serviceStatePaths[serviceId], json: req.body, wrap: false})}];
|
|
|
|
} else if (config.servicesPath && config.idPath && config.statePath) {
|
|
|
|
services = JSONPath({path: config.servicesPath, json: req.body})
|
|
|
|
.map(s => ({
|
2021-01-10 15:06:18 +00:00
|
|
|
id: JSONPath({path: config.idPath, json: s, wrap: false}),
|
|
|
|
state: JSONPath({path: config.statePath, json: s, wrap: false})
|
2020-07-16 20:08:02 +00:00
|
|
|
}));
|
2020-05-06 15:25:35 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 20:08:02 +00:00
|
|
|
services.forEach(s => {
|
|
|
|
if (config.stateValues.operational.includes(s.state)) {
|
2021-01-10 15:06:18 +00:00
|
|
|
updateServiceState(s.id, 'operational');
|
2020-07-16 20:08:02 +00:00
|
|
|
} else if (config.stateValues.maintenance.includes(s.state)) {
|
2021-01-10 15:06:18 +00:00
|
|
|
updateServiceState(s.id, 'maintenance');
|
2020-07-16 20:08:02 +00:00
|
|
|
} else {
|
2021-01-10 15:06:18 +00:00
|
|
|
updateServiceState(s.id, 'outage');
|
2020-07-16 20:08:02 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-05-06 15:25:35 +00:00
|
|
|
updateCache();
|
2021-01-10 15:06:18 +00:00
|
|
|
persistCache();
|
2020-05-06 15:25:35 +00:00
|
|
|
|
|
|
|
return res.send('OK');
|
|
|
|
});
|
|
|
|
|
|
|
|
api.get('/status', (req, res) => {
|
2021-01-10 15:06:18 +00:00
|
|
|
return res.json(cache);
|
|
|
|
});
|
|
|
|
|
|
|
|
api.get('/uptime', (req, res) => {
|
|
|
|
const serviceId = req.query.service as string;
|
|
|
|
const uptime = uptimeStates[serviceId];
|
|
|
|
if (uptime) {
|
|
|
|
return res.json(uptime);
|
|
|
|
}
|
|
|
|
return res.sendStatus(404);
|
2020-11-27 21:37:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
api.get('/badge', (req, res) => {
|
|
|
|
const serviceId = req.query.service as string;
|
|
|
|
if (!serviceId) {
|
|
|
|
return res.json({
|
2021-01-10 15:06:18 +00:00
|
|
|
'schemaVersion': 1,
|
|
|
|
'label': 'sp-status',
|
|
|
|
'message': 'service not provided',
|
|
|
|
'isError': true
|
2020-11-27 21:37:24 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
const service = cache.groups
|
|
|
|
.map(g => g.services).reduce((x, y) => x.concat(y), [])
|
|
|
|
.find(s => s.id === serviceId);
|
|
|
|
if (!service) {
|
|
|
|
return res.json({
|
2021-01-10 15:06:18 +00:00
|
|
|
'schemaVersion': 1,
|
|
|
|
'label': 'sp-status',
|
|
|
|
'message': 'service not found',
|
|
|
|
'isError': true
|
2020-11-27 21:37:24 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
const label = req.query.label || service.name;
|
|
|
|
let message;
|
|
|
|
let color;
|
|
|
|
switch (service.state) {
|
|
|
|
case 'operational':
|
|
|
|
message = req.query.operational || service.state;
|
|
|
|
color = '#7ed321';
|
|
|
|
break;
|
|
|
|
case 'outage':
|
|
|
|
message = req.query.outage || service.state;
|
|
|
|
color = '#ff6f6f';
|
|
|
|
break;
|
|
|
|
case 'maintenance':
|
|
|
|
message = req.query.maintenance || service.state;
|
|
|
|
color = '#f7ca18';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return res.json({
|
2021-01-10 15:06:18 +00:00
|
|
|
'schemaVersion': 1,
|
|
|
|
'label': label,
|
|
|
|
'message': message,
|
|
|
|
'color': color
|
2020-11-27 21:37:24 +00:00
|
|
|
});
|
2020-05-06 15:25:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
api.get('/info', (req, res) => {
|
|
|
|
return res.json({
|
|
|
|
title: config.title,
|
2021-01-10 15:06:18 +00:00
|
|
|
description: config.description,
|
|
|
|
translations: config.translations
|
2020-05-06 15:25:35 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-01-10 15:06:18 +00:00
|
|
|
function updateServiceState(id: string, state: string) {
|
2021-01-10 18:10:12 +00:00
|
|
|
if (!uptimeStates[id]) {
|
|
|
|
uptimeStates[id] = {days: [], events: []};
|
|
|
|
}
|
2021-01-10 15:06:18 +00:00
|
|
|
if (serviceStates[id] === state) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
serviceStates[id] = state;
|
|
|
|
if (uptimeStates[id].events.length === 0 && state !== 'operational' ||
|
|
|
|
uptimeStates[id].events.length > 0 && uptimeStates[id].events[0].state !== state) {
|
|
|
|
uptimeStates[id].events.unshift({state: state, date: new Date()});
|
|
|
|
console.log(`${id} changed to ${state}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 15:25:35 +00:00
|
|
|
function updateCache(): void {
|
2021-01-10 15:06:18 +00:00
|
|
|
updateUptime();
|
|
|
|
|
2020-05-06 15:25:35 +00:00
|
|
|
const groups = config.groups.map(group => {
|
|
|
|
const services = group.services.map(service => {
|
2021-01-10 15:06:18 +00:00
|
|
|
const uptime = uptimeStates[service.id];
|
2020-05-06 15:25:35 +00:00
|
|
|
return {
|
|
|
|
id: service.id,
|
|
|
|
name: service.name,
|
|
|
|
url: service.url,
|
2021-01-10 15:06:18 +00:00
|
|
|
state: serviceStates[service.id] || 'operational',
|
|
|
|
uptime: uptime ? uptime.days30 : 100
|
2020-07-05 18:19:52 +00:00
|
|
|
};
|
2020-05-06 15:25:35 +00:00
|
|
|
});
|
|
|
|
return {
|
|
|
|
id: group.id,
|
|
|
|
name: group.name,
|
2020-07-16 20:08:02 +00:00
|
|
|
url: group.url,
|
2020-05-06 15:25:35 +00:00
|
|
|
state: calculateOverallState(services.map(s => s.state)),
|
|
|
|
services: services
|
2020-07-05 18:19:52 +00:00
|
|
|
};
|
2020-05-06 15:25:35 +00:00
|
|
|
});
|
|
|
|
cache = {
|
|
|
|
state: calculateOverallState(groups.map(g => g.state)),
|
|
|
|
groups: groups
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-01-10 15:06:18 +00:00
|
|
|
function updateUptime() {
|
|
|
|
const now = dayjs.utc();
|
|
|
|
const today = now.startOf('d');
|
|
|
|
const eventLimit = now.subtract(7, 'd');
|
|
|
|
for (const id in uptimeStates) {
|
|
|
|
if (uptimeStates.hasOwnProperty(id)) {
|
|
|
|
const uptime = uptimeStates[id] as UptimeStatus;
|
|
|
|
if (uptime.days.length < 90) {
|
|
|
|
for (let i = 0; i < 90; i++) {
|
2021-01-10 18:10:12 +00:00
|
|
|
uptime.days.push({date: today.subtract(90 - i, 'd').toDate(), uptime: 100});
|
2021-01-10 15:06:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (today.diff(dayjs.utc(uptime.days[uptime.days.length - 1].date), 'd') >= 1) {
|
|
|
|
uptime.days.push({date: today.toDate(), uptime: 0});
|
|
|
|
}
|
|
|
|
if (uptime.days.length > 90) {
|
|
|
|
uptime.days.splice(0, uptime.days.length - 90);
|
|
|
|
}
|
|
|
|
for (let i = uptime.days.length - 3; i < uptime.days.length; i++) {
|
|
|
|
const start = dayjs.utc(uptime.days[i].date);
|
|
|
|
let end = start.add(1, 'd');
|
|
|
|
if (end.isAfter(now)) {
|
|
|
|
end = now;
|
|
|
|
}
|
|
|
|
uptime.days[i].uptime = calculateUptime(start, end, uptime.events);
|
|
|
|
}
|
|
|
|
uptime.hours24 = calculateUptime(now.subtract(24, 'h'), now, uptime.events);
|
|
|
|
uptime.days7 = uptime.days.slice(uptime.days.length - 7, uptime.days.length).map(e => e.uptime).reduce((a, b) => a + b) / 7;
|
|
|
|
uptime.days30 = uptime.days.slice(uptime.days.length - 30, uptime.days.length).map(e => e.uptime).reduce((a, b) => a + b) / 30;
|
|
|
|
uptime.days90 = uptime.days.slice(uptime.days.length - 90, uptime.days.length).map(e => e.uptime).reduce((a, b) => a + b) / 90;
|
|
|
|
uptime.events = uptime.events.filter(e => dayjs.utc(e.date).isAfter(eventLimit));
|
|
|
|
if (uptime.events.length > 0 && uptime.events[uptime.events.length - 1].state === 'operational') {
|
|
|
|
uptime.events.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function calculateUptime(start: Dayjs, end: Dayjs, events: { state: State; date: Date; }[]): number {
|
|
|
|
if (events.filter(event => dayjs.utc(event.date).isBetween(start, end)).length == 0) {
|
|
|
|
const lastEvent = events.filter(event => dayjs.utc(event.date).isBefore(start))[0];
|
|
|
|
if (lastEvent && lastEvent.state !== 'operational') {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 100;
|
|
|
|
}
|
|
|
|
let uptimeMillis = 0;
|
|
|
|
let newestEventDate;
|
|
|
|
for (let i = events.length - 1; i >= 0; i--) {
|
|
|
|
const event = events[i];
|
|
|
|
const eventDate = dayjs.utc(event.date);
|
|
|
|
const lastEvent = events[i + 1];
|
|
|
|
let lastEventDate = lastEvent ? dayjs.utc(lastEvent.date) : start;
|
|
|
|
if (lastEventDate.isBefore(start)) {
|
|
|
|
lastEventDate = start;
|
|
|
|
}
|
|
|
|
if (eventDate.isBetween(start, end)) {
|
|
|
|
if (event.state === 'operational') {
|
|
|
|
newestEventDate = eventDate;
|
|
|
|
} else if (!lastEvent || lastEvent.state === 'operational') {
|
|
|
|
newestEventDate = null;
|
|
|
|
uptimeMillis += eventDate.diff(lastEventDate, 'ms');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (newestEventDate) {
|
|
|
|
uptimeMillis += end.diff(newestEventDate, 'ms');
|
|
|
|
}
|
|
|
|
return uptimeMillis / end.diff(start, 'ms') * 100;
|
|
|
|
}
|
|
|
|
|
2020-05-06 15:25:35 +00:00
|
|
|
function calculateOverallState(states: State[]): State {
|
2020-07-05 18:19:52 +00:00
|
|
|
return states.includes('outage') ? 'outage' : states.includes('maintenance') ? 'maintenance' : 'operational';
|
2020-05-06 15:25:35 +00:00
|
|
|
}
|
|
|
|
|
2021-01-10 15:06:18 +00:00
|
|
|
function persistCache() {
|
|
|
|
writeFileSync('cache.json', JSON.stringify(serviceStates), {encoding: 'utf-8'});
|
|
|
|
writeFileSync('uptime.json', JSON.stringify(uptimeStates), {encoding: 'utf-8'});
|
|
|
|
}
|
|
|
|
|
|
|
|
new CronJob('0 * * * * *', () => updateCache(), null, true, 'UTC').start();
|
|
|
|
new CronJob('0 0 * * * *', () => persistCache(), null, true, 'UTC').start();
|
|
|
|
|
2020-05-06 15:25:35 +00:00
|
|
|
export {api};
|