Refactor code
This commit is contained in:
		
							parent
							
								
									ca3efce708
								
							
						
					
					
						commit
						4d148279e0
					
				| 
						 | 
					@ -236,7 +236,7 @@ export default class ErpsController {
 | 
				
			||||||
      const res = await axios.get(BASE_URL + "/" + fName);
 | 
					      const res = await axios.get(BASE_URL + "/" + fName);
 | 
				
			||||||
      const arrayLine = res?.data?.split("\n");
 | 
					      const arrayLine = res?.data?.split("\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      if (range >= line) {
 | 
					      if (Number(range) >= Number(line)) {
 | 
				
			||||||
        response.status(200).json({
 | 
					        response.status(200).json({
 | 
				
			||||||
          content: arrayLine?.slice(0, line + range)?.join("\n"),
 | 
					          content: arrayLine?.slice(0, line + range)?.join("\n"),
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -4,27 +4,10 @@ import KeyValue from "App/Models/KeyValue";
 | 
				
			||||||
import LogDetectFile from "App/Models/LogDetectFile";
 | 
					import LogDetectFile from "App/Models/LogDetectFile";
 | 
				
			||||||
import axios from "axios";
 | 
					import axios from "axios";
 | 
				
			||||||
import Env from "@ioc:Adonis/Core/Env";
 | 
					import Env from "@ioc:Adonis/Core/Env";
 | 
				
			||||||
 | 
					import { checkSpecialVersion } from "App/utils/helper";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const BASE_URL = Env.get("BASE_URL_LOG");
 | 
					const BASE_URL = Env.get("BASE_URL_LOG");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Utility function for version detection
 | 
					 | 
				
			||||||
const checkSpecialVersion = (paragraph: string): string => {
 | 
					 | 
				
			||||||
  try {
 | 
					 | 
				
			||||||
    const patterns = [
 | 
					 | 
				
			||||||
      /\(CAT[1-9]K.*Version 16\.9\.[2-9]/,
 | 
					 | 
				
			||||||
      /\(CAT[1-9]K.*Version 1[7-9]\.[0-9]\.[0-9]/,
 | 
					 | 
				
			||||||
      /\(CAT[1-9]K.*Version [2-9][0-9]\.[0-9]\.[0-9]/,
 | 
					 | 
				
			||||||
    ];
 | 
					 | 
				
			||||||
    for (const regex of patterns) {
 | 
					 | 
				
			||||||
      const match = paragraph.match(regex);
 | 
					 | 
				
			||||||
      if (match) return match[0];
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    return "";
 | 
					 | 
				
			||||||
  } catch {
 | 
					 | 
				
			||||||
    return "";
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
export default class LogsController {
 | 
					export default class LogsController {
 | 
				
			||||||
  public async showLog({ request, response }: HttpContextContract) {
 | 
					  public async showLog({ request, response }: HttpContextContract) {
 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
| 
						 | 
					@ -41,7 +24,7 @@ export default class LogsController {
 | 
				
			||||||
      );
 | 
					      );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      const lines = [...new Set(logsDetect[0].map((obj) => obj.line))].sort(
 | 
					      const lines = [...new Set(logsDetect[0].map((obj) => obj.line))].sort(
 | 
				
			||||||
        (a, b) => a - b
 | 
					        (a: number, b: number) => a - b
 | 
				
			||||||
      );
 | 
					      );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      const logUrl = fileName.includes("AUTO")
 | 
					      const logUrl = fileName.includes("AUTO")
 | 
				
			||||||
| 
						 | 
					@ -101,7 +84,8 @@ export default class LogsController {
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      for (const lineIndex of lines) {
 | 
					      for (const lineIndex of lines) {
 | 
				
			||||||
        const line = processedData[lineIndex - 1];
 | 
					        const line = processedData[Number(lineIndex) - 1];
 | 
				
			||||||
 | 
					        if (!line) continue;
 | 
				
			||||||
        if (EXCLUDE_ERR.some((err) => line.includes(err))) continue;
 | 
					        if (EXCLUDE_ERR.some((err) => line.includes(err))) continue;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (
 | 
					        if (
 | 
				
			||||||
| 
						 | 
					@ -120,6 +104,7 @@ export default class LogsController {
 | 
				
			||||||
        contentFile: processedData.join("\n"),
 | 
					        contentFile: processedData.join("\n"),
 | 
				
			||||||
      });
 | 
					      });
 | 
				
			||||||
    } catch (error) {
 | 
					    } catch (error) {
 | 
				
			||||||
 | 
					      console.error("Error fetching log:", error);
 | 
				
			||||||
      response.status(203).send("FILE NOT FOUND");
 | 
					      response.status(203).send("FILE NOT FOUND");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,16 @@
 | 
				
			||||||
 | 
					export const checkSpecialVersion = (paragraph: string): string => {
 | 
				
			||||||
 | 
					  try {
 | 
				
			||||||
 | 
					    const patterns = [
 | 
				
			||||||
 | 
					      /\(CAT[1-9]K.*Version 16\.9\.[2-9]/,
 | 
				
			||||||
 | 
					      /\(CAT[1-9]K.*Version 1[7-9]\.[0-9]\.[0-9]/,
 | 
				
			||||||
 | 
					      /\(CAT[1-9]K.*Version [2-9][0-9]\.[0-9]\.[0-9]/,
 | 
				
			||||||
 | 
					    ];
 | 
				
			||||||
 | 
					    for (const regex of patterns) {
 | 
				
			||||||
 | 
					      const match = paragraph.match(regex);
 | 
				
			||||||
 | 
					      if (match) return match[0];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return "";
 | 
				
			||||||
 | 
					  } catch {
 | 
				
			||||||
 | 
					    return "";
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
| 
						 | 
					@ -9,7 +9,9 @@ import moment from "moment";
 | 
				
			||||||
import { checkIndexSN } from "./checkIndexSN";
 | 
					import { checkIndexSN } from "./checkIndexSN";
 | 
				
			||||||
import { getListLineByItem } from "./getListLineByItem";
 | 
					import { getListLineByItem } from "./getListLineByItem";
 | 
				
			||||||
import { sendMessToZulip } from "./sendMessToZulip";
 | 
					import { sendMessToZulip } from "./sendMessToZulip";
 | 
				
			||||||
export async function runtimeCheckLogs(folderPath) {
 | 
					import { checkSpecialVersion } from "./helper";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export async function runtimeCheckLogs(folderPath: string) {
 | 
				
			||||||
  try {
 | 
					  try {
 | 
				
			||||||
    let cacheFile: any = [];
 | 
					    let cacheFile: any = [];
 | 
				
			||||||
    let fileList = Array();
 | 
					    let fileList = Array();
 | 
				
			||||||
| 
						 | 
					@ -34,22 +36,6 @@ export async function runtimeCheckLogs(folderPath) {
 | 
				
			||||||
      });
 | 
					      });
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const checkSpecialVersion = (paragraph: string): string => {
 | 
					 | 
				
			||||||
      try {
 | 
					 | 
				
			||||||
        const patterns = [
 | 
					 | 
				
			||||||
          /\(CAT[1-9]K.*Version 16\.9\.[2-9]/,
 | 
					 | 
				
			||||||
          /\(CAT[1-9]K.*Version 1[7-9]\.[0-9]\.[0-9]/,
 | 
					 | 
				
			||||||
          /\(CAT[1-9]K.*Version [2-9][0-9]\.[0-9]\.[0-9]/,
 | 
					 | 
				
			||||||
        ];
 | 
					 | 
				
			||||||
        for (const regex of patterns) {
 | 
					 | 
				
			||||||
          const match = paragraph.match(regex);
 | 
					 | 
				
			||||||
          if (match) return match[0];
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        return "";
 | 
					 | 
				
			||||||
      } catch {
 | 
					 | 
				
			||||||
        return "";
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
    // Watch the folder for new files
 | 
					    // Watch the folder for new files
 | 
				
			||||||
    const folderWatcher = chokidar.watch(folderPath);
 | 
					    const folderWatcher = chokidar.watch(folderPath);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -349,7 +335,7 @@ export async function runtimeCheckLogs(folderPath) {
 | 
				
			||||||
              );
 | 
					              );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
              //Get all report newest
 | 
					              //Get all report newest
 | 
				
			||||||
              let listReport = await getListLineByItem(
 | 
					              let listReport: any = await getListLineByItem(
 | 
				
			||||||
                logsDetect
 | 
					                logsDetect
 | 
				
			||||||
                  .map((obj) => obj.$original)
 | 
					                  .map((obj) => obj.$original)
 | 
				
			||||||
                  .filter((l) => l.line > lastLine)
 | 
					                  .filter((l) => l.line > lastLine)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -53,13 +53,13 @@ export const sendDeviceInfora = async () => {
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
              //Array line
 | 
					              //Array line
 | 
				
			||||||
              const lines = data?.split("\n");
 | 
					              const lines = data?.split("\n");
 | 
				
			||||||
              const linesInventory = [];
 | 
					              const linesInventory: any = [];
 | 
				
			||||||
              let DBFileCheck = dataFile.filter(
 | 
					              let DBFileCheck = dataFile.filter(
 | 
				
			||||||
                (i) => i.$attributes.file_name === file
 | 
					                (i) => i.$attributes.file_name === file
 | 
				
			||||||
              )[0];
 | 
					              )[0];
 | 
				
			||||||
              if (DBFileCheck !== undefined) {
 | 
					              if (DBFileCheck !== undefined) {
 | 
				
			||||||
                if (lines.length > DBFileCheck?.$extras.last_check_SN) {
 | 
					                if (lines.length > DBFileCheck?.$extras.last_check_SN) {
 | 
				
			||||||
                  const DBFile = await LogDetectFile.find(
 | 
					                  const DBFile: any = await LogDetectFile.find(
 | 
				
			||||||
                    DBFileCheck?.$attributes.id_ldf
 | 
					                    DBFileCheck?.$attributes.id_ldf
 | 
				
			||||||
                  );
 | 
					                  );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -91,13 +91,13 @@ export const sendDeviceInfora = async () => {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
              //cut content with content1 = [linesInventory[index],linesInventory[index+1]] ...
 | 
					              //cut content with content1 = [linesInventory[index],linesInventory[index+1]] ...
 | 
				
			||||||
              linesInventory?.map((line, index) => {
 | 
					              linesInventory?.map((line, index) => {
 | 
				
			||||||
                const deviceContent = lines?.slice(
 | 
					                const deviceContent: any = lines?.slice(
 | 
				
			||||||
                  linesInventory[index],
 | 
					                  linesInventory[index],
 | 
				
			||||||
                  linesInventory[index + 1]
 | 
					                  linesInventory[index + 1]
 | 
				
			||||||
                );
 | 
					                );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                let backgroundColor = "rgb(200 200 200 / 30%)";
 | 
					                let backgroundColor = "rgb(200 200 200 / 30%)";
 | 
				
			||||||
                const showInventory = [];
 | 
					                const showInventory: any = [];
 | 
				
			||||||
                let check = true;
 | 
					                let check = true;
 | 
				
			||||||
                let begin = 0;
 | 
					                let begin = 0;
 | 
				
			||||||
                let end = 4;
 | 
					                let end = 4;
 | 
				
			||||||
| 
						 | 
					@ -324,6 +324,8 @@ export const sendDeviceInfora = async () => {
 | 
				
			||||||
        i.flash !== "" ||
 | 
					        i.flash !== "" ||
 | 
				
			||||||
        specialModel.filter((m) => i.PID.includes(m)).length > 0
 | 
					        specialModel.filter((m) => i.PID.includes(m)).length > 0
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    //Check if listInformation has data
 | 
				
			||||||
    if (listInformation.length > 0) {
 | 
					    if (listInformation.length > 0) {
 | 
				
			||||||
      console.log(listInformation);
 | 
					      console.log(listInformation);
 | 
				
			||||||
      listInformation.map((inf) => {
 | 
					      listInformation.map((inf) => {
 | 
				
			||||||
| 
						 | 
					@ -356,13 +358,16 @@ export const sendDeviceInfora = async () => {
 | 
				
			||||||
          "</tbody>\
 | 
					          "</tbody>\
 | 
				
			||||||
        </table>",
 | 
					        </table>",
 | 
				
			||||||
      };
 | 
					      };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      //Send mail
 | 
				
			||||||
      transporter.sendMail(options);
 | 
					      transporter.sendMail(options);
 | 
				
			||||||
      await InfoDevice.createMany(listInformation);
 | 
					      await InfoDevice.createMany(listInformation);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      //Write log
 | 
				
			||||||
      const fileName =
 | 
					      const fileName =
 | 
				
			||||||
        "./app/store/logsAPI/" +
 | 
					        "./app/store/logsAPI/" +
 | 
				
			||||||
        moment(Date.now()).format("DD_MM_YYYY").toString() +
 | 
					        moment(Date.now()).format("DD_MM_YYYY").toString() +
 | 
				
			||||||
        ".log";
 | 
					        ".log";
 | 
				
			||||||
 | 
					 | 
				
			||||||
      addLogFunction(
 | 
					      addLogFunction(
 | 
				
			||||||
        fileName,
 | 
					        fileName,
 | 
				
			||||||
        JSON.stringify(listInformation[0], null, 2),
 | 
					        JSON.stringify(listInformation[0], null, 2),
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue