예제 #1
0
        public async Task <IActionResult> AssignDatasourceVersionToRelationship([FromRoute] string relationshipId, [FromRoute] string specificationId,
                                                                                [FromRoute] string datasetVersionId)
        {
            Guard.IsNullOrWhiteSpace(relationshipId, nameof(relationshipId));
            Guard.IsNullOrWhiteSpace(specificationId, nameof(specificationId));
            Guard.IsNullOrWhiteSpace(datasetVersionId, nameof(datasetVersionId));

            ApiResponse <SelectDatasourceModel> sourcesResponse =
                await _datasetApiClient.GetDataSourcesByRelationshipId(relationshipId);

            if (sourcesResponse.StatusCode != HttpStatusCode.OK || sourcesResponse.Content == null)
            {
                _logger.Error($"Failed to fetch data sources with status code {sourcesResponse.StatusCode.ToString()}");
                return(NotFound());
            }

            bool isAuthorizedToMap = await _authorizationHelper.DoesUserHavePermission(User,
                                                                                       sourcesResponse.Content.SpecificationId, SpecificationActionTypes.CanMapDatasets);

            if (!isAuthorizedToMap)
            {
                return(new ForbidResult());
            }

            if (string.IsNullOrWhiteSpace(datasetVersionId))
            {
                SelectDataSourceViewModel viewModel = PopulateViewModel(sourcesResponse.Content);
                this.ModelState.AddModelError($"{nameof(SelectDataSourceViewModel)}.{nameof(datasetVersionId)}", "");
                if (viewModel == null)
                {
                    return(new StatusCodeResult(500));
                }
            }

            string[] datasetVersionArray = datasetVersionId.Split("_");
            if (datasetVersionArray.Length != 2)
            {
                _logger.Error($"Dataset version: {datasetVersionId} is invalid");
                return(new StatusCodeResult(500));
            }

            AssignDatasourceModel assignDatasetVersion = new AssignDatasourceModel
            {
                RelationshipId = relationshipId,
                DatasetId      = datasetVersionArray[0],
                Version        = Convert.ToInt32(datasetVersionArray[1])
            };

            HttpStatusCode httpStatusCode =
                await _datasetApiClient.AssignDatasourceVersionToRelationship(assignDatasetVersion);

            if (httpStatusCode == HttpStatusCode.OK || httpStatusCode == HttpStatusCode.NoContent)
            {
                return(new OkObjectResult(true));
            }

            _logger.Error($"Failed to assign dataset version with status code: {httpStatusCode.ToString()}");

            return(new StatusCodeResult(500));
        }
        public Task <IActionResult> AssignDatasourceVersionToRelationship([FromBody] AssignDatasourceModel assignDatasourceModel)
        {
            Reference user          = ControllerContext.HttpContext.Request.GetUserOrDefault();
            string    correlationId = ControllerContext.HttpContext.Request.GetCorrelationId();

            return(_definitionSpecificationRelationshipService.AssignDatasourceVersionToRelationship(assignDatasourceModel, user, correlationId));
        }
        public async Task <ApiResponse <JobCreationResponse> > AssignDatasourceVersionToRelationship(AssignDatasourceModel assignDatasourceModel)
        {
            Guard.ArgumentNotNull(assignDatasourceModel, nameof(assignDatasourceModel));

            return(await PostAsync <JobCreationResponse, AssignDatasourceModel>(
                       DataSetsUriFor("assign-datasource-to-relationship"),
                       assignDatasourceModel));
        }
예제 #4
0
        public async Task <HttpStatusCode> AssignDatasourceVersionToRelationship(AssignDatasourceModel assignDatasourceModel)
        {
            Guard.ArgumentNotNull(assignDatasourceModel, nameof(assignDatasourceModel));

            return(await PostAsync(DataSetsUriFor("assign-datasource-to-relationship"), assignDatasourceModel));
        }
        public async Task <IActionResult> AssignDatasourceVersionToRelationship(HttpRequest request)
        {
            string json = await request.GetRawBodyStringAsync();

            AssignDatasourceModel model = JsonConvert.DeserializeObject <AssignDatasourceModel>(json);

            if (model == null)
            {
                _logger.Error("Null AssignDatasourceModel was provided to AssignDatasourceVersionToRelationship");
                return(new BadRequestObjectResult("Null AssignDatasourceModel was provided"));
            }

            Dataset dataset = await _datasetRepository.GetDatasetByDatasetId(model.DatasetId);

            if (dataset == null)
            {
                _logger.Error($"Dataset not found for dataset id: {model.DatasetId}");
                return(new StatusCodeResult(412));
            }

            DefinitionSpecificationRelationship relationship = await _datasetRepository.GetDefinitionSpecificationRelationshipById(model.RelationshipId);

            if (relationship == null)
            {
                _logger.Error($"Relationship not found for relationship id: {model.RelationshipId}");
                return(new StatusCodeResult(412));
            }

            relationship.DatasetVersion = new DatasetRelationshipVersion
            {
                Id      = model.DatasetId,
                Version = model.Version
            };

            HttpStatusCode statusCode = await _datasetRepository.UpdateDefinitionSpecificationRelationship(relationship);

            if (!statusCode.IsSuccess())
            {
                _logger.Error($"Failed to assign data source to relationship : {model.RelationshipId} with status code {statusCode.ToString()}");

                return(new StatusCodeResult((int)statusCode));
            }

            Reference user = request.GetUser();

            Trigger trigger = new Trigger
            {
                EntityId   = dataset.Id,
                EntityType = nameof(Dataset),
                Message    = $"Mapping dataset: '{dataset.Id}'"
            };

            string correlationId = request.GetCorrelationId();

            JobCreateModel job = new JobCreateModel
            {
                InvokerUserDisplayName = user.Name,
                InvokerUserId          = user.Id,
                JobDefinitionId        = JobConstants.DefinitionNames.MapDatasetJob,
                MessageBody            = JsonConvert.SerializeObject(dataset),
                Properties             = new Dictionary <string, string>
                {
                    { "specification-id", relationship.Specification.Id },
                    { "relationship-id", relationship.Id }
                },
                SpecificationId = relationship.Specification.Id,
                Trigger         = trigger,
                CorrelationId   = correlationId
            };

            await _jobsApiClientPolicy.ExecuteAsync(() => _jobsApiClient.CreateJob(job));

            return(new NoContentResult());
        }
예제 #6
0
        public async Task <IActionResult> AssignDatasourceVersionToRelationship(AssignDatasourceModel model, Reference user, string correlationId)
        {
            if (model == null)
            {
                _logger.Error("Null AssignDatasourceModel was provided to AssignDatasourceVersionToRelationship");
                return(new BadRequestObjectResult("Null AssignDatasourceModel was provided"));
            }

            Dataset dataset = await _datasetRepository.GetDatasetByDatasetId(model.DatasetId);

            if (dataset == null)
            {
                _logger.Error($"Dataset not found for dataset id: {model.DatasetId}");
                return(new StatusCodeResult(412));
            }

            DefinitionSpecificationRelationship relationship = await _datasetRepository.GetDefinitionSpecificationRelationshipById(model.RelationshipId);

            if (relationship == null)
            {
                _logger.Error($"Relationship not found for relationship id: {model.RelationshipId}");
                return(new StatusCodeResult(412));
            }

            relationship.Author         = user;
            relationship.LastUpdated    = _dateTimeProvider.UtcNow;
            relationship.DatasetVersion = new DatasetRelationshipVersion
            {
                Id      = model.DatasetId,
                Version = model.Version
            };

            HttpStatusCode statusCode = await _datasetRepository.UpdateDefinitionSpecificationRelationship(relationship);

            if (!statusCode.IsSuccess())
            {
                _logger.Error($"Failed to assign data source to relationship : {model.RelationshipId} with status code {statusCode.ToString()}");

                return(new StatusCodeResult((int)statusCode));
            }

            IEnumerable <CalculationResponseModel> allCalculations = await _calcsRepository.GetCurrentCalculationsBySpecificationId(relationship.Specification.Id);

            bool generateCalculationAggregations = !allCalculations.IsNullOrEmpty() &&
                                                   SourceCodeHelpers.HasCalculationAggregateFunctionParameters(allCalculations.Select(m => m.SourceCode));

            Trigger trigger = new Trigger
            {
                EntityId   = dataset.Id,
                EntityType = nameof(Dataset),
                Message    = $"Mapping dataset: '{dataset.Id}'"
            };

            JobCreateModel job = new JobCreateModel
            {
                InvokerUserDisplayName = user?.Name,
                InvokerUserId          = user?.Id,
                JobDefinitionId        = JobConstants.DefinitionNames.MapDatasetJob,
                MessageBody            = JsonConvert.SerializeObject(dataset),
                Properties             = new Dictionary <string, string>
                {
                    { "specification-id", relationship.Specification.Id },
                    { "relationship-id", relationship.Id },
                    { "user-id", user?.Id },
                    { "user-name", user?.Name },
                },
                SpecificationId = relationship.Specification.Id,
                Trigger         = trigger,
                CorrelationId   = correlationId
            };

            Job parentJob = null;

            if (relationship.IsSetAsProviderData)
            {
                string parentJobDefinition = generateCalculationAggregations ?
                                             JobConstants.DefinitionNames.MapScopedDatasetJobWithAggregation :
                                             JobConstants.DefinitionNames.MapScopedDatasetJob;

                parentJob = await _jobManagement.QueueJob(new JobCreateModel
                {
                    InvokerUserDisplayName = user?.Name,
                    InvokerUserId          = user?.Id,
                    JobDefinitionId        = parentJobDefinition,
                    Properties             = new Dictionary <string, string>
                    {
                        { "specification-id", relationship.Specification.Id },
                        { "provider-cache-key", $"{CacheKeys.ScopedProviderSummariesPrefix}{relationship.Specification.Id}" },
                        { "specification-summary-cache-key", $"{CacheKeys.SpecificationSummaryById}{relationship.Specification.Id}" }
                    },
                    SpecificationId = relationship.Specification.Id,
                    Trigger         = trigger,
                    CorrelationId   = correlationId
                });
            }

            if (parentJob != null)
            {
                job.ParentJobId = parentJob.Id;
                job.Properties.Add("parentJobId", parentJob.Id);
            }

            await _jobManagement.QueueJob(job);

            return(new NoContentResult());
        }