feat: leaderboards precomputation
This commit is contained in:
+51
-55
@@ -390,23 +390,25 @@
|
|||||||
"def precompute_operator_leaderboard():\n",
|
"def precompute_operator_leaderboard():\n",
|
||||||
" \"\"\"Pre-compute and cache operator kill counts using Redis sorted sets.\"\"\"\n",
|
" \"\"\"Pre-compute and cache operator kill counts using Redis sorted sets.\"\"\"\n",
|
||||||
" key = \"leaderboard:operators:total_kills\"\n",
|
" key = \"leaderboard:operators:total_kills\"\n",
|
||||||
" \n",
|
|
||||||
" client.delete(key)\n",
|
" client.delete(key)\n",
|
||||||
" \n",
|
" query = (AggregateRequest( # pyright: ignore[reportUnknownMemberType,reportUnknownVariableType, reportAssignmentType]\n",
|
||||||
" aggr = client.ft().aggregate(\n",
|
" \"*\"\n",
|
||||||
" \"*\",\n",
|
" ).group_by(\n",
|
||||||
" [\n",
|
" \"@operator\",\n",
|
||||||
" {\n",
|
" reducers.sum(\"@nbkills\").alias(\"total_kills\")\n",
|
||||||
" \"$group\": {\n",
|
" ).sort_by(Desc(\"@total_kills\")) # pyright: ignore[reportArgumentType]\n",
|
||||||
" \"_id\": \"$operator\",\n",
|
" .limit(0, 35565).cursor(10, 1)\n",
|
||||||
" \"total_kills\": {\"$sum\": \"$nbkills\"},\n",
|
|
||||||
" }\n",
|
|
||||||
" },\n",
|
|
||||||
" {\"$sort\": {\"total_kills\": -1}},\n",
|
|
||||||
" ]\n",
|
|
||||||
" )\n",
|
" )\n",
|
||||||
"\n",
|
"\n",
|
||||||
" mapping = {r[\"_id\"]: r[\"total_kills\"] for r in aggr[\"results\"]}\n",
|
" response = client.ft().aggregate(query)\n",
|
||||||
|
" rows = response[0][\"results\"]\n",
|
||||||
|
" cursor = response[1]\n",
|
||||||
|
" while cursor > 0:\n",
|
||||||
|
" response= client.ft().aggregate(Cursor(cursor))\n",
|
||||||
|
" rows.extend(response[0][\"results\"])\n",
|
||||||
|
" cursor = response[1]\n",
|
||||||
|
" \n",
|
||||||
|
" mapping = {attrs[\"extra_attributes\"][\"operator\"]: attrs[\"extra_attributes\"][\"total_kills\"] for attrs in rows}\n",
|
||||||
" if mapping:\n",
|
" if mapping:\n",
|
||||||
" client.zadd(key, mapping)\n",
|
" client.zadd(key, mapping)\n",
|
||||||
" \n",
|
" \n",
|
||||||
@@ -432,25 +434,26 @@
|
|||||||
" \"\"\"Pre-compute operator win rates and store in Redis.\"\"\"\n",
|
" \"\"\"Pre-compute operator win rates and store in Redis.\"\"\"\n",
|
||||||
" key = \"leaderboard:operators:winrate\"\n",
|
" key = \"leaderboard:operators:winrate\"\n",
|
||||||
" client.delete(key)\n",
|
" client.delete(key)\n",
|
||||||
" \n",
|
" query = (AggregateRequest( # pyright: ignore[reportUnknownMemberType,reportUnknownVariableType, reportAssignmentType]\n",
|
||||||
" aggr = client.ft().aggregate(\n",
|
" \"*\"\n",
|
||||||
" \"*\",\n",
|
" ).group_by(\n",
|
||||||
" [\n",
|
" \"@operator\",\n",
|
||||||
" {\n",
|
" reducers.sum(\"@haswon\").alias(\"wins\"),\n",
|
||||||
" \"$group\": {\n",
|
" reducers.count().alias(\"total\")\n",
|
||||||
" \"_id\": \"$operator\",\n",
|
" ).sort_by(Desc(\"@wins\")) # pyright: ignore[reportArgumentType]\n",
|
||||||
" \"wins\": {\"$sum\": \"$haswon\"},\n",
|
" .filter(\"@total >= 100\")\n",
|
||||||
" \"total\": {\"$sum\": 1},\n",
|
" .limit(0, 35565).cursor(10, 1)\n",
|
||||||
" }\n",
|
|
||||||
" },\n",
|
|
||||||
" {\"$match\": {\"total\": {\"$gte\": 100}}},\n",
|
|
||||||
" ]\n",
|
|
||||||
" )\n",
|
" )\n",
|
||||||
"\n",
|
"\n",
|
||||||
" mapping = {}\n",
|
" response = client.ft().aggregate(query)\n",
|
||||||
" for r in aggr[\"results\"]:\n",
|
" rows = response[0][\"results\"]\n",
|
||||||
" winrate = (r[\"wins\"] / r[\"total\"]) * 100\n",
|
" cursor = response[1]\n",
|
||||||
" mapping[r[\"_id\"]] = winrate\n",
|
" while cursor > 0:\n",
|
||||||
|
" response= client.ft().aggregate(Cursor(cursor))\n",
|
||||||
|
" rows.extend(response[0][\"results\"])\n",
|
||||||
|
" cursor = response[1]\n",
|
||||||
|
" \n",
|
||||||
|
" mapping = {attrs[\"extra_attributes\"][\"operator\"]: (int(attrs[\"extra_attributes\"][\"wins\"])/int(attrs[\"extra_attributes\"][\"total\"]))*100 for attrs in rows}\n",
|
||||||
" \n",
|
" \n",
|
||||||
" if mapping:\n",
|
" if mapping:\n",
|
||||||
" client.zadd(key, mapping)\n",
|
" client.zadd(key, mapping)\n",
|
||||||
@@ -478,20 +481,25 @@
|
|||||||
" key = \"leaderboard:maps:popularity\"\n",
|
" key = \"leaderboard:maps:popularity\"\n",
|
||||||
" client.delete(key)\n",
|
" client.delete(key)\n",
|
||||||
" \n",
|
" \n",
|
||||||
" aggr = client.ft().aggregate(\n",
|
" query = (AggregateRequest( # pyright: ignore[reportUnknownMemberType,reportUnknownVariableType, reportAssignmentType]\n",
|
||||||
" \"*\",\n",
|
" \"*\"\n",
|
||||||
" [\n",
|
" ).group_by(\n",
|
||||||
" {\n",
|
" \"@mapname\",\n",
|
||||||
" \"$group\": {\n",
|
" reducers.count().alias(\"rounds\")\n",
|
||||||
" \"_id\": \"$mapname\",\n",
|
" ).sort_by(Desc(\"@rounds\")) # pyright: ignore[reportArgumentType]\n",
|
||||||
" \"rounds\": {\"$sum\": 1},\n",
|
" .limit(0, 35565).cursor(10, 1)\n",
|
||||||
" }\n",
|
|
||||||
" },\n",
|
|
||||||
" {\"$sort\": {\"rounds\": -1}},\n",
|
|
||||||
" ]\n",
|
|
||||||
" )\n",
|
" )\n",
|
||||||
"\n",
|
"\n",
|
||||||
" mapping = {r[\"_id\"]: r[\"rounds\"] for r in aggr[\"results\"]}\n",
|
" response = client.ft().aggregate(query)\n",
|
||||||
|
" rows = response[0][\"results\"]\n",
|
||||||
|
" cursor = response[1]\n",
|
||||||
|
" while cursor > 0:\n",
|
||||||
|
" response= client.ft().aggregate(Cursor(cursor))\n",
|
||||||
|
" rows.extend(response[0][\"results\"])\n",
|
||||||
|
" cursor = response[1]\n",
|
||||||
|
" \n",
|
||||||
|
" mapping = {attrs[\"extra_attributes\"][\"mapname\"]: int(attrs[\"extra_attributes\"][\"rounds\"]) for attrs in rows}\n",
|
||||||
|
" \n",
|
||||||
" if mapping:\n",
|
" if mapping:\n",
|
||||||
" client.zadd(key, mapping)\n",
|
" client.zadd(key, mapping)\n",
|
||||||
" \n",
|
" \n",
|
||||||
@@ -595,18 +603,6 @@
|
|||||||
"display_name": "Python 3 (ipykernel)",
|
"display_name": "Python 3 (ipykernel)",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "python3"
|
"name": "python3"
|
||||||
},
|
|
||||||
"language_info": {
|
|
||||||
"codemirror_mode": {
|
|
||||||
"name": "ipython",
|
|
||||||
"version": 3
|
|
||||||
},
|
|
||||||
"file_extension": ".py",
|
|
||||||
"mimetype": "text/x-python",
|
|
||||||
"name": "python",
|
|
||||||
"nbconvert_exporter": "python",
|
|
||||||
"pygments_lexer": "ipython3",
|
|
||||||
"version": "3.13.12"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
Reference in New Issue
Block a user