Skip to content

Commit

Permalink
use indexed query
Browse files Browse the repository at this point in the history
  • Loading branch information
usmanmani1122 committed Nov 22, 2024
1 parent 6a6fa21 commit 6e2c6ec
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions app/api/logs/block-height/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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}'
)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 6e2c6ec

Please sign in to comment.