Skip to content

Commit

Permalink
chore(cron): Add custom results key (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
chef-huan authored Apr 1, 2022
1 parent b687eb0 commit 3181926
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
47 changes: 25 additions & 22 deletions api/cron/refreshLeaderboard.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { VercelRequest, VercelResponse } from "@vercel/node";
import { gql, request } from "graphql-request";
import { getModel } from "../../utils/mongo";
import { getTradingCompId, getTradingCompSubgraph } from "../../utils";
import { getLeaderboardKey, getTradingCompId, getTradingCompSubgraph } from "../../utils";

type User = {
id: string;
Expand Down Expand Up @@ -54,7 +54,7 @@ const getUserNextPages = async (tradingCompSubgraph: string, maxVolumeUSD: strin
return users;
};

const updateLeaderboard = async (users: User[]) => {
const updateLeaderboard = async (competitionId: string, users: User[]) => {
const teamUser = new Map();
for (let i = 0; i < users.length; i++) {
users[i].globalRank = i + 1;
Expand Down Expand Up @@ -101,7 +101,7 @@ const updateLeaderboard = async (users: User[]) => {
.updateOne(
query,
{
leaderboard_mobox: {
[getLeaderboardKey(competitionId)]: {
global: value[i].globalRank,
team: value[i].teamRank,
volume: value[i].volumeUSD,
Expand All @@ -120,28 +120,31 @@ const updateLeaderboard = async (users: User[]) => {
}
};

const refreshTradingCompLeaderboard = async (tradingCompSubgraph: string) => {
const refreshTradingCompLeaderboard = async (competitionId: string) => {
console.log("TradingCompLeaderboard refresh start");
const tradingCompSubgraph = getTradingCompSubgraph(competitionId);
let users = await getUsersFirstPage(tradingCompSubgraph);
console.log("Fetched users count:", users.length);

let allFetched = false;
while (!allFetched) {
const list = await getUserNextPages(tradingCompSubgraph, users[users.length - 1].volumeUSD);
console.log(
"Fetched users count:",
list.length,
"lastPageMaxVolumeUSD",
users[users.length - 1].volumeUSD
);
if (list.length > 0) {
users = users.concat(list);
} else {
allFetched = true;
if (users.length > 0) {
let allFetched = false;
while (!allFetched) {
const list = await getUserNextPages(tradingCompSubgraph, users[users.length - 1].volumeUSD);
console.log(
"Fetched users count:",
list.length,
"lastPageMaxVolumeUSD",
users[users.length - 1].volumeUSD
);
if (list.length > 0) {
users = users.concat(list);
} else {
allFetched = true;
}
}
}

await updateLeaderboard(users);
await updateLeaderboard(competitionId, users);
}

console.log("Fetched users count: {}", users.length);
console.log("TradingCompLeaderboard refresh end");
Expand All @@ -154,10 +157,10 @@ export default async (req: VercelRequest, res: VercelResponse): Promise<VercelRe

if (authorization === `${process.env.CRON_API_SECRET_KEY}`) {
try {
const { competitionId } = req.query;
const tradingCompSubgraph = getTradingCompSubgraph(getTradingCompId(competitionId));
let { competitionId } = req.query;
competitionId = getTradingCompId(competitionId);

await refreshTradingCompLeaderboard(tradingCompSubgraph);
await refreshTradingCompLeaderboard(competitionId);
res.status(200).json({ success: true });
} catch (error) {
throw new Error("Error refreshing Trading Competition Leaderboard");
Expand Down
13 changes: 13 additions & 0 deletions utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ export const getTradingCompId = (competitionID: string | string[]): string => {
return "2";
};

export const getLeaderboardKey = (competitionID: string): string => {
switch (competitionID) {
case "1":
return "leaderboard";
case "2":
return "leaderboard_fantoken";
case "3":
return "leaderboard_mobox";
default:
return "leaderboard_fantoken";
}
};

/**
* Check for the validity of a username based on rules (see documentation).
*
Expand Down

1 comment on commit 3181926

@vercel
Copy link

@vercel vercel bot commented on 3181926 Apr 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.