Fix formatting issues in MonitorType.ts,

MonitorCriteriaInstance.ts, ApiMonitor.ts,
CriteriaFilter.tsx, MonitorStep.tsx,
MonitorService.ts, WebsiteMonitor.ts, and
PingMonitor.ts
This commit is contained in:
Simon Larsen 2023-11-22 16:40:19 +00:00
parent b375e8341d
commit 61d3604a9d
No known key found for this signature in database
GPG Key ID: AB45983AA9C81CDE
11 changed files with 87 additions and 86 deletions

View File

@ -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 = {

View File

@ -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');

View File

@ -69,7 +69,7 @@ export class MonitorTypeHelper {
title: 'Port',
description:
'This monitor types lets you monitor any TCP or UDP port.',
}
},
];
return monitorTypeProps;

View File

@ -151,7 +151,7 @@ export class Service extends DatabaseService<Model> {
(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(

View File

@ -44,7 +44,7 @@ const CriteriaFilterElement: FunctionComponent<ComponentProps> = (
if (
props.monitorType === MonitorType.Ping ||
props.monitorType === MonitorType.IP ||
props.monitorType === MonitorType.IP ||
props.monitorType === MonitorType.Port
) {
options = options.filter((i: DropdownOption) => {

View File

@ -219,7 +219,7 @@ const MonitorStepElement: FunctionComponent<ComponentProps> = (
<Input
initialValue={monitorStep?.data?.monitorDestinationPort?.toString()}
onChange={(value: string) => {
const port = new Port(value);
const port: Port = new Port(value);
monitorStep.setPort(port);
setMonitorStep(
MonitorStep.clone(monitorStep)

View File

@ -104,7 +104,7 @@ const MonitorStepElement: FunctionComponent<ComponentProps> = (
'Port of the resource you would like us to ping.',
fieldType: FieldType.Port,
placeholder: 'No port entered',
}
},
];
} else if (props.monitorType === MonitorType.IP) {
fields = [

View File

@ -95,12 +95,11 @@ export default class ApiMonitor {
isOnlineCheckRequest?: boolean | undefined;
}
): Promise<APIResponse | null> {
if(!options){
if (!options) {
options = {};
}
if(options?.currentRetryCount === undefined){
if (options?.currentRetryCount === undefined) {
options.currentRetryCount = 1;
}

View File

@ -77,15 +77,14 @@ export default class PingMonitor {
host: Hostname | IPv4 | IPv6 | URL,
pingOptions?: PingOptions
): Promise<PingResponse | null> {
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;

View File

@ -99,7 +99,6 @@ export default class PortMonitor {
port: Port,
pingOptions?: PingOptions
): Promise<PortMonitorResponse | null> {
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<PositiveNumber> = 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;

View File

@ -97,16 +97,14 @@ export default class WebsiteMonitor {
isOnlineCheckRequest?: boolean | undefined;
}
): Promise<ProbeWebsiteResponse | null> {
if(!options){
if (!options) {
options = {};
}
if(options?.currentRetryCount === undefined){
if (options?.currentRetryCount === undefined) {
options.currentRetryCount = 1;
}
let requestType: HTTPMethod = HTTPMethod.GET;
if (options.isHeadRequest) {