| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- 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<typeof setTimeout>|boolean = false;
- private static getData(): Promise<void> {
- return new Promise(ok => {
- $.get("/api/sysinfo", response => {
- this.lastCachedData = response;
- ok();
- });
- });
- }
- private static getDataWithCache(): Promise<void> {
- if (this.lastCachedData)
- return Promise.resolve();
- return this.getData();
- }
- public static async getInfo(hostname: string): Promise<ModelSystemInfo|undefined> {
- await this.getDataWithCache();
- return this.lastCachedData?.systemInfo[hostname];
- }
- public static async listHosts(): Promise<Array<string>> {
- 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<TimedLiveSystemInfo> {
- return new Promise(ok => {
- $.get("/api/sysinfo/live", response => {
- ok(this.pushLiveData(response.liveSystemInfo));
- });
- });
- }
- private static async pollTick(): Promise<TimedLiveSystemInfo> {
- let result = await this.pollNow();
- this.liveHandler = setTimeout(this.pollTick.bind(this), 10000);
- return result;
- }
- public static async startLivePolling(): Promise<TimedLiveSystemInfo> {
- 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<TimedLiveSystemInfo> {
- return await this.startLivePolling();
- }
- public static async getServices(): Promise<HostnameServiceDescription> {
- return new Promise(ok => {
- $.get("/api/services/list", response => {
- ok(response.services);
- });
- });
- }
- }
- }
|