-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
215 additions
and
73 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { sql } from "../../utils/db"; | ||
|
||
async function aggregateDailyVolume() { | ||
try { | ||
await sql` | ||
INSERT INTO bridges.daily_volume ( | ||
bridge_id, | ||
ts, | ||
total_deposited_usd, | ||
total_withdrawn_usd, | ||
total_deposit_txs, | ||
total_withdrawal_txs, | ||
chain | ||
) | ||
SELECT | ||
ha.bridge_id, | ||
date_trunc('day', ha.ts) as ts, | ||
CAST(SUM(ha.total_deposited_usd) AS NUMERIC) as total_deposited_usd, | ||
CAST(SUM(ha.total_withdrawn_usd) AS NUMERIC) as total_withdrawn_usd, | ||
CAST(SUM(ha.total_deposit_txs) AS INTEGER) as total_deposit_txs, | ||
CAST(SUM(ha.total_withdrawal_txs) AS INTEGER) as total_withdrawal_txs, | ||
c.chain | ||
FROM bridges.hourly_aggregated ha | ||
JOIN bridges.config c ON ha.bridge_id = c.id | ||
WHERE | ||
ha.ts < DATE_TRUNC('day', NOW()) | ||
AND (ha.total_deposited_usd IS NOT NULL AND ha.total_deposited_usd::text ~ '^[0-9]+(\.[0-9]+)?$') | ||
AND (ha.total_withdrawn_usd IS NOT NULL AND ha.total_withdrawn_usd::text ~ '^[0-9]+(\.[0-9]+)?$') | ||
AND (ha.total_deposit_txs IS NOT NULL AND ha.total_deposit_txs::text ~ '^[0-9]+$') | ||
AND (ha.total_withdrawal_txs IS NOT NULL AND ha.total_withdrawal_txs::text ~ '^[0-9]+$') | ||
GROUP BY | ||
ha.bridge_id, | ||
date_trunc('day', ha.ts), | ||
c.chain | ||
ON CONFLICT (bridge_id, ts, chain) DO UPDATE SET | ||
total_deposited_usd = EXCLUDED.total_deposited_usd, | ||
total_withdrawn_usd = EXCLUDED.total_withdrawn_usd, | ||
total_deposit_txs = EXCLUDED.total_deposit_txs, | ||
total_withdrawal_txs = EXCLUDED.total_withdrawal_txs; | ||
`; | ||
|
||
console.log("Daily volume migration completed successfully"); | ||
} catch (error) { | ||
console.error("Error during daily volume migration:", error); | ||
throw error; | ||
} | ||
} | ||
|
||
export { aggregateDailyVolume }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import NodeCache from "node-cache"; | ||
import hash from "object-hash"; | ||
|
||
export const cache = new NodeCache({ | ||
stdTTL: 600, | ||
checkperiod: 120, | ||
maxKeys: 10000, | ||
useClones: false, | ||
deleteOnExpire: true, | ||
}); | ||
|
||
interface APIEvent { | ||
pathParameters?: Record<string, any>; | ||
queryStringParameters?: Record<string, any>; | ||
body?: any; | ||
} | ||
|
||
export const generateApiCacheKey = (event: APIEvent): string => { | ||
const eventToNormalize = { | ||
path: event.pathParameters || {}, | ||
query: event.queryStringParameters || {}, | ||
body: event.body || {}, | ||
}; | ||
|
||
return hash(eventToNormalize, { | ||
algorithm: "sha256", | ||
encoding: "hex", | ||
unorderedArrays: true, | ||
unorderedObjects: true, | ||
}).substring(0, 16); | ||
}; | ||
|
||
export const getCacheKey = (...parts: (string | undefined)[]) => parts.filter(Boolean).join(":"); | ||
|
||
export const DEFAULT_TTL = 600; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.