From 9d1b43561a7a5510b1c5e93a36585db70d437d1d Mon Sep 17 00:00:00 2001 From: Bright Date: Thu, 21 Nov 2024 18:31:02 +0700 Subject: [PATCH] api report logsDetail --- src/controllers/report-controller.ts | 70 +++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/src/controllers/report-controller.ts b/src/controllers/report-controller.ts index ef734e2..b161c29 100644 --- a/src/controllers/report-controller.ts +++ b/src/controllers/report-controller.ts @@ -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); + } + } }