import ujson as json
from redis import StrictRedis

from keywords import get_db


db = get_db()
redis = StrictRedis()

stats = db.command("collstats", "tweets", scale=int(1E9))
count = stats["count"]               # number of tweets
size = stats["size"]                 # raw collection size
storage_size = stats["storageSize"]  # compressed collection size

oldest_tweet = db.tweets.find({"num_keywords": 0}).sort("datetime", 1).limit(1)[0]
latest_tweet = db.tweets.find({"num_keywords": 0}).sort("datetime", -1).limit(1)[0]
oldest_date = oldest_tweet["datetime"].strftime("%Y-%m-%d")
latest_date = latest_tweet["datetime"].strftime("%Y-%m-%d")

try:
    prev_stats = json.loads(redis.get("t:stats"))
    prev_count = prev_stats["count"]
except TypeError:
    prev_count = 0
count_24h = count - prev_count  # number of tweets in the last 24h

stats = {
    "count": count,
    "count_24h": count_24h,
    "size": size,
    "storage_size": storage_size,
    "date_oldest_tweet": oldest_date,
    "date_latest_tweet": latest_date
}
redis.set("t:stats", json.dumps(stats))

@@ -112,16 +112,140 @@
 edis()%0A%0A
+format_count = lambda c: %22%7B:,%7D%22.format(c)  # use , for thousands separator%0Aclean_count = lambda c: int(c.replace(%22,%22, %22%22))%0A%0A
 stats = 
@@ -836,16 +836,28 @@
 count = 
+clean_count(
 prev_sta
@@ -867,16 +867,17 @@
 %22count%22%5D
+)
 %0Aexcept 
@@ -997,21 +997,35 @@
 count%22: 
-count
+format_count(count)
 ,%0A    %22c
@@ -1035,16 +1035,29 @@
 t_24h%22: 
+format_count(
 count_24
@@ -1057,16 +1057,17 @@
 ount_24h
+)
 ,%0A    %22s

use , as thousands separator for number of tweets