api report logsDetail

This commit is contained in:
Bright 2024-11-21 18:31:02 +07:00
parent 25323e8b67
commit 9d1b43561a

View file

@ -1,4 +1,4 @@
import { Body, Controller, Get, Post, Query, Route, Security, Tags, Response } from "tsoa";
import { Body, Controller, Get, Post, Query, Route, Security, Tags, Res, TsoaResponse } from "tsoa";
import { Client as ElasticsearchClient } from "@elastic/elasticsearch";
import * as fs from 'fs';
import * as os from 'os';
@ -243,4 +243,72 @@ export class ReportController extends Controller {
};
}
}
@Post("logsDetail")
async reportLogsDetail(
@Res() downloadFile: TsoaResponse<200, object | string | any>,
@Query() id: string,
) {
try {
const queryData = await elasticsearch.search({
index: ELASTICSEARCH_INDEX,
query: {
bool: {
must: [ ...(id ? [{term: {_id: id}} ] : []) ],
},
},
});
interface DocumentSource {
startTimeStamp: Date;
userName: string;
host: string;
endpoint: string;
method: string;
responseCode: string;
logType: string;
responseDescription: string;
input: string;
output: string;
sequence: string[];
}
const content = queryData.hits.hits.map((x) => {
const source = x._source as DocumentSource;
return {
id: x._id,
startTimeStamp: source.startTimeStamp,
userName: source.userName,
host: source.host,
endpoint: source.endpoint,
method: source.method,
responseCode: source.responseCode,
logType: source.logType,
responseDescription: source.responseDescription,
input: source.input,
output: source.output,
sequence: source.sequence ? source.sequence : [{
action:"-",
status:"-",
description:"-",
request:{
method:"-",
url:"-",
response:"-",
},
}],
};
});
const contentString = JSON.parse(JSON.stringify(content, null, 2));
downloadFile(200, contentString, {
"Content-Type": "application/octet-stream",
"Content-Disposition": `attachment; filename="log_${id}.txt"`,
"Content-Length": contentString.length.toString(),
});
}
catch (error:any) {
console.error("An error occurred:", error);
throw new Error("Failed to process logs: " + error.message);
}
}
}