public void BuildsUpCosmosSqlForSuppliedParameters(IEnumerable <string> fundingStreamIds,
                                                           IEnumerable <string> fundingPeriodIds,
                                                           IEnumerable <string> groupingReasons,
                                                           IEnumerable <string> variationReasons,
                                                           int top,
                                                           int?pageRef,
                                                           int totalCount,
                                                           string expectedSql)
        {
            CosmosDbQuery query = _queryBuilder.BuildQuery(fundingStreamIds,
                                                           fundingPeriodIds,
                                                           groupingReasons,
                                                           variationReasons,
                                                           top,
                                                           pageRef,
                                                           totalCount);

            query
            .Should()
            .NotBeNull();

            query
            .QueryText
            .Trim()
            .Should()
            .Be(expectedSql);
        }
示例#2
0
        public async Task <ProviderResult> GetProviderResultByProviderIdAndSpecificationId(string providerId, string specificationId)
        {
            if (string.IsNullOrWhiteSpace(providerId))
            {
                throw new ArgumentNullException(nameof(providerId));
            }

            if (string.IsNullOrWhiteSpace(specificationId))
            {
                throw new ArgumentNullException(nameof(specificationId));
            }

            CosmosDbQuery cosmosDbQuery = new CosmosDbQuery
            {
                QueryText  = @"SELECT *
                            FROM    c 
                            WHERE   c.documentType = 'ProviderResult'
                                    AND c.content.specification.id = @SpecificationId",
                Parameters = new[]
                {
                    new CosmosDbQueryParameter("@SpecificationId", specificationId)
                }
            };

            ProviderResult providerResult = (await _cosmosRepository.QueryPartitionedEntity <ProviderResult>(cosmosDbQuery, partitionKey: providerId)).FirstOrDefault();

            return(providerResult);
        }
示例#3
0
        public async Task <bool> DatasetExistsWithGivenName(string datasetName, string datasetId)
        {
            CosmosDbQuery cosmosDbQuery = new CosmosDbQuery
            {
                QueryText  = @"SELECT VALUE COUNT(1)
                            FROM    datasets d
                            WHERE   d.deleted = false 
                                    AND d.documentType = ""DatasetDefinition""
                                    AND d.id != @DatasetId
                                    AND LOWER(d.content.name) = @DatasetName",
                Parameters = new[]
                {
                    new CosmosDbQueryParameter("@DatasetName", datasetName.ToLowerInvariant()),
                    new CosmosDbQueryParameter("@DatasetId", datasetId)
                }
            };

            IEnumerable <dynamic> results = await _cosmosRepository.DynamicQuery(cosmosDbQuery, 10);

            if (results.IsNullOrEmpty())
            {
                return(false);
            }

            dynamic item = results.FirstOrDefault();

            return((long)item > 0);
        }
示例#4
0
        public async Task <FundingStreamPermission> GetFundingStreamPermission(string userId, string fundingStreamId)
        {
            Guard.IsNullOrWhiteSpace(userId, nameof(userId));
            Guard.IsNullOrWhiteSpace(fundingStreamId, nameof(fundingStreamId));

            CosmosDbQuery cosmosDbQuery = new CosmosDbQuery
            {
                QueryText  = @"SELECT *
                            FROM    Root r
                            WHERE   r.content.fundingStreamId = @FundingStreamID
                                    AND r.documentType = @DocumentType
                                    AND r.deleted = false",
                Parameters = new[]
                {
                    new CosmosDbQueryParameter("@FundingStreamID", fundingStreamId),
                    new CosmosDbQueryParameter("@DocumentType", nameof(FundingStreamPermission))
                }
            };

            IEnumerable <FundingStreamPermission> permission = await _cosmosRepository.QueryPartitionedEntity <FundingStreamPermission>(cosmosDbQuery, partitionKey : userId);

            if (!permission.AnyWithNullCheck())
            {
                return(null);
            }

            return(permission.Single());
        }
        public async Task <IEnumerable <CalculationMetadata> > GetCalculationsMetatadataBySpecificationId(string specificationId)
        {
            CosmosDbQuery cosmosDbQuery = new CosmosDbQuery
            {
                QueryText  = @"SELECT 
                                    c.content.specificationId as SpecificationId,
                                    c.content.fundingStreamId as FundingStreamId,
                                    c.content.current.calculationId as CalculationId,
                                    c.content.current.valueType as ValueType,
                                    c.content.current.name as Name,
                                    c.content.current.sourceCodeName as SourceCodeName,
                                    c.content.current.calculationType as CalculationType,
                                    c.content.current.namespace as Namespace,
                                    c.content.current.wasTemplateCalculation as WasTemplateCalculation,
                                    c.content.current.description as Description,
                                    c.content.current.publishStatus as PublishStatus
                                    FROM c 
                                    WHERE c.content.specificationId = @SpecificationId and c.documentType = 'Calculation'",
                Parameters = new[]
                {
                    new CosmosDbQueryParameter("@SpecificationId", specificationId)
                }
            };

            IEnumerable <dynamic> results = await _cosmosRepository.DynamicQuery(cosmosDbQuery);

            string resultsString = JsonConvert.SerializeObject(results.ToArray());

            CalculationMetadata[] items = JsonConvert.DeserializeObject <CalculationMetadata[]>(resultsString);

            return(await Task.FromResult(items));
        }
示例#6
0
        public async Task <IEnumerable <string> > GetDistinctRelationshipSpecificationIdsForDatasetDefinitionId(string datasetDefinitionId)
        {
            CosmosDbQuery cosmosDbQuery = new CosmosDbQuery
            {
                QueryText  = @"SELECT d.content.Specification.id AS specificationId
                            FROM    datasets d
                            WHERE   d.deleted = false 
                                    AND d.documentType = ""DefinitionSpecificationRelationship"" 
                                    AND d.content.DatasetDefinition.id = @DatasetDefinitionId",
                Parameters = new[]
                {
                    new CosmosDbQueryParameter("@DatasetDefinitionID", datasetDefinitionId)
                }
            };

            HashSet <string> specificationIds = new HashSet <string>();

            IEnumerable <dynamic> results = await _cosmosRepository.DynamicQuery(cosmosDbQuery, 1000);

            foreach (dynamic result in results)
            {
                specificationIds.Add(result.specificationId);
            }

            return(specificationIds);
        }
示例#7
0
        public async Task <IEnumerable <KeyValuePair <string, int> > > GetDatasetLatestVersions(IEnumerable <string> datasetIds)
        {
            Guard.IsNotEmpty(datasetIds, nameof(datasetIds));

            StringBuilder queryTextBuilder = new StringBuilder(@"
                SELECT d.id, d.content.current.version
                FROM datasets d
                WHERE d.deleted = false 
                AND d.documentType = 'Dataset'");

            string documentIdQueryText = string.Join(',', datasetIds.Select((_, index) => $"@datasetId_{index}"));

            queryTextBuilder.Append($" AND d.id IN ({documentIdQueryText})");

            IEnumerable <CosmosDbQueryParameter> cosmosDbQueryParameters = datasetIds.Select((_, index) => new CosmosDbQueryParameter($"@datasetId_{index}", _));

            CosmosDbQuery cosmosDbQuery = new CosmosDbQuery
            {
                QueryText  = queryTextBuilder.ToString(),
                Parameters = cosmosDbQueryParameters
            };

            IDictionary <string, int> results      = new Dictionary <string, int>();
            IEnumerable <dynamic>     queryResults = await _cosmosRepository
                                                     .DynamicQuery(cosmosDbQuery);

            foreach (dynamic item in queryResults)
            {
                results.Add((string)item.id, (int)item.version);
            }

            return(await Task.FromResult(results));
        }
示例#8
0
        public async Task QueryPublishedFundingCountBuildsQueryAndTreatsResultsAsScalarCountJObject()
        {
            IEnumerable <string> fundingStreamIds = EnumerableFor(NewRandomString(), NewRandomString());
            IEnumerable <string> fundingPeriodIds = EnumerableFor(NewRandomString(), NewRandomString(), NewRandomString());
            IEnumerable <string> groupingReasons  = EnumerableFor(NewRandomString());
            IEnumerable <string> variationReasons = EnumerableFor(NewRandomString());

            CosmosDbQuery query = new CosmosDbQuery();

            GivenTheCosmosDbCountQuery(fundingStreamIds,
                                       fundingPeriodIds,
                                       groupingReasons,
                                       variationReasons,
                                       query);

            int expectedCount             = new RandomNumberBetween(1, 10000);
            IEnumerable <dynamic> results = new dynamic[] { expectedCount };

            AndTheDynamicResultsForTheQuery(query, results);

            int actualCount = await _repository.QueryPublishedFundingCount(fundingStreamIds,
                                                                           fundingPeriodIds,
                                                                           groupingReasons,
                                                                           variationReasons);

            actualCount
            .Should()
            .Be(expectedCount);
        }
示例#9
0
        public async Task QueryPublishedFundingDelegatesToQueryBuilderAndExecutesCosmosDbQueryItCreates()
        {
            IEnumerable <string> fundingStreamIds = EnumerableFor(NewRandomString(), NewRandomString());
            IEnumerable <string> fundingPeriodIds = EnumerableFor(NewRandomString(), NewRandomString(), NewRandomString());
            IEnumerable <string> groupingReasons  = EnumerableFor(NewRandomString());
            IEnumerable <string> variationReasons = EnumerableFor(NewRandomString());
            int top     = NewRandomNumber();
            int pageRef = NewRandomNumber();


            IEnumerable <PublishedFundingIndex> expectedResults = new PublishedFundingIndex[0];
            CosmosDbQuery query = new CosmosDbQuery();

            GivenTheCosmosDbQuery(fundingStreamIds,
                                  fundingPeriodIds,
                                  groupingReasons,
                                  variationReasons,
                                  top,
                                  pageRef,
                                  query);
            AndTheDynamicResultsForTheQuery(query, expectedResults);

            IEnumerable <PublishedFundingIndex> actualResults = await _repository.QueryPublishedFunding(fundingStreamIds,
                                                                                                        fundingPeriodIds,
                                                                                                        groupingReasons,
                                                                                                        variationReasons,
                                                                                                        top,
                                                                                                        pageRef,
                                                                                                        0);

            actualResults
            .Should()
            .BeEquivalentTo(expectedResults);
        }
示例#10
0
        public async Task <IEnumerable <ProviderResult> > GetProviderResultsBySpecificationIdAndProviders(IEnumerable <string> providerIds, string specificationId)
        {
            Guard.ArgumentNotNull(providerIds, nameof(providerIds));
            Guard.IsNullOrWhiteSpace(specificationId, nameof(specificationId));

            if (providerIds.IsNullOrEmpty())
            {
                return(Enumerable.Empty <ProviderResult>());
            }

            List <Task> allTasks = new List <Task>(providerIds.Count());

            ConcurrentBag <ProviderResult> results = new ConcurrentBag <ProviderResult>();

            SemaphoreSlim throttler = new SemaphoreSlim(_engineSettings.CalculateProviderResultsDegreeOfParallelism);

            foreach (string providerId in providerIds)
            {
                await throttler.WaitAsync();

                allTasks.Add(
                    Task.Run(async() =>
                {
                    try
                    {
                        CosmosDbQuery cosmosDbQuery = new CosmosDbQuery
                        {
                            QueryText  = @"SELECT * 
                                FROM    Root r 
                                WHERE   r.documentType = @DocumentType 
                                        AND r.content.specificationId = @SpecificationId
                                        AND r.deleted = false",
                            Parameters = new[]
                            {
                                new CosmosDbQueryParameter("@DocumentType", nameof(ProviderResult)),
                                new CosmosDbQueryParameter("@SpecificationId", specificationId)
                            }
                        };

                        IEnumerable <ProviderResult> providerResults = await _cosmosRepository.QueryPartitionedEntity <ProviderResult>(cosmosDbQuery, partitionKey: providerId);
                        foreach (ProviderResult providerResult in providerResults)
                        {
                            results.Add(providerResult);
                        }
                    }
                    finally
                    {
                        throttler.Release();
                    }
                }));
            }

            await TaskHelper.WhenAllAndThrow(allTasks.ToArray());

            return(results.AsEnumerable());
        }
        public async Task <IEnumerable <JobLog> > GetJobLogsByJobId(string jobId)
        {
            Guard.IsNullOrWhiteSpace(jobId, nameof(jobId));

            CosmosDbQuery cosmosDbQuery = new CosmosDbQuery("SELECT j FROM Jobs j WHERE j.documentType = \"JobLog\" AND j.deleted = false");

            IEnumerable <JobLog> jobLogs = await _cosmosRepository.QueryPartitionedEntity <JobLog>(cosmosDbQuery, partitionKey : jobId);

            return(jobLogs);
        }
示例#12
0
        public void Given_Null_When_GetUrlItemEntityAsync_Invoked_Then_It_Should_Throw_Exception()
        {
            var helper = new Mock <ICosmosDbContainerHelper>();

            var query = new CosmosDbQuery(helper.Object);

            Func <Task> func = async() => await query.GetUrlItemEntityAsync(null).ConfigureAwait(false);

            func.Should().Throw <ArgumentNullException>();
        }
示例#13
0
        internal async Task <IEnumerable <dynamic> > GetDocuments(CosmosDbQuery cosmosDbQuery, string containerName)
        {
            _cosmosDbSettings.ContainerName = containerName;

            ICosmosRepository cosmosRepository = new CosmosRepository(_cosmosDbSettings);

            IEnumerable <dynamic> queryResults = await cosmosRepository
                                                 .DynamicQuery(cosmosDbQuery);

            return(queryResults);
        }
        public async Task <IEnumerable <TestScenarioResult> > GetCurrentTestResults(IEnumerable <string> providerIds, string specificationId)
        {
            Guard.ArgumentNotNull(providerIds, nameof(providerIds));
            Guard.IsNullOrWhiteSpace(specificationId, nameof(specificationId));

            if (providerIds.IsNullOrEmpty())
            {
                return(Enumerable.Empty <TestScenarioResult>());
            }

            ConcurrentBag <TestScenarioResult> results = new ConcurrentBag <TestScenarioResult>();

            int completedCount = 0;

            ParallelLoopResult result = Parallel.ForEach(providerIds, new ParallelOptions()
            {
                MaxDegreeOfParallelism = _engineSettings.GetCurrentProviderTestResultsDegreeOfParallelism
            }, async(providerId) =>
            {
                try
                {
                    CosmosDbQuery cosmosDbQuery = new CosmosDbQuery
                    {
                        QueryText  = @"SELECT * 
                                FROM    Root r 
                                WHERE   r.documentType = @DocumentType 
                                        AND r.content.specification.id = @SpecificationId
                                        AND r.deleted = false",
                        Parameters = new[]
                        {
                            new CosmosDbQueryParameter("@DocumentType", nameof(TestScenarioResult)),
                            new CosmosDbQueryParameter("@SpecificationId", specificationId)
                        }
                    };

                    IEnumerable <TestScenarioResult> testScenarioResults = await _cosmosRepository.QueryPartitionedEntity <TestScenarioResult>(cosmosDbQuery, partitionKey: providerId);
                    foreach (TestScenarioResult testScenarioResult in testScenarioResults)
                    {
                        results.Add(testScenarioResult);
                    }
                }
                finally
                {
                    completedCount++;
                }
            });

            while (completedCount < providerIds.Count())
            {
                await Task.Delay(20);
            }

            return(results.AsEnumerable());
        }
        public async Task <IEnumerable <string> > GetDistinctFundingStreamsForSpecifications()
        {
            CosmosDbQuery cosmosDbQuery = new CosmosDbQuery
            {
                QueryText = @" SELECT Distinct value c.id
                                FROM specifications f 
                                JOIN c IN f.content.current.fundingStreams 
                                WHERE f.documentType = 'Specification' and f.deleted = false"
            };

            return(await _repository.RawQuery <string>(cosmosDbQuery));
        }
示例#16
0
 private void GivenTheCosmosDbCountQuery(IEnumerable <string> fundingStreamIds,
                                         IEnumerable <string> fundingPeriodIds,
                                         IEnumerable <string> groupingReasons,
                                         IEnumerable <string> variationReasons,
                                         CosmosDbQuery expectedQuery)
 {
     _publishedFundingQueryBuilder.Setup(_ => _.BuildCountQuery(fundingStreamIds,
                                                                fundingPeriodIds,
                                                                groupingReasons,
                                                                variationReasons))
     .Returns(expectedQuery);
 }
示例#17
0
        public async Task ProviderResultsBatchProcessing(string specificationId, Func <List <ProviderResult>, Task> processProcessProviderResultsBatch, int itemsPerPage = 1000)
        {
            CosmosDbQuery cosmosDbQuery = new CosmosDbQuery
            {
                QueryText  = $@"SELECT
                                c.id as id,
                                c.createdAt as createdAt,
                                c.content.specificationId as specificationId,
                                {{
                                ""urn"" : c.content.provider.urn,
                                ""ukPrn"" : c.content.provider.ukPrn,
                                ""upin"" : c.content.provider.upin,
                                ""Id"" : c.content.provider.id,
                                ""Name"" : c.content.provider.name,
                                ""providerType"" : c.content.provider.providerType,
                                ""providerSubType"" : c.content.provider.providerSubType,
                                ""authority"" : c.content.provider.authority,
                                ""laCode"" : c.content.provider.laCode,
                                ""localAuthorityName"" : c.content.provider.localAuthorityName,
                                ""establishmentNumber"" : c.content.provider.establishmentNumber,
                                ""dateOpened"" : c.content.provider.dateOpened }} AS provider,
                                ARRAY(
                                    SELECT calcResult.calculation as calculation, 
                                    calcResult[""value""],
                                    calcResult.exceptionType as exceptionType,
                                    calcResult.exceptionMessage as exceptionMessage,
                                    calcResult.calculationType as calculationType
                                    FROM calcResult IN c.content.calcResults) AS calcResults,
                                ARRAY(
                                    SELECT fundingLineResult.fundingLine as fundingLine,
                                    fundingLineResult.fundingLineFundingStreamId as fundingLineFundingStreamId,
                                    fundingLineResult[""value""],
                                    fundingLineResult.exceptionType as exceptionType,
                                    fundingLineResult.exceptionMessage as exceptionMessage
                                    FROM fundingLineResult IN c.content.fundingLineResults) AS fundingLineResults
                            FROM    calculationresults c
                            WHERE   c.content.specificationId = @SpecificationId 
                                    AND c.documentType = 'ProviderResult' 
                                    AND c.deleted = false",
                Parameters = new[]
                {
                    new CosmosDbQueryParameter("@SpecificationId", specificationId)
                }
            };

            await _cosmosRepository.DocumentsBatchProcessingAsync(persistBatchToIndex : processProcessProviderResultsBatch,
                                                                  cosmosDbQuery : cosmosDbQuery,
                                                                  itemsPerPage : itemsPerPage);
        }
示例#18
0
        public async Task <bool> ProviderHasResultsBySpecificationId(string specificationId)
        {
            CosmosDbQuery cosmosDbQuery = new CosmosDbQuery
            {
                QueryText  = "SELECT VALUE COUNT(1) FROM c WHERE c.documentType = 'ProviderResult' AND  c.content.specificationId = @SpecificationId",
                Parameters = new[]
                {
                    new CosmosDbQueryParameter("@SpecificationId", specificationId)
                }
            };

            IEnumerable <bool> result = await _cosmosRepository.RawQuery <bool>(cosmosDbQuery, 1);

            return(result.FirstOrDefault());
        }
示例#19
0
        public async Task <IEnumerable <FundingStreamPermission> > GetFundingStreamPermissions(string userId)
        {
            Guard.IsNullOrWhiteSpace(userId, nameof(userId));

            CosmosDbQuery cosmosDbQuery = new CosmosDbQuery
            {
                QueryText  = @"SELECT * FROM Root r WHERE r.content.userId = @UserId AND r.documentType = @DocumentType AND r.deleted = false",
                Parameters = new[]
                {
                    new CosmosDbQueryParameter("@UserId", userId),
                    new CosmosDbQueryParameter("@DocumentType", nameof(FundingStreamPermission))
                }
            };

            return(await _cosmosRepository.QueryPartitionedEntity <FundingStreamPermission>(cosmosDbQuery, partitionKey : userId));
        }
        public async Task Run()
        {
            try
            {
                await _cosmosRepository.SetThroughput(100000);

                Console.WriteLine("Set calcresults RU at 100000");

                CosmosDbQuery query = new CosmosDbQuery
                {
                    QueryText = @"  SELECT * 
                                    FROM c 
                                    WHERE c.documentType = 'ProviderResult' 
                                    AND c.deleted = false"
                };

                ICosmosDbFeedIterator <ProviderResult> feed = _cosmosRepository.GetFeedIterator <ProviderResult>(query);

                ApiResponse <IEnumerable <FundingPeriod> > fundingPeriods = await _policiesPolicy.ExecuteAsync(() => _policies.GetFundingPeriods());

                MergeContext context = new MergeContext(feed, fundingPeriods.Content);

                IProducerConsumer producerConsumer = _producerConsumerFactory.CreateProducerConsumer(ProduceSpecificationInformation,
                                                                                                     ConsumeSpecificationInformation,
                                                                                                     8,
                                                                                                     4,
                                                                                                     _logger);

                await producerConsumer.Run(context);

                Console.WriteLine($"Starting bulk upsert of {_cachedProviderWithResultsForSpecifications.Count} providers with results for specifications summaries");

                await _cosmosRepository.BulkUpsertAsync(_cachedProviderWithResultsForSpecifications);
            }
            catch (Exception e)
            {
                _logger.Error(e, "Unable to complete provider version migration");

                throw;
            }
            finally
            {
                await _cosmosRepository.SetThroughput(10000);

                Console.WriteLine("Set calcresults RU at 10000");
            }
        }
示例#21
0
        public ICosmosDbFeedIterator <ProviderWithResultsForSpecifications> GetProvidersWithResultsForSpecificationBySpecificationId(string specificationId)
        {
            CosmosDbQuery cosmosDbQuery = new CosmosDbQuery
            {
                QueryText  = @"SELECT * 
                              FROM    providerWithResultsForSpecifications p
                              WHERE   p.documentType = 'ProviderWithResultsForSpecifications'
                              AND p.deleted = false
                              AND EXISTS(SELECT VALUE specification FROM specification IN p.content.specifications WHERE specification.id = @specificationId)",
                Parameters = new []
                {
                    new CosmosDbQueryParameter("@specificationId", specificationId),
                }
            };

            return(_cosmosRepository.GetFeedIterator <ProviderWithResultsForSpecifications>(cosmosDbQuery));
        }
示例#22
0
        public async Task GetTemplatesForIndexing(Func <List <Template>, Task> persistIndexBatch, int batchSize)
        {
            CosmosDbQuery query = new CosmosDbQuery
            {
                QueryText = @"SELECT
        c.content.templateId,
        c.content.name,
        { 
           'name'         : c.content.name,
           'templateId'   : c.content.current.templateId,
           'date'         : c.content.current.date,
           'majorVersion' : c.content.current.majorVersion,
           'minorVersion' : c.content.current.minorVersion,
           'status'       : c.content.current.status,
           'version'      : c.content.current.version,
           'author'       : {
              'id'           : c.content.current.author.id,
              'name'         : c.content.current.author.name
            }
        } AS Current,
        { 
           'majorVersion' : c.content.released.majorVersion,
           'minorVersion' : c.content.released.minorVersion,
           'status'       : c.content.released.status,
           'version'      : c.content.released.version
        } AS Released,
        {
           'id'           : c.content.fundingStream.id,
           'name'         : c.content.fundingStream.name,
           'shortName'    : c.content.fundingStream.shortName
        } AS FundingStream,
        {
           'id'           : c.content.fundingPeriod.id,
           'name'         : c.content.fundingPeriod.name,
           'shortName'    : c.content.fundingPeriod.shortName
        } AS FundingPeriod
FROM     templateBuilder c
WHERE    c.documentType = 'Template' 
AND      c.deleted = false"
            };

            await _cosmosRepository.DocumentsBatchProcessingAsync(persistBatchToIndex : persistIndexBatch,
                                                                  cosmosDbQuery : query,
                                                                  itemsPerPage : batchSize);
        }
示例#23
0
        public async Task <IEnumerable <FundingStreamPermission> > GetUsersWithFundingStreamPermissions(string fundingStreamId)
        {
            CosmosDbQuery cosmosDbQuery = new CosmosDbQuery
            {
                QueryText  = @"SELECT * 
                            FROM    Root r 
                            WHERE   r.content.fundingStreamId = @FundingStreamID 
                                    AND r.documentType = @DocumentType
                                    AND r.deleted = false",
                Parameters = new[]
                {
                    new CosmosDbQueryParameter("@FundingStreamID", fundingStreamId),
                    new CosmosDbQueryParameter("@DocumentType", nameof(FundingStreamPermission))
                }
            };

            return(await _cosmosRepository.QuerySql <FundingStreamPermission>(cosmosDbQuery));
        }
示例#24
0
        public async Task Run()
        {
            try
            {
                CosmosDbQuery query = new CosmosDbQuery
                {
                    QueryText = @"  SELECT * 
                                    FROM c 
                                    WHERE c.documentType = 'PublishedProvider' 
                                    AND c.content.current.status = 'Released'
                                    AND IS_DEFINED(c.content.current.released) = false"
                };
                int batchCount = 1;

                await _resiliencePolicy.ExecuteAsync(() => _cosmosRepository.DocumentsBatchProcessingAsync <DocumentEntity <PublishedProvider> >(async publishedProviders =>
                {
                    List <DocumentEntity <PublishedProvider> > listPublishedProviders = new List <DocumentEntity <PublishedProvider> >();

                    foreach (var publishedProvider in publishedProviders)
                    {
                        if (publishedProvider.Content.Current.Status != PublishedProviderStatus.Released)
                        {
                            continue;
                        }
                        publishedProvider.Content.Released = publishedProvider.Content.Current;
                        listPublishedProviders.Add(publishedProvider);
                    }

                    _logger.Information(
                        $"Bulk upserting batch number {batchCount} of published provider release property update migration. Total published provider migrated will be {batchCount * 10}.");

                    await _cosmosRepository.BulkUpsertAsync(listPublishedProviders.Select(_ => _.Content).ToArray(), 1);
                    batchCount++;
                },
                                                                                                                                                 query,
                                                                                                                                                 10));
            }
            catch (Exception e)
            {
                _logger.Error(e, "Unable to complete published provider release property update migration");

                throw;
            }
        }
示例#25
0
        public async Task Given_Values_When_GetVisitItemEntityCollectionAsync_Invoked_Then_It_Should_Return_Result(string shortUrl)
        {
            var records = new List <VisitItemEntity>()
            {
                new VisitItemEntity()
                {
                    ShortUrl = shortUrl, DateGenerated = DateTimeOffset.UtcNow.AddHours(-1)
                },
                new VisitItemEntity()
                {
                    ShortUrl = shortUrl, DateGenerated = DateTimeOffset.UtcNow.AddHours(-2)
                }
            };

            var feed = new Mock <FeedResponse <VisitItemEntity> >();

            feed.SetupGet(p => p.Resource).Returns(records);
            feed.Setup(p => p.GetEnumerator()).Returns(records.GetEnumerator());

            var iterator = new Mock <FeedIterator <VisitItemEntity> >();

            iterator.SetupSequence(p => p.HasMoreResults)
            .Returns(true)
            .Returns(false);
            iterator.Setup(p => p.ReadNextAsync(It.IsAny <CancellationToken>())).ReturnsAsync(feed.Object);

            var container = new Mock <Container>();

            container.Setup(p => p.GetItemQueryIterator <VisitItemEntity>(It.IsAny <QueryDefinition>(), It.IsAny <string>(), It.IsAny <QueryRequestOptions>()))
            .Returns(iterator.Object);

            var helper = new Mock <ICosmosDbContainerHelper>();

            helper.Setup(p => p.GetContainerAsync()).ReturnsAsync(container.Object);

            var query = new CosmosDbQuery(helper.Object);

            var result = await query.GetVisitItemEntityCollectionAsync(shortUrl).ConfigureAwait(false);

            result.Should().NotBeNull();
            result.Items.Should().HaveCount(records.Count);
            result.Items.All(p => p.ShortUrl == shortUrl).Should().BeTrue();
        }
        public async Task Run()
        {
            try
            {
                CosmosDbQuery query = new CosmosDbQuery
                {
                    QueryText = @"  SELECT * 
                                    FROM c 
                                    WHERE c.documentType = 'PublishedProviderVersion' 
                                    AND c.deleted = false
                                    AND (c.content.status = 'Approved' OR c.content.status = 'Draft') 
                                    AND c.content.majorVersion = 1 
                                    AND c.content.minorVersion = 0"
                };

                int batchCount = 1;

                await _resiliencePolicy.ExecuteAsync(() => _cosmosRepository.DocumentsBatchProcessingAsync <DocumentEntity <PublishedProviderVersion> >(async providerVersions =>
                {
                    foreach (var providerVersion in providerVersions)
                    {
                        providerVersion.Content.MajorVersion = 0;
                        providerVersion.Content.MinorVersion = 1;
                    }

                    _logger.Information(
                        $"Bulk upserting batch number {batchCount} of provider version migration. Total provider versions migrated will be {batchCount * 10}.");

                    await _cosmosRepository.BulkUpsertAsync(providerVersions.Select(_ => _.Content).ToArray(), 1);

                    batchCount++;
                },
                                                                                                                                                        query,
                                                                                                                                                        10));
            }
            catch (Exception e)
            {
                _logger.Error(e, "Unable to complete provider version migration");

                throw;
            }
        }
        public Task <IEnumerable <ProviderSourceDataset> > GetProviderSourceDatasets(string providerId, string specificationId)
        {
            CosmosDbQuery cosmosDbQuery = new CosmosDbQuery
            {
                QueryText  = @"SELECT *
                            FROM    Root r
                            WHERE   r.content.providerId = @ProviderId
                                    AND r.content.specificationId = @SpecificationId
                                    AND r.documentType = @DocumentType 
                                    AND r.deleted = false",
                Parameters = new []
                {
                    new CosmosDbQueryParameter("@ProviderId", providerId),
                    new CosmosDbQueryParameter("@SpecificationId", specificationId),
                    new CosmosDbQueryParameter("@DocumentType", nameof(ProviderSourceDataset))
                }
            };

            return(_cosmosRepository.QueryPartitionedEntity <ProviderSourceDataset>(cosmosDbQuery, partitionKey: providerId));
        }
示例#28
0
        public async Task <decimal> GetCalculationResultTotalForSpecificationId(string specificationId)
        {
            CosmosDbQuery cosmosDbQuery = new CosmosDbQuery
            {
                QueryText  = @"SELECT value sum(c[""value""])
                            FROM results f 
                                JOIN c IN f.content.calcResults
                            WHERE c.calculationType = 10 
                                AND c[""value""] != null 
                                AND f.content.specificationId = @SpecificationId",
                Parameters = new[]
                {
                    new CosmosDbQueryParameter("@SpecificationId", specificationId)
                }
            };

            IEnumerable <decimal> result = await _cosmosRepository.RawQuery <decimal>(cosmosDbQuery, 1);

            return(result.First());
        }
        private async Task <IEnumerable <int> > GetCountOfNonApprovedTemplateCalculationsForSpecificationId(string specificationId)
        {
            CosmosDbQuery cosmosDbQuery = new CosmosDbQuery
            {
                QueryText  = $@"SELECT VALUE COUNT(1)
                            FROM    c 
                            WHERE   c.documentType = 'Calculation' 
                                    AND c.content.current.publishStatus != 'Approved'
                                    AND c.content.specificationId = @SpecificationId
                                    AND c.content.current.calculationType = 'Template'",
                Parameters = new[]
                {
                    new CosmosDbQueryParameter("@SpecificationId", specificationId)
                }
            };

            IEnumerable <int> result = await _cosmosRepository.RawQuery <int>(cosmosDbQuery, 1);

            return(result);
        }
示例#30
0
        public async Task <IEnumerable <ProviderSourceDataset> > GetCurrentProviderSourceDatasets(string specificationId, string relationshipId)
        {
            CosmosDbQuery cosmosDbQuery = new CosmosDbQuery
            {
                QueryText  = @"SELECT *
                            FROM    r
                            WHERE   r.content.specificationId = @SpecificationId
                                    AND r.content.dataRelationship.id = @RelationshipId
                                    AND r.deleted = false
                                    AND r.documentType = @DocumentType",
                Parameters = new[]
                {
                    new CosmosDbQueryParameter("@SpecificationId", specificationId),
                    new CosmosDbQueryParameter("@RelationshipId", relationshipId),
                    new CosmosDbQueryParameter("@DocumentType", nameof(ProviderSourceDataset))
                }
            };

            return(await _cosmosRepository.QuerySql <ProviderSourceDataset>(cosmosDbQuery, itemsPerPage : 1000));
        }