import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' import Database from '@ioc:Adonis/Lucid/Database'; import KeyValue from 'App/Models/KeyValue'; import LogDetectFile from 'App/Models/LogDetectFile'; import axios from 'axios'; export default class LogsController { public async showLog({ request, response }: HttpContextContract) { try { // fghf const checkSpecialVersion = (paragraph) => { try { const regex = /\(CAT[1-9]K.*Version 16\.9\.[2-9]/; const regex1 = /\(CAT[1-9]K.*Version 1[7-9]\.[0-9]\.[0-9]/; const regex2 = /\(CAT[1-9]K.*Version [2-9][0-9]\.[0-9]\.[0-9]/; // Use the regular expression to find the match const match = paragraph.match(regex); const match1 = paragraph.match(regex1); const match2 = paragraph.match(regex2); if (match || match1 || match2) { if (match) { return match[0]; } if (match1) { return match1[0]; } if (match2) { return match2[0]; } } else { return ""; } } catch (error) { console.log(error); } }; let fileDetect = await LogDetectFile.findBy( "file_name", request.params().name ); let logsDetect = await Database.rawQuery( "select * from log_reports where id_file = " + fileDetect?.id_ldf ); let modelSpecialDetected = []; let issueSpecialDetected = []; let listLine = logsDetect[0] .map((obj) => obj.line) .filter((value, index, self) => { return self.indexOf(value) === index; }) .sort((a, b) => a - b); const content = await axios.get( request.params().name.search("AUTO") !== -1 ? "http://192.168.5.7:8080/AUTOlog/" + request.params().name : "http://192.168.5.7:8080/" + request.params().name ); let allValue = await KeyValue.all(); let listKeyValues = allValue.filter( (i) => i.$original.key === "MODEL_SPECIAL" || i.$original.key === "CATCH_FAULTY" ); let MODEL_SPECIAL = allValue .filter((i) => i.$original.key === "MODEL_SPECIAL") .map((obj) => obj.$original.value); let listExcludeErr = allValue .filter((i) => i.$original.key === "EXCLUDE_ERR") .map((obj) => obj.$original.value); let data = content.data.split("\n"); data.map(async (line, index) => { data[index] = index + 1 + "|-|" + line; if (checkSpecialVersion(line) !== "") { const specialVersion = checkSpecialVersion(line) data[index] = data[index].slice(0, data[index].indexOf(specialVersion)) + "|-|" + specialVersion + "|-|" + data[index].slice(data[index].indexOf(specialVersion) + specialVersion.length); } listKeyValues .map((obj) => obj.$original.value) .map(async (value) => { if ( line.indexOf(value) !== -1 && listExcludeErr.filter((err) => line.includes(err)).length === 0 ) { data[index] = data[index].slice(0, data[index].indexOf(value)) + "|-|" + value + "|-|" + data[index].slice(data[index].indexOf(value) + value.length); // } } }); }); listLine.map(async (u) => { if ( listExcludeErr.filter((err) => data[u - 1].includes(err)).length === 0 ) { if (MODEL_SPECIAL.filter((i) => data[u - 1].includes(i)).length > 0) { modelSpecialDetected.push(data[u - 1]); } else { if (checkSpecialVersion(data[u - 1]) !== "") { modelSpecialDetected.push(data[u - 1]); } else { issueSpecialDetected.push(data[u - 1]); } } } }); let modelSpecial = modelSpecialDetected.length > 0 ? modelSpecialDetected.join("\n") : ""; let issueItem = issueSpecialDetected.length > 0 ? issueSpecialDetected.join("\n") : ""; response.status(200).send({ modelSpecial: modelSpecial, issueItem: issueItem, contentFile: data.join("\n"), }); } catch (error) { // console.error(error); response.status(203).send("FILE NOT FOUND"); } } public async getAllLogDetect({ request, response }: HttpContextContract) { try { let fileDetect = await LogDetectFile.all(); let listFiles = fileDetect.map((obj) => obj.file_name); response.status(200).send(listFiles); } catch (error) { response.status(203).send("NO FILE"); } } public async store({ }: HttpContextContract) { } public async show({ }: HttpContextContract) { } public async edit({ }: HttpContextContract) { } public async update({ }: HttpContextContract) { } public async destroy({ }: HttpContextContract) { } }