@@ -2,7 +2,7 @@ package archiver
22
33import (
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