update runtimeCheckLog && routes

This commit is contained in:
joseph le 2023-08-28 11:27:09 +07:00
parent ceec326b21
commit 247c4364c8
2 changed files with 190 additions and 59 deletions

View File

@ -64,7 +64,7 @@ export async function runtimeCheckLogs(folderPath) {
//get date now
//check new file
await fileList
?.filter((i) => fileList_old.includes(i) === false)
?.forEach((fileName) => {
@ -72,7 +72,7 @@ export async function runtimeCheckLogs(folderPath) {
const filePath = `${folderPath}/${fileName}`;
const scan = fs.watchFile(
filePath,
{ interval: 120000 },
{ interval: 5000 },
async (eventType) => {
//check special item, extra RAM, error in log
let lines = [];
@ -85,61 +85,114 @@ export async function runtimeCheckLogs(folderPath) {
.map((obj) => obj.$original.value)
.map(async (value) => {
if (line.search(value) !== -1) {
const log = await LogDetectFile.findBy(
"file_name",
fileName
);
let checkLog = await Database.rawQuery(
"select * from log_reports where id_file = " +
log?.id_ldf +
" and line = " +
(index + 1) +
" and detected_content='" +
value +
"'"
);
console.log(checkLog[0].length)
if (checkLog[0].length === 0) {
console.log(content[index])
await LogReport.create({
detected_content: value,
line: index + 1,
id_file: log?.id_ldf,
});
lines.push(index + 1);
let keyWord = "";
line.split(" ").map((word) => {
if (word.toLocaleLowerCase().includes(value)) {
keyWord = keyWord + word;
}
});
if (value.length === keyWord.length) {
const log = await LogDetectFile.findBy(
"file_name",
fileName
);
let checkLog = await Database.rawQuery(
"select * from log_reports where id_file = " +
log?.id_ldf +
" and line = " +
(index + 1) +
" and detected_content='" +
value +
"'"
);
console.log(checkLog[0].length);
if (checkLog[0].length === 0) {
console.log(content[index]);
await LogReport.create({
detected_content: value,
line: index + 1,
id_file: log?.id_ldf,
});
lines.push(index + 1);
}
}
}
});
});
//true: import log to log_report table, send report to Zulip
setTimeout(async() => {
setTimeout(async () => {
if (lines.length === 0) {
console.log(`${fileName} has changed (${eventType})---Good`);
} else {
console.log(
`${fileName} has changed (${eventType})---SOS---${lines.length}`
);
let fileDetect = await LogDetectFile.findBy(
"file_name",
fileName
);
let fileDetect = await LogDetectFile.findBy(
"file_name",
fileName
);
let logsDetect = await Database.rawQuery(
"select * from log_reports where id_file = " + fileDetect?.id_ldf
);
let content = ":warning: :warning: "+fileName+"\n\n| | Item/error | Line | Detected at | Report\n|---|:---:|:---:|:---:|:-----------:\n"
if(logsDetect[0].length > 10){
content = content + "|1|**" + logsDetect[0][0].detected_content +"**|" + logsDetect[0][0].line +"|"+logsDetect[0][0].created_at+"|[View](https://auto.danielvu.com/api/log?name="+fileName+")\n|...|**...**|...|...|...\n"
logsDetect[0].slice(logsDetect[0].length-6, logsDetect[0].length-1)?.map((log, index)=>{
content = content + "|" + (index+1+logsDetect[0].length-6) +"|**" + log.detected_content +"**|" + log.line +"|"+log.created_at+"|[View](https://auto.danielvu.com/api/log?name="+fileName+")\n"
})
}else{
logsDetect[0].map((log, index)=>{
content = content + "|" + (index+1) +"|**" + log.detected_content +"**|" + log.line +"|"+log.created_at+"|[View](https://auto.danielvu.com/api/log?name="+fileName+")\n"
})
}
let logsDetect = await Database.rawQuery(
"select * from log_reports where id_file = " +
fileDetect?.id_ldf
);
let content =
":warning: :warning: " +
fileName +
"\n\n| | Item/error | Line | Detected at | Report\n|---|:---:|:---:|:---:|:-----------:\n";
if (logsDetect[0].length > 10) {
content =
content +
"|1|**" +
logsDetect[0][0].detected_content +
"**|" +
logsDetect[0][0].line +
"|" +
logsDetect[0][0].created_at +
"|[View](https://auto.danielvu.com/api/log?name=" +
fileName +
")\n|...|**...**|...|...|...\n";
logsDetect[0]
.slice(logsDetect[0].length - 6, logsDetect[0].length - 1)
?.map((log, index) => {
content =
content +
"|" +
(index + 1 + logsDetect[0].length - 6) +
"|**" +
log.detected_content +
"**|" +
log.line +
"|" +
log.created_at +
"|[View](https://auto.danielvu.com/api/log?name=" +
fileName +
")\n";
});
} else {
logsDetect[0].map((log, index) => {
content =
content +
"|" +
(index + 1) +
"|**" +
log.detected_content +
"**|" +
log.line +
"|" +
log.created_at +
"|[View](https://auto.danielvu.com/api/log?name=" +
fileName +
")\n";
});
}
// setTimeout(() => {
sendMessToZulip("stream", "Result test - auto.nswteam.net", "Log Alert",content)
sendMessToZulip(
"stream",
"Result test - auto.nswteam.net",
"Log Alert",
content
);
// }, 3000);
}
}, 3000);
@ -151,8 +204,6 @@ export async function runtimeCheckLogs(folderPath) {
// }
});
}
} catch (error) {
console.log(error);
}

View File

@ -1,3 +1,4 @@
import axios from "axios";
/*
|--------------------------------------------------------------------------
| Routes
@ -18,21 +19,100 @@
|
*/
import Route from '@ioc:Adonis/Core/Route'
import LogDetectFile from 'App/Models/LogDetectFile'
import { runtimeCheckLogs } from 'App/utils/runtimeCheckLogs'
import Env from '@ioc:Adonis/Core/Env';
import Route from "@ioc:Adonis/Core/Route";
import LogDetectFile from "App/Models/LogDetectFile";
import { runtimeCheckLogs } from "App/utils/runtimeCheckLogs";
import Env from "@ioc:Adonis/Core/Env";
import KeyValue from "App/Models/KeyValue";
runtimeCheckLogs(Env.get("FOLDER_LOGS"))
runtimeCheckLogs(Env.get("FOLDER_LOGS"));
Route.get('/api/list', async () => {
const a = await LogDetectFile.all()
Route.get("/api/list", async () => {
const a = await LogDetectFile.all();
// console.log("first")
return a
})
return a;
});
Route.post('/api/getIndexSerialNumber', "ErpsController.getIndexSerialNumber").middleware("checkToken")
Route.post('/api/getParagraph', "ErpsController.getParagraph").middleware("checkToken")
Route.post(
"/api/getIndexSerialNumber",
"ErpsController.getIndexSerialNumber"
).middleware("checkToken");
Route.post("/api/getParagraph", "ErpsController.getParagraph").middleware(
"checkToken"
);
Route.post('/api/account/createUser', "UsersController.create")
Route.post('/api/account/checkLogin', "UsersController.checkLogin")
Route.post("/api/account/createUser", "UsersController.create");
Route.post("/api/account/checkLogin", "UsersController.checkLogin");
Route.get("/api/log/showLog/:name?", async ({ request, response }) => {
const content = await axios.get(
"http://192.168.5.7:8080/" + request.params().name
);
let listKeyValues = await KeyValue.all();
// console.log(listKeyValues)
let data = content.data.split("\n");
data.map((line, index) => {
// console.log(line)
data[index] =
"<span style='background-color:pink;'>" + (index + 1) + "</span>" + line;
listKeyValues
.map((obj) => obj.$original.value)
.map(async (value) => {
if (line.search(value) !== -1) {
let keyWord = "";
line.split(" ").map((word) => {
if (word.toLocaleLowerCase().includes(value)) {
keyWord = keyWord + word;
}
});
if (value.length === keyWord.length) {
data[index] =
"<span style='background-color:pink;'>" +
(index + 1) +
"</span>" +
line.slice(0, line.search(value)) +
"<span style='background-color:yellow;'>" +
value +
"</span>" +
line.slice(line.search(value) + value.length);
}
}
});
});
// console.log(data)
response.send(
"<html>\
<body>\
<div style='width:100%;height:100%;word-wrap:break-word;white-space:pre;overflow: auto;font-family: monospace;text-rendering: auto;\
color: fieldtext;\
letter-spacing: normal;\
word-spacing: normal;\
line-height: normal;\
text-transform: none;\
text-indent: 0px;\
text-shadow: none;\
display: inline-block;\
text-align: start;\
appearance: auto;\
-webkit-rtl-ordering: logical;\
resize: auto;\
cursor: text;\
background-color: field;\
column-count: initial !important;\
writing-mode: horizontal-tb !important;\
box-sizing: border-box;\
margin: 0em;\
border-width: 1px;\
border-style: solid;\
border-color: -internal-light-dark(rgb(118, 118, 118), rgb(133, 133, 133));\
border-image: initial;\
padding: 2px;'>" +
data.join("\n") +
"</div>\
</body>\
</html>"
);
});