Skip to content

Commit

Permalink
fix(dashboard): fix missed el rewards in group summary for a period
Browse files Browse the repository at this point in the history
* See BEDS-1165
  • Loading branch information
sasha-bitfly committed Jan 21, 2025
1 parent 0ed1e97 commit e59c0cb
Showing 1 changed file with 49 additions and 16 deletions.
65 changes: 49 additions & 16 deletions backend/pkg/api/data_access/vdb_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,39 @@ func (d *DataAccessService) GetValidatorDashboardGroupSummary(ctx context.Contex
return nil, err
}

getMinMaxEpochs := func() (uint64, uint64, error) {
ds := goqu.Dialect("postgres").
Select(
goqu.L("MIN(epoch_start) as min_epoch_start"),
goqu.L("MAX(epoch_end) as max_epoch_end")).
From(goqu.L(fmt.Sprintf(`%s AS r FINAL`, clickhouseTable)))

if dashboardId.Validators == nil {
ds = ds.
With("validators", goqu.L("(SELECT validator_index as validator_index, group_id FROM users_val_dashboards_validators WHERE dashboard_id = ? AND (group_id = ? OR ?::smallint = -1))", dashboardId.Id, groupId, groupId)).
InnerJoin(goqu.L("validators v"), goqu.On(goqu.L("r.validator_index = v.validator_index"))).
Where(goqu.L("validator_index IN (SELECT validator_index FROM validators)"))
} else {
ds = ds.
Where(goqu.L("validator_index IN ?", dashboardId.Validators))
}

query, args, err := ds.Prepared(true).ToSQL()
if err != nil {
return 0, 0, err
}

var epoch_row struct {
MinEpochStart *uint64 `db:"min_epoch_start"`
MaxEpochEnd *uint64 `db:"max_epoch_end"`
}
err = d.clickhouseReader.GetContext(ctx, &epoch_row, query, args...)
if err != nil {
return 0, 0, err
}
return *epoch_row.MinEpochStart, *epoch_row.MaxEpochEnd, nil
}

getMissedELRewards := func(epochStart, epochEnd uint64) (float64, error) {
// Initialize the result variable
var totalMissedRewardsEl float64
Expand Down Expand Up @@ -599,19 +632,17 @@ func (d *DataAccessService) GetValidatorDashboardGroupSummary(ctx context.Contex
return totalMissedRewardsEl, nil
}

getLastScheduledBlockAndSyncDate := func() (time.Time, time.Time, uint64, uint64, error) {
getLastScheduledBlockAndSyncDate := func() (time.Time, time.Time, error) {
// we need to go to the all time table for last scheduled block/sync committee epoch
clickhouseTotalTable, _, err := d.getTablesForPeriod(enums.AllTime)
if err != nil {
return time.Time{}, time.Time{}, 0, 0, err
return time.Time{}, time.Time{}, err
}

ds := goqu.Dialect("postgres").
Select(
goqu.L("MAX(last_scheduled_block_epoch) as last_scheduled_block_epoch"),
goqu.L("MAX(last_scheduled_sync_epoch) as last_scheduled_sync_epoch"),
goqu.L("MIN(epoch_start) as min_epoch_start"),
goqu.L("MAX(epoch_end) as max_epoch_end")).
goqu.L("MAX(last_scheduled_sync_epoch) as last_scheduled_sync_epoch")).
From(goqu.L(fmt.Sprintf(`%s AS r FINAL`, clickhouseTotalTable)))

if dashboardId.Validators == nil {
Expand All @@ -626,28 +657,24 @@ func (d *DataAccessService) GetValidatorDashboardGroupSummary(ctx context.Contex

query, args, err := ds.Prepared(true).ToSQL()
if err != nil {
return time.Time{}, time.Time{}, 0, 0, err
return time.Time{}, time.Time{}, err
}

var row struct {
LastScheduledBlockEpoch *int64 `db:"last_scheduled_block_epoch"`
LastSyncEpoch *int64 `db:"last_scheduled_sync_epoch"`
MinEpochStart *uint64 `db:"min_epoch_start"`
MaxEpochEnd *uint64 `db:"max_epoch_end"`
LastScheduledBlockEpoch *int64 `db:"last_scheduled_block_epoch"`
LastSyncEpoch *int64 `db:"last_scheduled_sync_epoch"`
}
err = d.clickhouseReader.GetContext(ctx, &row, query, args...)
if err != nil {
return time.Time{}, time.Time{}, 0, 0, err
return time.Time{}, time.Time{}, err
}

if row.LastScheduledBlockEpoch == nil || row.LastSyncEpoch == nil {
return time.Time{}, time.Time{}, 0, 0, nil
return time.Time{}, time.Time{}, nil
}

return utils.EpochToTime(uint64(*row.LastScheduledBlockEpoch)),
utils.EpochToTime(uint64(*row.LastSyncEpoch)),
*row.MinEpochStart,
*row.MaxEpochEnd,
nil
}

Expand Down Expand Up @@ -738,11 +765,17 @@ func (d *DataAccessService) GetValidatorDashboardGroupSummary(ctx context.Contex
return nil
})

var minEpochStart, maxEpochEnd uint64
var lastBlockTs, lastSyncTs time.Time
errGroup.Go(func() error {
var err error
lastBlockTs, lastSyncTs, minEpochStart, maxEpochEnd, err = getLastScheduledBlockAndSyncDate()
lastBlockTs, lastSyncTs, err = getLastScheduledBlockAndSyncDate()
return err
})

var minEpochStart, maxEpochEnd uint64
errGroup.Go(func() error {
var err error
minEpochStart, maxEpochEnd, err = getMinMaxEpochs()
return err
})

Expand Down

0 comments on commit e59c0cb

Please sign in to comment.