change runtimeCheckLog

This commit is contained in:
joseph le 2023-10-04 08:29:40 +07:00
parent b67c3a3580
commit 97c5c1285f
2 changed files with 85 additions and 70 deletions

View File

@ -1,4 +1,4 @@
import Env from '@ioc:Adonis/Core/Env';
import Env from "@ioc:Adonis/Core/Env";
import fs from "fs";
import chokidar from "chokidar";
import moment from "moment";
@ -8,7 +8,7 @@ import KeyValue from "App/Models/KeyValue";
import Database from "@ioc:Adonis/Lucid/Database";
import { sendMessToZulip } from "./sendMessToZulip";
import { getListLineByItem } from "./getListLineByItem";
import { checkIndexSN } from './checkIndexSN';
import { checkIndexSN } from "./checkIndexSN";
export async function runtimeCheckLogs(folderPath) {
try {
@ -88,17 +88,21 @@ export async function runtimeCheckLogs(folderPath) {
// Watch for changes in the files listed
async function watchFilesInList() {
//only check new file ---> fileList - fileList_old = new file
await fileList.slice(0,20)
const watcher = chokidar.watch(
fileList
?.filter((i) => fileList_old.includes(i) === false)
.map((file) => folderPath + "/" + file)
);
await fileList
?.filter((i) => fileList_old.includes(i) === false)
?.forEach((fileName) => {
//path file
const filePath = `${folderPath}/${fileName}`;
const scan = fs.watchFile(
filePath,
{ interval: 15000 },
async (eventType) => {
//check special item, extra RAM, error in log
watcher.on("change", async (path) => {
// fs.watchFile(filePath,{ interval: 15000 },
// async (eventType) => {
//check special item, extra RAM, error in log
if (path === filePath) {
let lines = [];
let allFile = await LogDetectFile.all();
let allReport = await LogReport.all();
@ -132,7 +136,7 @@ export async function runtimeCheckLogs(folderPath) {
//get index SN and send to ERP
checkIndexSN(contentFile, lastLine, fileName)
checkIndexSN(contentFile, lastLine, fileName);
//get list item to check
let listKeyValues = allValue.filter(
@ -171,7 +175,12 @@ export async function runtimeCheckLogs(folderPath) {
// fileName
// );
//check report of line not exist
let checkLog = allReport?.filter((report)=>report.$original.id_file === log?.id_ldf && report.$original.line === (index + 1) && report.$original.detected_content === value)
let checkLog = allReport?.filter(
(report) =>
report.$original.id_file === log?.id_ldf &&
report.$original.line === index + 1 &&
report.$original.detected_content === value
);
// await Database.rawQuery(
// "select * from log_reports where id_file = " +
@ -220,7 +229,9 @@ export async function runtimeCheckLogs(folderPath) {
// );
//Get all report newest
let listReport = await getListLineByItem(
logsDetect.map((obj) => obj.$original).filter((l) => l.line > lastLine)
logsDetect
.map((obj) => obj.$original)
.filter((l) => l.line > lastLine)
);
let content =
@ -283,9 +294,7 @@ export async function runtimeCheckLogs(folderPath) {
}
}, 3000);
}
);
scan.setMaxListeners(0);
});
});
}
} catch (error) {

View File

@ -1,63 +1,69 @@
const puppeteer = require("puppeteer");
const zulip = require("zulip-js");
let a = [1,2,3]
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
console.log(a.map(i=>i+1))
// Điều hướng đến trang web chứa thẻ <a>
await page.goto("https://www.cskh.evnspc.vn/TraCuu/LichNgungGiamCungCapDien");
// Tìm thẻ <a> cần click dựa trên selector CSS hoặc XPath
const selectBoxSelector1 = "select#idCongTyDienLuc";
await page.waitForSelector(selectBoxSelector1);
await page.select(selectBoxSelector1, "PB11");
// Chờ cho trang mới mở
// await page.waitForNavigation('select#idCongTyDienLuc');
// const puppeteer = require("puppeteer");
// const zulip = require("zulip-js");
// In ra nội dung của trang mới
setTimeout(async () => {
const tableData = await page.evaluate(() => {
const tbody = document.querySelector("table"); // Lựa chọn thẻ <tbody> cần trích xuất
const rows = tbody.querySelectorAll("tr"); // Lựa chọn tất cả các hàng (thẻ <tr>) bên trong thẻ <tbody>
// const headerRow = document.querySelectorAll("thead"); // Lựa chọn tất cả các hàng (thẻ <tr>) bên trong thẻ <tbody>
// Lấy tên các cột từ hàng đầu (thẻ <th>)
// const headerColumns = headerRow.querySelectorAll("tr");
// const columnNames = headerColumns.map((headerColumn) =>
// headerColumn.textContent.trim()
// );
const data1 = [];
// headerColumns.forEach((clo) => {
// const columns = Array.from(clo.querySelectorAll("th")); // Lựa chọn tất cả các cột (thẻ <td>) trong hàng
// const rowData = columns.map((column) => column.textContent.trim()); // Lấy nội dung của các cột và xóa khoảng trắng
// data1.push(rowData); // Thêm dữ liệu của hàng vào mảng data
// });
// (async () => {
// const browser = await puppeteer.launch();
// const page = await browser.newPage();
// Lặp qua từng hàng và lấy nội dung của các cột (thẻ <td>) trong hàng
const data = [];
rows.forEach((row) => {
const columns = Array.from(row.querySelectorAll("td")); // Lựa chọn tất cả các cột (thẻ <td>) trong hàng
const headers = Array.from(row.querySelectorAll("th"));
const rowHeader = headers.map((header) => header.textContent.trim());
const rowData = columns.map((column) => column.textContent.trim()); // Lấy nội dung của các cột và xóa khoảng trắng
data.push(rowData); // Thêm dữ liệu của hàng vào mảng data
data1.push(rowHeader)
});
// // Điều hướng đến trang web chứa thẻ <a>
// await page.goto("https://www.cskh.evnspc.vn/TraCuu/LichNgungGiamCungCapDien");
return { data: data, columnNames: data1 };
});
const content = []
tableData.data?.map((u,index)=>{
for(let i=0; i<u.length;i++){
tableData.data[index] = tableData.columnNames[index]+": "+u[]
}
})
console.log("Table data:", );
// // Tìm thẻ <a> cần click dựa trên selector CSS hoặc XPath
// const selectBoxSelector1 = "select#idCongTyDienLuc";
// await page.waitForSelector(selectBoxSelector1);
// await page.select(selectBoxSelector1, "PB11");
await browser.close();
}, 5000);
})();
// // Chờ cho trang mới mở
// // await page.waitForNavigation('select#idCongTyDienLuc');
// // In ra nội dung của trang mới
// setTimeout(async () => {
// const tableData = await page.evaluate(() => {
// const tbody = document.querySelector("table"); // Lựa chọn thẻ <tbody> cần trích xuất
// const rows = tbody.querySelectorAll("tr"); // Lựa chọn tất cả các hàng (thẻ <tr>) bên trong thẻ <tbody>
// // const headerRow = document.querySelectorAll("thead"); // Lựa chọn tất cả các hàng (thẻ <tr>) bên trong thẻ <tbody>
// // Lấy tên các cột từ hàng đầu (thẻ <th>)
// // const headerColumns = headerRow.querySelectorAll("tr");
// // const columnNames = headerColumns.map((headerColumn) =>
// // headerColumn.textContent.trim()
// // );
// const data1 = [];
// // headerColumns.forEach((clo) => {
// // const columns = Array.from(clo.querySelectorAll("th")); // Lựa chọn tất cả các cột (thẻ <td>) trong hàng
// // const rowData = columns.map((column) => column.textContent.trim()); // Lấy nội dung của các cột và xóa khoảng trắng
// // data1.push(rowData); // Thêm dữ liệu của hàng vào mảng data
// // });
// // Lặp qua từng hàng và lấy nội dung của các cột (thẻ <td>) trong hàng
// const data = [];
// rows.forEach((row) => {
// const columns = Array.from(row.querySelectorAll("td")); // Lựa chọn tất cả các cột (thẻ <td>) trong hàng
// const headers = Array.from(row.querySelectorAll("th"));
// const rowHeader = headers.map((header) => header.textContent.trim());
// const rowData = columns.map((column) => column.textContent.trim()); // Lấy nội dung của các cột và xóa khoảng trắng
// data.push(rowData); // Thêm dữ liệu của hàng vào mảng data
// data1.push(rowHeader)
// });
// return { data: data, columnNames: data1 };
// });
// const content = []
// tableData.data?.map((u,index)=>{
// for(let i=0; i<u.length;i++){
// tableData.data[index] = tableData.columnNames[index]+": "+u[]
// }
// })
// console.log("Table data:", );
// await browser.close();
// }, 5000);
// })();
// console.log("data: ",
// data.paragraphText
// .split(" ")