diff --git a/app/utils/runtimeCheckLogs.ts b/app/utils/runtimeCheckLogs.ts index b767ef8..244a789 100644 --- a/app/utils/runtimeCheckLogs.ts +++ b/app/utils/runtimeCheckLogs.ts @@ -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,22 +88,26 @@ 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(); let allValue = await KeyValue.all(); - + //get information file let fileDetect = allFile?.filter( (i) => i.$original.file_name === fileName @@ -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,8 +175,13 @@ 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 = " + // log?.id_ldf + @@ -220,9 +229,11 @@ 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 = "| |Last updated at | Item/error | Line | Report \n|---|:---:|:---|:---|:-----------:\n"; let spoiler = ""; @@ -283,9 +294,7 @@ export async function runtimeCheckLogs(folderPath) { } }, 3000); } - ); - - scan.setMaxListeners(0); + }); }); } } catch (error) { diff --git a/screenshot.js b/screenshot.js index 540b409..6da946d 100644 --- a/screenshot.js +++ b/screenshot.js @@ -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ẻ - await page.goto("https://www.cskh.evnspc.vn/TraCuu/LichNgungGiamCungCapDien"); - // Tìm thẻ 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ẻ cần trích xuất - const rows = tbody.querySelectorAll("tr"); // Lựa chọn tất cả các hàng (thẻ ) bên trong thẻ - // const headerRow = document.querySelectorAll("thead"); // Lựa chọn tất cả các hàng (thẻ ) bên trong thẻ - // Lấy tên các cột từ hàng đầu (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ẻ ) 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ẻ ) 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ẻ ) 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ẻ +// 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 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ẻ cần trích xuất +// const rows = tbody.querySelectorAll("tr"); // Lựa chọn tất cả các hàng (thẻ ) bên trong thẻ +// // const headerRow = document.querySelectorAll("thead"); // Lựa chọn tất cả các hàng (thẻ ) bên trong thẻ +// // Lấy tên các cột từ hàng đầu (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ẻ ) 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ẻ ) 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ẻ ) 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