From 06793389edd2c1c6228e6ed310ac73ac4a6de393 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 30 Jul 2024 09:15:12 +0700 Subject: [PATCH 1/3] feat: filter date range --- src/controllers/log-controller.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/controllers/log-controller.ts b/src/controllers/log-controller.ts index 22075b6..c99b4fe 100644 --- a/src/controllers/log-controller.ts +++ b/src/controllers/log-controller.ts @@ -26,7 +26,14 @@ export class LogController extends Controller { @Query() searchAfter?: number, @Query() systemName?: string, @Query() date?: Date, + @Query() startDate?: Date, + @Query() endDate?: Date, ) { + if (!endDate && !startDate) startDate = endDate = date; + + const startDateString = `${startDate.getFullYear()}-${(startDate.getMonth() + 1).toString().padStart(2, "0")}-${startDate.getDate().toString().padStart(2, "0")}T00:00:00`; + const endDateString = `${endDate.getFullYear()}-${(endDate.getMonth() + 1).toString().padStart(2, "0")}-${endDate.getDate().toString().padStart(2, "0")}T23:59:59`; + const queryData = await elasticsearch.search({ index: ELASTICSEARCH_INDEX, query: { @@ -55,8 +62,8 @@ export class LogController extends Controller { range: { startTimeStamp: { time_zone: "+07:00", - gte: `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, "0")}-${date.getDate().toString().padStart(2, "0")}T00:00:00`, - lte: `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, "0")}-${date.getDate().toString().padStart(2, "0")}T23:59:59`, + gte: startDateString, + lte: endDateString, }, }, }, From 6375a449e64c33765504f837986d3fe6b620b4c2 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 30 Jul 2024 09:15:26 +0700 Subject: [PATCH 2/3] feat: search also cover response code --- src/controllers/log-controller.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/controllers/log-controller.ts b/src/controllers/log-controller.ts index c99b4fe..7e1d7a7 100644 --- a/src/controllers/log-controller.ts +++ b/src/controllers/log-controller.ts @@ -31,8 +31,13 @@ export class LogController extends Controller { ) { if (!endDate && !startDate) startDate = endDate = date; - const startDateString = `${startDate.getFullYear()}-${(startDate.getMonth() + 1).toString().padStart(2, "0")}-${startDate.getDate().toString().padStart(2, "0")}T00:00:00`; - const endDateString = `${endDate.getFullYear()}-${(endDate.getMonth() + 1).toString().padStart(2, "0")}-${endDate.getDate().toString().padStart(2, "0")}T23:59:59`; + let startDateString: string | undefined; + let endDateString: string | undefined; + + if (startDate && endDate) { + startDateString = `${startDate.getFullYear()}-${(startDate.getMonth() + 1).toString().padStart(2, "0")}-${startDate.getDate().toString().padStart(2, "0")}T00:00:00`; + endDateString = `${endDate.getFullYear()}-${(endDate.getMonth() + 1).toString().padStart(2, "0")}-${endDate.getDate().toString().padStart(2, "0")}T23:59:59`; + } const queryData = await elasticsearch.search({ index: ELASTICSEARCH_INDEX, @@ -48,7 +53,7 @@ export class LogController extends Controller { { multi_match: { query: search, - fields: ["method", "logType", "endpoint", "host"], + fields: ["method", "logType", "endpoint", "host", "responseCode"], }, }, ], @@ -56,7 +61,7 @@ export class LogController extends Controller { }, ] : []), - ...(date + ...(startDateString || endDateString ? [ { range: { From 17205fc2bc20c3fcb3bd948212ca37ac33bf7829 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 30 Jul 2024 14:10:07 +0700 Subject: [PATCH 3/3] refactor: change from word match to wildcard --- src/controllers/log-controller.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/controllers/log-controller.ts b/src/controllers/log-controller.ts index 7e1d7a7..4a5ede5 100644 --- a/src/controllers/log-controller.ts +++ b/src/controllers/log-controller.ts @@ -49,14 +49,11 @@ export class LogController extends Controller { ? [ { bool: { - should: [ - { - multi_match: { - query: search, - fields: ["method", "logType", "endpoint", "host", "responseCode"], - }, - }, - ], + should: ["method", "logType", "endpoint", "host", "responseCode"].map( + (v) => ({ + wildcard: { [v]: "*" + search + "*" }, + }), + ), }, }, ]