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