create function findValue

This commit is contained in:
joseph le 2023-11-23 15:36:36 +07:00
parent 10d1bb5bed
commit de44c57046
3 changed files with 88 additions and 22 deletions

View File

@ -7,4 +7,5 @@ export const editValue = API + "/editValue";
export const getLog = API + "/log/showLog";
export const getListLog = API + "/getAllLogDetect";
export const listOnFolder = API + "/private-log/getFileOnFolder";
export const getContentFile = API + "/private-log/readFile";
export const getContentFile = API + "/private-log/readFile";
export const findValue = API + "/find-value";

View File

@ -1,12 +1,14 @@
import axios from "axios";
import React, { useEffect, useState } from "react";
import { Link, Navigate, useParams } from "react-router-dom";
import { getListLog } from "../../api/apiLog";
import { findValue, getListLog } from "../../api/apiLog";
import "./ListLog.css";
const ListLog = () => {
const [listFile, setListFile] = useState([]);
const [status, setStatus] = useState(200);
const [nameSearch, setNameSearch] = useState("");
const [valueSearch, setValueSearch] = useState("");
const [value, setValue] = useState("");
const getListFile = async () => {
try {
const res = await axios.get(getListLog);
@ -17,6 +19,15 @@ const ListLog = () => {
}
};
const findValueInLog = async () =>{
try {
const res = await axios.get(findValue);
setValue(res.data)
} catch (error) {
console.log(error)
}
}
useEffect(() => {
getListFile();
}, []);
@ -25,28 +36,59 @@ const ListLog = () => {
return (
<div className="mainList">
<div className="inputSearch">
<Link to={"/"}>
<button
style={{
color: "white",
backgroundColor: "blue",
cursor: "pointer",
float:"left"
}}
>
Home
</button></Link>
<label>Search: </label>
<input value={nameSearch} placeholder={"Enter a file name"} onChange={(e)=>{
setNameSearch(e.target.value)
}}></input>
</div>
{listFile?.filter((f)=>f.toLocaleLowerCase().search(nameSearch.toLocaleLowerCase())!==-1).map((file) => (
<Link to={"/"}>
<button
style={{
color: "white",
backgroundColor: "blue",
cursor: "pointer",
float: "left",
}}
>
Home
</button>
</Link>
<div>
<Link to={"/logs/" + file}>{file}</Link>
<br></br>
<label>Search file name: </label>
<input
value={nameSearch}
placeholder={"Enter a file name"}
onChange={(e) => {
setNameSearch(e.target.value);
}}
></input>
</div>
))}
<div>
<label>Search value: </label>
<input
value={valueSearch}
placeholder={"Enter value"}
onChange={(e) => {
setValueSearch(e.target.value);
}}
></input>
<button style={{backgroundColor:"#6cff00", cursor:"pointer"}} onClick={()=>findValueInLog()}>Run</button>
</div>
<textarea
style={{ height: "90vh", width: "50%", backgroundColor: "black", float:"right", color:"#6cff00", padding:10, display:value!==""?"block":"none" }}
value={value}
>
</textarea>
</div>
{listFile
?.filter(
(f) =>
f.toLocaleLowerCase().search(nameSearch.toLocaleLowerCase()) !==
-1
)
.map((file) => (
<div>
<Link to={"/logs/" + file}>{file}</Link>
<br></br>
</div>
))}
</div>
);
} else {

View File

@ -30,6 +30,7 @@ import { sendDeviceInfora } from "App/utils/sendDeviceInfor";
import InfoDevice from "App/Models/InfoDevice";
import LogReport from "App/Models/LogReport";
import Cache from "@ioc:Kaperskyguru/Adonis-Cache";
import { exec } from 'child_process';
runtimeCheckLogs(Env.get("FOLDER_LOGS"));
@ -176,6 +177,28 @@ Route.post("/api/private-log/readFile", async ({ request, response }) => {
}
})
Route.post("/api/find-value", async ({ request, response }) => {
try {
let result = "";
let value = request.all().value;
await exec("grep -nr "+value+" /home/logs", (error, stdout, stderr) => {
if (error) {
// console.error(`Error executing command: ${error.message}`);
return "Error:"+ error;
}
if (stdout !== "") {
result += stdout;
return result
} else {
result += "No result";
return result
}
});
} catch (error) {
console.log(error);
}
})
Route.post("/api/test", async () => {
try {
const logs = await Cache.get('logs')