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
21 changes: 20 additions & 1 deletion svc/api/spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,16 @@ You can use the API without a key, but registering for a key allows increased ra
type: "string", // todo: String for hero id?
},
},
{
name: "bracket",
in: "query",
description:
"Rank bracket from 1 (Herald) to 8 (Immortal) to benchmark against. Omit for matches of all ranks",
required: false,
schema: {
type: "integer",
},
},
],
responses: {
200: {
Expand All @@ -1438,7 +1448,16 @@ You can use the API without a key, but registering for a key allows increased ra
if (typeof req.query.hero_id !== "string") {
return res.status(400).json({ error: "hero_id is not a string" });
}
const result = await getHeroBenchmarks(req.query.hero_id);
let bracket: number | null = null;
if (req.query.bracket !== undefined) {
bracket = Number(req.query.bracket);
if (!Number.isInteger(bracket) || bracket < 1 || bracket > 8) {
return res
.status(400)
.json({ error: "bracket must be an integer between 1 and 8" });
}
}
const result = await getHeroBenchmarks(req.query.hero_id, bracket);
return res.json(result);
},
},
Expand Down
7 changes: 7 additions & 0 deletions svc/counts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ runReliableQueue("counts", 2, async (job: CountsJob, metadata) => {

async function updateBenchmarks() {
const turbo = isTurbo(match);
// 1 to 8 based on the average rank of the match, as in updateHeroCounts
const rank = !turbo && avg ? Math.floor(avg / 10) : null;
if (
match.match_id % 100 < Number(config.BENCHMARKS_SAMPLE_PERCENT) &&
(isSignificant(match) || turbo)
Expand Down Expand Up @@ -242,6 +244,11 @@ runReliableQueue("counts", 2, async (job: CountsJob, metadata) => {
2,
);
redis.expireat(rkey, expiretime);
if (rank) {
const rankKey = `${rkey}:${rank}`;
redis.zadd(rankKey, metric, match.match_id);
redis.expireat(rankKey, expiretime);
}
}
});
}
Expand Down
37 changes: 22 additions & 15 deletions svc/util/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,11 @@ export async function getHeroItemPopularity(heroId: string) {
late_game_items: lateGameItems,
};
}
export async function getHeroBenchmarks(heroId: string) {
export async function getHeroBenchmarks(heroId: string, bracket?: number | null) {
const ret: AnyDict = {};
const arr = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 0.99];
// Bracketed sets add the 1-8 rank bracket after the global/turbo slot
const suffix = bracket ? `:${bracket}` : "";
const items: [metric: string, percentile: number][] = [];
Object.keys(benchmarks).forEach((metric) => {
arr.forEach((percentile) => {
Expand All @@ -120,20 +122,25 @@ export async function getHeroBenchmarks(heroId: string) {
await Promise.all(
items.map(async ([metric, percentile]) => {
// Use data from previous epoch
let key = [
"benchmarks",
getStartOfBlockMinutes(Number(config.BENCHMARK_RETENTION_MINUTES), -1),
metric,
heroId,
"",
].join(":");
const backupKey = [
"benchmarks",
getStartOfBlockMinutes(Number(config.BENCHMARK_RETENTION_MINUTES), 0),
metric,
heroId,
"",
].join(":");
let key =
[
"benchmarks",
getStartOfBlockMinutes(
Number(config.BENCHMARK_RETENTION_MINUTES),
-1,
),
metric,
heroId,
"",
].join(":") + suffix;
const backupKey =
[
"benchmarks",
getStartOfBlockMinutes(Number(config.BENCHMARK_RETENTION_MINUTES), 0),
metric,
heroId,
"",
].join(":") + suffix;
const exists = await redis.exists(key);
if (exists === 0) {
// No data, use backup key (current epoch)
Expand Down
Loading