Ejemplo n.º 1
0
        //---------------------------------------------------------------------------------------------

        private async Task PopulateWithGoalCompletionStats(int goalId, GoalPeriodStatsData stats)
        {
            // All periods.
            var results = await _dbQuery.ExecuteSql <int>($"EXEC dbo.sp_CalculateGoalAverageCompletionAcrossAllPeriods {goalId}");

            if (results.Count > 0)
            {
                stats.AverageCompletionAcrossAllPeriods = results[0];
            }
            else
            {
                _log.LogError($"Failed to retrieve goal average completion across all periods stats for goal {goalId}.");
            }

            // Last 3 periods.
            results = await _dbQuery.ExecuteSql <int>($"EXEC dbo.sp_CalculateGoalAverageCompletionAcrossLast3Periods {goalId}");

            if (results.Count > 0)
            {
                stats.AverageCompletionAcrossLast3Periods = results[0];
            }
            else
            {
                _log.LogError($"Failed to retrieve goal average completion across last 3 periods stats for goal {goalId}.");
            }
        }
Ejemplo n.º 2
0
        public async Task <IHttpActionResult> GetPeriodStatsAsync(int goalId)
        {
            _log.LogDebug($"Request: goalId={goalId}");

            User user;

            try
            {
                user = await ControllerUtils.GetUserForRequestHeaderTokenAsync(this, _tokenStore, _userStore, _log);
            }
            catch (AuthenticationException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (InternalServerException)
            {
                return(InternalServerError());
            }

            Goal goal = await _goalStore.GetGoalAsync(goalId);

            if (goal == null || goal.UserId != user.Id)
            {
                _log.LogDebug($"Goal {goalId} not found for user {user.Id}.");
                return(NotFound());
            }

            GetPeriodBoundsForTime(goal, _dateTime.Now, out DateTime periodStart, out DateTime periodEnd);

            List <GoalAttempt> attemptsInDateOrder = await GetAttemptsForPeriod(goalId, periodStart, periodEnd);

            attemptsInDateOrder.Sort((a1, a2) => a1.Timestamp.CompareTo(a2.Timestamp));

            var mostRecentAttempt = attemptsInDateOrder.LastOrDefault(a => true);

            var stats = new GoalPeriodStatsData
            {
                GoalId             = goal.Id,
                PeriodStart        = periodStart,
                PeriodEnd          = periodEnd,
                AttemptCount       = attemptsInDateOrder.Count,
                LastAttemptDate    = mostRecentAttempt?.Timestamp,
                TargetAttemptCount = goal.FrequencyWithinPeriod
            };

            await PopulateWithGoalCompletionStats(goalId, stats);

            _log.LogDebug($"Retrieved stats for user {user.Id}, goal {goalId} : {JsonConvert.SerializeObject(stats)}");

            return(Ok(JsonConvert.SerializeObject(stats)));
        }