From 2a08583c03967e243b85ae813326e6ce46c453b3 Mon Sep 17 00:00:00 2001 From: joseph le Date: Thu, 7 Sep 2023 10:24:56 +0700 Subject: [PATCH] catch exclude error, format message --- app/store/logs/20230907-test-code.log | 21 ++++++ app/store/logs/abc.log | 3 +- app/utils/runtimeCheckLogs.ts | 97 ++++++++++++++++----------- manage-view/src/api/apiLog.js | 4 +- start/routes.ts | 16 ++++- 5 files changed, 97 insertions(+), 44 deletions(-) create mode 100644 app/store/logs/20230907-test-code.log diff --git a/app/store/logs/20230907-test-code.log b/app/store/logs/20230907-test-code.log new file mode 100644 index 0000000..b579e7d --- /dev/null +++ b/app/store/logs/20230907-test-code.log @@ -0,0 +1,21 @@ +fdgdfg +fksdfk trace ak +Power Failure +ERROR: % Incomplete command +ERROR: % Incomplete command +sdfhskd ERROR PVDM3- invalid |IOSXE_RP_NV-3-NV_ACCESS_FAIL|IOSXE_RP_NV-3- shjkg +sdfhskd ERROR PVDM3- invalid |IOSXE_RP_NV-3-NV_ACCESS_FAIL|IOSXE_RP_NV-3- shjkg +invalid |IOSXE_RP_NV-3-NV_ACCESS_FAIL|IOSXE_RP_NV-3- shjkg +invalid |IOSXE_RP_NV-3-NV_ACCESS_FAIL|IOSXE_RP_NV-3- shjkg +invalid |IOSXE_RP_NV-3-NV_ACCESS_FAIL|IOSXE_RP_NV-3- shjkg +invalid |IOSXE_RP_NV-3-NV_ACCESS_FAIL|IOSXE_RP_NV-3- shjkg +sdfhskd ERROR PVDM3- invalid |IOSXE_RP_NV-3-NV_ACCESS_FAIL|IOSXE_RP_NV-3- shjkg +invalid |IOSXE_RP_NV-3-NV_ACCESS_FAIL|IOSXE_RP_NV-3- shjkg +invalid |IOSXE_RP_NV-3-NV_ACCESS_FAIL|IOSXE_RP_NV-3- shjkg +invalid |IOSXE_RP_NV-3-NV_ACCESS_FAIL|IOSXE_RP_NV-3- shjkg +invalid |IOSXE_RP_NV-3-NV_ACCESS_FAIL|IOSXE_RP_NV-3- shjkg +invalid |IOSXE_RP_NV-3-NV_ACCESS_FAIL|IOSXE_RP_NV-3- shj +invalid |IOSXE_RP_NV-3-NV_ACCESS_FAIL|IOSXE_RP_NV-3- shj +invalid |IOSXE_RP_NV-3-NV_ACCESS_FAIL|IOSXE_RP_NV-3- shj pak_debug_init sdh +abc +123 \ No newline at end of file diff --git a/app/store/logs/abc.log b/app/store/logs/abc.log index 0bbe53a..ab492a8 100644 --- a/app/store/logs/abc.log +++ b/app/store/logs/abc.log @@ -11,4 +11,5 @@ WS-X68 WS-X68 yellow SM- -WS-X68 \ No newline at end of file +WS-X68 +fksdfk trace ak \ No newline at end of file diff --git a/app/utils/runtimeCheckLogs.ts b/app/utils/runtimeCheckLogs.ts index d5e9a8c..b2a6dc4 100644 --- a/app/utils/runtimeCheckLogs.ts +++ b/app/utils/runtimeCheckLogs.ts @@ -13,7 +13,7 @@ export async function runtimeCheckLogs(folderPath) { try { let fileList = Array(); let fileList_old = Array(); - let listKeyValues = await KeyValue.all(); + // console.log(listKeyValues.map(obj=>obj.$original.value)) // Function to update the list of files async function updateFileList() { @@ -41,20 +41,22 @@ export async function runtimeCheckLogs(folderPath) { folderWatcher.on("add", async (filePath) => { //import log new file + // console.log(filePath) if ( + //Sua \\ --> / filePath?.split(".")[filePath.split(".").length - 1] === "log" && - filePath.split("/")[filePath.split("/").length - 1]?.split("-")[0] === + filePath.split("\\")[filePath.split("\\").length - 1]?.split("-")[0] === moment(Date.now()).format("YYYYMMDD").toString() ) { console.log("New file added: ", filePath); - await LogDetectFile.firstOrCreate( - { file_name: filePath.split("/")[filePath.split("/").length - 1] }, - { file_name: filePath.split("/")[filePath.split("/").length - 1] } - ); // await LogDetectFile.firstOrCreate( - // { file_name: filePath.split("\\")[filePath.split("\\").length - 1] }, - // { file_name: filePath.split("\\")[filePath.split("\\").length - 1] } + // { file_name: filePath.split("/")[filePath.split("/").length - 1] }, + // { file_name: filePath.split("/")[filePath.split("/").length - 1] } // ); + await LogDetectFile.firstOrCreate( + { file_name: filePath.split("\\")[filePath.split("\\").length - 1] }, + { file_name: filePath.split("\\")[filePath.split("\\").length - 1] } + ); fileList_old = fileList; updateFileList(); @@ -77,7 +79,7 @@ export async function runtimeCheckLogs(folderPath) { const filePath = `${folderPath}/${fileName}`; const scan = fs.watchFile( filePath, - { interval: 300000 }, + { interval: 5000 }, async (eventType) => { //check special item, extra RAM, error in log let lines = []; @@ -85,11 +87,26 @@ export async function runtimeCheckLogs(folderPath) { .readFileSync(filePath) .toString() ?.split("\n"); + let allValue = await KeyValue.all(); + let listKeyValues = allValue.filter( + (i) => + i.$original.key === "MODEL_SPECIAL" || + i.$original.key === "CATCH_FAULTY" + ); + + let listExcludeErr = allValue + .filter((i) => i.$original.key === "EXCLUDE_ERR") + .map((obj) => obj.$original.value); + content.map((line, index) => { listKeyValues .map((obj) => obj.$original.value) .map(async (value) => { - if (line.search(value) !== -1) { + if ( + line.search(value) !== -1 && + listExcludeErr.filter((err) => line.includes(err)) + .length === 0 + ) { // let keyWord = ""; // line.split(" ").map((word) => { // if (word.toLocaleLowerCase().includes(value)) { @@ -118,10 +135,10 @@ export async function runtimeCheckLogs(folderPath) { }); lines.push(index + 1); } - // } } }); }); + // }, 5000); //true: import log to log_report table, send report to Zulip setTimeout(async () => { if (lines.length === 0) { @@ -141,7 +158,7 @@ export async function runtimeCheckLogs(folderPath) { fileDetect?.id_ldf ); - let listReport = await getListLineByItem(logsDetect[0]) + let listReport = await getListLineByItem(logsDetect[0]); let content = "| |Last updated at | Item/error | Line | Report \n|---|:---:|:---:|:---|:-----------:\n"; let spoiler = ""; @@ -200,23 +217,23 @@ export async function runtimeCheckLogs(folderPath) { // spoiler = spoiler + "\n```\n\n"; // } else { - listReport.map((log, index) => { - content = - content + - "|" + - (index + 1) + - "|**" + - moment(log.created_at).format("HH:mm - DD/MM") + - "**|" + - log.detected_content+ - "|" + - log.line.sort((a, b) => a - b).join(", ") + - // "|" + - // file[log.line - 1] + - "|[View](https://logs.danielvu.com/api/log/showLog/" + - fileName + - ")\n"; - }); + listReport.map((log, index) => { + content = + content + + "|" + + (index + 1) + + "|**" + + moment(log.created_at).format("HH:mm - DD/MM") + + "**|" + + log.detected_content + + "|" + + log.line.sort((a, b) => a - b).join(", ") + + // "|" + + // file[log.line - 1] + + "|[View](https://logs.danielvu.com/api/log/showLog/" + + fileName + + ")\n"; + }); // } // setTimeout(() => { sendMessToZulip( @@ -231,17 +248,17 @@ export async function runtimeCheckLogs(folderPath) { spoiler ); - sendMessToZulip( - "stream", - "AUS_VN_Test", - "Test Log Alerts", - ":warning: :warning: **" + - fileName + - "**\n\n" + - content + - "\n\n" + - spoiler - ); + // sendMessToZulip( + // "stream", + // "AUS_VN_Test", + // "Test Log Alerts", + // ":warning: :warning: **" + + // fileName + + // "**\n\n" + + // content + + // "\n\n" + + // spoiler + // ); // }, 3000); } }, 3000); diff --git a/manage-view/src/api/apiLog.js b/manage-view/src/api/apiLog.js index f9fb736..fa3f208 100644 --- a/manage-view/src/api/apiLog.js +++ b/manage-view/src/api/apiLog.js @@ -1,5 +1,5 @@ -const API = "https://logs.danielvu.com/api"; - +// const API = "https://logs.danielvu.com/api"; +const API = "http://localhost:3333/api"; export const getKeyValues = API + "/getKeyValue"; export const deleteValue = API + "/deleteValue"; export const addKeyValue = API + "/addValue"; \ No newline at end of file diff --git a/start/routes.ts b/start/routes.ts index f83d102..0b2c7fa 100644 --- a/start/routes.ts +++ b/start/routes.ts @@ -26,6 +26,7 @@ import Env from "@ioc:Adonis/Core/Env"; import KeyValue from "App/Models/KeyValue"; import ErpsController from "App/Controllers/Http/ErpsController"; import LogReport from "App/Models/LogReport"; +import Database from "@ioc:Adonis/Lucid/Database"; runtimeCheckLogs(Env.get("FOLDER_LOGS")); @@ -50,6 +51,19 @@ Route.post("/api/account/checkLogin", "UsersController.checkLogin"); //Log Route.get("/api/log/showLog/:name?", async ({ request, response }) => { try { + let fileDetect = await LogDetectFile.findBy( + "file_name", + request.params().name + ); + let logsDetect = await Database.rawQuery( + "select * from log_reports where id_file = " + + fileDetect?.id_ldf + ); + + let listLine = logsDetect[0].map(obj=>obj.line).filter((value, index, self) => { + return self.indexOf(value) === index; + }) + const content = await axios.get( "http://192.168.5.7:8080/" + request.params().name ); @@ -103,7 +117,7 @@ Route.get("/api/log/showLog/:name?", async ({ request, response }) => { border-color: -internal-light-dark(rgb(118, 118, 118), rgb(133, 133, 133));\ border-image: initial;\ padding: 2px;'>" + - data.join("\n") + + "Detected lines in file: "+listLine.join(",")+"\n\n"+data.join("\n") + "\ \ "