Skip to content

Commit a39af9b

Browse files
committed
count getLogs
1 parent 4e9fbb1 commit a39af9b

File tree

5 files changed

+52
-2
lines changed

5 files changed

+52
-2
lines changed

src/adapters/interport-finance/processTransactionsCustom.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { getProvider } from "@defillama/sdk/build/general";
88
import { PromisePool } from "@supercharge/promise-pool";
99
import { getConnection } from "../../helpers/solana";
1010
import web3, { Connection, PartiallyDecodedInstruction, PublicKey } from "@solana/web3.js";
11+
import { incrementGetLogsCount } from "../../utils/cache";
1112

1213
const EventKeyTypes = {
1314
blockNumber: "number",
@@ -128,7 +129,7 @@ export const getTxDataFromEVMEventLogsCustom = async (
128129
chain: overriddenChain,
129130
})
130131
).output;
131-
//console.log(logs)
132+
incrementGetLogsCount(adapterName, overriddenChain);
132133
break;
133134
} catch (e) {
134135
if (i >= 4) {

src/adapters/train/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { EventData } from "../../utils/types";
55
import { BigNumber } from "ethers";
66
import { getLogs, GetLogsOptions } from "@defillama/sdk/build/util/logs";
77
import { getTxDataFromEVMEventLogs } from "../../helpers/processTransactions";
8+
import { incrementGetLogsCount } from "../../utils/cache";
89

910
const contractsByChain: Record<string, string[]> = {
1011
ethereum: ["0x7E6f983f93fd12114DaFE0C69d1e55023EE0abCB"],
@@ -44,6 +45,7 @@ const constructParams = (chain: string) => {
4445
entireLog: true,
4546
};
4647
const logs = await getLogs(options);
48+
incrementGetLogsCount("train", chain);
4749
return logs;
4850
})
4951
);
@@ -76,6 +78,7 @@ const constructParams = (chain: string) => {
7678
entireLog: true,
7779
};
7880
const logs = await getLogs(options);
81+
incrementGetLogsCount("train", chain);
7982
return logs;
8083
})
8184
);
@@ -109,6 +112,7 @@ const constructParams = (chain: string) => {
109112
entireLog: true,
110113
};
111114
const logs = await getLogs(options);
115+
incrementGetLogsCount("train", chain);
112116
return logs;
113117
})
114118
);

src/helpers/processTransactions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ContractEventParams, PartialContractEventParams } from "../helpers/brid
66
import { EventData } from "../utils/types";
77
import { PromisePool } from "@supercharge/promise-pool";
88
import { getProvider } from "../utils/provider";
9+
import { incrementGetLogsCount } from "../utils/cache";
910

1011
const EventKeyTypes = {
1112
blockNumber: "number",
@@ -125,6 +126,7 @@ export const getTxDataFromEVMEventLogs = async (
125126
let logs = [] as any[];
126127
for (let i = 0; i < 5; i++) {
127128
try {
129+
incrementGetLogsCount(adapterName, overriddenChain);
128130
logs = (
129131
await getLogs({
130132
target: target!,
@@ -136,7 +138,6 @@ export const getTxDataFromEVMEventLogs = async (
136138
chain: overriddenChain,
137139
})
138140
).output;
139-
//console.log(logs)
140141
break;
141142
} catch (e) {
142143
if (i >= 4) {

src/server/cron.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import runHyperlane from "../handlers/runHyperlane";
1717
import runTeleswap from "../handlers/runTeleswap";
1818
import { handler as runRelay } from "../handlers/runRelay";
1919
import { handler as runCashmere } from "../handlers/runCashmere";
20+
import { getAllGetLogsCounts } from "../utils/cache";
2021

2122
const createTimeout = (minutes: number) =>
2223
new Promise((_, reject) =>
@@ -40,9 +41,20 @@ const withTimeout = async (jobName: string, promise: Promise<any>, timeoutMinute
4041
}
4142
};
4243

44+
const printGetLogsSummary = async () => {
45+
const counts = await getAllGetLogsCounts();
46+
const sorted = Object.entries(counts).sort((a, b) => b[1] - a[1]);
47+
console.log("[GETLOGS SUMMARY] Top callers today:");
48+
for (const [key, count] of sorted.slice(0, 30)) {
49+
console.log(` ${key}: ${count} calls`);
50+
}
51+
console.log(`[GETLOGS SUMMARY] Total unique adapter:chain combinations: ${sorted.length}`);
52+
};
53+
4354
const exit = () => {
4455
setTimeout(async () => {
4556
console.log("[INFO] Timeout! Shutting down. Bye bye!");
57+
await printGetLogsSummary();
4658
await sql.end();
4759
await querySql.end();
4860
process.exit(0);

src/utils/cache.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,35 @@ export const deleteCache = async (key: string): Promise<void> => {
112112
console.error(`[CACHE] deleteCache error for ${key}:`, e);
113113
}
114114
};
115+
116+
const GETLOGS_COUNT_TTL = 60 * 60 * 24 * 7;
117+
118+
export const incrementGetLogsCount = async (adapterName: string, chain: string): Promise<void> => {
119+
if (!redis) return;
120+
try {
121+
const today = new Date().toISOString().split("T")[0];
122+
const key = `getlogs_count:${today}:${adapterName.toLowerCase()}:${chain.toLowerCase()}`;
123+
await redis.incr(key);
124+
await redis.expire(key, GETLOGS_COUNT_TTL);
125+
} catch (e) {}
126+
};
127+
128+
export const getAllGetLogsCounts = async (): Promise<Record<string, number>> => {
129+
if (!redis) return {};
130+
try {
131+
const today = new Date().toISOString().split("T")[0];
132+
const keys = await redis.keys(`getlogs_count:${today}:*`);
133+
if (keys.length === 0) return {};
134+
135+
const counts: Record<string, number> = {};
136+
for (const key of keys) {
137+
const val = await redis.get(key);
138+
const parts = key.replace(`getlogs_count:${today}:`, "");
139+
counts[parts] = parseInt(val || "0", 10);
140+
}
141+
return counts;
142+
} catch (e) {
143+
console.error("[CACHE] getAllGetLogsCounts error:", e);
144+
return {};
145+
}
146+
};

0 commit comments

Comments
 (0)