From b578637112d97f625734cbe48eebcd68cda8cf5d Mon Sep 17 00:00:00 2001 From: ChemaVX Date: Fri, 12 Jun 2026 08:12:20 +0000 Subject: [PATCH] feat(polymarket-bot): daily retention CronJob for metrics_daily The bot snapshots metrics every cycle (~1,300 rows/day, 74K rows in 57 days) but only the last snapshot of each past UTC day is ever read back. Prune past days to their daily close at 00:10 UTC; today's rows are kept intact for /api/summary. Co-Authored-By: Claude Fable 5 --- polymarket-bot/cronjob-metrics-retention.yaml | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 polymarket-bot/cronjob-metrics-retention.yaml diff --git a/polymarket-bot/cronjob-metrics-retention.yaml b/polymarket-bot/cronjob-metrics-retention.yaml new file mode 100644 index 0000000..804adec --- /dev/null +++ b/polymarket-bot/cronjob-metrics-retention.yaml @@ -0,0 +1,54 @@ +apiVersion: batch/v1 +kind: CronJob +metadata: + name: metrics-retention + namespace: polymarket-bot +spec: + # Daily at 00:10 UTC: collapse every past UTC day in metrics_daily to its + # last snapshot (the "daily close"). The bot writes one snapshot per cycle + # (~1,300/day); only the close of past days is read back (DISTINCT ON in + # get_metrics_history / get_daily_pnl_closes), so intraday rows of past + # days are dead weight. Today's rows are never touched: the latest one + # feeds /api/summary and the close is still evolving. + schedule: "10 0 * * *" + concurrencyPolicy: Forbid + successfulJobsHistoryLimit: 3 + failedJobsHistoryLimit: 3 + jobTemplate: + spec: + backoffLimit: 2 + template: + spec: + restartPolicy: Never + containers: + - name: prune + image: postgres:16-alpine + env: + - name: DATABASE_URL + valueFrom: + secretKeyRef: + name: bot-secrets + key: DATABASE_URL + command: + - /bin/sh + - -c + - | + set -e + psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -c " + DELETE FROM metrics_daily m + USING ( + SELECT timestamp::date AS day, MAX(timestamp) AS keep_ts + FROM metrics_daily + WHERE timestamp::date < CURRENT_DATE + GROUP BY 1 + ) k + WHERE m.timestamp::date = k.day + AND m.timestamp < k.keep_ts; + " + psql "$DATABASE_URL" -c "VACUUM ANALYZE metrics_daily;" + resources: + requests: + cpu: 10m + memory: 32Mi + limits: + memory: 128Mi