func averageResulter(ctx context.Context, result model.KeyedResult, challenge model.Challenge) error { tasks := make([]model.Task, len(challenge.Tasks)) if err := datastore.GetMulti(ctx, challenge.Tasks, tasks); err != nil { return err } var user model.User if err := datastore.Get(ctx, result.Key.Parent().Parent(), &user); err != nil { return err } var nrOfComputations float64 average := model.Skills{} for i, task := range tasks { taskResult, err := Tasker(task.Tasker).Call(ctx, result, *task.Key(challenge.Tasks[i]), user, result.StartTimes[getTaskIndex(challenge, challenge.Tasks[i])]) if err != nil { // TODO: ignore error for now. We`ll treat it after we have all the taskers available //return err } else { average = average.Add(taskResult) nrOfComputations++ } } result.Skills = average.DivBy(nrOfComputations) result.Computed = time.Now() _, err := result.Put(ctx, result.Key) return err }
func init() { RegisterResulter(Average, func(ctx context.Context, resultKey *datastore.Key) error { var result model.Result if err := datastore.Get(ctx, resultKey, &result); err != nil { return err } var challenge model.Challenge if err := datastore.Get(ctx, result.Challenge, &challenge); err != nil { return err } var tasks model.Tasks if err := datastore.GetMulti(ctx, challenge.Tasks, &tasks); err != nil { return err } weightSum := model.Skills{} // this could be SkillWeights, but would need more conversions average := model.Skills{} for i, task := range tasks { taskResult, err := Tasker(task.Tasker).Call(ctx, challenge.Tasks[i], resultKey.Parent().Parent()) if err != nil { return err } average = average.Add(taskResult.Mul(model.Skills(task.SkillWeights))) weightSum = weightSum.Add(model.Skills(task.SkillWeights)) } result.Skills = average.Div(weightSum) result.Computed = time.Now() _, err := result.Put(ctx, resultKey) return err }) }