feature: suivi de certifications

This commit is contained in:
Cédric Moreau
2022-05-31 12:08:26 +02:00
parent 2ac42c4ec0
commit a49d13c8a9
4 changed files with 63 additions and 2 deletions

View File

@@ -11,6 +11,7 @@ import {jsonWatcher} from "./watchers/wotwizard/json-watcher";
import {initConfMail} from './mail'
import {membershipWatcher} from './watchers/bma/membership-watcher'
import {websocketWatcher} from "./watchers/websocket/websocket-watcher";
import {certificationsWatcher} from "./watchers/bma/certifications-watcher";
export async function dwatch(confFile: string) {
@@ -26,5 +27,6 @@ export async function dwatch(confFile: string) {
(await Promise.all((conf.headServers || []).map(headWatcher(conf)))).forEach(w => watchers.push(w));
(await Promise.all((conf.wwMeta || []).map(jsonWatcher(conf)))).forEach(w => watchers.push(w));
(await Promise.all((conf.memberships || []).map(membershipWatcher(conf)))).forEach(w => watchers.push(w));
(await Promise.all((conf.certifications || []).map(certificationsWatcher(conf)))).forEach(w => watchers.push(w));
return watchers
}

View File

@@ -10,6 +10,7 @@ export interface Conf {
headServers: ConfHead[]
wwMeta: ConfWWMeta[]
memberships: ConfMembership[]
certifications: ConfCertifications[]
mail: ConfMail
}
@@ -52,6 +53,12 @@ export interface ConfMembership extends ConfURL {
mustRemain: number
}
export interface ConfCertifications extends ConfURL {
pubkey: string
mustRemainAtLeast: number
mustLongAtLeast: number
}
export interface ConfHead extends ConfURL {
observedPubkey: string
maxLateBlocks: number

View File

@@ -0,0 +1,48 @@
import {Conf, ConfCertifications, ConfMembership} from "../../types/conf";
import {urlWatcher, UrlWatcherResult} from '../abstract/url-watcher'
import {moment} from "duniter/app/lib/common-libs/moment";
export function certificationsWatcher(conf: Conf) {
const URL_PATH = '/wot/certifiers-of/'
return async (confCerts: ConfCertifications) => {
function getSubjectTitle(state: string) {
return `State ${state} certifications of ${confCerts.name}`
}
let state = 'INIT'
return urlWatcher(conf, async (data) => {
const json = data as {
certifications: { uid: string, cert_time: { medianTime: number } }[]
}
const now = Math.floor(Date.now() / 1000)
const expiresSoon = json.certifications.filter((c:any) => {
const diff = now - c.cert_time.medianTime
return diff < confCerts.mustLongAtLeast
})
const remainingDays = Math.floor(confCerts.mustLongAtLeast / (3600 * 24))
const willExpireMessage = `${expiresSoon.length} certifications are going to expire in less than ${remainingDays.toFixed(0)} days`
if (json.certifications.length < confCerts.mustRemainAtLeast) {
state = 'ERROR'
return UrlWatcherResult.ko(`Not enough certifications`)
} else {
if (json.certifications.length - expiresSoon.length < confCerts.mustRemainAtLeast) {
state = 'WARNING'
return UrlWatcherResult.ko(willExpireMessage)
}
}
state = 'OK'
return UrlWatcherResult.ok(`${json.certifications.length} valid [${willExpireMessage}]`)
})({
name: `certifications of ${confCerts.pubkey.substr(0, 6)} (${confCerts.name})`,
address: confCerts.address + URL_PATH + confCerts.pubkey,
frequency: confCerts.frequency
},
() => getSubjectTitle('OK'),
() => getSubjectTitle(state),
() => getSubjectTitle('RECOVERED'))
}
}