add function getListLineByItem

This commit is contained in:
joseph le 2023-09-04 15:44:31 +07:00
parent 80458b4738
commit 7dfa2c3c88
3 changed files with 145 additions and 94 deletions

View File

@ -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);
}
}

View File

@ -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) => {
// 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";
// });
// 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";
});
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) => {
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";
});
}
// }
// setTimeout(() => {
sendMessToZulip(
"stream",

View File

@ -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"));
@ -56,7 +57,10 @@ Route.get("/api/log/showLog/:name?", async ({ request, response }) => {
let data = content.data.split("\n");
data.map((line, index) => {
data[index] =
"<span style='background-color:pink;'>" + (index + 1) + "</span>" + line;
"<span style='background-color:pink;'>" +
(index + 1) +
"</span>" +
line;
listKeyValues
.map((obj) => obj.$original.value)
.map(async (value) => {
@ -105,9 +109,7 @@ Route.get("/api/log/showLog/:name?", async ({ request, response }) => {
</html>"
);
} catch (error) {
response.send(
"<h1>-------------- File not found --------------</h1>"
);
response.send("<h1>-------------- File not found --------------</h1>");
}
});
@ -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 }) => {
});