// 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> Statistics(int projectId, [FromQuery] DateTime start, [FromQuery] DateTime?end, [FromQuery] List <string> groupBy) { var project = await _projects.GetProjectAsync(projectId); if (project == null) { return(NotFound()); } if (!await _authorizationService.AuthorizeAsync(User, project, "IsOwner")) { return(NotFound()); } TimeZoneInfo timeZone = _projects.GetTimeZone(project); var timers = _timers.GetTimersInInterval(project, start, end); IQueryable grouped = null; if (groupBy.Count != 0) { TimersGroupFlags groupByFlag = TimersGroupFlags.none; if (groupBy.Contains("day")) { groupByFlag |= TimersGroupFlags.day; } if (groupBy.Contains("month")) { groupByFlag |= TimersGroupFlags.month; } grouped = _timers.GroupTimers(timers, groupByFlag); } return(new OkObjectResult(grouped ?? timers)); }