229 lines
		
	
	
		
			8.5 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
			
		
		
	
	
			229 lines
		
	
	
		
			8.5 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
import fs from "fs";
 | 
						|
import chokidar from "chokidar";
 | 
						|
import moment from "moment";
 | 
						|
import LogDetectFile from "App/Models/LogDetectFile";
 | 
						|
import LogReport from "App/Models/LogReport";
 | 
						|
import KeyValue from "App/Models/KeyValue";
 | 
						|
import Database from "@ioc:Adonis/Lucid/Database";
 | 
						|
import { sendMessToZulip } from "./sendMessToZulip";
 | 
						|
 | 
						|
// export default class loggerAPI {
 | 
						|
export async function runtimeCheckLogs(folderPath) {
 | 
						|
  try {
 | 
						|
    let fileList = Array();
 | 
						|
    let fileList_old = Array();
 | 
						|
    let listKeyValues = await KeyValue.all();
 | 
						|
    // console.log(listKeyValues.map(obj=>obj.$original.value))
 | 
						|
    // Function to update the list of files
 | 
						|
    async function updateFileList() {
 | 
						|
      //get date now
 | 
						|
      let dateNow = moment(Date.now()).format("YYYY/MM/DD");
 | 
						|
      //Get list file in folder
 | 
						|
      fileList = fs.readdirSync(folderPath);
 | 
						|
      //Get date modified
 | 
						|
      fileList.map((file) => {
 | 
						|
        const filePath = `${folderPath}/${file}`;
 | 
						|
        if (file?.split(".")[filePath.split(".").length - 1] === "log") {
 | 
						|
          const stats = fs.statSync(filePath);
 | 
						|
 | 
						|
          //scan file today
 | 
						|
          if (dateNow !== moment(stats.mtime).format("YYYY/MM/DD")) {
 | 
						|
            fileList = fileList.filter((i) => i !== file);
 | 
						|
          }
 | 
						|
        }
 | 
						|
      });
 | 
						|
    }
 | 
						|
 | 
						|
    // Watch the folder for new files
 | 
						|
    const folderWatcher = chokidar.watch(folderPath);
 | 
						|
    // folderWatcher.setMaxListeners(20);
 | 
						|
 | 
						|
    folderWatcher.on("add", async (filePath) => {
 | 
						|
      //import log new file
 | 
						|
      console.log(`New file added: ${filePath}`);
 | 
						|
      if (filePath?.split(".")[filePath.split(".").length - 1] === "log") {
 | 
						|
        const conn = await LogDetectFile.firstOrCreate(
 | 
						|
          { file_name: filePath.split("/")[filePath.split("/").length - 1] },
 | 
						|
          { file_name: filePath.split("/")[filePath.split("/").length - 1] }
 | 
						|
        );
 | 
						|
        // await LogDetectFile.firstOrCreate(
 | 
						|
        //   { file_name: filePath.split("\\")[filePath.split("\\").length - 1] },
 | 
						|
        //   { file_name: filePath.split("\\")[filePath.split("\\").length - 1] }
 | 
						|
        // );
 | 
						|
 | 
						|
        fileList_old = fileList;
 | 
						|
        updateFileList();
 | 
						|
        watchFilesInList();
 | 
						|
 | 
						|
        await Database.manager.close("conn")
 | 
						|
      }
 | 
						|
      // updateFile;
 | 
						|
    });
 | 
						|
 | 
						|
    // Watch for changes in the files listed
 | 
						|
    async function watchFilesInList() {
 | 
						|
      // console.log("--------->", fileList);
 | 
						|
      //get date now
 | 
						|
 | 
						|
      //check new file
 | 
						|
 | 
						|
      await fileList
 | 
						|
        ?.filter((i) => fileList_old.includes(i) === false)
 | 
						|
        ?.forEach((fileName) => {
 | 
						|
          //path file
 | 
						|
          const filePath = `${folderPath}/${fileName}`;
 | 
						|
          const scan = fs.watchFile(
 | 
						|
            filePath,
 | 
						|
            { interval: 300000 },
 | 
						|
            async (eventType) => {
 | 
						|
              //check special item, extra RAM, error in log
 | 
						|
              let lines = [];
 | 
						|
              let content = await fs
 | 
						|
                .readFileSync(filePath)
 | 
						|
                .toString()
 | 
						|
                ?.split("\n");
 | 
						|
              content.map((line, index) => {
 | 
						|
                listKeyValues
 | 
						|
                  .map((obj) => obj.$original.value)
 | 
						|
                  .map(async (value) => {
 | 
						|
                    if (line.search(value) !== -1) {
 | 
						|
                      // let keyWord = "";
 | 
						|
                      // line.split(" ").map((word) => {
 | 
						|
                      //   if (word.toLocaleLowerCase().includes(value)) {
 | 
						|
                      //     keyWord = keyWord + word;
 | 
						|
                      //   }
 | 
						|
                      // });
 | 
						|
                      // if (value.length === keyWord.length) {
 | 
						|
                        const log = await LogDetectFile.findBy(
 | 
						|
                          "file_name",
 | 
						|
                          fileName
 | 
						|
                        );
 | 
						|
                        let checkLog = await Database.rawQuery(
 | 
						|
                          "select * from log_reports where id_file = " +
 | 
						|
                            log?.id_ldf +
 | 
						|
                            " and line = " +
 | 
						|
                            (index + 1) +
 | 
						|
                            " and detected_content='" +
 | 
						|
                            value +
 | 
						|
                            "'"
 | 
						|
                        );
 | 
						|
                        if (checkLog[0].length === 0) {
 | 
						|
                          await LogReport.create({
 | 
						|
                            detected_content: value,
 | 
						|
                            line: index + 1,
 | 
						|
                            id_file: log?.id_ldf,
 | 
						|
                          });
 | 
						|
                          lines.push(index + 1);
 | 
						|
                        }
 | 
						|
                      // }
 | 
						|
                    }
 | 
						|
                  });
 | 
						|
              });
 | 
						|
              //true: import log to log_report table, send report to Zulip
 | 
						|
              setTimeout(async () => {
 | 
						|
                if (lines.length === 0) {
 | 
						|
                  console.log(`${fileName} has changed (${eventType})---Good`);
 | 
						|
                } else {
 | 
						|
                  
 | 
						|
                  console.log(
 | 
						|
                    `${fileName} has changed (${eventType})---SOS---${lines.length}`
 | 
						|
                  );
 | 
						|
 | 
						|
                  let fileDetect = await LogDetectFile.findBy(
 | 
						|
                    "file_name",
 | 
						|
                    fileName
 | 
						|
                  );
 | 
						|
 | 
						|
                  let logsDetect = await Database.rawQuery(
 | 
						|
                    "select * from log_reports where id_file = " +
 | 
						|
                      fileDetect?.id_ldf
 | 
						|
                  );
 | 
						|
 | 
						|
                  let content = "|  |Detected at | Line | Item/error | Content | Report\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
 | 
						|
 | 
						|
                    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 {
 | 
						|
                    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",
 | 
						|
                    "Result test - auto.nswteam.net",
 | 
						|
                    "Log Alert",
 | 
						|
                    ":warning: :warning: **" +
 | 
						|
                    fileName +
 | 
						|
                    "**\n\n"+content+"\n\n"+spoiler 
 | 
						|
                  );
 | 
						|
                  // }, 3000);
 | 
						|
                }
 | 
						|
              }, 3000);
 | 
						|
              //false: next
 | 
						|
            }
 | 
						|
          );
 | 
						|
          //setMaxListeners
 | 
						|
          scan.setMaxListeners(0);
 | 
						|
          // }
 | 
						|
        });
 | 
						|
    }
 | 
						|
  } catch (error) {
 | 
						|
    console.log(error);
 | 
						|
  }
 | 
						|
}
 |