เพิ่ม multiSearch ปรับการส่ง params เป็น body บางส่วน และ เปลี่ยน method จาก get เป็น post

This commit is contained in:
AdisakKanthawilang 2024-08-07 10:52:53 +07:00
parent 8189813b4f
commit ed23b8690d
2 changed files with 78 additions and 63 deletions

View file

@ -1,5 +1,6 @@
import { Controller, Get, Query, Route, Security } from "tsoa";
import { Body, Controller, Get, Post, Query, Route, Security } from "tsoa";
import { Client as ElasticsearchClient } from "@elastic/elasticsearch";
import { match } from "assert";
function getEnvVar(name: string) {
const value = process.env[name];
@ -19,15 +20,19 @@ const elasticsearch = new ElasticsearchClient({
@Route("/api/v1/log")
@Security("keycloak")
export class LogController extends Controller {
@Get()
async GET(
@Query() size: number = 30,
@Query() search?: string,
@Query() searchAfter?: number,
@Post()
async Post(
@Body()
body: {
size: number;
search?: string;
multiSearch?: string[];
searchAfter?: number;
date?: Date;
startDate?: Date;
endDate?: Date;
},
@Query() systemName?: string,
@Query() date?: Date,
@Query() startDate?: Date,
@Query() endDate?: Date,
@Query() timezone = "+07:00",
@Query() sort: "asc" | "desc" = "desc",
) {
@ -36,12 +41,12 @@ export class LogController extends Controller {
let startTimeString = "00:00:00";
let endTimeString = "23:59:59";
if (date && !startDate && !endDate) {
startDateString = `${date.getFullYear()}-${`${date.getMonth() + 1}`.padStart(2, "0")}-${`${date.getDate()}`.padStart(2, "0")}T${startTimeString}`;
endDateString = `${date.getFullYear()}-${`${date.getMonth() + 1}`.padStart(2, "0")}-${`${date.getDate()}`.padStart(2, "0")}T${endTimeString}`;
if (body.date && !body.startDate && !body.endDate) {
startDateString = `${body.date.getFullYear()}-${`${body.date.getMonth() + 1}`.padStart(2, "0")}-${`${body.date.getDate()}`.padStart(2, "0")}T${startTimeString}`;
endDateString = `${body.date.getFullYear()}-${`${body.date.getMonth() + 1}`.padStart(2, "0")}-${`${body.date.getDate()}`.padStart(2, "0")}T${endTimeString}`;
}
if (startDate) startDateString = startDate.toISOString();
if (endDate) endDateString = endDate.toISOString();
if (body.startDate) startDateString = body.startDate.toISOString();
if (body.endDate) endDateString = body.endDate.toISOString();
const queryData = await elasticsearch.search({
index: ELASTICSEARCH_INDEX,
@ -49,7 +54,7 @@ export class LogController extends Controller {
bool: {
must: [
...(systemName ? [{ match: { systemName } }] : []),
...(search
...(body.search
? [
{
bool: {
@ -61,7 +66,7 @@ export class LogController extends Controller {
"responseCode",
"output",
].map((v) => ({
wildcard: { [v]: "*" + search + "*" },
wildcard: { [v]: "*" + body.search + "*" },
})),
},
},
@ -80,12 +85,25 @@ export class LogController extends Controller {
},
]
: []),
...(body.multiSearch
? [
{
bool: {
should: body.multiSearch.map((v) => ({
match_phrase: {
userId: v,
},
})),
},
},
]
: []),
],
},
},
search_after: searchAfter ? [searchAfter] : undefined,
search_after: body.searchAfter ? [body.searchAfter] : undefined,
sort: [{ startTimeStamp: sort }],
size: size,
size: body.size,
});
return queryData.hits.hits;
}