|
@@ -1,9 +1,10 @@
|
|
|
import { RouterOptions, json } from 'express';
|
|
import { RouterOptions, json } from 'express';
|
|
|
|
|
|
|
|
-import { ServiceCheckData } from '../../../common/lib/http-check-data.module';
|
|
|
|
|
|
|
+import { HttpCheckData, HttpCheckStatus, ServiceCheckData, ServiceCheckDataEntry } from '../../../common/lib/http-check-data.module';
|
|
|
import { HttpStatusException } from '../../../common/lib/http-status.exception';
|
|
import { HttpStatusException } from '../../../common/lib/http-status.exception';
|
|
|
|
|
|
|
|
import { ControllerPool } from '../ctrl/controller-pool.interface';
|
|
import { ControllerPool } from '../ctrl/controller-pool.interface';
|
|
|
|
|
+import { HealthCheckDataProvider } from '../ctrl/health-check-data-provider.interface';
|
|
|
import { ServiceChangedStatus } from '../lib/service-changed-status.enum';
|
|
import { ServiceChangedStatus } from '../lib/service-changed-status.enum';
|
|
|
import { WebHandler } from './web-handler.base';
|
|
import { WebHandler } from './web-handler.base';
|
|
|
|
|
|
|
@@ -124,6 +125,19 @@ export class ServicesAPIHandler extends WebHandler {
|
|
|
next(err);
|
|
next(err);
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
|
|
+
|
|
|
|
|
+ this.router.post('/test', async (req, res, next) => {
|
|
|
|
|
+ try {
|
|
|
|
|
+ const config = req.body as HttpCheckConfig;
|
|
|
|
|
+ const mockDB = new HealthCheckDatabaseMock(config);
|
|
|
|
|
+
|
|
|
|
|
+ await this.ctrlPool.httpChecks.runCheck(config, mockDB);
|
|
|
|
|
+
|
|
|
|
|
+ res.send(mockDB.log);
|
|
|
|
|
+ } catch (err) {
|
|
|
|
|
+ next(err);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private validateNumber(id: string, field: string) {
|
|
private validateNumber(id: string, field: string) {
|
|
@@ -136,3 +150,44 @@ export class ServicesAPIHandler extends WebHandler {
|
|
|
return num;
|
|
return num;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+class HealthCheckDatabaseMock implements HealthCheckDataProvider {
|
|
|
|
|
+ public log: HttpCheckData[] = [];
|
|
|
|
|
+
|
|
|
|
|
+ constructor(private config: HttpCheckConfig) {}
|
|
|
|
|
+
|
|
|
|
|
+ async open() {}
|
|
|
|
|
+
|
|
|
|
|
+ async getHttpCheckConfigByID(serverID: number, configID: number) {
|
|
|
|
|
+ return this.config;
|
|
|
|
|
+ }
|
|
|
|
|
+ async insertHealthCheckData(confID: number, time: Date, status: HttpCheckStatus, message: string) {
|
|
|
|
|
+ const logEntry = { configId: confID, id: new Date().getTime(), time, status, message };
|
|
|
|
|
+ this.log.push(logEntry);
|
|
|
|
|
+ return logEntry;
|
|
|
|
|
+ }
|
|
|
|
|
+ async getLastErrors(confID: number, threshold: number) {
|
|
|
|
|
+ if (this.log.length === 0) return [];
|
|
|
|
|
+
|
|
|
|
|
+ const mapByTimestamp = new Map<number, ServiceCheckDataEntry[]>();
|
|
|
|
|
+ mapByTimestamp.set(this.log[0].time.getTime(), this.log);
|
|
|
|
|
+
|
|
|
|
|
+ const errors: ServiceCheckData[] = [];
|
|
|
|
|
+ for (const entry of mapByTimestamp.entries()) {
|
|
|
|
|
+ const time = entry[0];
|
|
|
|
|
+ const data = entry[1];
|
|
|
|
|
+
|
|
|
|
|
+ const errorData = data.filter(d => d.status !== HttpCheckStatus.OK);
|
|
|
|
|
+ if (!errorData.length) break;
|
|
|
|
|
+
|
|
|
|
|
+ errors.push({
|
|
|
|
|
+ time: new Date(time),
|
|
|
|
|
+ data: errorData
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return errors;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async close() {}
|
|
|
|
|
+}
|