This commit is contained in:
Simon Larsen
2023-05-12 12:29:51 +01:00
parent d9724cfd37
commit 74cc556f7c
6 changed files with 58 additions and 6 deletions

View File

@@ -29,6 +29,7 @@ export default class OneUptimeDate {
}
public static getSecondsTo(date: Date): number {
date = this.fromString(date);
const dif: number = date.getTime() - this.getCurrentDate().getTime();
const Seconds_from_T1_to_T2: number = dif / 1000;
return Math.abs(Seconds_from_T1_to_T2);
@@ -45,10 +46,12 @@ export default class OneUptimeDate {
}
public static timezoneOffsetDate(date: Date): Date {
date = this.fromString(date);
return this.addRemoveMinutes(date, date.getTimezoneOffset());
}
public static toDateTimeLocalString(date: Date): string {
date = this.fromString(date);
const ten: Function = (i: number): string => {
return (i < 10 ? '0' : '') + i;
},
@@ -63,6 +66,7 @@ export default class OneUptimeDate {
}
public static addRemoveMinutes(date: Date, minutes: number): Date {
date = this.fromString(date);
return moment(date).add(minutes, 'minutes').toDate();
}
@@ -95,6 +99,7 @@ export default class OneUptimeDate {
date: Date,
days: PositiveNumber | number
): Date {
date = this.fromString(date);
if (!(days instanceof PositiveNumber)) {
days = new PositiveNumber(days);
}
@@ -107,6 +112,7 @@ export default class OneUptimeDate {
date: Date,
days: PositiveNumber | number
): Date {
date = this.fromString(date);
if (!(days instanceof PositiveNumber)) {
days = new PositiveNumber(days);
}
@@ -117,6 +123,7 @@ export default class OneUptimeDate {
date: Date,
days: PositiveNumber | number
): Date {
date = this.fromString(date);
if (!(days instanceof PositiveNumber)) {
days = new PositiveNumber(days);
}
@@ -214,6 +221,8 @@ export default class OneUptimeDate {
}
public static getGreaterDate(a: Date, b: Date): Date {
a = this.fromString(a);
b = this.fromString(b);
if (this.isAfter(a, b)) {
return a;
}
@@ -222,6 +231,8 @@ export default class OneUptimeDate {
}
public static getLesserDate(a: Date, b: Date): Date {
a = this.fromString(a);
b = this.fromString(b);
if (this.isBefore(a, b)) {
return a;
}
@@ -230,6 +241,8 @@ export default class OneUptimeDate {
}
public static getSecondsBetweenDates(start: Date, end: Date): number {
start = this.fromString(start);
end = this.fromString(end);
const duration: moment.Duration = moment.duration(
moment(end).diff(moment(start))
);
@@ -291,10 +304,12 @@ export default class OneUptimeDate {
}
public static getStartOfDay(date: Date): Date {
date = this.fromString(date);
return moment(date).startOf('day').toDate();
}
public static getEndOfDay(date: Date): Date {
date = this.fromString(date);
return moment(date).endOf('day').toDate();
}
@@ -303,18 +318,27 @@ export default class OneUptimeDate {
startDate: Date,
endDate: Date
): boolean {
date = this.fromString(date);
startDate = this.fromString(startDate);
endDate = this.fromString(endDate);
return moment(date).isBetween(startDate, endDate);
}
public static isAfter(date: Date, startDate: Date): boolean {
date = this.fromString(date);
startDate = this.fromString(startDate);
return moment(date).isAfter(startDate);
}
public static hasExpired(expiratinDate: Date): boolean {
expiratinDate = this.fromString(expiratinDate);
return !moment(this.getCurrentDate()).isBefore(expiratinDate);
}
public static isBefore(date: Date, endDate: Date): boolean {
date = this.fromString(date);
endDate = this.fromString(endDate);
return moment(date).isBefore(endDate);
}
@@ -326,6 +350,8 @@ export default class OneUptimeDate {
date: string | Date,
onlyShowDate?: boolean
): string {
date = this.fromString(date);
let formatstring: string = 'MMM DD YYYY, HH:mm';
if (onlyShowDate) {
@@ -343,6 +369,9 @@ export default class OneUptimeDate {
date: string | Date,
onlyShowDate?: boolean
): string {
date = this.fromString(date);
let formatstring: string = 'MMM DD YYYY, HH:mm';
if (onlyShowDate) {
@@ -363,23 +392,32 @@ export default class OneUptimeDate {
}
public static getDateString(date: Date): string {
date = this.fromString(date);
return this.getDateAsLocalFormattedString(date, true);
}
public static isInThePast(date: string | Date): boolean {
date = this.fromString(date);
return moment(date).isBefore(new Date());
}
public static isInTheFuture(date: string | Date): boolean {
date = this.fromString(date);
return moment(date).isAfter(new Date());
}
public static fromString(date: string | JSONObject): Date {
public static fromString(date: string | JSONObject | Date): Date {
if (date instanceof Date) {
return date;
}
if (typeof date === 'string') {
return moment(date).toDate();
}
if (date && date['value'] && typeof date['value'] === 'string') {
if (date && date['value'] && typeof date['value'] === 'string' && date['_type'] && (date['_type'] === 'Date' || date['_type'] === 'DateTime')) {
return moment(date['value']).toDate();
}
@@ -387,6 +425,7 @@ export default class OneUptimeDate {
}
public static asDateForDatabaseQuery(date: string | Date): string {
date = this.fromString(date);
const formatstring: string = 'YYYY-MM-DD';
return moment(date).local().format(formatstring);
}

View File

@@ -23,7 +23,7 @@ export const dataSourceOptions: DataSourceOptions = {
migrationsTableName: 'migrations',
migrations: Migrations,
entities: Entities,
//logging: 'all',
// logging: 'all',
// synchronize: Env === AppEnvironment.Development,
synchronize: true,
};

View File

@@ -36,6 +36,8 @@ RunCron(
Monitor
);
const monitoringPromises: Array<Promise<void>> = [];
for (const monitor of monitors) {

View File

@@ -12,17 +12,21 @@ import { JSONObject } from 'Common/Types/JSON';
import WebsiteMonitor, { ProbeWebsiteResponse } from './MonitorTypes/WebsiteMonitor';
import ApiMonitor, { APIResponse } from './MonitorTypes/ApiMonitor';
import JSONFunctions from 'Common/Types/JSONFunctions';
import logger from 'CommonServer/Utils/Logger';
export default class MonitorUtil {
public static async probeMonitor(
monitor: Monitor
): Promise<Array<ProbeMonitorResponse>> {
const results: Array<ProbeMonitorResponse> = [];
if (
!monitor.monitorSteps ||
monitor.monitorSteps.data?.monitorStepsInstanceArray.length === 0
) {
logger.info('No monitor steps found');
return [];
}
@@ -32,6 +36,8 @@ export default class MonitorUtil {
continue;
}
console.log("Monitor destination: "+monitorStep.data?.monitorDestination?.toString())
const result: ProbeMonitorResponse = await this.probeMonitorStep(
monitorStep,
monitor

View File

@@ -4,6 +4,7 @@ import PositiveNumber from 'Common/Types/PositiveNumber';
import Protocol from 'Common/Types/API/Protocol';
import WebsiteRequest, { WebsiteResponse } from 'Common/Types/WebsiteRequest';
import HTML from 'Common/Types/Html';
import logger from 'CommonServer/Utils/Logger';
export interface ProbeWebsiteResponse {
url: URL;
@@ -27,8 +28,6 @@ export default class WebsiteMonitor {
(endTime[0] * 1000000000 + endTime[1]) / 1000000
);
console.log('Website monitor result');
console.log(responseTimeInMS);
return {
url: url,
@@ -41,6 +40,9 @@ export default class WebsiteMonitor {
responseHeaders: result.responseHeaders,
};
} catch (err) {
logger.error(err);
return {
url: url,
isOnline: false,

View File

@@ -82,6 +82,9 @@ router.post(
// update the lastMonitoredAt field of the monitors
for (const monitorProbe of monitorProbes) {
if (!monitorProbe.monitor) {
continue;
}
@@ -105,7 +108,7 @@ router.post(
return monitorProbe.monitor!;
})
.filter((monitor: Monitor) => {
return Boolean(monitor);
return Boolean(monitor._id);
});
// return the list of monitors to be monitored