Exemplo n.º 1
0
        private async Task <long> QueueSearchIndexWriterJob(
            IEnumerable <ProviderResult> providerResults,
            SpecificationSummary specificationSummary,
            Reference user,
            string correlationId,
            string parentJobId)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();

            string specificationId           = specificationSummary.GetSpecificationId();
            IEnumerable <string> providerIds = providerResults.Select(x => x.Provider?.Id).Where(x => !string.IsNullOrWhiteSpace(x)).ToArray();

            if (!providerIds.Any())
            {
                return(0);
            }

            try
            {
                Common.ApiClient.Jobs.Models.Job searchIndexWriterJob = await _jobManagement.QueueJob(
                    new JobCreateModel()
                {
                    Trigger = new Trigger
                    {
                        EntityId   = specificationId,
                        EntityType = "Specification",
                        Message    = "Write ProviderCalculationResultsIndex serach index for specification"
                    },
                    InvokerUserId          = user.Id,
                    InvokerUserDisplayName = user.Name,
                    JobDefinitionId        = JobConstants.DefinitionNames.SearchIndexWriterJob,
                    ParentJobId            = parentJobId,
                    SpecificationId        = specificationId,
                    CorrelationId          = correlationId,
                    Properties             = new Dictionary <string, string>
                    {
                        { "specification-id", specificationId },
                        { "specification-name", specificationSummary.Name },
                        { "index-writer-type", SearchIndexWriterTypes.ProviderCalculationResultsIndexWriter }
                    },
                    MessageBody = JsonConvert.SerializeObject(providerIds)
                });
            }
            catch (Exception ex)
            {
                string errorMessage = $"Failed to queue SearchIndexWriterJob for specification - {specificationId}";
                _logger.Error(ex, errorMessage);
                throw;
            }

            stopwatch.Stop();
            return(stopwatch.ElapsedMilliseconds);
        }
        public async Task SaveProviderResults_WhenExcludedResultsAndIsNewProviderCalculationResultsIndexEnabled_ThenResultsSavedToCosmosSavesNull()
        {
            // Arrange
            ICosmosRepository cosmosRepository = CreateCosmosRepository();
            IJobManagement    jobManagement    = CreateJobManagement();

            ProviderResultsRepository repo = CreateProviderResultsRepository(
                cosmosRepository,
                jobManagement: jobManagement);

            SpecificationSummary specificationSummary = new SpecificationSummary
            {
                Name          = "Specification 1",
                FundingPeriod = new Reference()
            };

            IEnumerable <ProviderResult> results = new List <ProviderResult>
            {
                new ProviderResult
                {
                    CalculationResults = new List <CalculationResult>
                    {
                        new CalculationResult
                        {
                            Calculation = new Reference {
                                Id = "calc1", Name = "calculation one"
                            },
                            CalculationType = Models.Calcs.CalculationType.Template,
                            Value           = null
                        }
                    },
                    FundingLineResults = new List <FundingLineResult>
                    {
                        new FundingLineResult
                        {
                            FundingLine = new Reference {
                                Id = "fl1", Name = "funding line one"
                            },
                            FundingLineFundingStreamId = "FS1",
                            Value = null
                        }
                    },
                    Id       = Guid.NewGuid().ToString(),
                    Provider = new ProviderSummary
                    {
                        Id                  = "prov1",
                        Name                = "Provider 1",
                        ProviderType        = "TYpe 1",
                        ProviderSubType     = "Sub type 1",
                        Authority           = "Authority",
                        UKPRN               = "ukprn123",
                        URN                 = "urn123",
                        EstablishmentNumber = "en123",
                        UPIN                = "upin123",
                        DateOpened          = DateTime.Now
                    },
                    SpecificationId = "spec1"
                }
            };

            // Act
            await repo.SaveProviderResults(results, specificationSummary, 1, 1, _user, _correlationId, _jobId);

            // Assert
            await cosmosRepository.Received(1).UpsertAsync <ProviderResult>(
                Arg.Any <ProviderResult>(),
                Arg.Any <string>(),
                Arg.Is <bool>(false),
                Arg.Is <bool>(false));

            await jobManagement.Received(1).QueueJob(Arg.Is <JobCreateModel>(_ =>
                                                                             _.JobDefinitionId == JobConstants.DefinitionNames.SearchIndexWriterJob &&
                                                                             _.ParentJobId == _jobId &&
                                                                             _.Properties["specification-id"] == specificationSummary.GetSpecificationId() &&
                                                                             _.Properties["specification-name"] == specificationSummary.Name &&
                                                                             _.Properties["index-writer-type"] == SearchIndexWriterTypes.ProviderCalculationResultsIndexWriter &&
                                                                             _.MessageBody == JsonConvert.SerializeObject(results.Select(x => x.Provider.Id))));
        }
        public async Task SaveProviderResults_WhenResultsAndIsNewProviderCalculationResultsIndexEnabled_ThenQueueSearchIndexWriterJob()
        {
            // Arrange
            ICosmosRepository cosmosRepository = CreateCosmosRepository();
            IJobManagement    jobManagement    = CreateJobManagement();

            ProviderResultsRepository repo = CreateProviderResultsRepository(
                cosmosRepository,
                jobManagement: jobManagement);

            SpecificationSummary specificationSummary = new SpecificationSummary
            {
                Id            = NewRandomString(),
                Name          = NewRandomString(),
                FundingPeriod = new Reference
                {
                    Id = NewRandomString()
                },
                LastEditedDate = new RandomDateTime(),
                FundingStreams = new[]
                {
                    new Reference
                    {
                        Id = NewRandomString()
                    },
                    new Reference
                    {
                        Id = NewRandomString()
                    },
                    new Reference
                    {
                        Id = NewRandomString()
                    }
                }
            };

            IEnumerable <ProviderResult> results = new List <ProviderResult>
            {
                new ProviderResult
                {
                    CalculationResults = new List <CalculationResult>
                    {
                        new CalculationResult
                        {
                            Calculation = new Reference {
                                Id = "calc1", Name = "calculation one"
                            },
                            CalculationType = CalculationType.Template,
                            Value           = 1112.3M
                        }
                    },
                    FundingLineResults = new List <FundingLineResult>
                    {
                        new FundingLineResult
                        {
                            FundingLine = new Reference {
                                Id = "fl1", Name = "funding line one"
                            },
                            FundingLineFundingStreamId = "FS1",
                            Value = 112.3M
                        }
                    },
                    Id       = Guid.NewGuid().ToString(),
                    Provider = new ProviderSummary
                    {
                        Id                  = "prov1",
                        Name                = "Provider 1",
                        ProviderType        = "TYpe 1",
                        ProviderSubType     = "Sub type 1",
                        Authority           = "Authority",
                        UKPRN               = "ukprn123",
                        URN                 = "urn123",
                        EstablishmentNumber = "en123",
                        UPIN                = "upin123",
                        DateOpened          = DateTime.Now
                    },
                    SpecificationId = "spec1"
                }
            };

            // Act
            await repo.SaveProviderResults(results, specificationSummary, 1, 1, _user, _correlationId, _jobId);

            // Assert
            await jobManagement.Received(1).QueueJob(Arg.Is <JobCreateModel>(_ =>
                                                                             _.JobDefinitionId == JobConstants.DefinitionNames.SearchIndexWriterJob &&
                                                                             _.Properties["specification-id"] == specificationSummary.GetSpecificationId() &&
                                                                             _.Properties["specification-name"] == specificationSummary.Name &&
                                                                             _.Properties["index-writer-type"] == SearchIndexWriterTypes.ProviderCalculationResultsIndexWriter &&
                                                                             _.ParentJobId == _jobId &&
                                                                             _.MessageBody == JsonConvert.SerializeObject(results.Select(x => x.Provider.Id))));

            await _resultsApiClient.Received(1)
            .QueueMergeSpecificationInformationJob(Arg.Is <MergeSpecificationInformationRequest>(_ =>
                                                                                                 _.SpecificationInformation.Id == specificationSummary.Id &&
                                                                                                 _.SpecificationInformation.Name == specificationSummary.Name &&
                                                                                                 _.SpecificationInformation.LastEditDate == specificationSummary.LastEditedDate &&
                                                                                                 _.SpecificationInformation.FundingStreamIds.SequenceEqual(specificationSummary.FundingStreams.Select(fs => fs.Id).ToArray()) &&
                                                                                                 _.SpecificationInformation.FundingPeriodId == specificationSummary.FundingPeriod.Id &&
                                                                                                 _.ProviderIds.SequenceEqual(new[] { "prov1" })));
        }