|
@@ -6,6 +6,7 @@ import { Database as SQLiteDB, OPEN_CREATE, OPEN_READWRITE } from 'sqlite3';
|
|
|
|
|
|
|
|
import { ServiceConfig, validateParamType } from '../../../common/interfaces/service-config.interface';
|
|
import { ServiceConfig, validateParamType } from '../../../common/interfaces/service-config.interface';
|
|
|
import { Logger } from '../../../common/util/logger.class';
|
|
import { Logger } from '../../../common/util/logger.class';
|
|
|
|
|
+import { arrayDiff } from '../../../common/util/object-utils';
|
|
|
|
|
|
|
|
import { DBMigration } from './db-migration.class';
|
|
import { DBMigration } from './db-migration.class';
|
|
|
import { SQLiteController } from './sqlite-controller.base';
|
|
import { SQLiteController } from './sqlite-controller.base';
|
|
@@ -224,7 +225,7 @@ export class Database extends SQLiteController {
|
|
|
return result.rows.map(r => ({ time: new Date(r.Timegroup), avg: r.avg, peak: r.peak, max: r.max }));
|
|
return result.rows.map(r => ({ time: new Date(r.Timegroup), avg: r.avg, peak: r.peak, max: r.max }));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- async getHealthCheckConfigs(serverID: number) {
|
|
|
|
|
|
|
+ private async getHealthCheckConfigs(serverID: number) {
|
|
|
const res = await this.stmt(
|
|
const res = await this.stmt(
|
|
|
`SELECT
|
|
`SELECT
|
|
|
HealthCheckConfig.*,
|
|
HealthCheckConfig.*,
|
|
@@ -238,7 +239,73 @@ export class Database extends SQLiteController {
|
|
|
[serverID]
|
|
[serverID]
|
|
|
);
|
|
);
|
|
|
|
|
|
|
|
- return res.rows.reduce((res: ServiceConfig[], line, i) => {
|
|
|
|
|
|
|
+ return this.configFromResultRows(res.rows);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public async getHttpCheckConfigs(serverID: number) {
|
|
|
|
|
+ return (await this.getHealthCheckConfigs(serverID)).map(this.httpCheckConfigFrom);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private async getHealtCheckConfigByID(serverID: number, configID: number) {
|
|
|
|
|
+ if (!serverID && !configID) return null;
|
|
|
|
|
+
|
|
|
|
|
+ const res = await this.stmt(
|
|
|
|
|
+ `SELECT
|
|
|
|
|
+ HealthCheckConfig.*,
|
|
|
|
|
+ HealthCheckParams.Type as '_ParamType',
|
|
|
|
|
+ HealthCheckParams.Key as '_ParamKey',
|
|
|
|
|
+ HealthCheckParams.Value as '_ParamValue'
|
|
|
|
|
+ FROM HealthCheckConfig
|
|
|
|
|
+ LEFT OUTER JOIN HealthCheckParams ON HealthCheckConfig.ID = HealthCheckParams.ConfigID
|
|
|
|
|
+ WHERE HealtCheckConfig.ID = ?
|
|
|
|
|
+ AND HealthCheckConfig.ServerID = ?
|
|
|
|
|
+ ORDER BY HealthCheckConfig.Title, _ParamType, _ParamKey`,
|
|
|
|
|
+ [configID, serverID]
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ if (!res.rows.length) return null;
|
|
|
|
|
+
|
|
|
|
|
+ const configs = this.configFromResultRows(res.rows);
|
|
|
|
|
+
|
|
|
|
|
+ return configs[0];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public async getHttpCheckConfigByID(serverID: number, configID: number) {
|
|
|
|
|
+ return this.httpCheckConfigFrom(await this.getHealtCheckConfigByID(serverID, configID));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public async saveHttpCheckConfig(serverID: number, conf: HttpCheckConfig) {
|
|
|
|
|
+ conf.serverId = serverID;
|
|
|
|
|
+
|
|
|
|
|
+ const oldConf = await this.getHttpCheckConfigByID(serverID, conf.id);
|
|
|
|
|
+ await this.beginTransaction();
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (oldConf) {
|
|
|
|
|
+ // UPDATE
|
|
|
|
|
+ if (oldConf.title !== conf.title) {
|
|
|
|
|
+ await this.stmt('UPDATE HealthCheckConfig SET Title = ?', [conf.title]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const sql = `UPDATE HealthCheckParams SET Value = ? WHERE ConfigID = ? AND Key = ?;`;
|
|
|
|
|
+
|
|
|
|
|
+ const updValues: any[][] = [];
|
|
|
|
|
+ if (oldConf.interval !== conf.interval) updValues.push([conf.interval, conf.id, 'interval']);
|
|
|
|
|
+ if (oldConf.url !== conf.url) updValues.push([conf.url, conf.id, 'url']);
|
|
|
|
|
+ const checksDiff = arrayDiff(oldConf.checks, conf.checks);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // INSERT
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ this.commit();
|
|
|
|
|
+ return conf;
|
|
|
|
|
+ } catch (err) {
|
|
|
|
|
+ this.rollback();
|
|
|
|
|
+ throw err;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private configFromResultRows(rows: any[]) {
|
|
|
|
|
+ return rows.reduce((res: ServiceConfig[], line, i) => {
|
|
|
const configID = line['ID'];
|
|
const configID = line['ID'];
|
|
|
let config: ServiceConfig;
|
|
let config: ServiceConfig;
|
|
|
if (i === 0 || res[res.length - 1].id !== configID) {
|
|
if (i === 0 || res[res.length - 1].id !== configID) {
|
|
@@ -256,13 +323,13 @@ export class Database extends SQLiteController {
|
|
|
if (!!line['_ParamKey']) {
|
|
if (!!line['_ParamKey']) {
|
|
|
const type = validateParamType(line['_ParamType']);
|
|
const type = validateParamType(line['_ParamType']);
|
|
|
const key = line['_ParamKey'];
|
|
const key = line['_ParamKey'];
|
|
|
- if (type === 'check') {
|
|
|
|
|
- let checkParam = config.params.find(c => c.type === 'check');
|
|
|
|
|
|
|
+ if (key === 'check') {
|
|
|
|
|
+ let checkParam = config.params.find(c => c.key === 'check');
|
|
|
if (!checkParam) {
|
|
if (!checkParam) {
|
|
|
config.params.push(
|
|
config.params.push(
|
|
|
(checkParam = {
|
|
(checkParam = {
|
|
|
- key: 'regexp',
|
|
|
|
|
- type: 'check',
|
|
|
|
|
|
|
+ key: 'check',
|
|
|
|
|
+ type: 'regexp',
|
|
|
value: []
|
|
value: []
|
|
|
})
|
|
})
|
|
|
);
|
|
);
|
|
@@ -280,4 +347,19 @@ export class Database extends SQLiteController {
|
|
|
return res;
|
|
return res;
|
|
|
}, [] as ServiceConfig[]);
|
|
}, [] as ServiceConfig[]);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ private httpCheckConfigFrom(hcConf: ServiceConfig | null): HttpCheckConfig | null {
|
|
|
|
|
+ if (!hcConf) return null;
|
|
|
|
|
+ const params = {
|
|
|
|
|
+ url: hcConf.params?.find(p => p.key === 'url')?.value as string,
|
|
|
|
|
+ interval: hcConf.params?.find(p => p.key === 'interval')?.value as number,
|
|
|
|
|
+ checks: hcConf.params?.reduce((res, p) => (p.key === 'check' && Array.isArray(p.value) ? [...res, ...p.value] : res), [] as string[])
|
|
|
|
|
+ };
|
|
|
|
|
+ return {
|
|
|
|
|
+ id: hcConf.id,
|
|
|
|
|
+ title: hcConf.title,
|
|
|
|
|
+ type: hcConf.type,
|
|
|
|
|
+ ...params
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|