// Calc and return progress public async Task <NormProgress> GetProgressAsync(Project project, DateTime date) { Norm norm = await GetNormAsync(project); switch (norm.Type) { case NormType.Day: var duration = _timers.GetDuration(_timers.GetTimersInInterval(project, date.Date, date.Date.AddDays(1))); return(new NormProgress(norm, norm.Goal, duration)); case NormType.Week: var startOfWeek = DateHelpers.GetStartOfWeek(date, _projects.GetCulture(project)); // Get [(start of week)..date] duration var curWeekDuration = _timers.GetDuration(_timers.GetTimersInInterval(project, startOfWeek, date)); return(new NormProgress(norm, norm.Goal, curWeekDuration)); case NormType.Month: var startOfMonth = DateHelpers.GetStartOfMonth(date); // Get [(start of month)..date] duration var curMonthDuration = _timers.GetDuration(_timers.GetTimersInInterval(project, startOfMonth, date)); return(new NormProgress(norm, norm.Goal, curMonthDuration)); case NormType.Project: var timers = _timers.GetDuration(_timers.GetTimersInInterval(project, null, date)); return(new NormProgress(norm, norm.Goal, timers)); case NormType.None: return(new NormProgress(norm, new TimeSpan(0), new TimeSpan(0))); } throw new ArgumentException(); }
public async Task <IActionResult> TodayStatistics(int projectId) { var project = await _projects.GetProjectAsync(projectId); if (project == null) { return(NotFound()); } if (!await _authorizationService.AuthorizeAsync(User, project, "IsOwner")) { return(NotFound()); } var now = _timers.GetNowTime(project); var timers = _timers.GetTimersInInterval(project, now.Date, null); var duration = _timers.GetDuration(timers); return(new OkObjectResult(new { timers = timers, duration = duration, isRunning = await _timers.GetOpenedTimerAsync(project) != null, progress = await _norms.GetProgressAsync(project, now.Date) })); }