Exemplo n.º 1
0
        public async Task Save(string company, Queue <CollisionDTO> items)
        {
            CollisionDTO item;

            while (items.TryDequeue(out item))
            {
                var collision = Mapper.Map <API.Models.CollisionDTO>(item);

                var neighborhoodTask = NeighborhoodRepository.Find(Mapper.Map <Point>(item.Location));
                var patternAreaTask  = NeighborhoodRepository.Find(Mapper.Map <Point>(item.Location));

                collision.NeighborhoodKey = (await neighborhoodTask)?.Key;
                collision.PatternAreaKey  = (await patternAreaTask)?.Key;

                // add it to the database
                try
                {
                    var response = await HttpClient.PostAsJsonAsync("collision", collision);

                    response.EnsureSuccessStatusCode();
                }
                catch (HttpRequestException e)
                {
                    Logger.LogWarning("Error saving trip record for {company}:\n{message}", company, e.Message);
                }
            }
        }
Exemplo n.º 2
0
        public async Task Save(string company, Queue <TripDTO> items)
        {
            while (items.Count > 0)
            {
                var item = items.Dequeue();

                var trip = Mapper.Map <API.Models.TripDTO>(item);

                trip.StreetSegmentGroupKey = await StreetSegmentGroupRepository.FindGroupKey(trip.AlternateKey);

                trip.BicyclePathGroupKey = await BicyclePathGroupRepository.FindGroupKey(trip.AlternateKey);

                // if we've already calculated a segment group, don't repeat the expensive process
                Task <List <StreetSegment> > segmentsTask = null;
                if (trip.StreetSegmentGroupKey == null)
                {
                    segmentsTask = StreetSegmentRepository.Find(Mapper.Map <LineString>(trip.Route));
                }
                Task <List <BicyclePath> > pathsTask = null;
                if (trip.BicyclePathGroupKey == null)
                {
                    pathsTask = BicyclePathRepository.Find(Mapper.Map <LineString>(trip.Route));
                }

                var neighborhoodStartTask = NeighborhoodRepository.Find(Mapper.Map <Point>(item.StartPoint));
                var neighborhoodEndTask   = NeighborhoodRepository.Find(Mapper.Map <Point>(item.EndPoint));
                var patternAreaStartTask  = PatternAreaRepository.Find(Mapper.Map <Point>(item.StartPoint));
                var patternAreaEndTask    = PatternAreaRepository.Find(Mapper.Map <Point>(item.EndPoint));

                trip.NeighborhoodStartKey = (await neighborhoodStartTask)?.Key;
                trip.NeighborhoodEndKey   = (await neighborhoodEndTask)?.Key;
                trip.PatternAreaStartKey  = (await patternAreaStartTask)?.Key;
                trip.PatternAreaEndKey    = (await patternAreaEndTask)?.Key;

                trip.StreetSegmentGroupKey = trip.StreetSegmentGroupKey ??
                                             (await StreetSegmentGroupRepository.CreateGroup(await segmentsTask))?.Key;
                trip.BicyclePathGroupKey = trip.BicyclePathGroupKey ??
                                           (await BicyclePathGroupRepository.CreateGroup(await pathsTask))?.Key;

                // add it to the database
                try
                {
                    var response = await HttpClient.PostAsJsonAsync("trip", trip);

                    response.EnsureSuccessStatusCode();
                }
                catch (HttpRequestException e)
                {
                    Logger.LogWarning("Error saving trip record for {company}:\n{message}", company, e.Message);
                }
            }
        }
Exemplo n.º 3
0
        public async Task Save(string company, Queue <Integration.Models.DeploymentDTO> items)
        {
            var now = DateTime.Now;

            // get all the active deployments
            //  this is the only awaited one so I can manage the list of active deployments
            var response = await HttpClient.GetAsync($"deployment/{company}/active");

            response.EnsureSuccessStatusCode();
            var activeDeployments = await response.DeserializeJson(new List <API.Models.DeploymentDTO>());

            Logger.LogDebug($"Found {activeDeployments.Count} active deployments for {company}.");

            Models.DeploymentDTO item;
            var currentDeployments = 0;
            var newDeployments     = 0;

            while (items.TryDequeue(out item))
            {
                try
                {
                    // Use automapper for the original properties
                    var deployment = Mapper.Map <API.Models.DeploymentDTO>(item);

                    // for this vehicle, is there an active deployment?
                    var currentDeployment = activeDeployments.FirstOrDefault(
                        x => x.VehicleName == deployment.VehicleName &&
                        x.StartTime == deployment.StartTime
                        );
                    if (currentDeployment != null)
                    {
                        currentDeployments += 1;
                        // remove from active list
                        activeDeployments.Remove(currentDeployment);
                        currentDeployment.LastSeen = now;
                        // only other thing that we should track as changed is a possible end time
                        currentDeployment.EndTime = deployment.EndTime;
                        response = await HttpClient.PutAsJsonAsync <API.Models.DeploymentDTO>($"deployment/{currentDeployment.Key}", currentDeployment);

                        response.EnsureSuccessStatusCode();
                    }
                    else
                    {
                        // if there isn't start a new one
                        newDeployments += 1;

                        // Get the reference properties set up
                        var neighborhoodTask = NeighborhoodRepository.Find(Mapper.Map <Point>(deployment.Location));
                        var patternAreaTask  = PatternAreaRepository.Find(Mapper.Map <Point>(deployment.Location));

                        deployment.NeighborhoodKey = (await neighborhoodTask)?.Key;
                        deployment.PatternAreaKey  = (await patternAreaTask)?.Key;

                        deployment.FirstSeen = deployment.LastSeen = now;
                        response             = await HttpClient.PostAsJsonAsync("deployment", deployment);

                        response.EnsureSuccessStatusCode();
                    }
                }
                catch (Exception e)
                {
                    // this might be because we are seeing something for the first time
                    Logger.LogWarning("Failed to save a deployment record:\n{message}\n{inner}", e.Message, e.InnerException?.Message);
                }
            }

            Logger.LogDebug($"Found {currentDeployments} existing deployments and {newDeployments} new deployments.");
            Logger.LogDebug($"Ending {activeDeployments.Count} deployments.");
            // for all the other active deployments that we didn't see, update the end time to now
            foreach (var deploymentToEnd in activeDeployments)
            {
                try
                {
                    deploymentToEnd.EndTime  = now;
                    deploymentToEnd.LastSeen = now;
                    response = await HttpClient.PutAsJsonAsync <API.Models.DeploymentDTO>($"deployment/{deploymentToEnd.Key}", deploymentToEnd);

                    response.EnsureSuccessStatusCode();
                }
                catch (Exception e)
                {
                    // this might be because we are seeing something for the first time
                    Logger.LogWarning("Failed to save a deployment record:\n{message}\n{inner}", e.Message, e.InnerException?.Message);
                }
            }
        }