Skip to content

Commit 1b38dff

Browse files
committed
style: clarified variable naming, moved timePeriod table mapping
1 parent d83accf commit 1b38dff

File tree

7 files changed

+62
-57
lines changed

7 files changed

+62
-57
lines changed

backend/pkg/api/data_access/mobile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ func (d *DataAccessService) GetValidatorDashboardMobileValidators(ctx context.Co
388388
var efficienciesMap map[uint64]float64
389389
wg.Go(func() error {
390390
var err error
391-
clickhouseTable, err := getTablesForPeriod(period)
391+
clickhouseTable, err := period.Table()
392392
if err != nil {
393393
return err
394394
}

backend/pkg/api/data_access/vdb_helpers.go

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ type IncomeInfo struct {
118118

119119
func (d *DataAccessService) getElClAPR(ctx context.Context, dashboardId t.VDBId, groupId int64, timeFrame enums.TimePeriod) (rewardsApr IncomeInfo, err error) {
120120
result := IncomeInfo{}
121-
table, err := getTablesForPeriod(timeFrame)
121+
table, err := timeFrame.Table()
122122
if err != nil {
123123
return result, err
124124
}
@@ -164,7 +164,7 @@ func (d *DataAccessService) getElClAPR(ctx context.Context, dashboardId t.VDBId,
164164
return IncomeInfo{}, nil
165165
}
166166

167-
epochDuration := time.Duration(d.config.Chain.ClConfig.SecondsPerSlot * d.config.Chain.ClConfig.SlotsPerEpoch * uint64(time.Second))
167+
epochDuration := time.Second * time.Duration(d.config.Chain.ClConfig.SecondsPerSlot*d.config.Chain.ClConfig.SlotsPerEpoch)
168168
rewards := big.Int{}
169169
rewards.Sub(rewardsResultTable.RoiDividend, rewardsResultTable.RoiDivisor)
170170
cumulativeClBaseMain := d.convertClToMain(decimal.NewFromBigInt(rewardsResultTable.RoiDivisor, 0))
@@ -204,13 +204,13 @@ func (d *DataAccessService) getElClAPR(ctx context.Context, dashboardId t.VDBId,
204204
}
205205

206206
// precondition: invested amount and rewards are in the same currency
207-
func calcAPR(rewards, cumulativeDivisor decimal.Decimal, epochDuration time.Duration) float64 {
207+
func calcAPR(rewards, cumulativeDivisor decimal.Decimal, duration time.Duration) float64 {
208208
if rewards.IsZero() || cumulativeDivisor.IsZero() {
209209
return 0
210210
}
211-
epochDurationScaleFactor := decimal.NewFromInt(int64(utils.Year.Seconds())).Div(decimal.NewFromInt(int64(epochDuration.Seconds())))
211+
annualizationFactor := decimal.NewFromInt(utils.Year.Nanoseconds()).Div(decimal.NewFromInt(duration.Nanoseconds()))
212212
percentScaleFactor := decimal.NewFromInt(100) // TODO remove BEDS-1147
213-
return rewards.Div(cumulativeDivisor).Mul(epochDurationScaleFactor).Mul(percentScaleFactor).InexactFloat64()
213+
return rewards.Div(cumulativeDivisor).Mul(annualizationFactor).Mul(percentScaleFactor).InexactFloat64()
214214
}
215215

216216
// converts a cl amount to the main currency
@@ -421,7 +421,7 @@ func processSyncCommitteeResults(queryResult []SyncCommitteeResult, currentSyncP
421421

422422
// Retrieves the start epoch for a given time period (last 1h, 24h, 7d, 30d)
423423
func (d *DataAccessService) getEpochStart(ctx context.Context, period enums.TimePeriod) (uint64, error) {
424-
clickhouseTable, err := getTablesForPeriod(period)
424+
clickhouseTable, err := period.Table()
425425
if err != nil {
426426
return 0, err
427427
}
@@ -499,7 +499,7 @@ func buildMinMaxEpochsQuery(dashboardId t.VDBId, groupId int64, clickhouseTable
499499

500500
// GetMinMaxEpochs is the main function that ties all steps together
501501
func (d *DataAccessService) getMinMaxEpochs(ctx context.Context, dashboardId t.VDBId, groupId int64, period enums.TimePeriod) (uint64, uint64, error) {
502-
clickhouseTable, err := getTablesForPeriod(period)
502+
clickhouseTable, err := period.Table()
503503
if err != nil {
504504
return 0, 0, err
505505
}
@@ -542,7 +542,7 @@ func buildLastScheduledBlockAndSyncDateQuery(clickhouseTable string, dashboardId
542542
// Gets last scheduled block/sync committee epoch
543543
func (d *DataAccessService) getLastScheduledBlockAndSyncDate(ctx context.Context, dashboardId t.VDBId, groupId int64) (time.Time, time.Time, error) {
544544
// we need to use clickhouse table for all_time period
545-
clickhouseTotalTable, err := getTablesForPeriod(enums.AllTime)
545+
clickhouseTotalTable, err := enums.AllTime.Table()
546546
if err != nil {
547547
return time.Time{}, time.Time{}, err
548548
}
@@ -645,29 +645,6 @@ func processPastSyncCommitteesResults(validatorIndices []uint64) (map[uint64]uin
645645
return validatorCountMap, nil
646646
}
647647

648-
// Determines the validator dashboard data table
649-
// based on the given time period (1h, 24h, 7d, 30d, all_time)
650-
func getTablesForPeriod(period enums.TimePeriod) (string, error) {
651-
table := ""
652-
653-
switch period {
654-
case enums.TimePeriods.Last1h:
655-
table = "validator_dashboard_data_rolling_1h"
656-
case enums.TimePeriods.Last24h:
657-
table = "validator_dashboard_data_rolling_24h"
658-
case enums.TimePeriods.Last7d:
659-
table = "validator_dashboard_data_rolling_7d"
660-
case enums.TimePeriods.Last30d:
661-
table = "validator_dashboard_data_rolling_30d"
662-
case enums.TimePeriods.AllTime:
663-
table = "validator_dashboard_data_rolling_total"
664-
default:
665-
return "", fmt.Errorf("not-implemented time period: %v", period)
666-
}
667-
668-
return table, nil
669-
}
670-
671648
// Retrieves the validator dashboard data table and corresponding date column
672649
// for a given chart aggregation range (epoch, hourly, daily, weekly)
673650
func getTableAndDateColumn(aggregation enums.ChartAggregation) (string, string, error) {

backend/pkg/api/data_access/vdb_helpers_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ func TestGetEpochStart(t *testing.T) {
277277

278278
for _, tt := range tests {
279279
t.Run(tt.name, func(t *testing.T) {
280-
table, err := getTablesForPeriod(tt.period)
280+
table, err := tt.period.Table()
281281
assert.NoError(t, err)
282282

283283
ds := buildEpochStartQuery(table)
@@ -345,7 +345,7 @@ func TestGetLastScheduledBlockAndSyncDate(t *testing.T) {
345345

346346
for _, tt := range tests {
347347
t.Run(tt.name, func(t *testing.T) {
348-
clickhouseTotalTable, err := getTablesForPeriod(tt.period)
348+
clickhouseTotalTable, err := tt.period.Table()
349349
assert.NoError(t, err)
350350

351351
ds := buildLastScheduledBlockAndSyncDateQuery(clickhouseTotalTable, dashboardId, groupId)
@@ -413,7 +413,7 @@ func TestGetMinMaxEpochs(t *testing.T) {
413413

414414
for _, tt := range tests {
415415
t.Run(tt.name, func(t *testing.T) {
416-
clickhouseTable, err := getTablesForPeriod(tt.period)
416+
clickhouseTable, err := tt.period.Table()
417417
assert.NoError(t, err)
418418

419419
ds := buildMinMaxEpochsQuery(dashboardId, groupId, clickhouseTable)
@@ -546,7 +546,7 @@ func TestGetTable(t *testing.T) {
546546
}
547547
for _, tc := range cases {
548548
t.Run(tc.name, func(t *testing.T) {
549-
result, err := getTablesForPeriod(tc.input)
549+
result, err := tc.input.Table()
550550

551551
assert.Equal(t, tc.expected, result)
552552
if tc.expectErr != nil {

backend/pkg/api/data_access/vdb_management.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ func (d *DataAccessService) GetValidatorDashboardOverview(ctx context.Context, d
377377
return nil
378378
})
379379

380-
retrieveRewardsAndEfficiency := func(table string, timeFrame enums.TimePeriod, rewards *t.ClElValue[decimal.Decimal], apr *t.ClElValue[float64], efficiency *float64) {
380+
retrieveRewardsAndEfficiency := func(timeFrame enums.TimePeriod, rewards *t.ClElValue[decimal.Decimal], apr *t.ClElValue[float64], efficiency *float64) {
381381
// Rewards + APR
382382
eg.Go(func() error {
383383
incomeInfo, err := d.getElClAPR(ctx, dashboardId, -1, timeFrame)
@@ -391,6 +391,10 @@ func (d *DataAccessService) GetValidatorDashboardOverview(ctx context.Context, d
391391

392392
// Efficiency
393393
eg.Go(func() error {
394+
table, err := timeFrame.Table()
395+
if err != nil {
396+
return err
397+
}
394398
ds := goqu.Dialect("postgres").
395399
From(goqu.L(fmt.Sprintf(`%s AS r FINAL`, table))).
396400
With("validators", goqu.L("(SELECT dashboard_id, validator_index FROM users_val_dashboards_validators WHERE dashboard_id = ?)", dashboardId.Id)).
@@ -413,10 +417,10 @@ func (d *DataAccessService) GetValidatorDashboardOverview(ctx context.Context, d
413417
})
414418
}
415419

416-
retrieveRewardsAndEfficiency("validator_dashboard_data_rolling_24h", enums.Last24h, &data.Rewards.Last24h, &data.Apr.Last24h, &data.Efficiency.Last24h)
417-
retrieveRewardsAndEfficiency("validator_dashboard_data_rolling_7d", enums.Last7d, &data.Rewards.Last7d, &data.Apr.Last7d, &data.Efficiency.Last7d)
418-
retrieveRewardsAndEfficiency("validator_dashboard_data_rolling_30d", enums.Last30d, &data.Rewards.Last30d, &data.Apr.Last30d, &data.Efficiency.Last30d)
419-
retrieveRewardsAndEfficiency("validator_dashboard_data_rolling_total", enums.AllTime, &data.Rewards.AllTime, &data.Apr.AllTime, &data.Efficiency.AllTime)
420+
retrieveRewardsAndEfficiency(enums.Last24h, &data.Rewards.Last24h, &data.Apr.Last24h, &data.Efficiency.Last24h)
421+
retrieveRewardsAndEfficiency(enums.Last7d, &data.Rewards.Last7d, &data.Apr.Last7d, &data.Efficiency.Last7d)
422+
retrieveRewardsAndEfficiency(enums.Last30d, &data.Rewards.Last30d, &data.Apr.Last30d, &data.Efficiency.Last30d)
423+
retrieveRewardsAndEfficiency(enums.AllTime, &data.Rewards.AllTime, &data.Apr.AllTime, &data.Efficiency.AllTime)
420424

421425
err = eg.Wait()
422426

backend/pkg/api/data_access/vdb_summary.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (d *DataAccessService) GetValidatorDashboardSummary(ctx context.Context, da
3636
wg := errgroup.Group{}
3737

3838
// Get the table name based on the period
39-
clickhouseTable, err := getTablesForPeriod(period)
39+
clickhouseTable, err := period.Table()
4040
if err != nil {
4141
return nil, nil, err
4242
}
@@ -489,7 +489,7 @@ func (d *DataAccessService) GetValidatorDashboardGroupSummary(ctx context.Contex
489489
}
490490

491491
// Get the table names based on the period
492-
clickhouseTable, err := getTablesForPeriod(period)
492+
clickhouseTable, err := period.Table()
493493
if err != nil {
494494
return nil, err
495495
}
@@ -1219,7 +1219,7 @@ func (d *DataAccessService) GetValidatorDashboardSlashingsSummaryValidators(ctx
12191219
result := &t.VDBSlashingsSummaryValidators{}
12201220

12211221
// Get the table names based on the period
1222-
clickhouseTable, err := getTablesForPeriod(period)
1222+
clickhouseTable, err := period.Table()
12231223
if err != nil {
12241224
return nil, err
12251225
}
@@ -1454,7 +1454,7 @@ func (d *DataAccessService) GetValidatorDashboardProposalSummaryValidators(ctx c
14541454
}
14551455

14561456
// Get the table name based on the period
1457-
clickhouseTable, err := getTablesForPeriod(period)
1457+
clickhouseTable, err := period.Table()
14581458
if err != nil {
14591459
return nil, err
14601460
}

backend/pkg/api/enums/enums.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package enums
22

3-
import "time"
3+
import (
4+
"fmt"
5+
"time"
6+
)
47

58
type Enum interface {
69
Int() int
@@ -159,6 +162,27 @@ func (t TimePeriod) Duration() time.Duration {
159162
}
160163
}
161164

165+
func (t TimePeriod) Table() (string, error) {
166+
table := ""
167+
168+
switch t {
169+
case TimePeriods.Last1h:
170+
table = "validator_dashboard_data_rolling_1h"
171+
case TimePeriods.Last24h:
172+
table = "validator_dashboard_data_rolling_24h"
173+
case TimePeriods.Last7d:
174+
table = "validator_dashboard_data_rolling_7d"
175+
case TimePeriods.Last30d:
176+
table = "validator_dashboard_data_rolling_30d"
177+
case TimePeriods.AllTime:
178+
table = "validator_dashboard_data_rolling_total"
179+
default:
180+
return "", fmt.Errorf("not-implemented time period: %v", t)
181+
}
182+
183+
return table, nil
184+
}
185+
162186
// ----------------
163187
// Validator Duties
164188

backend/pkg/api/services/service_average_network_efficiency.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ func (s *Services) updateEfficiencyData() error {
4848
efficiencyInfo := s.initEfficiencyInfo()
4949
efficiencyMutex := &sync.RWMutex{}
5050

51-
setEfficiencyData := func(tableName string, period enums.TimePeriod) error {
51+
setEfficiencyData := func(period enums.TimePeriod) error {
52+
tableName, err := period.Table()
53+
if err != nil {
54+
return err
55+
}
56+
5257
var queryResult struct {
5358
TotalEfficiency sql.NullFloat64 `db:"total_efficiency"`
5459
AttestationEfficiency sql.NullFloat64 `db:"attestation_efficiency"`
@@ -89,24 +94,19 @@ func (s *Services) updateEfficiencyData() error {
8994
wg := &errgroup.Group{}
9095

9196
wg.Go(func() error {
92-
err := setEfficiencyData("validator_dashboard_data_rolling_1h", enums.TimePeriods.Last1h)
93-
return err
97+
return setEfficiencyData(enums.TimePeriods.Last1h)
9498
})
9599
wg.Go(func() error {
96-
err := setEfficiencyData("validator_dashboard_data_rolling_24h", enums.TimePeriods.Last24h)
97-
return err
100+
return setEfficiencyData(enums.TimePeriods.Last24h)
98101
})
99102
wg.Go(func() error {
100-
err := setEfficiencyData("validator_dashboard_data_rolling_7d", enums.TimePeriods.Last7d)
101-
return err
103+
return setEfficiencyData(enums.TimePeriods.Last7d)
102104
})
103105
wg.Go(func() error {
104-
err := setEfficiencyData("validator_dashboard_data_rolling_30d", enums.TimePeriods.Last30d)
105-
return err
106+
return setEfficiencyData(enums.TimePeriods.Last30d)
106107
})
107108
wg.Go(func() error {
108-
err := setEfficiencyData("validator_dashboard_data_rolling_total", enums.TimePeriods.AllTime)
109-
return err
109+
return setEfficiencyData(enums.TimePeriods.AllTime)
110110
})
111111

112112
err := wg.Wait()

0 commit comments

Comments
 (0)