Ejemplo n.º 1
0
        public async Task <EventStatisticsQueryResponse> Handle(EventStatisticsQuery request, CancellationToken cancellationToken)
        {
            var allSessionsQuery = context.SkateLogEntries.Where(x => x.ApplicationUser.HasPaid);
            var doingPeriodRange = request.DateFrom.HasValue && request.DateTo.HasValue;

            if (doingPeriodRange)
            {
                allSessionsQuery = allSessionsQuery.Where(x => x.Logged >= request.DateFrom.Value && x.Logged <= request.DateTo.Value);
            }

            var allSessions = await allSessionsQuery.ToListAsync(cancellationToken);

            var allSkaters = await context.Users.Where(x => x.HasPaid).ToListAsync(cancellationToken);

            var skaterLogs = allSkaters.Select(x => skaterTargetAnalyser.Analyse(x, allSessions)).Where(x => x.TotalSessions > 0).ToList();
            var allWeeks   = GetAllWeeks(allSessions);

            if (doingPeriodRange)
            {
                allWeeks = allWeeks.Where(x => x.Item1 <= request.DateTo.Value).ToList();
            }

            return(new EventStatisticsQueryResponse
            {
                ShortestSingleDistance = GetShortestDistance(allSessions, allSkaters),
                LongestSingleDistance = GetLongestDistance(allSessions, allSkaters),
                LongestTotalDistance = GetLongestTotalDistance(allSessions, allSkaters),
                MostJourneys = GetHighestNumberOfJourneys(allSessions, allSkaters),
                BestTopSpeed = GetFastestSkater(allSessions, allSkaters),
                BestAverageSpeed = GetBestAverageSpeedSkater(allSessions, allSkaters),
                GreatestClimb = GetGreatestClimber(allSessions, allSkaters),
                SkybornSkater = GetSkybornSkater(allSessions, allSkaters),
                TotalMiles = allSessions.Sum(x => x.DistanceInMiles),
                TotalSkateSessions = allSessions.Count,
                MilesByStrava = allSessions.Where(x => !string.IsNullOrWhiteSpace(x.StravaId)).Sum(x => x.DistanceInMiles),
                MilesByManual = allSessions.Where(x => string.IsNullOrWhiteSpace(x.StravaId)).Sum(x => x.DistanceInMiles),
                JourneysByStrava = allSessions.Count(x => !string.IsNullOrWhiteSpace(x.StravaId)),
                JourneysByManual = allSessions.Count(x => string.IsNullOrWhiteSpace(x.StravaId)),
                SkateDistances = GetMilesPerWeek(allSessions, allWeeks).ToList(),
                SkateSessions = GetSessionsPerWeek(allSessions, allWeeks).ToList(),
                CheckPoints = GetCheckPointStatistics(skaterLogs).ToList(),
                ActivitiesByDay = GetActivitiesPerWeekDay(allSessions).ToList(),
                MilesByDay = GetMilesPerWeekDay(allSessions).ToList()
            });
        }
Ejemplo n.º 2
0
        public override async Task <PageViewModel <SkaterProgressViewModel> > Build()
        {
            var command = new SkaterLogQuery {
                Skater = User
            };
            var commandResponse = await mediator.Send(command);

            var mileageEntries = commandResponse.Entries ?? new List <SkateLogEntry>();
            var analysis       = skaterTargetAnalyser.Analyse(this.User, mileageEntries);

            var totalDistance      = mileageEntries.Sum(x => x.DistanceInMiles);
            var checkPoints        = checkPointRepository.Get();
            var checkPointsReached = checkPoints.Where(x => x.Distance <= totalDistance).OrderBy(x => x.Distance);
            var targetCheckPoint   = checkPoints.First(x => x.SkateTarget.Equals(User.Target));
            var nextCheckPoint     = checkPoints.Where(x => x.Distance > totalDistance && x.Distance <= targetCheckPoint.Distance).OrderBy(x => x.Distance).FirstOrDefault();

            var model = await base.Build();

            model.PageTitle           = "Your Progress";
            model.DisplayPageTitle    = "Your Progress";
            model.IsNoIndexPage       = true;
            model.Content.MilesSkated = totalDistance;
            model.Content.TargetMiles = targetCheckPoint.Distance;
            model.Content.Entries     = mileageEntries;

            model.Content.CheckPointsReached =
                (from cp in checkPointsReached
                 join acp in analysis.CheckPointDates on cp.SkateTarget equals acp.Key
                 orderby acp.Value
                 select new SkaterProgressCheckPoint
            {
                SkateTarget = cp.SkateTarget,
                Distance = cp.Distance,
                Title = cp.Title,
                Description = cp.Description,
                Longitude = cp.Longitude,
                Latitude = cp.Latitude,
                Url = cp.Url,
                Image = cp.Image,
                DigitalBadge = cp.DigitalBadge,
                FinisherDigitalBadge = cp.FinisherDigitalBadge,
                DateAchieved = acp.Value
            }).ToList();

            if (nextCheckPoint != null)
            {
                var distanceToNextCheckpoint = nextCheckPoint.Distance - totalDistance;
                model.Content.NextCheckPoint = new SkaterProgressCheckPoint
                {
                    Title       = "Your Next Checkpoint",
                    Description = $"You have to skate a further {distanceToNextCheckpoint:F2} miles to reach {nextCheckPoint.Title}.",
                    Longitude   = nextCheckPoint.Longitude,
                    Latitude    = nextCheckPoint.Latitude,
                    Url         = nextCheckPoint.Url
                };
            }
            else
            {
                var lastCheckPointReached = model.Content.CheckPointsReached.Last();
                model.Content.NextCheckPoint = lastCheckPointReached;
                model.Content.CheckPointsReached.Remove(lastCheckPointReached);
            }

            return(model);
        }