From 60000968836526c929d58ba6e45c483e3bb57159 Mon Sep 17 00:00:00 2001 From: dongfang <1136005348@qq.com> Date: Thu, 6 Feb 2025 02:20:40 +0000 Subject: [PATCH 01/11] Add monitor fields --- Server/db/models/Monitor.js | 14 ++++++++++++++ Server/validation/joi.js | 6 ++++++ 2 files changed, 20 insertions(+) diff --git a/Server/db/models/Monitor.js b/Server/db/models/Monitor.js index 90cbb1d62..eb5f67c1b 100644 --- a/Server/db/models/Monitor.js +++ b/Server/db/models/Monitor.js @@ -38,6 +38,20 @@ const MonitorSchema = mongoose.Schema( "distributed_http", ], }, + jsonPath: { + type: String, + }, + expectedValue: { + type: String, + }, + matchMethod: { + type: String, + enum: [ + "equal", + "include", + "regex", + ], + }, url: { type: String, required: true, diff --git a/Server/validation/joi.js b/Server/validation/joi.js index 42c41d8a5..b60022fc5 100644 --- a/Server/validation/joi.js +++ b/Server/validation/joi.js @@ -194,6 +194,9 @@ const createMonitorBodyValidation = joi.object({ }), notifications: joi.array().items(joi.object()), secret: joi.string(), + jsonPath: joi.string(), + expectedValue: joi.string(), + matchMethod: joi.string(), }); const editMonitorBodyValidation = joi.object({ @@ -202,6 +205,9 @@ const editMonitorBodyValidation = joi.object({ interval: joi.number(), notifications: joi.array().items(joi.object()), secret: joi.string(), + jsonPath: joi.string(), + expectedValue: joi.string(), + matchMethod: joi.string(), }); const pauseMonitorParamValidation = joi.object({ From b7a8596b7b435fb45c12632142183108e07067b8 Mon Sep 17 00:00:00 2001 From: dongfang <1136005348@qq.com> Date: Thu, 6 Feb 2025 03:36:52 +0000 Subject: [PATCH 02/11] Add jmespath denpendency --- Server/package-lock.json | 10 ++++++++++ Server/package.json | 1 + 2 files changed, 11 insertions(+) diff --git a/Server/package-lock.json b/Server/package-lock.json index 4b6ef1a2b..cb6cfc11b 100644 --- a/Server/package-lock.json +++ b/Server/package-lock.json @@ -19,6 +19,7 @@ "handlebars": "^4.7.8", "helmet": "^8.0.0", "ioredis": "^5.4.2", + "jmespath": "^0.16.0", "joi": "^17.13.1", "jsonwebtoken": "9.0.2", "mailersend": "^2.2.0", @@ -4441,6 +4442,15 @@ "@pkgjs/parseargs": "^0.11.0" } }, + "node_modules/jmespath": { + "version": "0.16.0", + "resolved": "https://registry.npmjs.org/jmespath/-/jmespath-0.16.0.tgz", + "integrity": "sha512-9FzQjJ7MATs1tSpnco1K6ayiYE3figslrXA72G2HQ/n76RzvYlofyi5QM+iX4YRs/pu3yzxlVQSST23+dMDknw==", + "license": "Apache-2.0", + "engines": { + "node": ">= 0.6.0" + } + }, "node_modules/joi": { "version": "17.13.3", "resolved": "https://registry.npmjs.org/joi/-/joi-17.13.3.tgz", diff --git a/Server/package.json b/Server/package.json index 6695a444a..735a12048 100644 --- a/Server/package.json +++ b/Server/package.json @@ -26,6 +26,7 @@ "handlebars": "^4.7.8", "helmet": "^8.0.0", "ioredis": "^5.4.2", + "jmespath": "^0.16.0", "joi": "^17.13.1", "jsonwebtoken": "9.0.2", "mailersend": "^2.2.0", From 81a0ec8b23b8f96fcb2aea7db6625ff4d0cf1829 Mon Sep 17 00:00:00 2001 From: dongfang <1136005348@qq.com> Date: Thu, 6 Feb 2025 03:38:05 +0000 Subject: [PATCH 03/11] Replace variable declarations with destructuring --- Server/service/networkService.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Server/service/networkService.js b/Server/service/networkService.js index a1cb64ff2..120fa5b2a 100644 --- a/Server/service/networkService.js +++ b/Server/service/networkService.js @@ -122,20 +122,19 @@ class NetworkService { */ async requestHttp(job) { try { - const url = job.data.url; + const { url, secret, _id, teamId, type, jsonPath, matchMethod, expectedValue } = job.data; const config = {}; - job.data.secret !== undefined && - (config.headers = { Authorization: `Bearer ${job.data.secret}` }); + secret !== undefined && (config.headers = { Authorization: `Bearer ${secret}` }); const { response, responseTime, error } = await this.timeRequest(() => this.axios.get(url, config) ); const httpResponse = { - monitorId: job.data._id, - teamId: job.data.teamId, - type: job.data.type, + monitorId: _id, + teamId, + type, responseTime, payload: response?.data, }; From 8b496033823ca40742fa4845183d4eb8093bed78 Mon Sep 17 00:00:00 2001 From: dongfang <1136005348@qq.com> Date: Thu, 6 Feb 2025 03:38:36 +0000 Subject: [PATCH 04/11] Support jsonPath and expectedValue --- Server/service/networkService.js | 51 +++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/Server/service/networkService.js b/Server/service/networkService.js index 120fa5b2a..9a661f104 100644 --- a/Server/service/networkService.js +++ b/Server/service/networkService.js @@ -1,3 +1,5 @@ +import jmespath from 'jmespath'; +import { errorMessages, successMessages } from "../utils/messages.js"; const SERVICE_NAME = "NetworkService"; const UPROCK_ENDPOINT = "https://api.uprock.com/checkmate/push"; @@ -146,8 +148,55 @@ class NetworkService { httpResponse.message = this.http.STATUS_CODES[code] || "Network Error"; return httpResponse; } - httpResponse.status = true; + httpResponse.code = response.status; + + if (expectedValue) { + // validate if response data match expected value + let result = response?.data; + + this.logger.info({ + service: this.SERVICE_NAME, + method: "requestHttp", + message: "match result with expected value", + details: { expectedValue, result, jsonPath, matchMethod } + }); + + if (jsonPath) { + const contentType = response.headers['content-type']; + + if (contentType && contentType.includes('application/json')) { + try { + result = jmespath.search(result, jsonPath).toString(); + } catch (error) { + httpResponse.status = false; + httpResponse.message = "JSONPath Search Error"; + return httpResponse; + } + } else { + httpResponse.status = false; + httpResponse.message = "Response Not JSON"; + return httpResponse; + } + } + + if (!result) { + httpResponse.status = false; + httpResponse.message = "Empty Result"; + return httpResponse; + } + + let match; + if (matchMethod === "include") match = result.includes(expectedValue); + else if (matchMethod === "regex") match = new RegExp(expectedValue).test(result); + else match = result === expectedValue; + + httpResponse.status = match; + httpResponse.message = match ? "Match" : "Not Match"; + return httpResponse; + } + + httpResponse.status = true; httpResponse.message = this.http.STATUS_CODES[response.status]; return httpResponse; } catch (error) { From b263e2746cf506fe8ebfd0121e493e890522f19f Mon Sep 17 00:00:00 2001 From: dongfang <1136005348@qq.com> Date: Thu, 6 Feb 2025 03:46:06 +0000 Subject: [PATCH 05/11] Convert result to string --- Server/service/networkService.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Server/service/networkService.js b/Server/service/networkService.js index 9a661f104..362e43db3 100644 --- a/Server/service/networkService.js +++ b/Server/service/networkService.js @@ -167,7 +167,7 @@ class NetworkService { if (contentType && contentType.includes('application/json')) { try { - result = jmespath.search(result, jsonPath).toString(); + result = jmespath.search(result, jsonPath); } catch (error) { httpResponse.status = false; httpResponse.message = "JSONPath Search Error"; @@ -180,13 +180,14 @@ class NetworkService { } } - if (!result) { + if (result === null || result === undefined) { httpResponse.status = false; httpResponse.message = "Empty Result"; return httpResponse; } let match; + result = result.toString(); if (matchMethod === "include") match = result.includes(expectedValue); else if (matchMethod === "regex") match = new RegExp(expectedValue).test(result); else match = result === expectedValue; From 05e448c4c5b1d3993f6638f3ab7dec9bbd4f9a1a Mon Sep 17 00:00:00 2001 From: dongfang <1136005348@qq.com> Date: Thu, 6 Feb 2025 06:28:29 +0000 Subject: [PATCH 06/11] Add job name and id in log --- Server/service/networkService.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Server/service/networkService.js b/Server/service/networkService.js index 362e43db3..ef035be31 100644 --- a/Server/service/networkService.js +++ b/Server/service/networkService.js @@ -124,7 +124,7 @@ class NetworkService { */ async requestHttp(job) { try { - const { url, secret, _id, teamId, type, jsonPath, matchMethod, expectedValue } = job.data; + const { url, secret, _id, name, teamId, type, jsonPath, matchMethod, expectedValue } = job.data; const config = {}; secret !== undefined && (config.headers = { Authorization: `Bearer ${secret}` }); @@ -158,13 +158,13 @@ class NetworkService { this.logger.info({ service: this.SERVICE_NAME, method: "requestHttp", - message: "match result with expected value", + message: `Job: [${name}](${_id}) match result with expected value`, details: { expectedValue, result, jsonPath, matchMethod } }); - + if (jsonPath) { const contentType = response.headers['content-type']; - + if (contentType && contentType.includes('application/json')) { try { result = jmespath.search(result, jsonPath); @@ -191,7 +191,7 @@ class NetworkService { if (matchMethod === "include") match = result.includes(expectedValue); else if (matchMethod === "regex") match = new RegExp(expectedValue).test(result); else match = result === expectedValue; - + httpResponse.status = match; httpResponse.message = match ? "Match" : "Not Match"; return httpResponse; From 9fe2de8969933eb2a01847e9bf749e1357afeaf1 Mon Sep 17 00:00:00 2001 From: dongfang <1136005348@qq.com> Date: Thu, 6 Feb 2025 06:39:32 +0000 Subject: [PATCH 07/11] Use JSON.stringify instead of toString --- Server/service/networkService.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Server/service/networkService.js b/Server/service/networkService.js index ef035be31..3ceeef743 100644 --- a/Server/service/networkService.js +++ b/Server/service/networkService.js @@ -187,7 +187,7 @@ class NetworkService { } let match; - result = result.toString(); + result = typeof result === "object" ? JSON.stringify(result) : result.toString(); if (matchMethod === "include") match = result.includes(expectedValue); else if (matchMethod === "regex") match = new RegExp(expectedValue).test(result); else match = result === expectedValue; From 4d12608a220ba242f33619045f71f1f297eef9cb Mon Sep 17 00:00:00 2001 From: dongfang <1136005348@qq.com> Date: Mon, 10 Feb 2025 09:08:54 +0000 Subject: [PATCH 08/11] return early instead of nesting if/else blocks --- Server/service/networkService.js | 91 ++++++++++++++++---------------- 1 file changed, 46 insertions(+), 45 deletions(-) diff --git a/Server/service/networkService.js b/Server/service/networkService.js index 3ceeef743..3a2bddb8a 100644 --- a/Server/service/networkService.js +++ b/Server/service/networkService.js @@ -151,54 +151,55 @@ class NetworkService { httpResponse.code = response.status; - if (expectedValue) { - // validate if response data match expected value - let result = response?.data; - - this.logger.info({ - service: this.SERVICE_NAME, - method: "requestHttp", - message: `Job: [${name}](${_id}) match result with expected value`, - details: { expectedValue, result, jsonPath, matchMethod } - }); - - if (jsonPath) { - const contentType = response.headers['content-type']; - - if (contentType && contentType.includes('application/json')) { - try { - result = jmespath.search(result, jsonPath); - } catch (error) { - httpResponse.status = false; - httpResponse.message = "JSONPath Search Error"; - return httpResponse; - } - } else { - httpResponse.status = false; - httpResponse.message = "Response Not JSON"; - return httpResponse; - } - } - - if (result === null || result === undefined) { - httpResponse.status = false; - httpResponse.message = "Empty Result"; - return httpResponse; - } - - let match; - result = typeof result === "object" ? JSON.stringify(result) : result.toString(); - if (matchMethod === "include") match = result.includes(expectedValue); - else if (matchMethod === "regex") match = new RegExp(expectedValue).test(result); - else match = result === expectedValue; - - httpResponse.status = match; - httpResponse.message = match ? "Match" : "Not Match"; + if (!expectedValue) { + // not configure expected value, return + httpResponse.status = true; + httpResponse.message = this.http.STATUS_CODES[response.status]; return httpResponse; } - httpResponse.status = true; - httpResponse.message = this.http.STATUS_CODES[response.status]; + // validate if response data match expected value + let result = response?.data; + + this.logger.info({ + service: this.SERVICE_NAME, + method: "requestHttp", + message: `Job: [${name}](${_id}) match result with expected value`, + details: { expectedValue, result, jsonPath, matchMethod } + }); + + if (jsonPath) { + const contentType = response.headers['content-type']; + + if (contentType && contentType.includes('application/json')) { + try { + result = jmespath.search(result, jsonPath); + } catch (error) { + httpResponse.status = false; + httpResponse.message = "JSONPath Search Error"; + return httpResponse; + } + } else { + httpResponse.status = false; + httpResponse.message = "Response Not JSON"; + return httpResponse; + } + } + + if (result === null || result === undefined) { + httpResponse.status = false; + httpResponse.message = "Empty Result"; + return httpResponse; + } + + let match; + result = typeof result === "object" ? JSON.stringify(result) : result.toString(); + if (matchMethod === "include") match = result.includes(expectedValue); + else if (matchMethod === "regex") match = new RegExp(expectedValue).test(result); + else match = result === expectedValue; + + httpResponse.status = match; + httpResponse.message = match ? "Match" : "Not Match"; return httpResponse; } catch (error) { error.service = this.SERVICE_NAME; From c3d92ec812a6942bbefc51e0b41dd6ebbdcb5e34 Mon Sep 17 00:00:00 2001 From: dongfang <1136005348@qq.com> Date: Wed, 12 Feb 2025 03:05:58 +0000 Subject: [PATCH 09/11] return early if not json --- Server/service/networkService.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Server/service/networkService.js b/Server/service/networkService.js index 3a2bddb8a..741b69bef 100644 --- a/Server/service/networkService.js +++ b/Server/service/networkService.js @@ -1,5 +1,4 @@ import jmespath from 'jmespath'; -import { errorMessages, successMessages } from "../utils/messages.js"; const SERVICE_NAME = "NetworkService"; const UPROCK_ENDPOINT = "https://api.uprock.com/checkmate/push"; @@ -171,19 +170,20 @@ class NetworkService { if (jsonPath) { const contentType = response.headers['content-type']; - if (contentType && contentType.includes('application/json')) { - try { - result = jmespath.search(result, jsonPath); - } catch (error) { - httpResponse.status = false; - httpResponse.message = "JSONPath Search Error"; - return httpResponse; - } - } else { + const isJson = contentType?.includes('application/json'); + if (!isJson) { httpResponse.status = false; httpResponse.message = "Response Not JSON"; return httpResponse; } + + try { + result = jmespath.search(result, jsonPath); + } catch (error) { + httpResponse.status = false; + httpResponse.message = "JSONPath Search Error"; + return httpResponse; + } } if (result === null || result === undefined) { From 6837adb5aa3e8fd9dbf84f3b94759caf983a76f1 Mon Sep 17 00:00:00 2001 From: dongfang <1136005348@qq.com> Date: Wed, 12 Feb 2025 03:12:33 +0000 Subject: [PATCH 10/11] add locales for http request --- Server/locales/en.json | 8 +++++++- Server/service/stringService.js | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/Server/locales/en.json b/Server/locales/en.json index ba50e1d40..8657deb73 100644 --- a/Server/locales/en.json +++ b/Server/locales/en.json @@ -144,5 +144,11 @@ "monitorResume": "Monitor resumed successfully", "statusPageDelete": "Status page deleted successfully", "statusPageUpdate": "Status page updated successfully", - "statusPageByTeamId": "Got status pages by team id successfully" + "statusPageByTeamId": "Got status pages by team id successfully", + "httpNetworkError": "Network error", + "httpNotJson": "Response data is not json", + "httpJsonPathError": "Failed to parse json data", + "httpEmptyResult": "Result is empty", + "httpMatchSuccess": "Response data match successfully", + "httpMatchFail": "Failed to match response data" } \ No newline at end of file diff --git a/Server/service/stringService.js b/Server/service/stringService.js index a65e1655b..1ef208d6c 100644 --- a/Server/service/stringService.js +++ b/Server/service/stringService.js @@ -321,6 +321,30 @@ class StringService { return this.translationService.getTranslation('getAppSettings'); } + get httpNetworkError() { + return this.translationService.getTranslation('httpNetworkError'); + } + + get httpNotJson() { + return this.translationService.getTranslation('httpNotJson'); + } + + get httpJsonPathError() { + return this.translationService.getTranslation('httpJsonPathError'); + } + + get httpEmptyResult() { + return this.translationService.getTranslation('httpEmptyResult'); + } + + get httpMatchSuccess() { + return this.translationService.getTranslation('httpMatchSuccess'); + } + + get httpMatchFail() { + return this.translationService.getTranslation('httpMatchFail'); + } + get updateAppSettings() { return this.translationService.getTranslation('updateAppSettings'); } From 1f0876dd7198ec59e9f5dadf310e56a1c84395bd Mon Sep 17 00:00:00 2001 From: dongfang <1136005348@qq.com> Date: Wed, 12 Feb 2025 03:16:07 +0000 Subject: [PATCH 11/11] use stringService in requestHttp --- Server/service/networkService.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Server/service/networkService.js b/Server/service/networkService.js index 741b69bef..89dfe85d1 100644 --- a/Server/service/networkService.js +++ b/Server/service/networkService.js @@ -144,7 +144,7 @@ class NetworkService { const code = error.response?.status || this.NETWORK_ERROR; httpResponse.code = code; httpResponse.status = false; - httpResponse.message = this.http.STATUS_CODES[code] || "Network Error"; + httpResponse.message = this.http.STATUS_CODES[code] || this.stringService.httpNetworkError; return httpResponse; } @@ -173,7 +173,7 @@ class NetworkService { const isJson = contentType?.includes('application/json'); if (!isJson) { httpResponse.status = false; - httpResponse.message = "Response Not JSON"; + httpResponse.message = this.stringService.httpNotJson; return httpResponse; } @@ -181,14 +181,14 @@ class NetworkService { result = jmespath.search(result, jsonPath); } catch (error) { httpResponse.status = false; - httpResponse.message = "JSONPath Search Error"; + httpResponse.message = this.stringService.httpJsonPathError; return httpResponse; } } if (result === null || result === undefined) { httpResponse.status = false; - httpResponse.message = "Empty Result"; + httpResponse.message = this.stringService.httpEmptyResult; return httpResponse; } @@ -199,7 +199,7 @@ class NetworkService { else match = result === expectedValue; httpResponse.status = match; - httpResponse.message = match ? "Match" : "Not Match"; + httpResponse.message = match ? this.stringService.httpMatchSuccess : this.stringService.httpMatchFail; return httpResponse; } catch (error) { error.service = this.SERVICE_NAME;