diff --git a/satellite/accounting/db.go b/satellite/accounting/db.go index a94d41bbb..bededc7f1 100644 --- a/satellite/accounting/db.go +++ b/satellite/accounting/db.go @@ -197,7 +197,7 @@ type StoragenodeAccounting interface { type ProjectAccounting interface { // SaveTallies saves the latest project info SaveTallies(ctx context.Context, intervalStart time.Time, bucketTallies map[metabase.BucketLocation]*BucketTally) error - // GetTallies retrieves all tallies + // GetTallies retrieves all tallies ordered by interval start desc GetTallies(ctx context.Context) ([]BucketTally, error) // CreateStorageTally creates a record for BucketStorageTally in the accounting DB table CreateStorageTally(ctx context.Context, tally BucketStorageTally) error diff --git a/satellite/accounting/tally/tally_test.go b/satellite/accounting/tally/tally_test.go index 0882f0c09..8d18cb3d4 100644 --- a/satellite/accounting/tally/tally_test.go +++ b/satellite/accounting/tally/tally_test.go @@ -318,6 +318,16 @@ func TestTallyOnCopiedObject(t *testing.T) { }, } + findTally := func(bucket string, tallies []accounting.BucketTally) accounting.BucketTally { + for _, v := range tallies { + if v.BucketName == bucket { + return v + } + } + t.Fatalf("unable to find tally for %s", bucket) + return accounting.BucketTally{} + } + for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { err := planet.Uplinks[0].CreateBucket(ctx, planet.Satellites[0], tc.name) @@ -340,7 +350,7 @@ func TestTallyOnCopiedObject(t *testing.T) { tallies, err := planet.Satellites[0].DB.ProjectAccounting().GetTallies(ctx) require.NoError(t, err) - lastTally := tallies[len(tallies)-1] + lastTally := findTally(tc.name, tallies) require.Equal(t, tc.name, lastTally.BucketName) require.Equal(t, tc.expectedTallyAfterCopy.ObjectCount, lastTally.ObjectCount) require.Equal(t, tc.expectedTallyAfterCopy.TotalBytes, lastTally.TotalBytes) @@ -354,7 +364,7 @@ func TestTallyOnCopiedObject(t *testing.T) { tallies, err = planet.Satellites[0].DB.ProjectAccounting().GetTallies(ctx) require.NoError(t, err) - lastTally = tallies[len(tallies)-1] + lastTally = findTally(tc.name, tallies) require.Equal(t, tc.name, lastTally.BucketName) require.Equal(t, tc.expectedTallyAfterDelete.ObjectCount, lastTally.ObjectCount) require.Equal(t, tc.expectedTallyAfterDelete.TotalBytes, lastTally.TotalBytes) diff --git a/satellite/satellitedb/dbx/satellitedb.dbx b/satellite/satellitedb/dbx/satellitedb.dbx index fa62e9f73..01b6b6568 100644 --- a/satellite/satellitedb/dbx/satellitedb.dbx +++ b/satellite/satellitedb/dbx/satellitedb.dbx @@ -585,6 +585,7 @@ model bucket_storage_tally ( read all ( select bucket_storage_tally + orderby desc bucket_storage_tally.interval_start ) read all ( diff --git a/satellite/satellitedb/dbx/satellitedb.dbx.go b/satellite/satellitedb/dbx/satellitedb.dbx.go index e019fb8bb..77e339617 100644 --- a/satellite/satellitedb/dbx/satellitedb.dbx.go +++ b/satellite/satellitedb/dbx/satellitedb.dbx.go @@ -13018,11 +13018,11 @@ func (obj *pgxImpl) Paged_BucketBandwidthRollupArchive_By_IntervalStart_GreaterO } -func (obj *pgxImpl) All_BucketStorageTally(ctx context.Context) ( +func (obj *pgxImpl) All_BucketStorageTally_OrderBy_Desc_IntervalStart(ctx context.Context) ( rows []*BucketStorageTally, err error) { defer mon.Task()(&ctx)(&err) - var __embed_stmt = __sqlbundle_Literal("SELECT bucket_storage_tallies.bucket_name, bucket_storage_tallies.project_id, bucket_storage_tallies.interval_start, bucket_storage_tallies.total_bytes, bucket_storage_tallies.inline, bucket_storage_tallies.remote, bucket_storage_tallies.total_segments_count, bucket_storage_tallies.remote_segments_count, bucket_storage_tallies.inline_segments_count, bucket_storage_tallies.object_count, bucket_storage_tallies.metadata_size FROM bucket_storage_tallies") + var __embed_stmt = __sqlbundle_Literal("SELECT bucket_storage_tallies.bucket_name, bucket_storage_tallies.project_id, bucket_storage_tallies.interval_start, bucket_storage_tallies.total_bytes, bucket_storage_tallies.inline, bucket_storage_tallies.remote, bucket_storage_tallies.total_segments_count, bucket_storage_tallies.remote_segments_count, bucket_storage_tallies.inline_segments_count, bucket_storage_tallies.object_count, bucket_storage_tallies.metadata_size FROM bucket_storage_tallies ORDER BY bucket_storage_tallies.interval_start DESC") var __values []interface{} @@ -19406,11 +19406,11 @@ func (obj *pgxcockroachImpl) Paged_BucketBandwidthRollupArchive_By_IntervalStart } -func (obj *pgxcockroachImpl) All_BucketStorageTally(ctx context.Context) ( +func (obj *pgxcockroachImpl) All_BucketStorageTally_OrderBy_Desc_IntervalStart(ctx context.Context) ( rows []*BucketStorageTally, err error) { defer mon.Task()(&ctx)(&err) - var __embed_stmt = __sqlbundle_Literal("SELECT bucket_storage_tallies.bucket_name, bucket_storage_tallies.project_id, bucket_storage_tallies.interval_start, bucket_storage_tallies.total_bytes, bucket_storage_tallies.inline, bucket_storage_tallies.remote, bucket_storage_tallies.total_segments_count, bucket_storage_tallies.remote_segments_count, bucket_storage_tallies.inline_segments_count, bucket_storage_tallies.object_count, bucket_storage_tallies.metadata_size FROM bucket_storage_tallies") + var __embed_stmt = __sqlbundle_Literal("SELECT bucket_storage_tallies.bucket_name, bucket_storage_tallies.project_id, bucket_storage_tallies.interval_start, bucket_storage_tallies.total_bytes, bucket_storage_tallies.inline, bucket_storage_tallies.remote, bucket_storage_tallies.total_segments_count, bucket_storage_tallies.remote_segments_count, bucket_storage_tallies.inline_segments_count, bucket_storage_tallies.object_count, bucket_storage_tallies.metadata_size FROM bucket_storage_tallies ORDER BY bucket_storage_tallies.interval_start DESC") var __values []interface{} @@ -23715,15 +23715,6 @@ func (rx *Rx) Rollback() (err error) { return err } -func (rx *Rx) All_BucketStorageTally(ctx context.Context) ( - rows []*BucketStorageTally, err error) { - var tx *Tx - if tx, err = rx.getTx(ctx); err != nil { - return - } - return tx.All_BucketStorageTally(ctx) -} - func (rx *Rx) All_BucketStorageTally_By_ProjectId_And_BucketName_And_IntervalStart_GreaterOrEqual_And_IntervalStart_LessOrEqual_OrderBy_Desc_IntervalStart(ctx context.Context, bucket_storage_tally_project_id BucketStorageTally_ProjectId_Field, bucket_storage_tally_bucket_name BucketStorageTally_BucketName_Field, @@ -23737,6 +23728,15 @@ func (rx *Rx) All_BucketStorageTally_By_ProjectId_And_BucketName_And_IntervalSta return tx.All_BucketStorageTally_By_ProjectId_And_BucketName_And_IntervalStart_GreaterOrEqual_And_IntervalStart_LessOrEqual_OrderBy_Desc_IntervalStart(ctx, bucket_storage_tally_project_id, bucket_storage_tally_bucket_name, bucket_storage_tally_interval_start_greater_or_equal, bucket_storage_tally_interval_start_less_or_equal) } +func (rx *Rx) All_BucketStorageTally_OrderBy_Desc_IntervalStart(ctx context.Context) ( + rows []*BucketStorageTally, err error) { + var tx *Tx + if tx, err = rx.getTx(ctx); err != nil { + return + } + return tx.All_BucketStorageTally_OrderBy_Desc_IntervalStart(ctx) +} + func (rx *Rx) All_CoinpaymentsTransaction_By_UserId_OrderBy_Desc_CreatedAt(ctx context.Context, coinpayments_transaction_user_id CoinpaymentsTransaction_UserId_Field) ( rows []*CoinpaymentsTransaction, err error) { @@ -25338,9 +25338,6 @@ func (rx *Rx) Update_User_By_Id(ctx context.Context, } type Methods interface { - All_BucketStorageTally(ctx context.Context) ( - rows []*BucketStorageTally, err error) - All_BucketStorageTally_By_ProjectId_And_BucketName_And_IntervalStart_GreaterOrEqual_And_IntervalStart_LessOrEqual_OrderBy_Desc_IntervalStart(ctx context.Context, bucket_storage_tally_project_id BucketStorageTally_ProjectId_Field, bucket_storage_tally_bucket_name BucketStorageTally_BucketName_Field, @@ -25348,6 +25345,9 @@ type Methods interface { bucket_storage_tally_interval_start_less_or_equal BucketStorageTally_IntervalStart_Field) ( rows []*BucketStorageTally, err error) + All_BucketStorageTally_OrderBy_Desc_IntervalStart(ctx context.Context) ( + rows []*BucketStorageTally, err error) + All_CoinpaymentsTransaction_By_UserId_OrderBy_Desc_CreatedAt(ctx context.Context, coinpayments_transaction_user_id CoinpaymentsTransaction_UserId_Field) ( rows []*CoinpaymentsTransaction, err error) diff --git a/satellite/satellitedb/projectaccounting.go b/satellite/satellitedb/projectaccounting.go index 1fe7bc481..afa7013e9 100644 --- a/satellite/satellitedb/projectaccounting.go +++ b/satellite/satellitedb/projectaccounting.go @@ -74,11 +74,11 @@ func (db *ProjectAccounting) SaveTallies(ctx context.Context, intervalStart time return Error.Wrap(err) } -// GetTallies saves the latest bucket info. +// GetTallies retrieves all tallies ordered by interval start (descending). func (db *ProjectAccounting) GetTallies(ctx context.Context) (tallies []accounting.BucketTally, err error) { defer mon.Task()(&ctx)(&err) - dbxTallies, err := db.db.All_BucketStorageTally(ctx) + dbxTallies, err := db.db.All_BucketStorageTally_OrderBy_Desc_IntervalStart(ctx) if err != nil { return nil, Error.Wrap(err) }