Skip to content

Commit 354b6d1

Browse files
committed
fix: use creation ts to determine oldest vdb for archival/removal
1 parent 3b2df71 commit 354b6d1

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

backend/pkg/api/data_access/archiver.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ func (d *DataAccessService) GetValidatorDashboardsCountInfo(ctx context.Context)
5050
FROM users_val_dashboards uvd
5151
LEFT JOIN dashboards_groups dg ON uvd.id = dg.dashboard_id
5252
LEFT JOIN dashboards_validators dv ON uvd.id = dv.dashboard_id
53+
ORDER BY
54+
uvd.created_at ASC
5355
`)
5456
if err != nil {
5557
return nil, err

backend/pkg/archiver/archiver.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package archiver
22

33
import (
44
"context"
5-
"slices"
5+
"sort"
66
"time"
77

88
dataaccess "github.com/gobitfly/beaconchain/pkg/api/data_access"
@@ -80,20 +80,32 @@ func (a *Archiver) updateArchivedStatus() error {
8080
// Check if the user still exceeds the maximum number of active dashboards
8181
dashboardLimit := int(userInfo.PremiumPerks.ValidatorDashboards)
8282
if len(activeDashboards) > dashboardLimit {
83-
slices.Sort(activeDashboards)
84-
for id := 0; id < len(activeDashboards)-dashboardLimit; id++ {
85-
dashboardsToBeArchived = append(dashboardsToBeArchived, t.ArchiverDashboardArchiveReason{DashboardId: activeDashboards[id], ArchivedReason: enums.VDBArchivedReasons.Dashboards})
83+
for _, id := range activeDashboards[:len(activeDashboards)-dashboardLimit] {
84+
dashboardsToBeArchived = append(dashboardsToBeArchived, t.ArchiverDashboardArchiveReason{DashboardId: id, ArchivedReason: enums.VDBArchivedReasons.Dashboards})
8685
}
8786
}
8887

8988
// Check if the user exceeds the maximum number of archived dashboards
9089
archivedLimit := handlers.MaxArchivedDashboardsCount
9190
if len(archivedDashboards)+len(dashboardsToBeArchived) > archivedLimit {
91+
// 1. merge current and about-to-be-archived dashboards
9292
dashboardsToBeDeletedForUser := archivedDashboards
9393
for _, dashboard := range dashboardsToBeArchived {
9494
dashboardsToBeDeletedForUser = append(dashboardsToBeDeletedForUser, dashboard.DashboardId)
9595
}
96-
slices.Sort(dashboardsToBeDeletedForUser)
96+
97+
// 2. sort by creation date
98+
// messy because collected data containers only contain id
99+
// -> HACK: create temp index map to original dashboard list (which was sorted by DB)
100+
indices := make(map[uint64]int, len(dashboards)) // dashboard id -> index in sorted "dashboards" list
101+
for i, v := range dashboards {
102+
indices[v.DashboardId] = i
103+
}
104+
sort.Slice(dashboardsToBeDeletedForUser, func(i, j int) bool {
105+
return indices[dashboardsToBeDeletedForUser[i]] < indices[dashboardsToBeDeletedForUser[j]]
106+
})
107+
108+
// 3. remove oldest dashboards
97109
dashboardsToBeDeletedForUser = dashboardsToBeDeletedForUser[:len(dashboardsToBeDeletedForUser)-archivedLimit]
98110
dashboardsToBeDeleted = append(dashboardsToBeDeleted, dashboardsToBeDeletedForUser...)
99111
}

0 commit comments

Comments
 (0)