Beispiel #1
0
        public async Task <IActionResult> HasCalculationResults(string calculationId)
        {
            Guard.IsNullOrWhiteSpace(calculationId, nameof(calculationId));

            Models.Calcs.Calculation calculation = await _calculationsRepositoryPolicy.ExecuteAsync(() => _calculationRepository.GetCalculationById(calculationId));

            if (calculation == null)
            {
                _logger.Error($"Calculation could not be found for calculation id '{calculationId}'");

                return(new NotFoundObjectResult($"Calculation could not be found for calculation id '{calculationId}'"));
            }

            ProviderResult providerResult = await _resultsRepositoryPolicy.ExecuteAsync(() => _resultsRepository.GetSingleProviderResultBySpecificationId(calculation.SpecificationId));

            if (providerResult != null)
            {
                CalculationResult calculationResult = providerResult.CalculationResults?.FirstOrDefault(m => string.Equals(m.Calculation.Id, calculationId, StringComparison.InvariantCultureIgnoreCase));

                if (calculationResult != null)
                {
                    return(new OkObjectResult(true));
                }
            }

            return(new OkObjectResult(false));
        }
        private async Task <IEnumerable <Job> > CreateGenerateAllocationJobs(JobViewModel parentJob, IEnumerable <IDictionary <string, string> > jobProperties)
        {
            HashSet <string> calculationsToAggregate = new HashSet <string>();

            if (parentJob.JobDefinitionId == JobConstants.DefinitionNames.CreateInstructGenerateAggregationsAllocationJob)
            {
                string calculationAggregatesCacheKeyPrefix = $"{CacheKeys.CalculationAggregations}{parentJob.SpecificationId}";

                await _cacheProvider.RemoveByPatternAsync(calculationAggregatesCacheKeyPrefix);

                IEnumerable <Models.Calcs.Calculation> calculations = await _calculationsRepository.GetCalculationsBySpecificationId(parentJob.SpecificationId);

                foreach (Models.Calcs.Calculation calculation in calculations)
                {
                    IEnumerable <string> aggregateParameters = SourceCodeHelpers.GetCalculationAggregateFunctionParameters(calculation.Current.SourceCode);
                    if (aggregateParameters.IsNullOrEmpty())
                    {
                        continue;
                    }

                    foreach (string aggregateParameter in aggregateParameters)
                    {
                        Models.Calcs.Calculation referencedCalculation = calculations.FirstOrDefault(m => string.Equals(CalculationTypeGenerator.GenerateIdentifier(m.Name.Trim()), aggregateParameter.Trim(), StringComparison.InvariantCultureIgnoreCase));

                        if (referencedCalculation != null)
                        {
                            calculationsToAggregate.Add(aggregateParameter);
                        }
                    }
                }
            }

            IList <JobCreateModel> jobCreateModels = new List <JobCreateModel>();

            Trigger trigger = new Trigger
            {
                EntityId   = parentJob.Id,
                EntityType = nameof(Job),
                Message    = $"Triggered by parent job with id: '{parentJob.Id}"
            };

            int    batchNumber      = 1;
            int    batchCount       = jobProperties.Count();
            string calcsToAggregate = string.Join(",", calculationsToAggregate);

            foreach (IDictionary <string, string> properties in jobProperties)
            {
                properties.Add("batch-number", batchNumber.ToString());
                properties.Add("batch-count", batchCount.ToString());
                properties.Add("calculations-to-aggregate", calcsToAggregate);

                JobCreateModel jobCreateModel = new JobCreateModel
                {
                    InvokerUserDisplayName = parentJob.InvokerUserDisplayName,
                    InvokerUserId          = parentJob.InvokerUserId,
                    JobDefinitionId        = parentJob.JobDefinitionId == JobConstants.DefinitionNames.CreateInstructAllocationJob ? JobConstants.DefinitionNames.CreateAllocationJob : JobConstants.DefinitionNames.GenerateCalculationAggregationsJob,
                    SpecificationId        = parentJob.SpecificationId,
                    Properties             = properties,
                    ParentJobId            = parentJob.Id,
                    Trigger       = trigger,
                    CorrelationId = parentJob.CorrelationId
                };

                batchNumber++;

                jobCreateModels.Add(jobCreateModel);
            }

            return(await _jobsApiClientPolicy.ExecuteAsync(() => _jobsApiClient.CreateJobs(jobCreateModels)));
        }
Beispiel #3
0
        private async Task <IEnumerable <Job> > CreateGenerateAllocationJobs(IEnumerable <IDictionary <string, string> > jobProperties, string specificationId)
        {
            bool calculationEngineRunning = await _calculationEngineRunningChecker.IsCalculationEngineRunning(Job.SpecificationId, new string[] { DefinitionNames.RefreshFundingJob });

            if (calculationEngineRunning)
            {
                throw new RefreshJobRunningException($"Can not create job for specification: {Job.SpecificationId} as there is an existing Refresh Funding Job running for it. Please wait for that job to finish.");
            }

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

            if (Job.JobDefinitionId == JobConstants.DefinitionNames.CreateInstructGenerateAggregationsAllocationJob)
            {
                string calculationAggregatesCacheKeyPrefix = $"{CacheKeys.CalculationAggregations}{Job.SpecificationId}";

                await _cacheProvider.RemoveByPatternAsync(calculationAggregatesCacheKeyPrefix);

                IEnumerable <Models.Calcs.Calculation> calculations = await _calculationsRepository.GetCalculationsBySpecificationId(Job.SpecificationId);

                foreach (Models.Calcs.Calculation calculation in calculations)
                {
                    IEnumerable <string> aggregateParameters = SourceCodeHelpers.GetCalculationAggregateFunctionParameters(calculation.Current.SourceCode);
                    if (aggregateParameters.IsNullOrEmpty())
                    {
                        continue;
                    }

                    foreach (string aggregateParameter in aggregateParameters)
                    {
                        Models.Calcs.Calculation referencedCalculation = calculations.FirstOrDefault(m => string.Equals(_typeIdentifierGenerator.GenerateIdentifier(m.Name.Trim()), aggregateParameter.Trim(), StringComparison.InvariantCultureIgnoreCase));

                        if (referencedCalculation != null)
                        {
                            calculationsToAggregate.Add(aggregateParameter);
                        }
                    }
                }
            }

            IList <JobCreateModel> jobCreateModels = new List <JobCreateModel>();

            Trigger trigger = new Trigger
            {
                EntityId   = Job.Id,
                EntityType = nameof(Job),
                Message    = $"Triggered by parent job with id: '{Job.Id}"
            };

            int    batchNumber      = 1;
            int    batchCount       = jobProperties.Count();
            string calcsToAggregate = string.Join(",", calculationsToAggregate);

            foreach (IDictionary <string, string> properties in jobProperties)
            {
                properties.Add("batch-number", batchNumber.ToString());
                properties.Add("batch-count", batchCount.ToString());
                properties.Add("calculations-to-aggregate", calcsToAggregate);
                properties.Add("session-id", $"{specificationId}-{batchNumber}");

                JobCreateModel jobCreateModel = new JobCreateModel
                {
                    InvokerUserDisplayName = Job.InvokerUserDisplayName,
                    InvokerUserId          = Job.InvokerUserId,
                    JobDefinitionId        = Job.JobDefinitionId == JobConstants.DefinitionNames.CreateInstructAllocationJob ? JobConstants.DefinitionNames.CreateAllocationJob : JobConstants.DefinitionNames.GenerateCalculationAggregationsJob,
                    SpecificationId        = Job.SpecificationId,
                    Properties             = properties,
                    ParentJobId            = Job.Id,
                    Trigger       = trigger,
                    CorrelationId = Job.CorrelationId
                };

                batchNumber++;

                jobCreateModels.Add(jobCreateModel);
            }

            return(await _jobManagement.QueueJobs(jobCreateModels));
        }