Skip to content

Commit

Permalink
feat(go/event): create func to rebuild stat and split func general re…
Browse files Browse the repository at this point in the history
…build with DRY concept
  • Loading branch information
MikaelVallenet committed Apr 7, 2023
1 parent 787803c commit 8d590a8
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions go/pkg/pwes/rebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,7 @@ func Rebuild(ctx context.Context, apiClient *pwapi.HTTPClient, opts Opts) error
return errcode.ErrNothingToRebuild
}

challengesMap := make(map[int64]*challengeValidation)
var seasonChallengesID []int64
for _, activity := range activities {
if _, ok := challengesMap[activity.SeasonChallengeID]; ok {
challengesMap[activity.SeasonChallengeID].seasonChallenge.NbValidations++
} else {
challengesMap[activity.SeasonChallengeID] = &challengeValidation{&pwdb.SeasonChallenge{ID: activity.SeasonChallengeID, NbValidations: 1}, 0}
seasonChallengesID = append(seasonChallengesID, activity.SeasonChallengeID)
}
}
challengesMap := RebuildNbValidations(activities)

listSeasonChallenges, err := apiClient.AdminListSeasonChallenges(ctx, &pwapi.AdminListSeasonChallenges_Input{Id: seasonChallengesID})
if err != nil {
Expand Down Expand Up @@ -87,3 +78,35 @@ func Rebuild(ctx context.Context, apiClient *pwapi.HTTPClient, opts Opts) error

return nil
}

func RebuildStats(ctx context.Context, apiClient *pwapi.HTTPClient, to *time.Time, seasonID int64) error {
if apiClient == nil {
return errcode.ErrMissingInput
}

res, err := apiClient.AdminListActivities(ctx, &pwapi.AdminListActivities_Input{FilteringPreset: "validations", To: to})
if err != nil {
return errcode.TODO.Wrap(err)
}

activities := res.GetActivities()
if len(activities) == 0 {
return errcode.ErrNothingToRebuild
}

return nil
}

func RebuildNbValidations(activities []*pwdb.Activity) map[int64]*challengeValidation {
challengesMap := make(map[int64]*challengeValidation)
var seasonChallengesID []int64
for _, activity := range activities {
if _, ok := challengesMap[activity.SeasonChallengeID]; ok {
challengesMap[activity.SeasonChallengeID].seasonChallenge.NbValidations++
} else {
challengesMap[activity.SeasonChallengeID] = &challengeValidation{&pwdb.SeasonChallenge{ID: activity.SeasonChallengeID, NbValidations: 1}, 0}
seasonChallengesID = append(seasonChallengesID, activity.SeasonChallengeID)
}
}
return challengesMap
}

0 comments on commit 8d590a8

Please sign in to comment.