Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/hot-candies-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@stackspulse/server": patch
---

Add logs for query time.
5 changes: 3 additions & 2 deletions apps/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"@stackspulse/protocols": "workspace:*",
"@t3-oss/env-core": "0.13.4",
"better-sqlite3": "11.10.0",
"db0": "0.2.4",
"consola": "3.4.2",
"db0": "0.3.2",
"drizzle-orm": "0.39.3",
"h3": "1.15.3",
"nitro-cors": "0.7.1",
Expand All @@ -29,6 +30,6 @@
},
"devDependencies": {
"nitropack": "2.11.11",
"prisma": "6.3.1"
"prisma": "6.7.0"
}
}
17 changes: 13 additions & 4 deletions apps/server/src/api/protocols/stackingdao/index.get.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { sql } from "~/db/db";
import { apiCacheConfig } from "~/lib/api";
import { consola } from "~/lib/consola";

type StackingDAOProtocolStatsResponse = {
month: string;
deposits: number;
withdrawals: number;
}[];

export default defineCachedEventHandler(async (event) => {
export default defineCachedEventHandler(async () => {
const queryStartTime = Date.now();
const result = await sql`
WITH monthly_blocks AS (
SELECT
Expand All @@ -26,16 +28,16 @@ deposits AS (
SELECT
mb.month,
SUM(se.amount) AS deposits
FROM
FROM
monthly_blocks mb
JOIN
JOIN
stx_events se ON se.block_height BETWEEN mb.min_block_height AND mb.max_block_height
WHERE
se.recipient = 'SP4SZE494VC2YC5JYG7AYFQ44F5Q4PYV7DVMDPBG.reserve-v1'
AND se.sender NOT LIKE 'SP4SZE494VC2YC5JYG7AYFQ44F5Q4PYV7DVMDPBG%'
AND canonical = TRUE
AND microblock_canonical = TRUE
GROUP BY
GROUP BY
mb.month
),

Expand Down Expand Up @@ -70,6 +72,13 @@ ORDER BY
month ASC
`;

const queryEndTime = Date.now();
consola.debug(
`StackingDAOProtocolStats: Query executed in ${
queryEndTime - queryStartTime
}ms`,
);

const stats: StackingDAOProtocolStatsResponse = result.map((row) => ({
// format of the month is "2021-08-01 00:00:00+00"
// we want to output "2021-08"
Expand Down
7 changes: 7 additions & 0 deletions apps/server/src/api/protocols/users/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type postgres from "postgres";
import { z } from "zod";
import { sql } from "~/db/db";
import { apiCacheConfig } from "~/lib/api";
import { consola } from "~/lib/consola";
import { getValidatedQueryZod } from "~/lib/nitro";

const protocolUsersRouteSchema = z.object({
Expand Down Expand Up @@ -92,6 +93,7 @@ const getProtocolUsersNested = async ({
dateCondition = `AND txs.block_time >= EXTRACT(EPOCH FROM (NOW() - INTERVAL '${daysToSubtract} days'))`;
}

const queryStartTime = Date.now();
const result = await sql`
WITH protocol_contracts AS (
SELECT id, UNNEST(contracts) AS contract_address
Expand Down Expand Up @@ -147,5 +149,10 @@ ORDER BY
LIMIT ${limit};
`;

const queryEndTime = Date.now();
consola.debug(
`ProtocolUsersRoute: Query executed in ${queryEndTime - queryStartTime}ms`,
);

return result;
};
7 changes: 7 additions & 0 deletions apps/server/src/api/transactions/index.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { type Protocol, protocols } from "@stackspulse/protocols";
import { z } from "zod";
import { sql } from "~/db/db";
import { apiCacheConfig } from "~/lib/api";
import { consola } from "~/lib/consola";
import { getValidatedQueryZod } from "~/lib/nitro";
import { stacksClient } from "~/lib/stacks";

Expand All @@ -22,6 +23,7 @@ export default defineCachedEventHandler(async (event) => {
protocolCondition = `AND dapps.id = '${query.protocol}'`;
}

const queryStartTime = Date.now();
const result = await sql<
{
protocol: Protocol;
Expand All @@ -46,6 +48,11 @@ ORDER BY
LIMIT 50
`;

const queryEndTime = Date.now();
consola.debug(
`TransactionsRoute: Query executed in ${queryEndTime - queryStartTime}ms`,
);

const formattedResult = result.map((r) => ({
protocol: r.protocol,
tx_id: `0x${r.tx_id.toString("hex")}`,
Expand Down
9 changes: 9 additions & 0 deletions apps/server/src/api/transactions/stats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { protocols } from "@stackspulse/protocols";
import { z } from "zod";
import { sql } from "~/db/db";
import { apiCacheConfig } from "~/lib/api";
import { consola } from "~/lib/consola";
import { getValidatedQueryZod } from "~/lib/nitro";

const transactionStatsRouteSchema = z.object({
Expand All @@ -21,6 +22,7 @@ export default defineCachedEventHandler(async (event) => {
protocolContractsCondition = `WHERE dapps.id = '${query.protocol}'`;
}

const queryStartTime = Date.now();
const result = await sql`
WITH protocol_contracts AS (
SELECT UNNEST(contracts) AS contract_address
Expand Down Expand Up @@ -69,6 +71,13 @@ JOIN
AND atxs.microblock_hash = txs.microblock_hash
`;

const queryEndTime = Date.now();
consola.debug(
`TransactionStatsRoute: Query executed in ${
queryEndTime - queryStartTime
}ms`,
);

const stats: TransactionStatsRouteResponse = {
count: Number.parseInt(result[0].count),
unique_senders: Number.parseInt(result[0].unique_senders),
Expand Down
9 changes: 9 additions & 0 deletions apps/server/src/api/transactions/unique-senders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { protocols } from "@stackspulse/protocols";
import { z } from "zod";
import { sql } from "~/db/db";
import { apiCacheConfig } from "~/lib/api";
import { consola } from "~/lib/consola";
import { getValidatedQueryZod } from "~/lib/nitro";

const transactionUniqueSendersRouteSchema = z.object({
Expand All @@ -19,6 +20,7 @@ export default defineCachedEventHandler(async (event) => {
transactionUniqueSendersRouteSchema,
);

const queryStartTime = Date.now();
const result = await sql`
WITH monthly_blocks AS (
SELECT
Expand Down Expand Up @@ -82,6 +84,13 @@ ORDER BY
mb.month ASC
`;

const queryEndTime = Date.now();
consola.debug(
`TransactionUniqueSendersRoute: Query executed in ${
queryEndTime - queryStartTime
}ms`,
);

const stats: TransactionUniqueSendersRouteResponse = result.map((row) => ({
// format of the month is "2021-08-01 00:00:00+00"
// we want to output "2021-08"
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const env = createEnv({
TWITTER_API_SECRET_KEY: z.string().min(1),
TWITTER_ACCESS_TOKEN: z.string().min(1),
TWITTER_ACCESS_TOKEN_SECRET: z.string().min(1),
CONSOLA_LEVEL: z.coerce.number().optional(),
},

runtimeEnv: process.env,
Expand Down
10 changes: 10 additions & 0 deletions apps/server/src/lib/consola.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createConsola } from "consola";
import { env } from "~/env";

export const consola = createConsola({
level: env.CONSOLA_LEVEL
? env.CONSOLA_LEVEL
: env.NODE_ENV === "development"
? 4
: 3,
});
Loading