Exemplo n.º 1
0
        public async Task DeleteDay(int id, bool removePings = false)
        {
            var day = await GetByIdAsync(id);

            var pings = new List <Ping>();

            // Find the previous day
            var previousDay = await GetPrevious(day);

            // Merge with the previous day
            if (previousDay is Day)
            {
                // Up the end time of the previous record
                previousDay.TimeTo = day.TimeTo;
                Update(previousDay);

                var rideId     = previousDay.RideId;
                var locationId = previousDay.LocationId;
                var dayId      = previousDay.Id;

                // Update the pings of the to be deleted day to the location of the previous day
                pings = await pingRepository.GetPings(day);

                foreach (var ping in pings)
                {
                    if (!removePings)
                    {
                        ping.RideId     = rideId;
                        ping.LocationId = locationId;
                        ping.DayId      = dayId;
                        pingRepository.Update(ping);
                    }
                    else
                    {
                        pingRepository.Delete(ping.Id);
                    }
                }

                await SaveAsync();

                // Check if the previous record is a ride. This also needs to be extended
                if (previousDay.Ride is Ride)
                {
                    previousDay.Ride.TimeTo = day.TimeTo;
                    rideRepository.Update(previousDay.Ride);
                    var nextDay = await GetNext(day);

                    // Is the next record also a ride, Merge them together.
                    if (nextDay is Day && nextDay.Ride is Ride)
                    {
                        previousDay.TimeTo                = nextDay.TimeTo;
                        previousDay.Ride.TimeTo           = nextDay.TimeTo;
                        previousDay.Ride.DistanceInMeters = null;

                        pings = await pingRepository.GetPings(nextDay);

                        foreach (var ping in pings)
                        {
                            ping.RideId     = rideId;
                            ping.LocationId = locationId;
                            ping.DayId      = dayId;
                            pingRepository.Update(ping);
                        }

                        rideRepository.Delete(nextDay.Ride.Id);
                        Delete(nextDay.Id);
                    }

                    previousDay.Ride.ResetDistance();
                    rideRepository.Update(previousDay.Ride);
                }

                Update(previousDay);
                await SaveAsync();

                if (day.Ride is Ride)
                {
                    // The previous merge actions removed all pings.
                    // But in case any pings didn't get moved double check by selecting with the ride instead of the day.
                    pings = await pingRepository.GetPings(day.Ride);

                    foreach (var ping in pings)
                    {
                        if (!removePings)
                        {
                            ping.RideId     = rideId;
                            ping.LocationId = locationId;
                            ping.DayId      = dayId;
                            pingRepository.Update(ping);
                        }
                        else
                        {
                            pingRepository.Delete(ping.Id);
                        }
                    }

                    rideRepository.Delete(day.Ride.Id);
                }
            }

            Delete(day.Id);
            await SaveAsync();
        }
Exemplo n.º 2
0
        public async Task Process()
        {
            var pings = await pingRepository.GetUnprocessed();

            Ping previousPing = await pingRepository.GetLastPing(true);

            List <Ping> ridePings     = new List <Ping>();
            List <Ping> locationPings = new List <Ping>();

            foreach (var ping in pings)
            {
                if (!(previousPing is Ping))
                {
                    previousPing = ping;
                    continue;
                }

                var distance = utility.GetDistanceInMeters(previousPing, ping);
                var kmh      = utility.GetSpeed(previousPing.Time, ping.Time, distance);

                // Moving
                if (kmh >= Constants.MINIMUM_MOVING_SPEED)
                {
                    // Finish the locationPings before starting to move.
                    if (await IsValidLocation(locationPings))
                    {
                        await SaveLocationPings(locationPings);

                        // Reset the location list. These functions can append pings 1 at a time if needed
                        locationPings = new List <Ping>();

                        // Reset the ride list because if i moved inside a location i don't need to map that to a ride
                        ridePings = new List <Ping>();
                    }

                    ridePings.Add(ping);
                }
                // Not moving
                else
                {
                    if (await IsValidRide(ridePings))
                    {
                        await SaveRidePings(ridePings);

                        // Reset the location list. These functions can append pings 1 at a time if needed
                        ridePings = new List <Ping>();

                        // Reset the location list because if i'm standing at a trafic light don't create a location.
                        locationPings = new List <Ping>();
                    }
                    // If there is a ridePing stuck while at a valid location delete it so speed the process up
                    else if (ridePings.Count > 0 && await IsValidLocation(locationPings))
                    {
                        ridePings = new List <Ping>();
                    }

                    locationPings.Add(ping);
                }

                previousPing = ping;
            }

            // Save the last batch
            if (await IsValidLocation(locationPings))
            {
                await SaveLocationPings(locationPings);
            }

            if (await IsValidRide(ridePings))
            {
                await SaveRidePings(ridePings);
            }

            // Mark all as done
            if (pings.Count > 0)
            {
                var lastPing = await pingRepository.GetLastPing(true);

                var remainingPings = (await pingRepository.GetBetweenDates(pings[0].Time, lastPing.Time))
                                     .Where(p => p.Processed == 0);
                foreach (var ping in remainingPings)
                {
                    ping.Processed = 1;
                    pingRepository.Update(ping);
                }
                await pingRepository.SaveAsync();
            }

            // RecurringJob.AddOrUpdate<ProcessPings>("ProcessPings", x => x.Process(), Cron.Minutely);
        }