From b6971755189152a68bf0a02bad1db3da47cf8ce8 Mon Sep 17 00:00:00 2001 From: joseph le Date: Wed, 4 Oct 2023 10:30:17 +0700 Subject: [PATCH] change runtimeCheckLog --- app/utils/runtimeCheckLogs.ts | 200 +++++++++++++++++----------------- 1 file changed, 97 insertions(+), 103 deletions(-) diff --git a/app/utils/runtimeCheckLogs.ts b/app/utils/runtimeCheckLogs.ts index db4d466..e9f3132 100644 --- a/app/utils/runtimeCheckLogs.ts +++ b/app/utils/runtimeCheckLogs.ts @@ -134,7 +134,7 @@ export async function runtimeCheckLogs(folderPath) { //get index SN and send to ERP - // checkIndexSN(contentFile, lastLine, fileName); + checkIndexSN(contentFile, lastLine, fileName); //get list item to check let listKeyValues = allValue.filter( @@ -156,9 +156,9 @@ export async function runtimeCheckLogs(folderPath) { console.log(contentFile.length) //Process file content - if(contentFile.length>30000){ - for (let i = 0; i < contentFile.length; i += 50000) { - const chunk = contentFile.slice(i, i + 50000); + if(contentFile.length>50000){ + for (let i = 0; i < contentFile.length; i += 1000) { + const chunk = contentFile.slice(i, i + 1000); chunk.map((line, index) => { //check line the line with errors and exclude errors @@ -182,11 +182,11 @@ export async function runtimeCheckLogs(folderPath) { ); if (checkLog?.length === 0) { - // await LogReport.create({ - // detected_content: value, - // line: index + 1, - // id_file: log?.id_ldf, - // }); + await LogReport.create({ + detected_content: value, + line: index + 1, + id_file: log?.id_ldf, + }); lines.push(index + 1); } } @@ -218,11 +218,11 @@ export async function runtimeCheckLogs(folderPath) { ); if (checkLog?.length === 0) { - // await LogReport.create({ - // detected_content: value, - // line: index + 1, - // id_file: log?.id_ldf, - // }); + await LogReport.create({ + detected_content: value, + line: index + 1, + id_file: log?.id_ldf, + }); lines.push(index + 1); } } @@ -233,101 +233,95 @@ export async function runtimeCheckLogs(folderPath) { } //true: import log to log_report table, send report to Zulip - // setTimeout(async () => { - // if (lines.length === 0) { - // console.log(`${fileName} has changed ---Good`); - // } else { - // console.log( - // `${fileName} has changed ---SOS---${lines.length}` - // ); - // let allReport_new = await LogReport.query() - // .whereRaw(`DATE(created_at) = ?`, [today]) + setTimeout(async () => { + if (lines.length === 0) { + console.log(`${fileName} has changed ---Good`); + } else { + console.log( + `${fileName} has changed ---SOS---${lines.length}` + ); + let allReport_new = await LogReport.query() + .whereRaw(`DATE(created_at) = ?`, [today]) - // let fileDetect = allFile?.filter( - // (i) => i.$original.file_name === fileName - // )[0]; + let fileDetect = allFile?.filter( + (i) => i.$original.file_name === fileName + )[0]; - // let logsDetect = allReport_new?.filter( - // (i) => i.$original.id_file === fileDetect?.id_ldf - // ); - // // console.log(logsDetect) - // // await Database.rawQuery( - // // "select * from log_reports where id_file = " + - // // fileDetect?.id_ldf - // // ); - // //Get all report newest - // let listReport = await getListLineByItem( - // logsDetect - // .map((obj) => obj.$original) - // .filter((l) => l.line > lastLine) - // ); + let logsDetect = allReport_new?.filter( + (i) => i.$original.id_file === fileDetect?.id_ldf + ); + // console.log(logsDetect) + // await Database.rawQuery( + // "select * from log_reports where id_file = " + + // fileDetect?.id_ldf + // ); + //Get all report newest + let listReport = await getListLineByItem( + logsDetect + .map((obj) => obj.$original) + .filter((l) => l.line > lastLine) + ); - // let content = - // "| |Last updated at | Item/error | Line | Report \n|---|:---:|:---|:---|:-----------:\n"; - // let spoiler = ""; - // let issueFound = ""; + let content = + "| |Last updated at | Item/error | Line | Report \n|---|:---:|:---|:---|:-----------:\n"; + let spoiler = ""; + let issueFound = ""; - // listReport.map((log, index) => { - // let item = listExtraItem.includes(log.detected_content) - // ? ":medal: **" + log.detected_content + "**" - // : ":small_orange_diamond: " + log.detected_content; + listReport.map((log, index) => { + let item = listExtraItem.includes(log.detected_content) + ? ":medal: **" + log.detected_content + "**" + : ":small_orange_diamond: " + log.detected_content; - // log.line?.map((line) => { - // issueFound = - // issueFound + - // "\n`" + - // line + - // "` " + - // contentFile[line - 1]?.replace( - // log.detected_content, - // "[" + - // log.detected_content + - // "](https://logs.danielvu.com/logs/" + - // fileName + - // "#" + - // line + - // ")" - // ); - // }); - // content = - // content + - // "|" + - // (index + 1) + - // "|**" + - // moment(Date.now()).format("HH:mm - DD/MM") + - // "**|" + - // item + - // "|" + - // log.line + - // "|[View](https://logs.danielvu.com/logs/" + - // fileName + - // "#" + - // log.line + - // ")\n"; - // }); + log.line?.map((line) => { + issueFound = + issueFound + + "\n`" + + line + + "` " + + contentFile[line - 1]?.replace( + log.detected_content, + "[" + + log.detected_content + + "](https://logs.danielvu.com/logs/" + + fileName + + "#" + + line + + ")" + ); + }); + content = + content + + "|" + + (index + 1) + + "|**" + + moment(Date.now()).format("HH:mm - DD/MM") + + "**|" + + item + + "|" + + log.line + + "|[View](https://logs.danielvu.com/logs/" + + fileName + + "#" + + log.line + + ")\n"; + }); - // sendMessToZulip( - // "stream", - // Env.get("ZULIP_STREAM_ALERT"), - // Env.get("ZULIP_TOPIC_ALERT"), - // "------------\n\n:warning: :warning: **" + - // fileName + - // "**\n\n" + - // content + - // "\n\n" + - // spoiler + - // "\n\n***Issue found:***\n" + - // issueFound - // ); - // } - // }, 3000); - - - - - - - console.log(path + " change") + sendMessToZulip( + "stream", + Env.get("ZULIP_STREAM_ALERT"), + Env.get("ZULIP_TOPIC_ALERT"), + "------------\n\n:warning: :warning: **" + + fileName + + "**\n\n" + + content + + "\n\n" + + spoiler + + "\n\n***Issue found:***\n" + + issueFound + ); + } + }, 3000) + // console.log(path + " change") }); // await fileList.slice(0,40) // ?.filter((i) => fileList_old.includes(i) === false)