Files
k8s-manifests/polymarket-bot/cronjob-metrics-retention.yaml
T
ChemaVXandClaude Fable 5 b578637112 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 <noreply@anthropic.com>
2026-06-12 08:12:20 +00:00

55 lines
1.9 KiB
YAML

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