Log_service/start/routes.ts

224 lines
6.8 KiB
TypeScript

import axios from "axios";
/*
|--------------------------------------------------------------------------
| Routes
|--------------------------------------------------------------------------
|
| This file is dedicated for defining HTTP routes. A single file is enough
| for majority of projects, however you can define routes in different
| files and just make sure to import them inside this file. For example
|
| Define routes in following two files
| ├── start/routes/cart.ts
| ├── start/routes/customer.ts
|
| and then import them inside `start/routes.ts` as follows
|
| import './routes/cart'
| import './routes/customer'
|
*/
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";
import ErpsController from "App/Controllers/Http/ErpsController";
import LogReport from "App/Models/LogReport";
import Database from "@ioc:Adonis/Lucid/Database";
import { exec } from "child_process";
runtimeCheckLogs(Env.get("FOLDER_LOGS"));
// Route.get("/api/list", async () => {
// const a = await LogDetectFile.all();
// return a;
// });
//ERP get index serial number
Route.post(
"/api/getIndexSerialNumber",
"ErpsController.getIndexSerialNumber"
).middleware("checkToken");
Route.post("/api/getParagraph", "ErpsController.getParagraph").middleware(
"checkToken"
);
//Users
Route.post("/api/account/createUser", "UsersController.create");
Route.post("/api/account/checkLogin", "UsersController.checkLogin");
//Log
Route.get("/api/log/showLog/:name?", async ({ request, response }) => {
try {
let fileDetect = await LogDetectFile.findBy(
"file_name",
request.params().name
);
let logsDetect = await Database.rawQuery(
"select * from log_reports where id_file = " + fileDetect?.id_ldf
);
let modelSpecialDetected = [];
let issueSpecialDetected = [];
let listLine = logsDetect[0]
.map((obj) => obj.line)
.filter((value, index, self) => {
return self.indexOf(value) === index;
})
.sort((a, b) => a - b);
const content = await axios.get(
"http://192.168.5.7:8080/" + request.params().name
);
let allValue = await KeyValue.all();
let listKeyValues = allValue.filter(
(i) =>
i.$original.key === "MODEL_SPECIAL" ||
i.$original.key === "CATCH_FAULTY"
);
let MODEL_SPECIAL = allValue.filter(
(i) =>
i.$original.key === "MODEL_SPECIAL"
).map(obj => obj.$original.value);
let listExcludeErr = allValue
.filter((i) => i.$original.key === "EXCLUDE_ERR")
.map((obj) => obj.$original.value);
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 &&
listExcludeErr.filter((err) => line.includes(err))
.length === 0) {
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);
// }
}
});
});
listLine.map((u) => {
if (listExcludeErr.filter((err) => data[u - 1].includes(err)).length === 0) {
if(MODEL_SPECIAL.filter((i)=>data[u - 1].includes(i)).length>0){
modelSpecialDetected.push(data[u - 1])
}else{
issueSpecialDetected.push(data[u - 1])
}
}
});
let modelSpecial = modelSpecialDetected.length>0?"\n\n<b>Extra items:</b>\n" + modelSpecialDetected.join("\n") + "\n":"\n"
let issueItem = issueSpecialDetected.length>0?"\n<b>Issue items:</b>\n" + issueSpecialDetected.join("\n")+"\n\n":"\n";
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;'><b>"+request.params().name+"</b>" + modelSpecial + issueItem
+
data.join("\n") +
"</div>\
</body>\
</html>"
);
} catch (error) {
console.log(error);
response.send("<h1>-------------- File not found --------------</h1>");
}
});
//Key-Value
Route.post("/api/getKeyValue", async ({ request, response }) => {
let data = await KeyValue.all();
response.status(200).send(data);
});
Route.post("/api/deleteValue", async ({ request, response }) => {
try {
const value = await KeyValue.findOrFail(request.all().id);
await value.delete();
response.status(200).send("DELETE VALUE SUCCESS!");
} catch (error) {
response.status(500).send("DELETE VALUE FAIL!");
}
});
Route.post("/api/addValue", async ({ request, response }) => {
try {
const value = await KeyValue.create({
key: request.all().key,
value: request.all().value,
model: "All",
});
response.status(200).send("ADD VALUE SUCCESS!");
} catch (error) {
response.status(500).send("ADD VALUE FAIL!");
}
});
Route.post("/api/gitea-webhook", ({ request, response }) => {
// Kiểm tra xem đó có phải sự kiện bạn muốn xử lý không
// if (request.headers['x-gitlab-event'] === 'push') {
// // Lấy dữ liệu sự kiện từ req.body
// const eventData = req.body;
// // Xử lý dữ liệu sự kiện ở đây
// console.log('Received push event from GitLab:');
// console.log(JSON.stringify(eventData, null, 2));
// }
console.log(request.headers());
console.log(request.all());
console.log("first3215423");
console.log("New event ---> ", request.header("x-gitea-event"));
// exec(
// "/home/joseph/adonis-gitlab-webhook/gitea.sh",
// (error, stdout, stderr) => {
// if (error) {
// console.error(`Error executing command: ${error}`);
// return;
// }
// console.log(`Command output:\n${stdout}`);
// }
// );
});