Skip to content

Commit

Permalink
Merge pull request #152 from SmoFlaDru/dev-benno
Browse files Browse the repository at this point in the history
Convert query in cronjob to Postgres syntax
  • Loading branch information
Bensge authored Jan 21, 2025
2 parents 8733889 + 769e6fb commit 32ec359
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions spybot/recorder/cron/cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,20 @@ def record_hourly_activity():
cursor.execute("""
INSERT INTO HourlyActivity(datetime, activity_hours)
WITH startOfHour AS (
SELECT UTC_TIMESTAMP - INTERVAL second(UTC_TIMESTAMP) SECOND - INTERVAL minute(UTC_TIMESTAMP) MINUTE AS stamp
SELECT DATE_TRUNC('hour', NOW()) - INTERVAL '6' HOUR AS stamp
),
activityHours AS (
SELECT
CAST(COALESCE(SUM(
EXTRACT(EPOCH FROM AGE(
COALESCE(endTime, NOW()),
CASE WHEN startOfHour.stamp > startTime THEN startOfHour.stamp ELSE startTime END
))
), 0) AS FLOAT) / 3600 AS activity_hours
FROM TSUserActivity, startOfHour
WHERE endTime IS NULL OR endTime > startOfHour.stamp
)
SELECT startOfHour.stamp AS datetime,
CAST(COALESCE(SUM(
TIMESTAMPDIFF(SECOND,
IF(startOfHour.stamp > startTime, startOfHour.stamp, startTime),
COALESCE(endTime, UTC_TIMESTAMP)
)
), 0) AS FLOAT) / 3600 AS activity_hours
FROM TSUserActivity, startOfHour
WHERE endTime IS NULL
OR endTime > startOfHour.stamp;
SELECT startOfHour.stamp, activityHours.activity_hours
FROM startOfHour, activityHours;
""")
print("Done collecting activity")

0 comments on commit 32ec359

Please sign in to comment.