def _monthly_lb(category): logger.info("generating monthly leaderboard") if category == "scores": key = "daily.score" elif category == "missed": key = "daily.incorrect" else: raise GenericError("Invalid category", 990) today = datetime.datetime.now(datetime.timezone.utc).date() past_month = pd.date_range( # pylint: disable=no-member today - datetime.timedelta(29), today ).date pipe = database.pipeline() for day in past_month: pipe.zrevrangebyscore(f"{key}:{day}", "+inf", "-inf", withscores=True) result = pipe.execute() totals = pd.Series(dtype="int64") for daily_score in result: daily_score = pd.Series( { e[0]: e[1] for e in map( lambda x: (x[0].decode("utf-8"), int(x[1])), daily_score ) } ) totals = totals.add(daily_score, fill_value=0) totals = totals.sort_values(ascending=False) return totals
def _server_total(ctx): logger.info("fetching server totals") channels = map( lambda x: x.decode("utf-8").split(":")[1], database.zrangebylex("channels:global", f"[{ctx.guild.id}", f"({ctx.guild.id}\xff"), ) pipe = database.pipeline() # use a pipeline to get all the scores for channel in channels: pipe.zscore("score:global", channel) scores = pipe.execute() return int(sum(scores))
def generate_dataframe(database_keys, titles): """Generates a pandas.DataFrame from multiple Redis sorted sets.""" pipe = database.pipeline() for key in database_keys: pipe.zrevrangebyscore(key, "+inf", "-inf", withscores=True) result = pipe.execute() df = pd.DataFrame() for i, item in enumerate(result): df.insert( len(df.columns), titles[i], pd.Series( { e[0]: e[1] for e in map(lambda x: (x[0].decode("utf-8"), int(x[1])), item) } ), ) df = df.fillna(value=0).astype(int) return df
def generate_dataframe(database_keys, titles, index=None): """Generates a pandas.DataFrame from multiple Redis sorted sets.""" pipe = database.pipeline() for key in database_keys: pipe.zrevrangebyscore(key, "+inf", "-inf", withscores=True) result = pipe.execute() index = {v[0].decode("utf-8") for r in result for v in r}.union(set(index if index else {})) df = pd.DataFrame(index=index) for title, item in zip(titles, result): df.insert( len(df.columns), title, pd.Series({ e[0]: e[1] for e in map(lambda x: (x[0].decode("utf-8"), int(x[1])), item) }), ) df = df.fillna(value=0).astype(int) return df