263 lines
8.6 KiB
JavaScript
263 lines
8.6 KiB
JavaScript
const axios = require("axios");
|
|
const fs = require("fs");
|
|
const http = require("http");
|
|
const jwt = require("jsonwebtoken");
|
|
|
|
/**
|
|
* Controller do tim cac serial number trong cac log trong khoang thoi gian xac dinh
|
|
* @param {Integer} from thoi gian bat dau YYYYMMDD (vd: 20230130)
|
|
* @param {Integer} to thoi gian ket thuc YYYYMMDD (vd: 20230230)
|
|
*/
|
|
exports.getIndexSerialNumber = async function (req, res) {
|
|
const { from, to } = req.body;
|
|
let accessToken = "";
|
|
const getListLog = async (from, to) => {
|
|
try {
|
|
// console.log("check!")
|
|
let listLog = [];
|
|
const response = await axios.get('http://192.168.5.7:8080/')
|
|
const data = response.data
|
|
const arrayLine = data
|
|
.split("\n")
|
|
.filter(
|
|
(i) => i.search("<a href") !== -1 && i.search(".log") !== -1
|
|
);
|
|
arrayLine.map((u) => {
|
|
let temp = u
|
|
.slice(u.search("<a ") + 9, u.search("</a>"))
|
|
.split(">")[1];
|
|
|
|
if (
|
|
parseInt(temp?.split("-")[0]) >= from &&
|
|
parseInt(temp?.split("-")[0]) <= to
|
|
) {
|
|
listLog.push(
|
|
"http://192.168.5.7:8080/" +
|
|
u.slice(u.search("<a ") + 9, u.search("</a>")).split(">")[1] +
|
|
" "
|
|
);
|
|
}
|
|
});
|
|
|
|
return listLog
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
};
|
|
|
|
|
|
const fetchFiles = async (from, to) => {
|
|
try {
|
|
console.log("1")
|
|
const urls = await getListLog(from, to);
|
|
let report = [];
|
|
const fileContents = await Promise.all(
|
|
urls.map(async (url) => {
|
|
const maxRetries = 10;
|
|
let retries = 0;
|
|
while (retries < maxRetries) {
|
|
try {
|
|
const response = await axios.get(url?.split(" ")[0]);
|
|
return response.data;
|
|
} catch (error) {
|
|
if (error.code !== "") {
|
|
//=== "ETIMEDOUT" || error.code === "ECONNRESET"
|
|
// console.log("Connection timed out. Retrying...");
|
|
retries++;
|
|
} else {
|
|
console.error("Error fetching file:", error);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
})
|
|
);
|
|
|
|
// Handle the file contents
|
|
fileContents.forEach((content, index) => {
|
|
console.log(`Content of file ${index + 1}:`);
|
|
const arrayLine = content?.split("\n");
|
|
let output = [];
|
|
if (arrayLine !== undefined) {
|
|
for (let i = 0; i < arrayLine.length; i++) {
|
|
if (
|
|
arrayLine[i].search("PID:") !== -1 &&
|
|
arrayLine[i].search("SN:") !== -1 && arrayLine[i].search("%") === -1 &&
|
|
arrayLine[i]
|
|
?.split(",")[2]
|
|
?.split(":")[1]
|
|
?.replace("\r", "")
|
|
.trim() !== "" &&
|
|
arrayLine[i]
|
|
?.split(",")[2]
|
|
?.split(":")[1]
|
|
?.replace("\r", "")
|
|
.trim() !== "N/A"
|
|
) {
|
|
if (
|
|
output.some(
|
|
(u) =>
|
|
u.SN ===
|
|
arrayLine[i]
|
|
?.split(",")[2]
|
|
?.split(":")[1]
|
|
?.replace("\r", "")
|
|
.trim()
|
|
)
|
|
) {
|
|
output.map((u, index) => {
|
|
if (
|
|
u.SN === arrayLine[i]?.split(",")[2]?.split(":")[1].trim()
|
|
) {
|
|
output[index].line = output[index].line.concat([i + 1]);
|
|
}
|
|
});
|
|
} else {
|
|
output.push({
|
|
PID:
|
|
arrayLine[i]?.split(",")[0] !== undefined
|
|
? arrayLine[i]?.split(",")[0]?.split(":")[1] !==
|
|
undefined
|
|
? arrayLine[i]?.split(",")[0]?.split(":")[1]?.trim()
|
|
: ""
|
|
: "",
|
|
VID:
|
|
arrayLine[i]?.split(",")[0] !== undefined
|
|
? arrayLine[i]?.split(",")[1]?.split(":")[1] !==
|
|
undefined
|
|
? arrayLine[i]?.split(",")[1]?.split(":")[1]?.trim()
|
|
: ""
|
|
: "",
|
|
SN:
|
|
arrayLine[i].split(",")[1] !== undefined
|
|
? arrayLine[i]
|
|
?.split(",")[2]
|
|
?.split(":")[1]
|
|
?.replace("\r", "")
|
|
.trim()
|
|
: "",
|
|
line: [i + 1],
|
|
fileName: urls[index]?.split("/")[urls[index]?.split("/")?.length-1]?.trim(),
|
|
});
|
|
}
|
|
}
|
|
|
|
if(arrayLine[i].search("PCB Serial Number") !== -1){
|
|
if (
|
|
output.some(
|
|
(u) =>
|
|
u.SN ===
|
|
arrayLine[i]
|
|
?.split(":")[1]
|
|
?.replace("\r", "")
|
|
.trim()
|
|
)
|
|
) {
|
|
output.map((u, index) => {
|
|
if (
|
|
u.SN === arrayLine[i]?.split(":")[1]?.replace("\r", "").trim()
|
|
) {
|
|
output[index].line = output[index].line.concat([i + 1]);
|
|
}
|
|
});
|
|
} else {
|
|
output.push({
|
|
PID: "",
|
|
VID: "",
|
|
SN: arrayLine[i]?.split(":")[1]?.replace("\r", "").trim(),
|
|
line: [i + 1],
|
|
fileName: urls[index]?.split("/")[urls[index]?.split("/")?.length-1]?.trim(),
|
|
});
|
|
}
|
|
}
|
|
}
|
|
report = report.concat(output);
|
|
}
|
|
});
|
|
|
|
fs.writeFile(
|
|
"./utils/ERP/indexSN.txt",
|
|
JSON.stringify(report).replace(/,{/g, "\n,{").replace(/\\u0000/g, ""),
|
|
function (err) {
|
|
if (err) {
|
|
return console.error(err);
|
|
}
|
|
console.log("Write loggg !");
|
|
}
|
|
);
|
|
|
|
res.json(report);
|
|
// }, 15000);
|
|
} catch (error) {
|
|
console.error("Error fetching files:", error);
|
|
}
|
|
};
|
|
if (
|
|
req.headers.authorization &&
|
|
req.headers.authorization.split(" ")[0] === "Bearer"
|
|
) {
|
|
accessToken = accessToken + req.headers.authorization.split(" ")[1];
|
|
}
|
|
|
|
jwt.verify(accessToken, "process.env.JWT_ACCESS_KEY", (err, user) => {
|
|
if (err) {
|
|
res.json({
|
|
pCode: 0,
|
|
pMess: "Token invalid",
|
|
});
|
|
} else {
|
|
const result = fetchFiles(from, to);
|
|
}
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Controller lay noi dung file log theo so dong
|
|
* @param {String} fileName url file log tren server log
|
|
* @param {Integer} line dong chua serial number trong log
|
|
* @param {Integer} range khoang dong truoc/sau line
|
|
* @author {Bearer Token} req.headers.authorization //token xac thuc
|
|
*/
|
|
exports.getParagraph = async function (req, res) {
|
|
const { fileName, line, range } = req.body;
|
|
let accessToken = "";
|
|
|
|
try {
|
|
if (
|
|
req.headers.authorization &&
|
|
req.headers.authorization.split(" ")[0] === "Bearer"
|
|
) {
|
|
accessToken = accessToken + req.headers.authorization.split(" ")[1];
|
|
}
|
|
|
|
jwt.verify(accessToken, "process.env.JWT_ACCESS_KEY", async(err, user) => {
|
|
if (err) {
|
|
res.json({
|
|
pCode: 0,
|
|
pMess: "Token invalid",
|
|
});
|
|
} else {
|
|
const file = fs.createWriteStream("./utils/ERP/logFile.log");
|
|
const response = await axios.get('http://192.168.5.7:8080/'+fileName)
|
|
const arrayLine = response.data.split("\n");
|
|
if(range>=line){
|
|
res.json({
|
|
content: arrayLine
|
|
?.slice(0, line + range)
|
|
?.join("\n"),
|
|
});
|
|
}else{
|
|
res.json({
|
|
content: arrayLine
|
|
?.slice(line - range - 1, line + range)
|
|
?.join("\n"),
|
|
});
|
|
}
|
|
// console.log()
|
|
}
|
|
});
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
};
|