import $ from "jquery" import { SystemInfoDescription, SystemInfo as ModelSystemInfo, HostnameLiveSystemInfoDescription } from "../../src/models/systemInfo"; import { HostnameServiceDescription } from "../../src/models/service"; export interface TimedLiveSystemInfo { time: Date; data: HostnameLiveSystemInfoDescription; } export namespace DAL { export class SystemInfo { private static lastCachedData: SystemInfoDescription|null = null; private static liveData: TimedLiveSystemInfo[] = []; private static liveHandler: ReturnType|boolean = false; private static getData(): Promise { return new Promise(ok => { $.get("/api/sysinfo", response => { this.lastCachedData = response; ok(); }); }); } private static getDataWithCache(): Promise { if (this.lastCachedData) return Promise.resolve(); return this.getData(); } public static async getInfo(hostname: string): Promise { await this.getDataWithCache(); return this.lastCachedData?.systemInfo[hostname]; } public static async listHosts(): Promise> { await this.getDataWithCache(); return Object.keys(this.lastCachedData?.systemInfo ?? {}); } private static pushLiveData(data: HostnameLiveSystemInfoDescription): TimedLiveSystemInfo { let result: TimedLiveSystemInfo = { data: data, time: new Date() }; this.liveData.push(result); return result; } private static pollNow(): Promise { return new Promise(ok => { $.get("/api/sysinfo/live", response => { ok(this.pushLiveData(response.liveSystemInfo)); }); }); } private static async pollTick(): Promise { let result = await this.pollNow(); this.liveHandler = setTimeout(this.pollTick.bind(this), 10000); return result; } public static async startLivePolling(): Promise { if (this.liveHandler !== false) return this.liveData[this.liveData.length-1]!; this.liveHandler = true; await this.getDataWithCache(); return await this.pollTick(); } public static async getLive(): Promise { return await this.startLivePolling(); } public static async getServices(): Promise { return new Promise(ok => { $.get("/api/services/list", response => { ok(response.services); }); }); } } }