Beispiel #1
0
        public virtual async Task <IList <IndexDocumentChange> > GetChangesAsync(DateTime?startDate, DateTime?endDate, long skip, long take)
        {
            var result = new List <IndexDocumentChange>();

            var criteria = new ChangeLogSearchCriteria
            {
                ObjectType = ChangeLogObjectType,
                StartDate  = startDate,
                EndDate    = endDate,
                Skip       = (int)skip,
                Take       = (int)take
            };

            // Get changes from operation log
            var operations = (await _changeLogSearchService.SearchAsync(criteria)).Results;

            var inventories = await _inventoryService.GetByIdsAsync(operations.Select(o => o.ObjectId).ToArray(), InventoryResponseGroup.Default.ToString());

            result = operations.Join(inventories, o => o.ObjectId, i => i.Id, (o, i) => new IndexDocumentChange
            {
                DocumentId = i.ProductId,
                ChangeType = IndexDocumentChangeType.Modified,
                ChangeDate = o.ModifiedDate ?? o.CreatedDate,
            }).ToList();


            return(await Task.FromResult(result));
        }
Beispiel #2
0
        /// <summary>
        /// Retrieves all price changes and groups them by the product Id they are associated with.
        /// </summary>
        /// <param name="startDate">The date time period from</param>
        /// <param name="endDate">The date time period to</param>
        /// <returns>The unique list of products identifiers associated with changed prices for passed period</returns>
        protected virtual async Task <ICollection <string> > GetProductIdsForChangedPricesAsync(DateTime?startDate, DateTime?endDate)
        {
            var result = new HashSet <string>();

            var criteria = new ChangeLogSearchCriteria
            {
                ObjectType = _changeLogObjectType,
                StartDate  = startDate,
                EndDate    = endDate,
                Take       = 0
            };

            // Get changes from operation log
            var operations = (await _changeLogSearchService.SearchAsync(criteria)).Results.Select(x => x.ObjectId);

            using (var repository = _repositoryFactory())
            {
                var totalCount = operations.Count();

                for (var i = 0; i < totalCount; i += _batchSize)
                {
                    var changedPriceIds = operations.Skip(i).Take(_batchSize).ToArray();

                    var productIds = await repository.Prices.Where(x => changedPriceIds.Contains(x.Id))
                                     .Select(x => x.ProductId)
                                     .Distinct()
                                     .ToArrayAsync();

                    result.AddRange(productIds);
                }
            }

            return(result);
        }
Beispiel #3
0
        public virtual async Task <ChangeLogSearchResult> SearchAsync(ChangeLogSearchCriteria criteria)
        {
            var cacheKey = CacheKey.With(GetType(), nameof(SearchAsync), criteria.GetCacheKey());
            var result   = await _memoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                cacheEntry.AddExpirationToken(ChangeLogCacheRegion.CreateChangeToken());
                var searchResult = AbstractTypeFactory <ChangeLogSearchResult> .TryCreateInstance();

                using (var repository = _repositoryFactory())
                {
                    repository.DisableChangesTracking();

                    var sortInfos = GetSortInfos(criteria);
                    var query     = GetQuery(repository, criteria, sortInfos);

                    searchResult.TotalCount = await query.CountAsync();
                    if (criteria.Take > 0)
                    {
                        searchResult.Results = (await query.AsNoTracking().Skip(criteria.Skip).Take(criteria.Take).ToArrayAsync()).Select(x => x.ToModel(AbstractTypeFactory <OperationLog> .TryCreateInstance())).ToList();
                    }
                }
                return(searchResult);
            });

            return(result);
        }
Beispiel #4
0
        public async Task <ActionResult <ChangeLogSearchResult> > SearchChanges([FromBody] ChangeLogSearchCriteria criteria)
        {
            // Get changes count from operation log
            var result = await _changeLogSearchService.SearchAsync(criteria);

            return(Ok(result.Results));
        }
        public async Task <ActionResult <OperationLog[]> > GetOrderChanges(string id)
        {
            var result = Array.Empty <OperationLog>();
            var order  = await _customerOrderService.GetByIdAsync(id);

            if (order != null)
            {
                var authorizationResult = await _authorizationService.AuthorizeAsync(User, order, new OrderAuthorizationRequirement(ModuleConstants.Security.Permissions.Read));

                if (!authorizationResult.Succeeded)
                {
                    return(Unauthorized());
                }

                //Load general change log for order
                var allHasChangesObjects = order.GetFlatObjectsListWithInterface <IHasChangesHistory>()
                                           .Distinct().ToArray();

                var criteria = new ChangeLogSearchCriteria
                {
                    ObjectIds   = allHasChangesObjects.Select(x => x.Id).Distinct().ToArray(),
                    ObjectTypes = allHasChangesObjects.Select(x => x.GetType().Name).Distinct().ToArray()
                };
                result = (await _changeLogSearchService.SearchAsync(criteria)).Results.ToArray();
            }
            return(Ok(result));
        }
        public virtual async Task <long> GetTotalChangesCountAsync(DateTime?startDate, DateTime?endDate)
        {
            long result;

            if (startDate == null && endDate == null)
            {
                // Get total products count
                using (var repository = _memberRepositoryFactory())
                {
                    result = repository.Members.Count();
                }
            }
            else
            {
                var criteria = new ChangeLogSearchCriteria
                {
                    ObjectType = ChangeLogObjectType,
                    StartDate  = startDate,
                    EndDate    = endDate,
                    Take       = 0
                };
                // Get changes count from operation log
                result = (await _changeLogSearchService.SearchAsync(criteria)).TotalCount;
            }

            return(result);
        }
Beispiel #7
0
        public virtual async Task <IList <IndexDocumentChange> > GetChangesAsync(DateTime?startDate, DateTime?endDate,
                                                                                 long skip, long take)
        {
            IList <IndexDocumentChange> result;

            if (startDate == null && endDate == null)
            {
                result = null;
            }
            else
            {
                var criteria = new ChangeLogSearchCriteria
                {
                    ObjectType = ChangeLogObjectType,
                    StartDate  = startDate,
                    EndDate    = endDate,
                    Skip       = (int)skip,
                    Take       = (int)take
                };

                // Get changes from operation log
                var operations = (await _changeLogSearchService.SearchAsync(criteria)).Results;

                var priceIds = operations.Select(c => c.ObjectId).ToArray();
                var priceIdsAndProductIds = await GetProductIdsAsync(priceIds);

                result = operations
                         .Where(o => priceIdsAndProductIds.ContainsKey(o.ObjectId))
                         .Select(o => new IndexDocumentChange
                {
                    DocumentId = priceIdsAndProductIds[o.ObjectId],
                    ChangeDate = o.ModifiedDate ?? o.CreatedDate,
                    ChangeType = IndexDocumentChangeType.Modified,
                })
                         .ToArray();

                var workSkip = (int)(skip - Math.Min(result.Count, skip));
                var workTake = (int)(take - Math.Min(take, Math.Max(0, result.Count - skip)));

                //Re-index calendar prices only once per defined time interval
                var lastIndexDate = _settingsManager.GetValue(ModuleConstants.Settings.General.IndexationDatePricingCalendar.Name,
                                                              (DateTime?)null) ?? DateTime.MinValue;
                if (DateTime.UtcNow - lastIndexDate > CalendarChangesInterval && startDate != null && endDate != null)
                {
                    var calendarChanges =
                        await GetCalendarChangesAsync(startDate.Value, endDate.Value, workSkip, workTake);

                    if (workTake > 0)
                    {
                        _settingsManager.SetValue(ModuleConstants.Settings.General.IndexationDatePricingCalendar.Name, DateTime.UtcNow);
                        result.AddRange(calendarChanges);
                    }
                }
            }
            return(result);
        }
Beispiel #8
0
        public virtual async Task <IList <IndexDocumentChange> > GetChangesAsync(DateTime?startDate, DateTime?endDate, long skip, long take)
        {
            IList <IndexDocumentChange> result;

            if (startDate == null && endDate == null)
            {
                // Get documents from repository and return them as changes
                using (var repository = _catalogRepositoryFactory())
                {
                    var productIds = await repository.Items
                                     .Where(i => i.ParentId == null)
                                     .OrderBy(i => i.CreatedDate)
                                     .Select(i => i.Id)
                                     .Skip((int)skip)
                                     .Take((int)take)
                                     .ToArrayAsync();

                    result = productIds.Select(id =>
                                               new IndexDocumentChange
                    {
                        DocumentId = id,
                        ChangeType = IndexDocumentChangeType.Modified,
                        ChangeDate = DateTime.UtcNow
                    }
                                               ).ToArray();
                }
            }
            else
            {
                // Get changes from operation log
                var criteria = new ChangeLogSearchCriteria
                {
                    ObjectType = ChangeLogObjectType,
                    StartDate  = startDate,
                    EndDate    = endDate,
                    Skip       = (int)skip,
                    Take       = (int)take
                };

                // Get changes from operation log
                var operations = (await _changeLogSearchService.SearchAsync(criteria)).Results;

                result = operations.Select(o =>
                                           new IndexDocumentChange
                {
                    DocumentId = o.ObjectId,
                    ChangeType = o.OperationType == EntryState.Deleted ? IndexDocumentChangeType.Deleted : IndexDocumentChangeType.Modified,
                    ChangeDate = o.ModifiedDate ?? o.CreatedDate,
                }
                                           ).ToArray();
            }

            return(result);
        }
Beispiel #9
0
        public async Task <ActionResult <OperationLog[]> > SearchTypeChangeHistory(string type, [FromQuery] DateTime?start = null, [FromQuery] DateTime?end = null)
        {
            var criteria = new ChangeLogSearchCriteria
            {
                ObjectType = type,
                StartDate  = start,
                EndDate    = end
            };
            // Get changes count from operation log
            var result = await _changeLogSearchService.SearchAsync(criteria);

            return(Ok(result.Results));
        }
Beispiel #10
0
        protected virtual IList <SortInfo> GetSortInfos(ChangeLogSearchCriteria criteria)
        {
            var sortInfos = criteria.SortInfos;

            if (sortInfos.IsNullOrEmpty())
            {
                sortInfos = new[]
                {
                    new SortInfo {
                        SortColumn = nameof(OperationLog.ModifiedDate), SortDirection = SortDirection.Descending
                    }
                };
            }
            return(sortInfos);
        }
        private async Task <int> GetCustomerSegmentsChangesCount(DateTime?startDate, DateTime?endDate)
        {
            var customerSegmentsChangesCount = 0;

            if (startDate != null || endDate != null)
            {
                var criteria = new ChangeLogSearchCriteria
                {
                    ObjectType = nameof(DemoCustomerSegment), StartDate = startDate, EndDate = endDate, Take = 0
                };
                // Get customer segments count from operation log
                customerSegmentsChangesCount = (await _changeLogSearchService.SearchAsync(criteria)).TotalCount;
            }

            return(customerSegmentsChangesCount);
        }
Beispiel #12
0
        public async Task <long> GetTotalChangesCountAsync(DateTime?startDate, DateTime?endDate)
        {
            long result;

            var criteria = new ChangeLogSearchCriteria
            {
                ObjectType = ChangeLogObjectType,
                StartDate  = startDate,
                EndDate    = endDate,
                Take       = 0
            };

            // Get changes count from operation log
            result = (await _changeLogSearchService.SearchAsync(criteria)).TotalCount;
            return(result);
        }
Beispiel #13
0
        private async Task <int> GetTotalDeletedProductsCount(DateTime?startDate, DateTime?endDate)
        {
            var criteria = new ChangeLogSearchCriteria
            {
                ObjectType     = ChangeLogObjectType,
                OperationTypes = new[] { EntryState.Deleted },
                StartDate      = startDate,
                EndDate        = endDate,
                Take           = 0
            };

            var deletedOperations = (await _changeLogSearchService.SearchAsync(criteria)).Results;
            var deletedCount      = deletedOperations.Count;

            return(deletedCount);
        }
Beispiel #14
0
        private async Task <ChangeLogSearchResult> SearchDeleteOperationsInLog(DateTime?startDate, DateTime?endDate, long skip, long take)
        {
            var criteria = new ChangeLogSearchCriteria
            {
                ObjectType     = ChangeLogObjectType,
                OperationTypes = new[] { EntryState.Deleted },
                StartDate      = startDate,
                EndDate        = endDate,
                Skip           = Convert.ToInt32(skip),
                Take           = Convert.ToInt32(take)
            };

            var searchResult = (await _changeLogSearchService.SearchAsync(criteria));

            return(searchResult);
        }
Beispiel #15
0
        public async Task <ActionResult <LastModifiedResponse> > GetLastModifiedDate([FromQuery] string scope = null)
        {
            var criteria = new ChangeLogSearchCriteria
            {
                Take = 1
            };
            // Get latest change  from operation log
            var latestChange = (await _changeLogSearchService.SearchAsync(criteria)).Results.FirstOrDefault();

            var result = new LastModifiedResponse
            {
                Scope            = scope,
                LastModifiedDate = new DateTime(Math.Max((latestChange?.ModifiedDate ?? _lastTimestamp).Ticks, _lastTimestamp.Ticks))
            };

            return(Ok(result));
        }
        public virtual async Task <ChangeLogSearchResult> SearchAsync(ChangeLogSearchCriteria criteria)
        {
            var result = AbstractTypeFactory <ChangeLogSearchResult> .TryCreateInstance();

            using (var repository = _repositoryFactory())
            {
                repository.DisableChangesTracking();

                var sortInfos = GetSortInfos(criteria);
                var query     = GetQuery(repository, criteria, sortInfos);

                result.TotalCount = await query.CountAsync();

                if (criteria.Take > 0)
                {
                    result.Results = (await query.Skip(criteria.Skip).Take(criteria.Take).ToArrayAsync()).Select(x => x.ToModel(AbstractTypeFactory <OperationLog> .TryCreateInstance())).ToList();
                }
            }
            return(result);
        }
        public async Task <ActionResult <OperationLog[]> > GetOrderChanges(string id)
        {
            var result = Array.Empty <OperationLog>();
            var order  = await _customerOrderService.GetByIdAsync(id);

            if (order != null)
            {
                //Load general change log for order
                var allHasHangesObjects = order.GetFlatObjectsListWithInterface <IHasChangesHistory>()
                                          .Distinct().ToArray();

                var criteria = new ChangeLogSearchCriteria
                {
                    ObjectIds   = allHasHangesObjects.Select(x => x.Id).Distinct().ToArray(),
                    ObjectTypes = allHasHangesObjects.Select(x => x.GetType().Name).Distinct().ToArray()
                };
                result = (await _changeLogSearchService.SearchAsync(criteria)).Results.ToArray();
            }
            return(Ok(result));
        }
        public async Task <ActionResult <ChangeLogSearchResult> > SearchOrderChanges([FromBody] CustomerOrderHistorySearchCriteria historySearchCriteria)
        {
            if (historySearchCriteria.OrderId == null)
            {
                throw new InvalidOperationException($"Order ID can not be null");
            }

            var order = await _customerOrderService.GetByIdAsync(historySearchCriteria.OrderId);

            if (order == null)
            {
                throw new InvalidOperationException($"Cannot find order with ID {historySearchCriteria.OrderId}");
            }

            var authorizationResult = await _authorizationService.AuthorizeAsync(User, order, new OrderAuthorizationRequirement(ModuleConstants.Security.Permissions.Read));

            if (!authorizationResult.Succeeded)
            {
                return(Unauthorized());
            }

            //Load general change log for order
            var allHasChangesObjects = order.GetFlatObjectsListWithInterface <IHasChangesHistory>()
                                       .Distinct().ToArray();

            var criteria = new ChangeLogSearchCriteria
            {
                ObjectIds   = allHasChangesObjects.Select(x => x.Id).Distinct().ToArray(),
                ObjectTypes = allHasChangesObjects.Select(x => x.GetType().Name).Distinct().ToArray(),
                Skip        = historySearchCriteria.Skip,
                Take        = historySearchCriteria.Take,
                Sort        = historySearchCriteria.Sort
            };

            var result = await _changeLogSearchService.SearchAsync(criteria);

            return(Ok(result));
        }
Beispiel #19
0
        public virtual async Task <long> GetTotalChangesCountAsync(DateTime?startDate, DateTime?endDate)
        {
            long result;

            if (startDate == null && endDate == null)
            {
                // We don't know the total products count
                result = 0L;
            }
            else
            {
                var criteria = new ChangeLogSearchCriteria
                {
                    ObjectType = ChangeLogObjectType,
                    StartDate  = startDate,
                    EndDate    = endDate,
                    Take       = 0
                };
                // Get changes count from operation log
                result = (await _changeLogSearchService.SearchAsync(criteria)).TotalCount;
            }

            return(result);
        }
Beispiel #20
0
        protected virtual IQueryable <OperationLogEntity> GetQuery(IPlatformRepository repository, ChangeLogSearchCriteria criteria, IEnumerable <SortInfo> sortInfos)
        {
            var query = repository.OperationLogs.Where(x => (criteria.StartDate == null || x.ModifiedDate >= criteria.StartDate) && (criteria.EndDate == null || x.ModifiedDate <= criteria.EndDate));

            if (!criteria.OperationTypes.IsNullOrEmpty())
            {
                var operationTypes = criteria.OperationTypes.Select(x => x.ToString());
                query = query.Where(x => operationTypes.Contains(x.OperationType));
            }
            if (!criteria.ObjectIds.IsNullOrEmpty())
            {
                query = query.Where(x => criteria.ObjectIds.Contains(x.ObjectId));
            }
            if (!criteria.ObjectTypes.IsNullOrEmpty())
            {
                query = query.Where(x => criteria.ObjectTypes.Contains(x.ObjectType));
            }

            return(query.OrderBySortInfos(sortInfos));
        }