Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix epoch smeshers stats #158

Merged
merged 3 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions internal/storage/storagereader/epochs.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ func (s *Reader) GetEpochs(ctx context.Context, query *bson.D, opts ...*options.
epoch.Stats.Current.RewardsNumber = count
epoch.Stats.Cumulative.Rewards = total
epoch.Stats.Cumulative.RewardsNumber = count

atxCount, err := s.CountActivations(context.Background(), &bson.D{{Key: "targetEpoch", Value: epoch.Number}})
if err != nil {
return nil, err
}
epoch.Stats.Current.Smeshers = atxCount
}

return epochs, nil
Expand All @@ -63,11 +69,21 @@ func (s *Reader) GetEpoch(ctx context.Context, epochNumber int) (*model.Epoch, e
total, count, err := s.GetTotalRewards(context.TODO(), &bson.D{{Key: "layer", Value: bson.D{
{Key: "$gte", Value: epoch.LayerStart}, {Key: "$lte", Value: epoch.LayerEnd}}},
})
if err != nil {
return nil, err
}

epoch.Stats.Current.Rewards = total
epoch.Stats.Current.RewardsNumber = count
epoch.Stats.Cumulative.Rewards = total
epoch.Stats.Cumulative.RewardsNumber = count

atxCount, err := s.CountActivations(context.Background(), &bson.D{{Key: "targetEpoch", Value: epoch.Number}})
if err != nil {
return nil, err
}
epoch.Stats.Current.Smeshers = atxCount
epoch.Stats.Cumulative.Smeshers = atxCount

return epoch, nil
}
23 changes: 23 additions & 0 deletions storage/reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,26 @@ func (s *Storage) SaveReward(parent context.Context, in *model.Reward) error {
}
return err
}

func (s *Storage) GetRewardsTotalSum(ctx context.Context) (int64, error) {
groupStage := bson.D{
{Key: "$group", Value: bson.D{
{Key: "_id", Value: ""},
{Key: "totalRewards", Value: bson.D{
{Key: "$sum", Value: "$total"},
},
},
}},
}
cursor, err := s.db.Collection("rewards").Aggregate(ctx, mongo.Pipeline{groupStage})
if err != nil {
return 0, err
}

if !cursor.Next(ctx) {
log.Info("GetSmesherRewards: Empty result")
return 0, nil
}
doc := cursor.Current
return utils.GetAsInt64(doc.Lookup("totalRewards")), nil
}
Loading