From 61d3604a9d0ae3ade96c9a570427f08164264ae8 Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Wed, 22 Nov 2023 16:40:19 +0000 Subject: [PATCH] Fix formatting issues in MonitorType.ts, MonitorCriteriaInstance.ts, ApiMonitor.ts, CriteriaFilter.tsx, MonitorStep.tsx, MonitorService.ts, WebsiteMonitor.ts, and PingMonitor.ts --- .../Types/Monitor/MonitorCriteriaInstance.ts | 2 +- Common/Types/Monitor/MonitorStep.ts | 20 +-- Common/Types/Monitor/MonitorType.ts | 2 +- CommonServer/Services/MonitorService.ts | 2 +- .../Form/Monitor/CriteriaFilter.tsx | 2 +- .../Components/Form/Monitor/MonitorStep.tsx | 2 +- .../Monitor/MonitorSteps/MonitorStep.tsx | 2 +- .../Utils/Monitors/MonitorTypes/ApiMonitor.ts | 5 +- .../Monitors/MonitorTypes/PingMonitor.ts | 7 +- .../Monitors/MonitorTypes/PortMonitor.ts | 123 +++++++++--------- .../Monitors/MonitorTypes/WebsiteMonitor.ts | 6 +- 11 files changed, 87 insertions(+), 86 deletions(-) diff --git a/Common/Types/Monitor/MonitorCriteriaInstance.ts b/Common/Types/Monitor/MonitorCriteriaInstance.ts index 0c462692da..fec432db96 100644 --- a/Common/Types/Monitor/MonitorCriteriaInstance.ts +++ b/Common/Types/Monitor/MonitorCriteriaInstance.ts @@ -103,7 +103,7 @@ export default class MonitorCriteriaInstance extends DatabaseProperty { if ( arg.monitorType === MonitorType.Ping || - arg.monitorType === MonitorType.IP || + arg.monitorType === MonitorType.IP || arg.monitorType === MonitorType.Port ) { monitorCriteriaInstance.data = { diff --git a/Common/Types/Monitor/MonitorStep.ts b/Common/Types/Monitor/MonitorStep.ts index b0bb3bced0..a16bc6c41c 100644 --- a/Common/Types/Monitor/MonitorStep.ts +++ b/Common/Types/Monitor/MonitorStep.ts @@ -156,8 +156,10 @@ export default class MonitorStep extends DatabaseProperty { return 'Request Type is required'; } - - if(monitorType === MonitorType.Port && !value.data.monitorDestinationPort) { + if ( + monitorType === MonitorType.Port && + !value.data.monitorDestinationPort + ) { return 'Port is required'; } @@ -172,7 +174,9 @@ export default class MonitorStep extends DatabaseProperty { id: this.data.id, monitorDestination: this.data?.monitorDestination?.toJSON() || undefined, - monitorDestinationPort: this.data?.monitorDestinationPort?.toJSON() || undefined, + monitorDestinationPort: + this.data?.monitorDestinationPort?.toJSON() || + undefined, monitorCriteria: this.data.monitorCriteria.toJSON(), requestType: this.data.requestType, requestHeaders: this.data.requestHeaders || undefined, @@ -212,8 +216,6 @@ export default class MonitorStep extends DatabaseProperty { ); } - - if ( json && json['monitorDestination'] && @@ -236,9 +238,11 @@ export default class MonitorStep extends DatabaseProperty { ); } - - const monitorDestinationPort: Port | undefined = json['monitorDestinationPort'] ? Port.fromJSON(json['monitorDestinationPort'] as JSONObject) : undefined; - + const monitorDestinationPort: Port | undefined = json[ + 'monitorDestinationPort' + ] + ? Port.fromJSON(json['monitorDestinationPort'] as JSONObject) + : undefined; if (!json['monitorCriteria']) { throw new BadDataException('Invalid monitor criteria'); diff --git a/Common/Types/Monitor/MonitorType.ts b/Common/Types/Monitor/MonitorType.ts index 6bcaa1fb01..d1fe9afdaf 100644 --- a/Common/Types/Monitor/MonitorType.ts +++ b/Common/Types/Monitor/MonitorType.ts @@ -69,7 +69,7 @@ export class MonitorTypeHelper { title: 'Port', description: 'This monitor types lets you monitor any TCP or UDP port.', - } + }, ]; return monitorTypeProps; diff --git a/CommonServer/Services/MonitorService.ts b/CommonServer/Services/MonitorService.ts index 8a9ba8aacd..282a0818df 100644 --- a/CommonServer/Services/MonitorService.ts +++ b/CommonServer/Services/MonitorService.ts @@ -151,7 +151,7 @@ export class Service extends DatabaseService { (createdItem.monitorType === MonitorType.API || createdItem.monitorType === MonitorType.Website || createdItem.monitorType === MonitorType.Ping || - createdItem.monitorType === MonitorType.IP || + createdItem.monitorType === MonitorType.IP || createdItem.monitorType === MonitorType.Port) ) { await this.addDefaultProbesToMonitor( diff --git a/Dashboard/src/Components/Form/Monitor/CriteriaFilter.tsx b/Dashboard/src/Components/Form/Monitor/CriteriaFilter.tsx index 3007695679..9fd3fa4b12 100644 --- a/Dashboard/src/Components/Form/Monitor/CriteriaFilter.tsx +++ b/Dashboard/src/Components/Form/Monitor/CriteriaFilter.tsx @@ -44,7 +44,7 @@ const CriteriaFilterElement: FunctionComponent = ( if ( props.monitorType === MonitorType.Ping || - props.monitorType === MonitorType.IP || + props.monitorType === MonitorType.IP || props.monitorType === MonitorType.Port ) { options = options.filter((i: DropdownOption) => { diff --git a/Dashboard/src/Components/Form/Monitor/MonitorStep.tsx b/Dashboard/src/Components/Form/Monitor/MonitorStep.tsx index 7a29ca5bd2..79c07cfddd 100644 --- a/Dashboard/src/Components/Form/Monitor/MonitorStep.tsx +++ b/Dashboard/src/Components/Form/Monitor/MonitorStep.tsx @@ -219,7 +219,7 @@ const MonitorStepElement: FunctionComponent = ( { - const port = new Port(value); + const port: Port = new Port(value); monitorStep.setPort(port); setMonitorStep( MonitorStep.clone(monitorStep) diff --git a/Dashboard/src/Components/Monitor/MonitorSteps/MonitorStep.tsx b/Dashboard/src/Components/Monitor/MonitorSteps/MonitorStep.tsx index 80ef30eaeb..110ec68f65 100644 --- a/Dashboard/src/Components/Monitor/MonitorSteps/MonitorStep.tsx +++ b/Dashboard/src/Components/Monitor/MonitorSteps/MonitorStep.tsx @@ -104,7 +104,7 @@ const MonitorStepElement: FunctionComponent = ( 'Port of the resource you would like us to ping.', fieldType: FieldType.Port, placeholder: 'No port entered', - } + }, ]; } else if (props.monitorType === MonitorType.IP) { fields = [ diff --git a/Probe/Utils/Monitors/MonitorTypes/ApiMonitor.ts b/Probe/Utils/Monitors/MonitorTypes/ApiMonitor.ts index 05192211ed..b74d1feb70 100644 --- a/Probe/Utils/Monitors/MonitorTypes/ApiMonitor.ts +++ b/Probe/Utils/Monitors/MonitorTypes/ApiMonitor.ts @@ -95,12 +95,11 @@ export default class ApiMonitor { isOnlineCheckRequest?: boolean | undefined; } ): Promise { - - if(!options){ + if (!options) { options = {}; } - if(options?.currentRetryCount === undefined){ + if (options?.currentRetryCount === undefined) { options.currentRetryCount = 1; } diff --git a/Probe/Utils/Monitors/MonitorTypes/PingMonitor.ts b/Probe/Utils/Monitors/MonitorTypes/PingMonitor.ts index c1f783d2e6..d1cf6dbd3d 100644 --- a/Probe/Utils/Monitors/MonitorTypes/PingMonitor.ts +++ b/Probe/Utils/Monitors/MonitorTypes/PingMonitor.ts @@ -77,15 +77,14 @@ export default class PingMonitor { host: Hostname | IPv4 | IPv6 | URL, pingOptions?: PingOptions ): Promise { - - if(!pingOptions){ + if (!pingOptions) { pingOptions = {}; } - if(pingOptions?.currentRetryCount === undefined){ + if (pingOptions?.currentRetryCount === undefined) { pingOptions.currentRetryCount = 1; } - + let hostAddress: string = ''; if (host instanceof Hostname) { hostAddress = host.hostname; diff --git a/Probe/Utils/Monitors/MonitorTypes/PortMonitor.ts b/Probe/Utils/Monitors/MonitorTypes/PortMonitor.ts index 57779bceb2..e27c10d9cf 100644 --- a/Probe/Utils/Monitors/MonitorTypes/PortMonitor.ts +++ b/Probe/Utils/Monitors/MonitorTypes/PortMonitor.ts @@ -99,7 +99,6 @@ export default class PortMonitor { port: Port, pingOptions?: PingOptions ): Promise { - if (!pingOptions) { pingOptions = {}; } @@ -130,77 +129,79 @@ export default class PortMonitor { } logger.info( - `Pinging host: ${pingOptions?.monitorId?.toString()} ${hostAddress}:${port.toString()} - Retry: ${pingOptions?.currentRetryCount + `Pinging host: ${pingOptions?.monitorId?.toString()} ${hostAddress}:${port.toString()} - Retry: ${ + pingOptions?.currentRetryCount }` ); try { // Ping a host with port - const promiseResult = new Promise((resolve, reject) => { - const startTime: [number, number] = process.hrtime(); + const promiseResult: Promise = new Promise( + (resolve: Function, reject: Function) => { + const startTime: [number, number] = process.hrtime(); - const socket = new net.Socket(); + const socket: net.Socket = new net.Socket(); - const timeout: number = pingOptions?.timeout?.toNumber() || 5000; + const timeout: number = + pingOptions?.timeout?.toNumber() || 5000; - socket.setTimeout(timeout); + socket.setTimeout(timeout); - if (!port) { - throw new BadDataException('Port is not specified'); + if (!port) { + throw new BadDataException('Port is not specified'); + } + + let hasPromiseResolved: boolean = false; + + socket.connect(port.toNumber(), hostAddress, () => { + const endTime: [number, number] = + process.hrtime(startTime); + const responseTimeInMS: PositiveNumber = + new PositiveNumber( + (endTime[0] * 1000000000 + endTime[1]) / 1000000 + ); + + logger.info( + `Pinging host ${pingOptions?.monitorId?.toString()} ${hostAddress}:${port!.toString()} success: Response Time ${responseTimeInMS} ms` + ); + + socket.destroy(); // Close the connection after success + if (!hasPromiseResolved) { + resolve(responseTimeInMS); + } + + hasPromiseResolved = true; + return; + }); + + socket.on('timeout', () => { + socket.destroy(); + logger.info('Ping timeout'); + + if (!hasPromiseResolved) { + reject(new UnableToReachServer('Ping timeout')); + } + + hasPromiseResolved = true; + return; + }); + + socket.on('error', (error: Error) => { + socket.destroy(); + logger.info( + 'Could not connect to: ' + host + ':' + port + ); + + if (!hasPromiseResolved) { + reject(error); + } + + hasPromiseResolved = true; + return; + }); } - - - let hasPromiseResolved: boolean = false; - - socket.connect(port.toNumber(), hostAddress, () => { - - - const endTime: [number, number] = process.hrtime(startTime); - const responseTimeInMS: PositiveNumber = new PositiveNumber( - (endTime[0] * 1000000000 + endTime[1]) / 1000000 - ); - - logger.info( - `Pinging host ${pingOptions?.monitorId?.toString()} ${hostAddress}:${port!.toString()} success: Response Time ${responseTimeInMS} ms` - ); - - socket.destroy(); // Close the connection after success - if (!hasPromiseResolved) { - resolve(responseTimeInMS); - } - - hasPromiseResolved = true; - return; - - }); - - socket.on('timeout', () => { - socket.destroy(); - logger.info('Ping timeout'); - - - if (!hasPromiseResolved) { - reject(new UnableToReachServer('Ping timeout')); - } - - hasPromiseResolved = true; - return; - - }); - - socket.on('error', error => { - socket.destroy(); - logger.info('Could not connect to: ' + host + ':' + port); - - if (!hasPromiseResolved) { - reject(error); - } - - hasPromiseResolved = true; - return; - }); - }); + ); const responseTimeInMS: PositiveNumber = (await promiseResult) as PositiveNumber; diff --git a/Probe/Utils/Monitors/MonitorTypes/WebsiteMonitor.ts b/Probe/Utils/Monitors/MonitorTypes/WebsiteMonitor.ts index cbdc555f5b..2e25372407 100644 --- a/Probe/Utils/Monitors/MonitorTypes/WebsiteMonitor.ts +++ b/Probe/Utils/Monitors/MonitorTypes/WebsiteMonitor.ts @@ -97,16 +97,14 @@ export default class WebsiteMonitor { isOnlineCheckRequest?: boolean | undefined; } ): Promise { - - if(!options){ + if (!options) { options = {}; } - if(options?.currentRetryCount === undefined){ + if (options?.currentRetryCount === undefined) { options.currentRetryCount = 1; } - let requestType: HTTPMethod = HTTPMethod.GET; if (options.isHeadRequest) {