change runtimeCheckLog

This commit is contained in:
joseph le 2023-10-04 08:48:57 +07:00
parent 49ad47f89b
commit ef07a6af47
1 changed files with 152 additions and 175 deletions

View File

@ -103,198 +103,175 @@ export async function runtimeCheckLogs(folderPath) {
// fs.watchFile(filePath,{ interval: 15000 }, // fs.watchFile(filePath,{ interval: 15000 },
// async (eventType) => { // async (eventType) => {
//check special item, extra RAM, error in log //check special item, extra RAM, error in log
if (path === filePath) { // if (path === filePath) {
let lines = []; // let lines = [];
let allFile = await LogDetectFile.all(); // let allFile = await LogDetectFile.all();
let allReport = await LogReport.all(); // let allReport = await LogReport.all();
let allValue = await KeyValue.all(); // let allValue = await KeyValue.all();
//get information file // //get information file
let fileDetect = allFile?.filter( // let fileDetect = allFile?.filter(
(i) => i.$original.file_name === fileName // (i) => i.$original.file_name === fileName
)[0]; // )[0];
// await LogDetectFile.findBy( // let logsDetect = allReport?.filter(
// "file_name", // (i) => i.$original.id_file === fileDetect?.id_ldf
// fileName // );
// ); // //get the last line detected
//get all log of file // let lastLine = Math.max(...logsDetect.map((obj) => obj.line));
// let logsDetect = await Database.rawQuery(
// "select * from log_reports where id_file = " +
// fileDetect?.id_ldf
// );
let logsDetect = allReport?.filter(
(i) => i.$original.id_file === fileDetect?.id_ldf
);
//get the last line detected
let lastLine = Math.max(...logsDetect.map((obj) => obj.line));
//get content file in local // //get content file in local
let contentFile = await fs // let contentFile = await fs
.readFileSync(filePath) // .readFileSync(filePath)
.toString() // .toString()
?.split("\n"); // ?.split("\n");
//get index SN and send to ERP // //get index SN and send to ERP
checkIndexSN(contentFile, lastLine, fileName); // checkIndexSN(contentFile, lastLine, fileName);
//get list item to check // //get list item to check
let listKeyValues = allValue.filter( // let listKeyValues = allValue.filter(
(i) => // (i) =>
i.$original.key === "MODEL_SPECIAL" || // i.$original.key === "MODEL_SPECIAL" ||
i.$original.key === "CATCH_FAULTY" // i.$original.key === "CATCH_FAULTY"
); // );
//get list exclude error // //get list exclude error
let listExcludeErr = allValue // let listExcludeErr = allValue
.filter((i) => i.$original.key === "EXCLUDE_ERR") // .filter((i) => i.$original.key === "EXCLUDE_ERR")
.map((obj) => obj.$original.value); // .map((obj) => obj.$original.value);
//get list item special // //get list item special
let listExtraItem = allValue // let listExtraItem = allValue
.filter((i) => i.$original.key === "MODEL_SPECIAL") // .filter((i) => i.$original.key === "MODEL_SPECIAL")
.map((obj) => obj.$original.value); // .map((obj) => obj.$original.value);
//Process file content // //Process file content
contentFile.map((line, index) => { // contentFile.map((line, index) => {
//check line the line with errors and exclude errors // //check line the line with errors and exclude errors
listKeyValues // listKeyValues
.map((obj) => obj.$original.value) // .map((obj) => obj.$original.value)
.map(async (value) => { // .map(async (value) => {
if ( // if (
line.search(value) !== -1 && // line.search(value) !== -1 &&
listExcludeErr.filter((err) => line.includes(err)) // listExcludeErr.filter((err) => line.includes(err))
.length === 0 // .length === 0
) { // ) {
let log = allFile?.filter( // let log = allFile?.filter(
(i) => i.$original.file_name === fileName // (i) => i.$original.file_name === fileName
)[0]; // )[0];
// const log = await LogDetectFile.findBy( // let checkLog = allReport?.filter(
// "file_name", // (report) =>
// fileName // report.$original.id_file === log?.id_ldf &&
// ); // report.$original.line === index + 1 &&
//check report of line not exist // report.$original.detected_content === value
let checkLog = allReport?.filter( // );
(report) =>
report.$original.id_file === log?.id_ldf &&
report.$original.line === index + 1 &&
report.$original.detected_content === value
);
// await Database.rawQuery( // if (checkLog?.length === 0) {
// "select * from log_reports where id_file = " + // await LogReport.create({
// log?.id_ldf + // detected_content: value,
// " and line = " + // line: index + 1,
// (index + 1) + // id_file: log?.id_ldf,
// " and detected_content='" + // });
// value + // lines.push(index + 1);
// "'" // }
// ); // }
// if (checkLog?.length === 0) { // // if(checkSpecialVersion())
// await LogReport.create({ // });
// detected_content: value, // });
// line: index + 1,
// id_file: log?.id_ldf,
// });
// lines.push(index + 1);
// }
}
// if(checkSpecialVersion()) // //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.all();
// let fileDetect = allFile?.filter(
// (i) => i.$original.file_name === fileName
// )[0];
//true: import log to log_report table, send report to Zulip // let logsDetect = allReport_new?.filter(
setTimeout(async () => { // (i) => i.$original.id_file === fileDetect?.id_ldf
if (lines.length === 0) { // );
console.log(`${fileName} has changed ---Good`); // // console.log(logsDetect)
} else { // // await Database.rawQuery(
console.log( // // "select * from log_reports where id_file = " +
`${fileName} has changed ---SOS---${lines.length}` // // fileDetect?.id_ldf
); // // );
let allReport_new = await LogReport.all(); // //Get all report newest
let fileDetect = allFile?.filter( // let listReport = await getListLineByItem(
(i) => i.$original.file_name === fileName // logsDetect
)[0]; // .map((obj) => obj.$original)
// .filter((l) => l.line > lastLine)
// );
let logsDetect = allReport_new?.filter( // let content =
(i) => i.$original.id_file === fileDetect?.id_ldf // "| |Last updated at | Item/error | Line | Report \n|---|:---:|:---|:---|:-----------:\n";
); // let spoiler = "";
// console.log(logsDetect) // let issueFound = "";
// 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 = // listReport.map((log, index) => {
"| |Last updated at | Item/error | Line | Report \n|---|:---:|:---|:---|:-----------:\n"; // let item = listExtraItem.includes(log.detected_content)
let spoiler = ""; // ? ":medal: **" + log.detected_content + "**"
let issueFound = ""; // : ":small_orange_diamond: " + log.detected_content;
listReport.map((log, index) => { // log.line?.map((line) => {
let item = listExtraItem.includes(log.detected_content) // issueFound =
? ":medal: **" + log.detected_content + "**" // issueFound +
: ":small_orange_diamond: " + log.detected_content; // "\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) => { // sendMessToZulip(
issueFound = // "stream",
issueFound + // Env.get("ZULIP_STREAM_ALERT"),
"\n`" + // Env.get("ZULIP_TOPIC_ALERT"),
line + // "------------\n\n:warning: :warning: **" +
"` " + // fileName +
contentFile[line - 1]?.replace( // "**\n\n" +
log.detected_content, // content +
"[" + // "\n\n" +
log.detected_content + // spoiler +
"](https://logs.danielvu.com/logs/" + // "\n\n***Issue found:***\n" +
fileName + // issueFound
"#" + // );
line + // }
")" // }, 3000);
); // }
}); console.log(path + " change")
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);
}
}); });
}); });
} }