@@ -345,8 +345,8 @@ func (d *DataAccessService) GetValidatorDashboardSummary(ctx context.Context, da
345345 total .Reward .El = total .Reward .El .Add (resultEntry .Reward .El )
346346
347347 // Efficiency
348- if queryEntry .EfficiencyDivisor .Valid && ! queryEntry . EfficiencyDivisor . Decimal . IsZero () {
349- resultEntry .Efficiency = queryEntry .EfficiencyDividend .Decimal . Div ( queryEntry .EfficiencyDivisor .Decimal ). InexactFloat64 () * 100
348+ if queryEntry .EfficiencyDivisor .Valid {
349+ resultEntry .Efficiency = calcEfficiency ( queryEntry .EfficiencyDividend .Decimal , queryEntry .EfficiencyDivisor .Decimal )
350350 }
351351
352352 // Add the duties info to the total
@@ -454,9 +454,7 @@ func (d *DataAccessService) GetValidatorDashboardSummary(ctx context.Context, da
454454 totalEntry .Proposals .Failed = total .BlocksScheduled - total .BlocksProposed
455455
456456 // Efficiency
457- if ! total .EfficiencyDivisor .IsZero () {
458- totalEntry .Efficiency = total .EfficiencyDividend .Div (total .EfficiencyDivisor ).InexactFloat64 () * 100
459- }
457+ totalEntry .Efficiency = calcEfficiency (total .EfficiencyDividend , total .EfficiencyDivisor )
460458
461459 result = append ([]t.VDBSummaryTableRow {totalEntry }, result ... )
462460 }
@@ -793,21 +791,10 @@ func (d *DataAccessService) GetValidatorDashboardGroupSummary(ctx context.Contex
793791 }
794792 }
795793
796- if ! totalEfficiencyTotalDivisor .IsZero () {
797- ret .Efficiency = totalEfficiencyTotalDividend .Div (totalEfficiencyTotalDivisor ).InexactFloat64 () * 100
798- }
799-
800- if ! totalEfficiencyAttestationsDivisor .IsZero () {
801- ret .AttestationEfficiency = totalEfficiencyAttestationsDividend .Div (totalEfficiencyAttestationsDivisor ).InexactFloat64 () * 100
802- }
803-
804- if ! totalEfficiencyProposalsDivisor .IsZero () {
805- ret .ProposalEfficiency = totalEfficiencyProposalsDividend .Div (totalEfficiencyProposalsDivisor ).InexactFloat64 () * 100
806- }
807-
808- if ! totalEfficiencySyncDivisor .IsZero () {
809- ret .SyncEfficiency = totalEfficiencySyncDividend .Div (totalEfficiencySyncDivisor ).InexactFloat64 () * 100
810- }
794+ ret .Efficiency = calcEfficiency (totalEfficiencyTotalDividend , totalEfficiencyTotalDivisor )
795+ ret .AttestationEfficiency = calcEfficiency (totalEfficiencyAttestationsDividend , totalEfficiencyAttestationsDivisor )
796+ ret .ProposalEfficiency = calcEfficiency (totalEfficiencyProposalsDividend , totalEfficiencyProposalsDivisor )
797+ ret .SyncEfficiency = calcEfficiency (totalEfficiencySyncDividend , totalEfficiencySyncDivisor )
811798
812799 rpOperatorInfo , err := d .getValidatorDashboardRpOperatorInfo (ctx , dashboardId )
813800 if err != nil {
@@ -826,6 +813,13 @@ func (d *DataAccessService) GetValidatorDashboardGroupSummary(ctx context.Contex
826813 return ret , nil
827814}
828815
816+ func calcEfficiency (dividend , divisor decimal.Decimal ) float64 {
817+ if divisor .IsZero () {
818+ return 0
819+ }
820+ return dividend .Div (divisor ).InexactFloat64 () * 100
821+ }
822+
829823// for summary charts: series id is group id, no stack
830824
831825func (d * DataAccessService ) GetValidatorDashboardSummaryChart (ctx context.Context , dashboardId t.VDBId , groupIds []int64 , efficiency enums.VDBSummaryChartEfficiencyType , aggregation enums.ChartAggregation , afterTs uint64 , beforeTs uint64 ) (* t.ChartData [int , float64 ], error ) {
@@ -947,11 +941,10 @@ func (d *DataAccessService) GetValidatorDashboardSummaryChart(ctx context.Contex
947941 }
948942
949943 if ! dashboardId .AggregateGroups && requestedGroupsMap [row .GroupId ] {
950- if row .EfficiencyDivisor . IsZero () {
951- data [row.Timestamp ][row.GroupId ] = 0
944+ data [ row.Timestamp ][row. GroupId ] = calcEfficiency ( row . EfficiencyDividend , row . EfficiencyDivisor )
945+ if data [row.Timestamp ][row.GroupId ] == 0 {
952946 continue
953947 }
954- data [row.Timestamp ][row.GroupId ] = row .EfficiencyDividend .Div (row .EfficiencyDivisor ).InexactFloat64 () * 100
955948 if data [row.Timestamp ][row.GroupId ] > 100 {
956949 log .Error (nil , "efficiency is greater than 100%" , 0 , map [string ]interface {}{"efficiency" : efficiency })
957950 data [row.Timestamp ][row.GroupId ] = 100
@@ -993,11 +986,10 @@ func (d *DataAccessService) GetValidatorDashboardSummaryChart(ctx context.Contex
993986 totalLineGroupId = t .DefaultGroupId
994987 }
995988 for _ , row := range totalEfficiencyMap {
996- if row .EfficiencyDivisor . IsZero () {
997- data [row.Timestamp ][totalLineGroupId ] = 0
989+ data [row. Timestamp ][ totalLineGroupId ] = calcEfficiency ( row .EfficiencyDividend , row . EfficiencyDivisor )
990+ if data [row.Timestamp ][totalLineGroupId ] == 0 {
998991 continue
999992 }
1000- data [row.Timestamp ][totalLineGroupId ] = row .EfficiencyDividend .Div (row .EfficiencyDivisor ).InexactFloat64 () * 100
1001993 if data [row.Timestamp ][totalLineGroupId ] > 100 {
1002994 log .Error (nil , "efficiency is greater than 100%" , 0 , map [string ]interface {}{"efficiency" : efficiency })
1003995 data [row.Timestamp ][totalLineGroupId ] = 100
0 commit comments