public async Task <ActionResult> DaySummary(DateTime summaryDate) { var intakes = await unit.IngredientIntakeRepository.GetIntakesForDay(summaryDate); var exercises = await unit.ExerciseRepository.GetExercisesForDayAsync(summaryDate); decimal allowedIntake = 2200m; allowedIntake += exercises.Sum(e => e.CountedCalories) - intakes.Sum(i => i.Calories); var model = new DaySummaryModel() { Calories = intakes.Sum(i => i.Calories), Weight = (await unit.WeightRepository.GetWeightAtSpecifiedDate(summaryDate))?.Value, AllowedIntakeLeft = allowedIntake, ExerciseCalories = exercises.Sum(e => e.CountedCalories) }; return(Ok(model)); }
private void AnalyzeTimeReportedFromLogs(ObservableCollection <DaySummaryModel> days) { DateTime nowLocalTime = DateTime.Now; DateTime endOfTodayLocal = new DateTime(nowLocalTime.Year, nowLocalTime.Month, nowLocalTime.Day, 23, 59, 59, DateTimeKind.Local); DateTime endOfRange = endOfTodayLocal; int daysIndex = 0; // Figure out how much total time has been reported across all active cards double timeAccountedFor = 0.0; foreach (WorkItem item in WorkItems) { double drilledUp = (item.Cost - item.CommittedCost); timeAccountedFor += drilledUp; } // Prepend an entry for time that is reported but not committed if (timeAccountedFor > 0) { string formattedDate = nowLocalTime.ToString("D"); Logic.TimeSpentTrace($"Prepending {timeAccountedFor} spent on {formattedDate} for pending items"); DaySummaryModel dayModel = days[daysIndex]; dayModel.Date = nowLocalTime; dayModel.Amount = timeAccountedFor; dayModel.Details = "Pending"; daysIndex++; } TimeSpan increment = TimeSpan.FromDays(1); for (int i = 0; i < DaySummaryRange; i++) { DateTime beginningOfRange = endOfRange - increment; Logic.TimeSpentTrace($"Looking for time spent between {beginningOfRange} and {endOfRange}"); List <string> subLog = new List <string>(); double timeSpentInRange = AnalyzeTimeReportedFromLogs(beginningOfRange, endOfRange, subLog); if (timeSpentInRange > 0) { string formattedDate = endOfRange.ToString("D"); Logic.TimeSpentTrace($"{timeSpentInRange} spent on {formattedDate}"); string details = $"{timeSpentInRange} spent on {formattedDate}"; foreach (string entry in subLog) { Logic.TimeSpentTrace(" " + entry); details += "\n" + entry; } if (daysIndex < days.Count) { DaySummaryModel dayModel = days[daysIndex]; // The logic above may prepend a day for pending items. If there happen to be edits in the same day we should collapse them into one entry. if ((daysIndex > 0) && (days[daysIndex - 1].Date.DayOfYear == endOfRange.DayOfYear)) { dayModel = days[daysIndex - 1]; dayModel.Amount += timeSpentInRange; dayModel.Details = details; } else { dayModel.Date = endOfRange; dayModel.Amount = timeSpentInRange; dayModel.Details = details; daysIndex++; } } } endOfRange = beginningOfRange; } for (int i = daysIndex; i < days.Count; i++) { DaySummaryModel dayModel = days[i]; dayModel.Date = DateTime.Now; dayModel.Amount = 0.0; dayModel.Details = null; } }