Skip to content

Commit

Permalink
chore: fix ci
Browse files Browse the repository at this point in the history
  • Loading branch information
sehyunc committed Sep 5, 2024
1 parent f6866de commit 9431ae4
Show file tree
Hide file tree
Showing 10 changed files with 224 additions and 153 deletions.
14 changes: 8 additions & 6 deletions .github/workflows/code-quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ jobs:
env:
NODE_AUTH_TOKEN: ${{ secrets.PKG_GITHUB_TOKEN }}

- name: Run linting, formatting check, and type checking
run: |
pnpm run lint &
pnpm prettier --check . &
pnpm run typecheck &
wait
- name: Run linting
run: pnpm run lint

- name: Run formatting check
run: pnpm prettier --check .

- name: Run type checking
run: pnpm run typecheck
59 changes: 35 additions & 24 deletions app/api/stats/external-transfer-logs/route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { NextRequest } from "next/server"

import { kv } from "@vercel/kv"

import {
BucketData,
ExternalTransferData,
INFLOWS_KEY,
INFLOWS_SET_KEY,
} from "@/app/api/stats/constants"
import { getAllSetMembers } from "@/app/lib/kv-utils"
import { kv } from "@vercel/kv"
import { NextRequest } from "next/server"

export const runtime = "edge"
export const dynamic = "force-dynamic"

Expand All @@ -16,51 +19,59 @@ function startOfPeriod(timestamp: number, intervalMs: number): number {

export async function GET(req: NextRequest) {
try {
const intervalMs = parseInt(req.nextUrl.searchParams.get("interval") || "86400000")
const intervalMs = parseInt(
req.nextUrl.searchParams.get("interval") || "86400000",
)

const transactionHashes = await getAllSetMembers(kv, INFLOWS_SET_KEY);
const transactionHashes = await getAllSetMembers(kv, INFLOWS_SET_KEY)

// Use pipelining to fetch all data in a single round-trip
const pipeline = kv.pipeline();
transactionHashes.forEach(hash => pipeline.get(`${INFLOWS_KEY}:${hash}`));
const data = await pipeline.exec();
const pipeline = kv.pipeline()
transactionHashes.forEach((hash) => pipeline.get(`${INFLOWS_KEY}:${hash}`))
const data = await pipeline.exec()

const buckets: Record<string, BucketData> = {};
const buckets: Record<string, BucketData> = {}

data.forEach((item) => {
if (item && typeof item === "object" && "timestamp" in item) {
const transfer = item as ExternalTransferData;
const bucketTimestamp = startOfPeriod(transfer.timestamp, intervalMs);
const bucketKey = bucketTimestamp.toString();
const transfer = item as ExternalTransferData
const bucketTimestamp = startOfPeriod(transfer.timestamp, intervalMs)
const bucketKey = bucketTimestamp.toString()

if (!buckets[bucketKey]) {
buckets[bucketKey] = {
timestamp: bucketKey,
depositAmount: 0,
withdrawalAmount: 0,
};
}
}

if (transfer.isWithdrawal) {
buckets[bucketKey].withdrawalAmount += transfer.amount;
buckets[bucketKey].withdrawalAmount += transfer.amount
} else {
buckets[bucketKey].depositAmount += transfer.amount;
buckets[bucketKey].depositAmount += transfer.amount
}
}
});
})

const sortedBucketData = Object.values(buckets).sort(
(a, b) => parseInt(a.timestamp) - parseInt(b.timestamp)
);
(a, b) => parseInt(a.timestamp) - parseInt(b.timestamp),
)

return new Response(JSON.stringify({ data: sortedBucketData, intervalMs }), {
headers: { "Content-Type": "application/json" },
});
return new Response(
JSON.stringify({ data: sortedBucketData, intervalMs }),
{
headers: { "Content-Type": "application/json" },
},
)
} catch (error) {
return new Response(JSON.stringify({ error: "Failed to fetch external transfer logs" }), {
status: 500,
headers: { "Content-Type": "application/json" },
});
return new Response(
JSON.stringify({ error: "Failed to fetch external transfer logs" }),
{
status: 500,
headers: { "Content-Type": "application/json" },
},
)
}
}

Expand Down
43 changes: 25 additions & 18 deletions app/api/stats/historical-volume-kv/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { NextRequest } from "next/server"

import { kv } from "@vercel/kv"

import { HISTORICAL_VOLUME_SET_KEY } from "@/app/api/stats/constants"
import { getAllSetMembers } from "@/app/lib/kv-utils"

Expand All @@ -20,45 +22,50 @@ export const dynamic = "force-dynamic"

export async function GET(req: NextRequest) {
try {
const allKeys = await getAllSetMembers(kv, HISTORICAL_VOLUME_SET_KEY);
const allKeys = await getAllSetMembers(kv, HISTORICAL_VOLUME_SET_KEY)

// Use pipelining to fetch all data in a single round-trip
const pipeline = kv.pipeline();
allKeys.forEach(key => pipeline.get(key));
const data = await pipeline.exec();
const pipeline = kv.pipeline()
allKeys.forEach((key) => pipeline.get(key))
const data = await pipeline.exec()

const volumeData: VolumeDataPoint[] = [];
let startTimestamp = Infinity;
let endTimestamp = -Infinity;
const volumeData: VolumeDataPoint[] = []
let startTimestamp = Infinity
let endTimestamp = -Infinity

data.forEach((item) => {
if (item && typeof item === "object" && "timestamp" in item && "volume" in item) {
const dataPoint = item as VolumeDataPoint;
volumeData.push(dataPoint);
startTimestamp = Math.min(startTimestamp, dataPoint.timestamp);
endTimestamp = Math.max(endTimestamp, dataPoint.timestamp);
if (
item &&
typeof item === "object" &&
"timestamp" in item &&
"volume" in item
) {
const dataPoint = item as VolumeDataPoint
volumeData.push(dataPoint)
startTimestamp = Math.min(startTimestamp, dataPoint.timestamp)
endTimestamp = Math.max(endTimestamp, dataPoint.timestamp)
}
});
})

volumeData.sort((a, b) => a.timestamp - b.timestamp);
volumeData.sort((a, b) => a.timestamp - b.timestamp)

const response: HistoricalVolumeResponse = {
data: volumeData,
startTimestamp: startTimestamp !== Infinity ? startTimestamp : 0,
endTimestamp: endTimestamp !== -Infinity ? endTimestamp : 0,
totalPoints: volumeData.length,
};
}

return new Response(JSON.stringify(response), {
headers: { "Content-Type": "application/json" },
});
})
} catch (error) {
return new Response(
JSON.stringify({ error: "Failed to fetch historical volume data" }),
{
status: 500,
headers: { "Content-Type": "application/json" },
}
);
},
)
}
}
48 changes: 28 additions & 20 deletions app/api/stats/net-flow/route.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
import { NET_FLOW_KEY } from "@/app/api/stats/constants"
import { kv } from "@vercel/kv"
import { NextRequest } from "next/server"

import { kv } from "@vercel/kv"

import { NET_FLOW_KEY } from "@/app/api/stats/constants"

export interface NetFlowResponse {
netFlow: number
timestamp: number
netFlow: number
timestamp: number
}

export const runtime = "edge"
export const dynamic = "force-dynamic"

export async function GET(req: NextRequest) {
try {
const data = await kv.get<NetFlowResponse>(NET_FLOW_KEY)
if (data) {
return new Response(JSON.stringify(data), {
headers: { "Content-Type": "application/json" },
})
}
return new Response(JSON.stringify({ error: "Net flow data not available" }), {
status: 404,
headers: { "Content-Type": "application/json" },
})
} catch (error) {
return new Response(JSON.stringify({ error: "Failed to retrieve net flow data" }), {
status: 500,
headers: { "Content-Type": "application/json" },
})
try {
const data = await kv.get<NetFlowResponse>(NET_FLOW_KEY)
if (data) {
return new Response(JSON.stringify(data), {
headers: { "Content-Type": "application/json" },
})
}
return new Response(
JSON.stringify({ error: "Net flow data not available" }),
{
status: 404,
headers: { "Content-Type": "application/json" },
},
)
} catch (error) {
return new Response(
JSON.stringify({ error: "Failed to retrieve net flow data" }),
{
status: 500,
headers: { "Content-Type": "application/json" },
},
)
}
}
22 changes: 14 additions & 8 deletions app/api/stats/set-historical-volume-kv/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,29 @@ interface VolumeData {
}

interface SearchParams {
to: number;
from: number;
interval: number;
to: number
from: number
interval: number
}

const DEFAULT_PARAMS: SearchParams = {
to: Math.floor(Date.now() / 1000),
from: 1693958400,
interval: 24 * 60 * 60,
};
}

export async function GET(req: NextRequest) {
console.log("Starting cron job: set-volume-kv")
try {
const ddog = new DDogClient()
const { searchParams } = new URL(req.url);
const { searchParams } = new URL(req.url)
const params: SearchParams = {
to: parseInt(searchParams.get("to") ?? String(DEFAULT_PARAMS.to)),
from: parseInt(searchParams.get("from") ?? String(DEFAULT_PARAMS.from)),
interval: parseInt(searchParams.get("interval") ?? String(DEFAULT_PARAMS.interval)),
};
interval: parseInt(
searchParams.get("interval") ?? String(DEFAULT_PARAMS.interval),
),
}

console.log(`Parameters: ${JSON.stringify(params)}`)

Expand All @@ -52,7 +54,11 @@ export async function GET(req: NextRequest) {
console.log(
`Fetching match volume from ${params.from} to ${params.to} with interval ${params.interval}`,
)
const res = await ddog.getMatchVolumePerInterval(params.from, params.to, params.interval)
const res = await ddog.getMatchVolumePerInterval(
params.from,
params.to,
params.interval,
)
console.log(`DDogClient response status: ${res.status}`)

if (
Expand Down
Loading

0 comments on commit 9431ae4

Please sign in to comment.