diff --git a/bin/welcomebot-reputation b/bin/welcomebot-reputation index 33b426b..7a5caa4 100755 --- a/bin/welcomebot-reputation +++ b/bin/welcomebot-reputation @@ -92,6 +92,18 @@ def limited(sql): return sql if limit == "all" else f"{sql} LIMIT {int(limit)}" +def since_expr(column): + """SQL for the --since bound. + + A leading +/- means "relative to now" — sqlite needs that spelled + datetime('now', '-24 hours'), NOT datetime('-24 hours'), which returns + NULL and silently matches no rows. + """ + if since.startswith(("+", "-")): + return f"{column} >= datetime('now', ?)" + return f"{column} >= datetime(?)" + + def tables_exist(): names = {r[0] for r in conn.execute( "SELECT name FROM sqlite_master WHERE type = 'table'")} @@ -106,7 +118,7 @@ if not tables_exist(): if mode == "stats": where, params = [], [] if since: - where.append("queried_at >= datetime(?)") + where.append(since_expr("queried_at")) params.append(since) clause = (" WHERE " + " AND ".join(where)) if where else "" row = conn.execute( @@ -150,7 +162,7 @@ if mode == "blocks": where.append("kind = ?") params.append(kind) if since: - where.append("first_blocked_at >= datetime(?)") + where.append(since_expr("first_blocked_at")) params.append(since) clause = (" WHERE " + " AND ".join(where)) if where else "" rows = conn.execute(limited( @@ -179,7 +191,7 @@ if kind: where.append("kind = ?") params.append(kind) if since: - where.append("queried_at >= datetime(?)") + where.append(since_expr("queried_at")) params.append(since) clause = (" WHERE " + " AND ".join(where)) if where else ""