File tree Expand file tree Collapse file tree 2 files changed +30
-1
lines changed Expand file tree Collapse file tree 2 files changed +30
-1
lines changed Original file line number Diff line number Diff line change @@ -1526,6 +1526,15 @@ func (q *FakeQuerier) DeleteOldWorkspaceAgentStats(_ context.Context) error {
1526
1526
)
1527
1527
FROM
1528
1528
template_usage_stats
1529
+ )
1530
+ AND created_at < (
1531
+ -- Delete at most in batches of 3 days (with a batch size of 3 days, we
1532
+ -- can clear out the previous 6 months of data in ~60 iterations) whilst
1533
+ -- keeping the DB load relatively low.
1534
+ SELECT
1535
+ COALESCE(MIN(created_at) + '3 days'::interval, NOW())
1536
+ FROM
1537
+ workspace_agent_stats
1529
1538
);
1530
1539
*/
1531
1540
@@ -1543,9 +1552,20 @@ func (q *FakeQuerier) DeleteOldWorkspaceAgentStats(_ context.Context) error {
1543
1552
}
1544
1553
1545
1554
var validStats []database.WorkspaceAgentStat
1555
+ var batchLimit time.Time
1556
+ for _ , stat := range q .workspaceAgentStats {
1557
+ if batchLimit .IsZero () || stat .CreatedAt .Before (batchLimit ) {
1558
+ batchLimit = stat .CreatedAt
1559
+ }
1560
+ }
1561
+ if batchLimit .IsZero () {
1562
+ batchLimit = time .Now ()
1563
+ } else {
1564
+ batchLimit = batchLimit .AddDate (0 , 0 , 3 )
1565
+ }
1546
1566
for _ , stat := range q .workspaceAgentStats {
1547
1567
fmt .Println (stat .CreatedAt , limit )
1548
- if stat .CreatedAt .Before (limit ) {
1568
+ if stat .CreatedAt .Before (limit ) && stat . CreatedAt . Before ( batchLimit ) {
1549
1569
fmt .Println ("delete" )
1550
1570
continue
1551
1571
}
Original file line number Diff line number Diff line change @@ -109,6 +109,15 @@ WHERE
109
109
)
110
110
FROM
111
111
template_usage_stats
112
+ )
113
+ AND created_at < (
114
+ -- Delete at most in batches of 3 days (with a batch size of 3 days, we
115
+ -- can clear out the previous 6 months of data in ~60 iterations) whilst
116
+ -- keeping the DB load relatively low.
117
+ SELECT
118
+ COALESCE(MIN (created_at) + ' 3 days' ::interval, NOW())
119
+ FROM
120
+ workspace_agent_stats
112
121
);
113
122
114
123
-- name: GetDeploymentWorkspaceAgentStats :one
You can’t perform that action at this time.
0 commit comments