From 6e2c6ecdb98bc744b6c5f2d75b97372c6c1a4556 Mon Sep 17 00:00:00 2001 From: usmanmani1122 Date: Fri, 22 Nov 2024 12:27:44 +0000 Subject: [PATCH] use indexed query --- app/api/logs/block-height/route.ts | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/app/api/logs/block-height/route.ts b/app/api/logs/block-height/route.ts index 3390855..17695f2 100644 --- a/app/api/logs/block-height/route.ts +++ b/app/api/logs/block-height/route.ts @@ -51,7 +51,7 @@ type LogEntry = { }; const COMMIT_BLOCK_FINISH_EVENT_TYPE = 'cosmic-swingset-commit-block-finish'; -const POD_FILTER = `"resourceLabels" @@ '$.pod_name == "follower-0"' OR "resourceLabels" @@ '$.pod_name == "fork1-0"' OR "resourceLabels" @@ '$.pod_name == "validator-primary-0"'`; +const POD_FILTER = `("resourceLabels" ->> 'pod_name') IN ('follower-0', 'fork1-0', 'validator-primary-0')`; const START_BLOCK_EVENT_TYPE = 'cosmic-swingset-begin-block'; type RequestContext = { @@ -121,9 +121,9 @@ const queryLog = async ( SELECT * FROM slogs WHERE ( - "resourceLabels" @@ '$.cluster_name == "${body.clusterName}"' - AND "resourceLabels" @@ '$.namespace_name == "${body.namespace}"' - AND (${POD_FILTER}) + ("resourceLabels" ->> 'cluster_name') = '${body.clusterName}' + AND ("resourceLabels" ->> 'namespace_name') = '${body.namespace}' + AND ${POD_FILTER} AND "timestamp" >= '${body.startTime}' AND "timestamp" <= '${body.endTime}' ) @@ -210,12 +210,13 @@ export const POST = async (request: NextRequest) => { SELECT timestamp FROM slogs WHERE ( - "jsonPayload" @@ '$.blockHeight == ${body.startBlockHeight}' - AND "jsonPayload" @@ '$.type == "${START_BLOCK_EVENT_TYPE}"' - AND "resourceLabels" @@ '$.cluster_name == "${body.clusterName}"' - AND "resourceLabels" @@ '$.namespace_name == "${body.namespace}"' - AND (${POD_FILTER}) + ("jsonPayload" ->> 'blockHeight')::integer = ${body.startBlockHeight} + AND ("jsonPayload" ->> 'type') = '${START_BLOCK_EVENT_TYPE}' + AND ("resourceLabels" ->> 'cluster_name') = '${body.clusterName}' + AND ("resourceLabels" ->> 'namespace_name') = '${body.namespace}' + AND ${POD_FILTER} ) + ORDER BY "timestamp" ASC `); if (!startBlockRows.length) @@ -228,12 +229,13 @@ export const POST = async (request: NextRequest) => { SELECT timestamp FROM slogs WHERE ( - "jsonPayload" @@ '$.blockHeight == ${body.endBlockHeight}' - AND "jsonPayload" @@ '$.type == "${COMMIT_BLOCK_FINISH_EVENT_TYPE}"' - AND "resourceLabels" @@ '$.cluster_name == "${body.clusterName}"' - AND "resourceLabels" @@ '$.namespace_name == "${body.namespace}"' - AND (${POD_FILTER}) + ("jsonPayload" ->> 'blockHeight')::integer = ${body.endBlockHeight} + AND ("jsonPayload" ->> 'type') = '${COMMIT_BLOCK_FINISH_EVENT_TYPE}' + AND ("resourceLabels" ->> 'cluster_name') = '${body.clusterName}' + AND ("resourceLabels" ->> 'namespace_name') = '${body.namespace}' + AND ${POD_FILTER} ) + ORDER BY "timestamp" ASC `); if (!endBlockRows.length)