public static async Task <DuplicateSlugReport> CreateReportAsync(
            DuplicateSlugInfo info,
            PublishedEntityFinder finder)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

            if (finder == null)
            {
                throw new ArgumentNullException(nameof(finder));
            }

            var publishedEntityIdentifier = await finder
                                            .FindPublishedEntityAsync(info.Key)
                                            .ConfigureAwait(false);

            return(publishedEntityIdentifier.Match(
                       identifier =>
            {
                Func <EntityIdentifier, ForgeEntity> projector = x =>
                                                                 new ForgeEntity(x.EntityId, x.TranslationId, x == identifier);
                return ToReport(info, projector);
            },
                       () =>
            {
                Func <EntityIdentifier, ForgeEntity> projector = x =>
                                                                 new ForgeEntity(x.EntityId, x.TranslationId, false);
                return ToReport(info, projector);
            }
                       ));
        }
        private static async Task <IEnumerable <DuplicateSlugReport> > CreateDuplicateSlugReportsAsync(
            IEnumerable <DuplicateSlugInfo> infos,
            PublishedEntityFinder finder)
        {
            var results = new List <DuplicateSlugReport>();
            var batches = infos.SplitInBatches(BatchSize).ToArray();

            Log.Information(
                "Querying distribution database. Number of batches to be processed is {NumBatches}",
                batches.Length);

            for (var index = 0; index < batches.Length; index++)
            {
                if (index == 0 || (index + 1) % 10 == 0 || index == batches.Length - 1)
                {
                    Log.Information("Processing batch number {BatchNumber}  (this could take a long time)", index + 1);
                }
                else
                {
                    Log.Debug("Processing batch number {BatchNumber}  (this could take a long time)", index + 1);
                }

                var batch   = batches[index];
                var reports = await ProcessBatchAsync(batch, finder).ConfigureAwait(false);

                results.AddRange(reports);
            }

            Log.Debug("Completed processing of batches. Duplicate slug reports created.");

            return(results);
        }
        private static async Task <IEnumerable <DuplicateSlugReport> > ProcessBatchAsync(
            IEnumerable <DuplicateSlugInfo> batch,
            PublishedEntityFinder finder)
        {
            var tasks = batch
                        .Select(async info => await CreateReportAsync(info, finder).ConfigureAwait(false));

            var reports = await Task.WhenAll(tasks).ConfigureAwait(false);

            return(reports);
        }
        public static PublishedEntityFinder CreatePublishedEntityFinder(
            IMongoDatabase distributionDatabase,
            ApplicationConfiguration configuration)
        {
            if (distributionDatabase == null)
            {
                throw new ArgumentNullException(nameof(distributionDatabase));
            }

            var entityCodeToDistributionCodeMap = CreateEntityCodeToDistributionCodeMap(configuration);

            var factory               = new DistributionCollectionFactory(distributionDatabase, entityCodeToDistributionCodeMap);
            var cachedFactory         = new CachedDistributionCollectionFactory(factory);
            var publishedEntityFinder = new PublishedEntityFinder(cachedFactory);

            return(publishedEntityFinder);
        }