add function getListLineByItem
This commit is contained in:
parent
80458b4738
commit
7dfa2c3c88
|
|
@ -0,0 +1,43 @@
|
|||
import LogReport from "App/Models/LogReport";
|
||||
|
||||
export async function getListLineByItem(listReport) {
|
||||
try {
|
||||
// Mảng ban đầu
|
||||
const list = [];
|
||||
|
||||
// Hàm kiểm tra xem một đối tượng có tồn tại trong mảng dựa trên thuộc tính nào đó
|
||||
function kiemTraTonTai(mang, thuocTinh, giaTri) {
|
||||
return mang.some((item) => item[thuocTinh] === giaTri);
|
||||
}
|
||||
|
||||
// Thêm đối tượng vào mảng nếu thuộc tính không trùng
|
||||
function themVaoMangNeuKhongTrung(mang, thuocTinh, giaTri, doiTuong) {
|
||||
if (!kiemTraTonTai(mang, thuocTinh, giaTri)) {
|
||||
mang.push(doiTuong);
|
||||
}
|
||||
}
|
||||
|
||||
listReport.map((u) => {
|
||||
themVaoMangNeuKhongTrung(list, "detected_content", u.detected_content, u);
|
||||
});
|
||||
|
||||
list.map((report, index) => {
|
||||
list[index].line = [list[index].line];
|
||||
});
|
||||
|
||||
list.map((report, index) => {
|
||||
listReport.map((u) => {
|
||||
if (
|
||||
report.detected_content === u.detected_content &&
|
||||
report.id_report !== u.id_report
|
||||
) {
|
||||
console.log(u.detected_content);
|
||||
list[index].line = list[index].line?.concat(u.line);
|
||||
}
|
||||
});
|
||||
});
|
||||
return list;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@ import LogReport from "App/Models/LogReport";
|
|||
import KeyValue from "App/Models/KeyValue";
|
||||
import Database from "@ioc:Adonis/Lucid/Database";
|
||||
import { sendMessToZulip } from "./sendMessToZulip";
|
||||
import { getListLineByItem } from "./getListLineByItem";
|
||||
|
||||
// export default class loggerAPI {
|
||||
export async function runtimeCheckLogs(folderPath) {
|
||||
|
|
@ -140,82 +141,83 @@ export async function runtimeCheckLogs(folderPath) {
|
|||
fileDetect?.id_ldf
|
||||
);
|
||||
|
||||
let listReport = await getListLineByItem(logsDetect[0])
|
||||
let content =
|
||||
"| |Detected at | Line | Item/error | Content | Report\n|---|:---:|:---:|:---:|:---|:-----------:\n";
|
||||
"| |Detected at | Line | Item/error | Content \n|---|:---:|:---:|:---:|:-----------:\n";
|
||||
let spoiler = "";
|
||||
let file = await fs
|
||||
.readFileSync(filePath)
|
||||
.toString()
|
||||
?.split("\n");
|
||||
if (logsDetect[0].length > 10) {
|
||||
spoiler =
|
||||
spoiler +
|
||||
"```spoiler ..." +
|
||||
(logsDetect[0].length - 5) +
|
||||
" more items\n" +
|
||||
content;
|
||||
// let file = await fs
|
||||
// .readFileSync(filePath)
|
||||
// .toString()
|
||||
// ?.split("\n");
|
||||
// if (logsDetect[0].length > 10) {
|
||||
// spoiler =
|
||||
// spoiler +
|
||||
// "```spoiler ..." +
|
||||
// (logsDetect[0].length - 5) +
|
||||
// " more items\n" +
|
||||
// content;
|
||||
|
||||
logsDetect[0]
|
||||
.reverse()
|
||||
.slice(0, 5)
|
||||
?.map((log, index) => {
|
||||
content =
|
||||
content +
|
||||
"|" +
|
||||
(logsDetect[0].length - index) +
|
||||
"|**" +
|
||||
moment(log.created_at).format("HH:mm - DD/MM") +
|
||||
"**|" +
|
||||
log.line +
|
||||
"|" +
|
||||
log.detected_content +
|
||||
"|" +
|
||||
file[log.line - 1] +
|
||||
"|[View](https://logs.danielvu.com/api/log/showLog/" +
|
||||
fileName +
|
||||
")\n";
|
||||
});
|
||||
// logsDetect[0]
|
||||
// .reverse()
|
||||
// .slice(0, 5)
|
||||
// ?.map((log, index) => {
|
||||
// content =
|
||||
// content +
|
||||
// "|" +
|
||||
// (logsDetect[0].length - index) +
|
||||
// "|**" +
|
||||
// moment(log.created_at).format("HH:mm - DD/MM") +
|
||||
// "**|" +
|
||||
// log.line +
|
||||
// "|" +
|
||||
// log.detected_content +
|
||||
// "|" +
|
||||
// file[log.line - 1] +
|
||||
// "|[View](https://logs.danielvu.com/api/log/showLog/" +
|
||||
// fileName +
|
||||
// ")\n";
|
||||
// });
|
||||
|
||||
logsDetect[0]
|
||||
.slice(5, logsDetect[0].length)
|
||||
?.map((log, index) => {
|
||||
spoiler =
|
||||
spoiler +
|
||||
"|" +
|
||||
(logsDetect[0].length - index - 5) +
|
||||
"|**" +
|
||||
moment(log.created_at).format("HH:mm - DD/MM") +
|
||||
"**|" +
|
||||
log.line +
|
||||
"|" +
|
||||
log.detected_content +
|
||||
"|" +
|
||||
file[log.line - 1] +
|
||||
"|[View](https://logs.danielvu.com/api/log/showLog/" +
|
||||
fileName +
|
||||
")\n";
|
||||
});
|
||||
// logsDetect[0]
|
||||
// .slice(5, logsDetect[0].length)
|
||||
// ?.map((log, index) => {
|
||||
// spoiler =
|
||||
// spoiler +
|
||||
// "|" +
|
||||
// (logsDetect[0].length - index - 5) +
|
||||
// "|**" +
|
||||
// moment(log.created_at).format("HH:mm - DD/MM") +
|
||||
// "**|" +
|
||||
// log.line +
|
||||
// "|" +
|
||||
// log.detected_content +
|
||||
// "|" +
|
||||
// file[log.line - 1] +
|
||||
// "|[View](https://logs.danielvu.com/api/log/showLog/" +
|
||||
// fileName +
|
||||
// ")\n";
|
||||
// });
|
||||
|
||||
spoiler = spoiler + "\n```\n\n";
|
||||
} else {
|
||||
logsDetect[0].reverse().map((log, index) => {
|
||||
// spoiler = spoiler + "\n```\n\n";
|
||||
// } else {
|
||||
listReport.map((log, index) => {
|
||||
content =
|
||||
content +
|
||||
"|" +
|
||||
(logsDetect[0].length - index) +
|
||||
index +
|
||||
"|**" +
|
||||
moment(log.created_at).format("HH:mm - DD/MM") +
|
||||
moment(Date.now()).format("HH:mm - DD/MM") +
|
||||
"**|" +
|
||||
log.line +
|
||||
log.line.join(", ") +
|
||||
"|" +
|
||||
log.detected_content +
|
||||
"|" +
|
||||
file[log.line - 1] +
|
||||
// "|" +
|
||||
// file[log.line - 1] +
|
||||
"|[View](https://logs.danielvu.com/api/log/showLog/" +
|
||||
fileName +
|
||||
")\n";
|
||||
});
|
||||
}
|
||||
// }
|
||||
// setTimeout(() => {
|
||||
sendMessToZulip(
|
||||
"stream",
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ import { runtimeCheckLogs } from "App/utils/runtimeCheckLogs";
|
|||
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";
|
||||
|
||||
runtimeCheckLogs(Env.get("FOLDER_LOGS"));
|
||||
|
||||
|
|
@ -48,32 +49,35 @@ Route.post("/api/account/checkLogin", "UsersController.checkLogin");
|
|||
|
||||
//Log
|
||||
Route.get("/api/log/showLog/:name?", async ({ request, response }) => {
|
||||
try {
|
||||
const content = await axios.get(
|
||||
"http://192.168.5.7:8080/" + request.params().name
|
||||
);
|
||||
let listKeyValues = await KeyValue.all();
|
||||
let data = content.data.split("\n");
|
||||
data.map((line, index) => {
|
||||
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) {
|
||||
data[index] =
|
||||
data[index].slice(0, data[index].search(value)) +
|
||||
"<span style='background-color:yellow;'>" +
|
||||
value +
|
||||
"</span>" +
|
||||
data[index].slice(data[index].search(value) + value.length);
|
||||
// }
|
||||
}
|
||||
});
|
||||
});
|
||||
// console.log(data)
|
||||
response.send(
|
||||
"<html>\
|
||||
try {
|
||||
const content = await axios.get(
|
||||
"http://192.168.5.7:8080/" + request.params().name
|
||||
);
|
||||
let listKeyValues = await KeyValue.all();
|
||||
let data = content.data.split("\n");
|
||||
data.map((line, index) => {
|
||||
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) {
|
||||
data[index] =
|
||||
data[index].slice(0, data[index].search(value)) +
|
||||
"<span style='background-color:yellow;'>" +
|
||||
value +
|
||||
"</span>" +
|
||||
data[index].slice(data[index].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;\
|
||||
|
|
@ -99,16 +103,14 @@ 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") +
|
||||
"</div>\
|
||||
data.join("\n") +
|
||||
"</div>\
|
||||
</body>\
|
||||
</html>"
|
||||
);
|
||||
} catch (error) {
|
||||
response.send(
|
||||
"<h1>-------------- File not found --------------</h1>"
|
||||
);
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
response.send("<h1>-------------- File not found --------------</h1>");
|
||||
}
|
||||
});
|
||||
|
||||
//Key-Value
|
||||
|
|
@ -140,3 +142,7 @@ Route.post("/api/addValue", async ({ request, response }) => {
|
|||
response.status(500).send("ADD VALUE FAIL!");
|
||||
}
|
||||
});
|
||||
|
||||
Route.post("/api/test", async ({ request, response }) => {
|
||||
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue