Ejemplo n.º 1
0
        public async Task <Responses.Indicator> AddIndicator(AddIndicator request)
        {
            // Get indicator
            var indicator = await _mainDbContext.Indicators
                            .Include(x => x.Dependencies)
                            .FirstOrDefaultAsync(x => x.IndicatorId == request.IndicatorId);

            // Throw ConflictException if it exists
            if (indicator != null)
            {
                throw new ConflictException(IndicatorMessage.IndicatorWithSameIdAlreadyExists);
            }

            // Check uniqueness
            indicator = await _mainDbContext.Indicators
                        .Include(x => x.Dependencies)
                        .FirstOrDefaultAsync(IndicatorExpression.IndicatorUnique(request.Name));

            // Throw ConflictException if it exists
            if (indicator != null)
            {
                throw new ConflictException(IndicatorMessage.IndicatorWithSameNameAlreadyExists);
            }

            // Get dependencies
            var dependencies = await GetDependencies(request.Dependencies);

            // Build dependency level
            var dependencyLevel = IndicatorBuilder.BuildDependencyLevel(dependencies);

            // Build new indicator dependencies
            var indicatorDependencies = IndicatorDependencyBuilder.BuildIndicatorDependencies(request.IndicatorId, dependencies);

            // Create
            indicator = new Indicator(
                request.IndicatorId,
                request.IndicatorType,
                request.UserId,
                request.Name,
                request.Description,
                request.Formula,
                indicatorDependencies,
                dependencyLevel,
                DateTime.Now);

            // Add
            _mainDbContext.Indicators.Add(indicator);

            // Save
            await _mainDbContext.SaveChangesAsync();

            // Log into Splunk
            _logger.LogSplunkInformation(request);

            // Response
            var response = _mapper.Map <Responses.Indicator>(indicator);

            // Return
            return(response);
        }
Ejemplo n.º 2
0
        public async Task <Responses.Indicator> UpdateIndicator(UpdateIndicator request)
        {
            // Get indicator
            var indicator = await _mainDbContext.Indicators
                            .Include(x => x.Dependencies)
                            .FirstOrDefaultAsync(x => x.IndicatorId == request.IndicatorId);

            // Indicator not found
            if (indicator == null)
            {
                throw new NotFoundException(IndicatorMessage.IndicatorNotFound);
            }

            // Get dependencies
            var newDependencies = await GetIndicators(request.Dependencies);

            // Build new indicator dependencies
            var newIndicatorDependencies = IndicatorDependencyBuilder.BuildIndicatorDependencies(indicator.IndicatorId, newDependencies);

            // Get current indicator dependencies
            var currentIndicatorDependencies = indicator.Dependencies;

            // Update dependencies
            _mainDbContext.UpdateCollection(currentIndicatorDependencies, newIndicatorDependencies);

            // Update indicator
            indicator.Update(request.Name, request.Description, request.Formula);

            // Update
            _mainDbContext.Indicators.Update(indicator);

            // Save
            await _mainDbContext.SaveChangesAsync();

            // Get indicator
            indicator = await _mainDbContext.Indicators
                        .Include(x => x.Dependencies)
                        .ThenInclude(x => x.Dependency)
                        .FirstOrDefaultAsync(x => x.IndicatorId == indicator.IndicatorId);

            // Response
            var response = _mapper.Map <Responses.Indicator>(indicator);

            // Log
            _logger.LogInformation("{@Event}, {@UserId}, {@Request}, {@Response}", "IndicatorUpdated", request.UserId, request, response);

            // Return
            return(response);
        }
Ejemplo n.º 3
0
        public async Task Run()
        {
            try
            {
                // Start watch
                var stopwatch = new Stopwatch();
                stopwatch.Start();

                // Get all dependencies
                var indicatorDependencies = await _indicatorDependencyRepository.GetAll();

                // Build
                IndicatorDependencyBuilder.BuildLevel(indicatorDependencies);

                // Update
                _indicatorDependencyRepository.UpdateRange(indicatorDependencies);

                // Save
                await _mainDbContext.SaveChangesAsync();

                // Stop watch
                stopwatch.Stop();

                // Log into Splunk
                _logger.LogSplunkJob(new
                {
                    MaxLevel      = indicatorDependencies.Select(x => x.Level).Max(),
                    ExecutionTime = stopwatch.Elapsed.TotalSeconds
                });

                // Return
                await Task.CompletedTask;
            }
            catch (Exception ex)
            {
                // Log into Splunk
                _logger.LogSplunkJob(new
                {
                    JobFailed = ex.Message
                });

                // Log error into Splunk
                _logger.LogSplunkError(ex);
            }
        }
Ejemplo n.º 4
0
        public async Task <Responses.Indicator> UpdateIndicator(UpdateIndicator request)
        {
            // Get indicator
            var indicator = await _indicatorRepository.GetSingle(request.IndicatorId);

            // Throw NotFound if it does not exist
            if (indicator == null)
            {
                throw new NotFoundException(IndicatorMessage.IndicatorNotFound);
            }

            // Get dependencies
            var newDependencies = await GetDependencies(request.Dependencies);

            // Build new indicator dependencies
            var newIndicatorDependencies = IndicatorDependencyBuilder.BuildIndicatorDependencies(indicator.IndicatorId, newDependencies);

            // Get current indicator dependencies
            var currentIndicatorDependencies = await _indicatorDependencyRepository.GetAll(IndicatorDependencyExpression.IndicatorDependencyFilter(indicator.IndicatorId));

            // Update dependencies
            _indicatorDependencyRepository.UpdateCollection(currentIndicatorDependencies, newIndicatorDependencies);

            // Update indicator
            indicator.Update(request.Name, request.Description, request.Formula);

            // Update
            _indicatorRepository.Update(indicator);

            // Set dependencies
            indicator.SetDependencies(newIndicatorDependencies);

            // Save
            await _dbContext.SaveChangesAsync();

            // Log into Splunk
            _logger.LogSplunkInformation(request);

            // Response
            var response = _mapper.Map <Responses.Indicator>(indicator);

            // Return
            return(response);
        }
        public void BuildDependencyLevel_1()
        {
            // Arrange
            var dependencyLevels = new List <IndicatorDependency>
            {
                new IndicatorDependency("2", "1", 0),
                new IndicatorDependency("3", "1", 0),
                new IndicatorDependency("3", "2", 0),
                new IndicatorDependency("4", "3", 0),
                new IndicatorDependency("5", "3", 0)
            };

            // Act
            IndicatorDependencyBuilder.BuildLevel(dependencyLevels);

            // Assert
            Assert.AreEqual(true, dependencyLevels[0].Level == 1);
            Assert.AreEqual(true, dependencyLevels[1].Level == 1);
            Assert.AreEqual(true, dependencyLevels[2].Level == 2);
            Assert.AreEqual(true, dependencyLevels[3].Level == 3);
            Assert.AreEqual(true, dependencyLevels[3].Level == 3);
        }
Ejemplo n.º 6
0
        public async Task <Responses.Indicator> AddIndicator(AddIndicator request)
        {
            // Get user
            var user = await _mainDbContext.Users.FindAsync(request.UserId);

            // User not found
            if (user == null)
            {
                throw new NotFoundException(UserMessage.UserNotFound);
            }

            // Get indicator
            var indicator = await _mainDbContext.Indicators
                            .Include(x => x.Dependencies)
                            .FirstOrDefaultAsync(IndicatorExpression.Unique(request.UserId, request.Abbreviation));

            // Throw ConflictException if it exists
            if (indicator != null)
            {
                throw new ConflictException(new Conflict <AddIndicatorConflictReason>(AddIndicatorConflictReason.INDICATOR_ALREADY_EXISTS, IndicatorMessage.IndicatorWithSameIdAlreadyExists));
            }

            // Get dependencies
            var dependencies = await GetIndicators(request.Dependencies);

            // Build dependency level
            var dependencyLevel = IndicatorBuilder.BuildDependencyLevel(dependencies);

            // Build indicator dependencies
            var indicatorDependencies = IndicatorDependencyBuilder.BuildIndicatorDependencies(request.IndicatorId, dependencies);

            // Create
            indicator = new Indicator(
                request.UserId,
                request.Abbreviation,
                request.Name,
                request.Description,
                request.Formula,
                indicatorDependencies,
                dependencyLevel,
                DateTime.UtcNow.StripSeconds());

            // Add
            _mainDbContext.Indicators.Add(indicator);

            // Save
            await _mainDbContext.SaveChangesAsync();

            // Get indicator
            indicator = await _mainDbContext.Indicators
                        .Include(x => x.Dependencies)
                        .ThenInclude(x => x.Dependency)
                        .FirstOrDefaultAsync(x => x.IndicatorId == indicator.IndicatorId);

            // Response
            var response = _mapper.Map <Responses.Indicator>(indicator);

            // Log
            _logger.LogInformation("{@Event}, {@UserId}, {@Request}, {@Response}", "IndicatorAdded", request.UserId, request, response);

            // Return
            return(response);
        }