示例#1
0
 public async static Task<MirrorStatus> RefreshCachedData(CacheUpdateStrategy updateStrategy = CacheUpdateStrategy.Modified)
 {
     try
     {
         return await new CacheProviderMongoDB().PopulatePOIMirror(updateStrategy);
     }
     catch (Exception)
     {
         return null;
     }
 }
示例#2
0
 public async static Task <MirrorStatus> RefreshCachedData(CacheUpdateStrategy updateStrategy = CacheUpdateStrategy.Modified, ILogger logger = null)
 {
     try
     {
         return(await CacheProviderMongoDB.DefaultInstance.PopulatePOIMirror(updateStrategy, logger));
     }
     catch (Exception exp)
     {
         return(new MirrorStatus {
             Description = exp.ToString(), StatusCode = System.Net.HttpStatusCode.ExpectationFailed
         });
     }
 }
        /// <summary>
        /// Perform full or partial repopulation of POI Mirror in MongoDB
        /// </summary>
        /// <returns></returns>
        public async Task <MirrorStatus> PopulatePOIMirror(CacheUpdateStrategy updateStrategy)
        {
            var dataModel = new Data.OCMEntities();

            if (!database.CollectionExists("poi"))
            {
                database.CreateCollection("poi");
            }

            if (updateStrategy != CacheUpdateStrategy.All)
            {
                if (!database.CollectionExists("reference"))
                {
                    database.CreateCollection("reference");
                }
                if (!database.CollectionExists("status"))
                {
                    database.CreateCollection("status");
                }
                if (!database.CollectionExists("poi"))
                {
                    database.CreateCollection("poi");
                }
            }

            CoreReferenceData coreRefData = new ReferenceDataManager().GetCoreReferenceData(false);

            if (coreRefData != null)
            {
                database.DropCollection("reference");
                //problems clearing data from collection...

                var reference = database.GetCollection <CoreReferenceData>("reference");
                var query     = new QueryDocument();
                reference.Remove(query, RemoveFlags.None);

                Thread.Sleep(300);
                reference.Insert(coreRefData);
            }

            var poiList       = GetPOIListToUpdate(dataModel, updateStrategy);
            var poiCollection = database.GetCollection <POIMongoDB>("poi");

            if (poiList != null && poiList.Any())
            {
                if (updateStrategy == CacheUpdateStrategy.All)
                {
                    poiCollection.RemoveAll();
                }

                RemoveAllPOI(poiList, poiCollection);
                Thread.Sleep(300);
                InsertAllPOI(poiList, poiCollection);

                poiCollection.CreateIndex(IndexKeys <POIMongoDB> .GeoSpatialSpherical(x => x.SpatialPosition));
                poiCollection.CreateIndex(IndexKeys <POIMongoDB> .Descending(x => x.DateLastStatusUpdate));
                poiCollection.CreateIndex(IndexKeys <POIMongoDB> .Descending(x => x.DateCreated));

                if (updateStrategy == CacheUpdateStrategy.All)
                {
                    poiCollection.ReIndex();
                }
            }

            long numPOIInMasterDB = dataModel.ChargePoints.LongCount();

            return(RefreshMirrorStatus(poiCollection.Count(), 1, numPOIInMasterDB));
        }
        public List <OCM.API.Common.Model.ChargePoint> GetPOIListToUpdate(OCMEntities dataModel, CacheUpdateStrategy updateStrategy)
        {
            if (!database.CollectionExists("poi"))
            {
                database.CreateCollection("poi");
            }
            var poiCollection = database.GetCollection <POIMongoDB>("poi");

            //incremental update based on POI Id - up to 100 results at a time
            if (updateStrategy == CacheUpdateStrategy.Incremental)
            {
                var maxPOI = poiCollection.FindAll().SetSortOrder(SortBy.Descending("ID")).SetLimit(1).FirstOrDefault();
                int maxId  = 0;
                if (maxPOI != null)
                {
                    maxId = maxPOI.ID;
                }

                //from max poi we have in mirror to next 100 results in order of ID
                var dataList = dataModel.ChargePoints.Include("AddressInfo,ConnectionInfo,MetadataValue,UserComment,MediaItem,User").OrderBy(o => o.ID).Where(o => o.ID > maxId).Take(100);
                var poiList  = new List <OCM.API.Common.Model.ChargePoint>();
                foreach (var cp in dataList)
                {
                    poiList.Add(OCM.API.Common.Model.Extensions.ChargePoint.FromDataModel(cp, true, true, true, true));
                }
                return(poiList);
            }

            //update based on POI last modified since last status update
            if (updateStrategy == CacheUpdateStrategy.Modified)
            {
                var      maxPOI           = poiCollection.FindAll().SetSortOrder(SortBy.Descending("DateLastStatusUpdate")).SetLimit(1).FirstOrDefault();
                DateTime?dateLastModified = null;
                if (maxPOI != null)
                {
                    dateLastModified = maxPOI.DateLastStatusUpdate.Value.AddMinutes(-10);
                }

                //determine POI updated since last status update we have in cache
                //db.poi.find().sort({DateLastStatusUpdate:-1})

                //"AddressInfo,ConnectionInfo,MetadataValue,UserComment,MediaItem,User"
                var dataList = new Data.OCMEntities().ChargePoints
                               .Include(a1 => a1.AddressInfo)
                               .Include(a1 => a1.Connections)
                               .Include(a1 => a1.MetadataValues)
                               .Include(a1 => a1.UserComments)
                               .Include(a1 => a1.MediaItems)
                               .OrderBy(o => o.DateLastStatusUpdate)
                               .Where(o => o.DateLastStatusUpdate > dateLastModified).ToList();
                var poiList = new List <OCM.API.Common.Model.ChargePoint>();
                foreach (var cp in dataList)
                {
                    poiList.Add(OCM.API.Common.Model.Extensions.ChargePoint.FromDataModel(cp, true, true, true, true));
                }
                return(poiList);
            }
            if (updateStrategy == CacheUpdateStrategy.All)
            {
                var dataList = new Data.OCMEntities().ChargePoints
                               .Include(a1 => a1.AddressInfo)
                               .Include(a1 => a1.Connections)
                               .Include(a1 => a1.MetadataValues)
                               .Include(a1 => a1.UserComments)
                               .Include(a1 => a1.MediaItems)
                               .OrderBy(o => o.ID)
                               .ToList();

                var poiList = new List <OCM.API.Common.Model.ChargePoint>();
                foreach (var cp in dataList)
                {
                    poiList.Add(OCM.API.Common.Model.Extensions.ChargePoint.FromDataModel(cp, true, true, true, true));
                }
                return(poiList);
            }

            return(null);
        }
        /// <summary>
        /// Perform full or partial repopulation of POI Mirror in MongoDB
        /// </summary>
        /// <returns></returns>
        public async Task <MirrorStatus> PopulatePOIMirror(CacheUpdateStrategy updateStrategy)
        {
            if (!database.CollectionExists("poi"))
            {
                database.CreateCollection("poi");
            }

            if (updateStrategy != CacheUpdateStrategy.All)
            {
                CoreReferenceData coreRefData = new ReferenceDataManager().GetCoreReferenceData();
                if (!database.CollectionExists("reference"))
                {
                    database.CreateCollection("reference");
                }
                if (!database.CollectionExists("status"))
                {
                    database.CreateCollection("status");
                }

                if (coreRefData != null)
                {
                    var reference = database.GetCollection <CoreReferenceData>("reference");
                    reference.RemoveAll();
                    reference.Insert(coreRefData);
                }
            }

            var poiList       = GetPOIListToUpdate(updateStrategy);
            var poiCollection = database.GetCollection <POIMongoDB>("poi");

            if (poiList != null && poiList.Any())
            {
                if (updateStrategy == CacheUpdateStrategy.All)
                {
                    poiCollection.RemoveAll();
                }

                RemoveAllPOI(poiList, poiCollection);
                Thread.Sleep(300);
                InsertAllPOI(poiList, poiCollection);

                poiCollection.CreateIndex(IndexKeys <POIMongoDB> .GeoSpatialSpherical(x => x.SpatialPosition));
                poiCollection.CreateIndex(IndexKeys <POIMongoDB> .Descending(x => x.DateLastStatusUpdate));
                poiCollection.CreateIndex(IndexKeys <POIMongoDB> .Descending(x => x.DateCreated));
            }

            var statusCollection = database.GetCollection <MirrorStatus>("status");

            statusCollection.RemoveAll();

            //new status
            MirrorStatus status = new MirrorStatus();

            //status.ID = Guid.NewGuid().ToString();
            status.Description = "MongoDB Cache of Open Charge Map POI Database";
            status.LastUpdated = DateTime.UtcNow;

            status.TotalPOI   = poiCollection.Count();
            status.StatusCode = HttpStatusCode.OK;
            if (poiList != null)
            {
                status.NumPOILastUpdated = poiList.Count;
            }
            else
            {
                status.NumPOILastUpdated = 0;
            }

            statusCollection.Insert(status);
            return(status);
        }