Actually correctly sort the sorted set
authorJacob <jobs@jacobcasper.com>
Sun, 7 Sep 2025 20:17:49 +0000 (15:17 -0500)
committerJacob <jobs@jacobcasper.com>
Sun, 7 Sep 2025 20:17:49 +0000 (15:17 -0500)
app.py
redis_client.py

diff --git a/app.py b/app.py
index 1b72227cf070e2e4450c68a99bd9be28aa8daa34..190e8844b47a4b112e5d63d2e6da254de699472f 100644 (file)
--- a/app.py
+++ b/app.py
@@ -8,6 +8,7 @@ from typing import Optional
 
 from redis_client import get_client
 
+import redis
 from flask import Flask, Response, request
 
 
@@ -21,8 +22,10 @@ def create_app():
     def stats():
         redis_client.increment_hits("stats")
         try:
-            stats = redis_client.get_stats()
-            return {"stats": [{stat[0]: stat[1]} for stat in stats][::-1]}
+            stats = sorted(
+                [stat for stat in redis_client.get_stats()], key=lambda stat: -stat[1]
+            )
+            return {"stats": [{stat[0]: stat[1]} for stat in stats]}
         except redis.exceptions.ConnectionError as e:
             app.logger.exception(e)
             return Response("Internal Server Error", 500)
index 576b993a16e879f622301de987f6a75ce1c75fb1..cb366eac0cbe1ac5b9abd8de20d61a5aff44f951 100644 (file)
@@ -23,7 +23,7 @@ class RedisClient:
 
     def get_stats(self) -> list[tuple]:
         """Returns stats values from a Redis sorted set as a tuple of key-value pairs."""
-        stats = self.r.zscan_iter("hits")
+        stats = self.r.zscan_iter("hits", score_cast_func=int)
         if stats is None:
             logger.debug("Uninitialized stats object")
             return []