Exemplo n.º 1
0
        /// <summary>
        /// Query to generate a list of activities which match given criteria
        /// </summary>
        /// <param name="userId">ASP.NET Identity Id</param>
        /// <param name="sport">Run, Ride, Swim, Other, All</param>
        /// <param name="start">Beginning of date range</param>
        /// <param name="end">End of date range</param>
        /// <returns></returns>
        public IQueryable <ActivityDto> GetSportSummaryQuery(string userId, SportType sport, DateTime start, DateTime end)
        {
            ActivityRepository activityRepo = new ActivityRepository(_context);

            IQueryable <ActivityDto> activityQuery = activityRepo.ActivitiesBySport(userId, sport)
                                                     .Include(r => r.ActivityType)
                                                     .Where(r => r.Start >= start && r.Start <= end)
                                                     .Select(r => new ActivityDto
            {
                Id                = r.Id,
                Name              = r.Name,
                ActivityTypeId    = r.ActivityTypeId,
                MovingTime        = r.MovingTime != null ? r.MovingTime.Value : new TimeSpan(0, 0, 0),
                Distance          = r.Distance,
                SufferScore       = r.SufferScore != null ? r.SufferScore.Value : 0,
                Calories          = r.Calories,
                ElevationGain     = r.ElevationGain,
                Start             = r.Start,
                StartDateLocal    = r.StartDateLocal,
                HasMap            = r.StartLatitude != null ? true : false,
                IsRide            = r.ActivityType.IsRide,
                IsRun             = r.ActivityType.IsRun,
                IsSwim            = r.ActivityType.IsSwim,
                IsOther           = r.ActivityType.IsOther,
                DetailsDownloaded = r.DetailsDownloaded,
                HasPowerMeter     = r.HasPowerMeter,
                TSS               = r.TSS != null ? r.TSS : 0
            });

            return(activityQuery);
        }
Exemplo n.º 2
0
        public List <KeyValuePair <DateTime, decimal> > GetDailyTSS(string userId, SportType sport, DateTime start, DateTime end)
        {
            // two select statement as can't call the KeyValuePair constructor from Linq To SQL.
            ActivityRepository activityRepo = new ActivityRepository(_context);
            var results = activityRepo.ActivitiesBySport(userId, sport).Where(a => a.StartDate >= start && a.StartDate <= end && a.TSS.HasValue)
                          .GroupBy(a => a.Start)
                          .Select(a => new { Key = a.Key, Value = a.Sum(g => g.TSS.Value) })
                          .ToList();

            return(results
                   .Select(a => new KeyValuePair <DateTime, decimal>(a.Key, a.Value))
                   .ToList());
        }
        public IEnumerable <PeriodDto> ActivityByWeek(string userId, SportType sport, DateTime start, DateTime end)
        {
            // get list of weeks in the period (to ensure we get full weeks date where the start and/or end date may be in the
            // middle of a week
            var weeks = _context.Calendar
                        .OrderBy(c => c.YearWeek)
                        .Where(c => c.Date >= start && c.Date <= end)
                        .Select(c => c.YearWeek)
                        .Distinct()
                        .ToList();

            // get activities which fall into the selected weeks.
            var activities = _activityrepo.ActivitiesBySport(userId, sport)
                             .Where(r => r.Athlete.UserId == userId &&
                                    weeks.Contains(r.Calendar.YearWeek))
                             .Select(r => new
            {
                Id           = r.Id,
                ActivityType = r.ActivityType.Description,
                Period       = r.Calendar.YearWeek,
                Distance     = r.Distance,
                Label        = r.Calendar.WeekLabel,
            })
                             .ToList();

            // group the activities by week.
            var weeklyTotals = activities
                               .GroupBy(r => new { Period = r.Period, Label = r.Label })
                               .Select(a => new PeriodDto
            {
                Period          = a.Key.Period,
                TotalDistance   = Math.Round(a.Sum(d => d.Distance).ToSportDistance(sport), 1),
                MaximumDistance = Math.Round(a.Max(d => d.Distance).ToSportDistance(sport), 1),
                Number          = a.Select(i => i.Id).Distinct().Count(),
                Label           = a.Key.Label
            })
                               .ToList();

            // get list of weeks in the period which we'l use to check for weeks with zero activities which won't be
            // included in weeklyTotals.
            var dummyWeeks = _context.Calendar
                             .OrderBy(c => c.YearWeek)
                             .Where(c => c.Date >= start && c.Date <= end)
                             .Select(c => new PeriodDto
            {
                Period          = c.YearWeek,
                TotalDistance   = 0,
                MaximumDistance = 0,
                Number          = 0,
                Label           = c.WeekLabel
            }
                                     )
                             .Distinct()
                             .ToList();

            // merge to ensure we include weeks with no activities.
            var result = weeklyTotals
                         .Union(dummyWeeks
                                .Where(e => !weeklyTotals.Select(x => x.Period).Contains(e.Period)))
                         .OrderBy(x => x.Period);

            // work out average weekly distance for the period.
            decimal averageDistance = Math.Round(result.Average(a => a.TotalDistance), 1);
            decimal periodAverageMaximumDistance = Math.Round(result.Average(a => a.MaximumDistance), 1);

            // update each week with the overall average.
            foreach (PeriodDto p in result)
            {
                p.PeriodAverageDistance        = averageDistance;
                p.PeriodAverageMaximumDistance = periodAverageMaximumDistance;
            }

            return(result);
        }