public static GenericTraveltimeSegment MergeDocuments(TraveltimeSegmentStatic staticData, TraveltimeSegment dynamicData)
        {
            if (staticData.Id != dynamicData.Id)
            {
                throw new InvalidOperationException("Documents do not match");
            }

            GenericTraveltimeSegment _new = new GenericTraveltimeSegment
            {
                Source        = "Be-Mobile",
                Timestamp     = new DocumentDate((DateTime)dynamicData.Timestamp),
                SegmentName   = staticData.Id,
                FromLatitude  = staticData.beginnodelatitude,
                ToLatitude    = staticData.endnodelatitude,
                FromLongitude = staticData.beginnodelongitude,
                ToLongitude   = staticData.endnodelongitude,
                Length        = staticData.lengthmm / 1000.0,
                Duration      = dynamicData.durationInMs / 1000,
                OptimalSpeed  = staticData.optimalspeedkph
            };

            _new.SetSpeed();
            _new.SetId();
            _new = FillLocationInfo(staticData, _new);

            return(_new);
        }
        public static int[] ProcessMerges(List <TraveltimeSegment> input, Verification verifyType, QueryManager <TraveltimeStatic> staticInfoQuerier)
        {
            int[] resultCount = { 0, 0, 0 }; //Success, Failed, Skipped

            foreach (TraveltimeSegment segment in input)
            {
                TraveltimeSegmentStatic  merged = GetStaticData(segment.Id, staticInfoQuerier);
                GenericTraveltimeSegment result = MergeDocuments(merged, segment);

                if (verifyType == Verification.CheckExisting)
                {
                    try
                    {
                        PersistDocuments.Get(result.Id).Wait();
                        resultCount[2]++;
                        // Console.WriteLine($"Merged document {result.Id} not persisted in the database, conflict found.");
                    }
                    catch (Exception ex)
                    {
                        resultCount[0]++;
                        PersistResultDocuments(result);
                        //Console.WriteLine($"Merged document {result.Id} persisted in the database");
                    }
                }
                else
                {
                    resultCount[0]++;
                    PersistResultDocuments(result);
                    //Console.WriteLine($"Merged document {result.Id} persisted in the database");
                }
            }
            return(resultCount);
        }
示例#3
0
        private static GenericTraveltimeSegment CreateSegmentFromRoute(SubRoute subRoute, long UpdateTime)
        {
            GenericTraveltimeSegment segment = new GenericTraveltimeSegment
            {
                Source       = "Waze",
                SegmentName  = subRoute.FromName + ";" + subRoute.ToName,
                FromPoint    = subRoute.FromName,
                ToPoint      = subRoute.ToName,
                Timestamp    = new DocumentDate(Util.GetDateTimeFromUnixInMillies(UpdateTime)),
                Coordinates  = subRoute.Line,
                Length       = subRoute.Length,
                Duration     = subRoute.Time,
                OptimalSpeed = subRoute.Length / (double)subRoute.HistoricTime * 3.6
            };

            try
            {
                segment.FromLatitude  = subRoute.Line[0]?.Latitude ?? 0;
                segment.FromLongitude = subRoute.Line[0]?.Longitude ?? 0;
                segment.ToLatitude    = subRoute.Line[subRoute.Line.Length - 1]?.Latitude ?? 0;
                segment.ToLongitude   = subRoute.Line[subRoute.Line.Length - 1]?.Longitude ?? 0;
            }
            catch (Exception e)
            {
                //do nothing values stay at 0

                // Console.WriteLine("Index out of range!");
            }

            segment.SetId();
            segment.SetSpeed();
            return(segment);
        }
 protected static void PersistResultDocuments(GenericTraveltimeSegment doc, int tryCount = 0)
 {
     try
     {
         PersistDocuments.Create(doc).Wait();
     }catch (DocumentClientException ex)
     {
         Console.WriteLine("Error occured:" + ex.Error);
         Console.WriteLine($"Retrycount: {tryCount}, {(tryCount <= 10 ? String.Format("retrying in 1 second") : String.Format("skipping document"))}");
         if (tryCount < 10)
         {
             System.Threading.Thread.Sleep(1000);
             PersistResultDocuments(doc, tryCount++);
         }
     }
 }
        public async void ProcessAndUploadSegment(SegmentTaskModel model)
        {
            GenericTraveltimeSegment merged = NormalizeSegment(model.TraveltimeSegment, model.TraveltimeStatic);

            try
            {
                await model.OutputQueryManager.Create(merged, 0, 10);

                _segmentQueue.Finished += 1;
            }
            catch (Microsoft.Azure.Documents.DocumentClientException ex)
            {
                //TODO: handle exception
            }
            catch (System.Threading.Tasks.TaskCanceledException ex)
            {
                _logger.LogWarning($"A task has been canceled: {ex.Task.Id}");
                _segmentQueue.Failed += 1;
            }
            catch (System.OperationCanceledException ex)
            {
                _logger.LogWarning($"An operation has been canceled: {ex.Message}");
            }
        }
        private static GenericTraveltimeSegment FillLocationInfo(TraveltimeSegmentStatic staticData, GenericTraveltimeSegment dynamicData)
        {
            if (staticData.googleLocationInfo != null)
            {
                List <LocationInfoModel.AddressComponent> components =
                    staticData.googleLocationInfo[0].result.address_components;
                LocationInfoModel.AddressComponent routeComponent = components[1];
                int index = 0;
                while (!routeComponent.types.Contains("route") && index < components.Count)
                {
                    routeComponent = components[index];
                }
                dynamicData.FromPoint = routeComponent.long_name;
                dynamicData.ToPoint   = routeComponent.long_name;
            }

            return(dynamicData);
        }