Beispiel #1
0
        // Connection related methods
        private void TearDownConnection()
        {
            // TODO: [TESTS] (RabbitConnection.TearDownConnection) Add tests
            if (_channel == null && _connection == null)
            {
                return;
            }

            _logger.Trace("Tearing down RabbitMQ connection");

            try
            {
                _channel?.Close();
                _connection?.Close();

                _channel?.Dispose();
                _connection?.Dispose();

                _channel    = null;
                _connection = null;
            }
            catch (Exception ex)
            {
                _logger.LogUnexpectedException(ex);
            }
        }
        public async Task <List <ProductDto> > GetAll(int userId, int clientId)
        {
            // TODO: [TESTS] (ProductService.GetAll) Add tests
            var builder = new ServiceMetricBuilder(nameof(ProductService), nameof(GetAll))
                          .WithCategory(MetricCategory.Product, MetricSubCategory.GetAll)
                          .WithCustomInt1(userId)
                          .WithCustomInt2(clientId);

            try
            {
                using (builder.WithTiming())
                {
                    List <ProductEntity> dbEntries;
                    using (builder.WithCustomTiming1())
                    {
                        builder.IncrementQueryCount();
                        dbEntries = await _productRepo.GetAll(clientId);

                        builder.WithResultsCount(dbEntries?.Count ?? 0);
                    }

                    if (dbEntries == null || dbEntries.Count == 0)
                    {
                        // TODO: [HANDLE] (ProductService.GetAll) Handle this
                        return(new List <ProductDto>());
                    }

                    if (dbEntries.First().UserId != userId)
                    {
                        // TODO: [HANDLE] (ProductService.GetAll) Handle this
                        return(new List <ProductDto>());
                    }

                    return(dbEntries
                           .AsQueryable()
                           .Select(ProductDto.Projection)
                           .ToList());
                }
            }
            catch (Exception ex)
            {
                _logger.LogUnexpectedException(ex);
                builder.WithException(ex);
                return(new List <ProductDto>());
            }
            finally
            {
                await _metrics.SubmitPointAsync(builder.Build());
            }
        }
Beispiel #3
0
        // Interface methods
        public async Task <UserDto> GetFromToken(string token)
        {
            // TODO: [TESTS] (UserService.GetFromToken) Add tests
            var builder = new ServiceMetricBuilder(nameof(UserService), nameof(GetFromToken))
                          .WithCategory(MetricCategory.User, MetricSubCategory.Extract);

            try
            {
                using (builder.WithTiming())
                {
                    // Try extract the current userId
                    int userId;
                    using (builder.WithCustomTiming1())
                    {
                        userId = ExtractUserId(token);
                        if (userId == 0)
                        {
                            return(null);
                        }
                    }

                    UserEntity userEntity;
                    using (builder.WithCustomTiming2())
                    {
                        builder.IncrementQueryCount();
                        userEntity = await _userRepo.GetUserById(userId);

                        builder.CountResult(userEntity);
                    }

                    return(userEntity == null ? null : UserDto.Projection.Compile()(userEntity));
                }
            }
            catch (Exception ex)
            {
                _logger.LogUnexpectedException(ex);
                builder.WithException(ex);
                return(null);
            }
            finally
            {
                await _metrics.SubmitPointAsync(builder.Build());
            }
        }
Beispiel #4
0
    public void SaveConfigState()
    {
        if (!ShiftConfigFiles())
        {
            _logger.LogError("Unable to manage configuration files, quitting!");
            _environment.Exit(10);
        }

        try
        {
            var configJson = _jsonHelper.SerializeObject(_dnsEntriesConfig, true);
            _file.WriteAllText(CoreConfig.ConfigFile, configJson);
            _logger.LogDebug("Updated configuration file");
        }
        catch (Exception ex)
        {
            _logger.LogUnexpectedException(ex);
            _environment.Exit(11);
        }
    }
        public async Task <List <ProjectDto> > GetAllForProduct(int userId, int productId)
        {
            // TODO: [TESTS] (ProjectService.GetAllForProduct) Add tests
            // TODO: [VALIDATION] (ProjectService.GetAllForProduct) Ensure that the current user can see these
            var builder = new ServiceMetricBuilder(nameof(ProjectService), nameof(GetAllForProduct))
                          .WithCategory(MetricCategory.Project, MetricSubCategory.GetAll)
                          .WithCustomInt1(userId)
                          .WithCustomInt2(productId)
                          .WithCustomInt3(0);

            try
            {
                using (builder.WithTiming())
                {
                    List <ProjectEntity> dbEntries;
                    using (builder.WithCustomTiming1())
                    {
                        builder.IncrementQueryCount();
                        dbEntries = await _projectRepo.GetAllForProduct(productId);

                        builder.WithResultsCount(dbEntries.Count);
                    }

                    return(dbEntries
                           .AsQueryable()
                           .Select(ProjectDto.Projection)
                           .ToList());
                }
            }
            catch (Exception ex)
            {
                _logger.LogUnexpectedException(ex);
                builder.WithException(ex);
                return(new List <ProjectDto>());
            }
            finally
            {
                await _metrics.SubmitPointAsync(builder.Build());
            }
        }
Beispiel #6
0
        // Public methods
        public string Encrypt(string plainText)
        {
            // TODO: [TESTS] (EncryptionService.Encrypt) Add tests
            // TODO: [METRICS] (EncryptionService.Encrypt) Add metrics
            if (!_config.Enabled || string.IsNullOrWhiteSpace(plainText))
            {
                return(null);
            }

            try
            {
                using var mStream = new MemoryStream();
                var cStream = _utils.CreateCryptoStream(mStream,
                                                        _utils.CreateEncryptor(_keyBytes, _ivBytes),
                                                        CryptoStreamMode.Write
                                                        );

                var inputBytes = _utils.GetBytes(plainText);
                cStream.Write(inputBytes, 0, inputBytes.Length);
                cStream.FlushFinalBlock();
                var returnBytes = mStream.ToArray();

                cStream.Close();
                mStream.Close();

                return(_utils.ToBase64String(returnBytes));
            }
            catch (Exception ex)
            {
                if (_config.LogDecryptInput)
                {
                    _logger.LogUnexpectedException(ex);
                }

                return(null);
            }
        }
        // Interface methods
        public async Task <RawOptions> GenerateOptions(string category, int userId)
        {
            // TODO: [TESTS] (OptionsService.GetRawOptionsForCategory) Add tests
            var builder = new ServiceMetricBuilder(nameof(OptionsService), nameof(GenerateOptions))
                          .WithCategory(MetricCategory.Option, MetricSubCategory.Generate)
                          .WithCustomInt1(userId)
                          .WithCustomTag1(category);

            var generated = new RawOptions(category);

            try
            {
                using (builder.WithTiming())
                {
                    List <OptionEntity> dbOptions;
                    using (builder.WithCustomTiming1())
                    {
                        builder.IncrementQueryCount();
                        dbOptions = await _optionRepo.GetRawOptionsForCategory(category, userId);

                        builder.WithResultsCount(dbOptions.Count);
                    }

                    foreach (var dbOption in dbOptions)
                    {
                        generated.AddOption(dbOption);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogUnexpectedException(ex);
                builder.WithException(ex);
            }
            finally
            {
                await _metrics.SubmitPointAsync(builder.Build());
            }

            return(generated);
        }
        private async Task LogApiResponseMetric(HttpContext httpContext)
        {
            // TODO: [TESTS] (ApiMetricsMiddleware.LogApiResponseMetric) Add tests
            try
            {
                // Ensure we have something to work with
                var metricContext = httpContext.GetApiRequestMetricContext();
                if (metricContext == null)
                {
                    return;
                }
                metricContext.CompleteMiddlewareRequest(httpContext, _dateTime.UtcNow);

                await _metrics.SubmitPointAsync(
                    new ApiCallMetricBuilder(metricContext).Build()
                    );
            }
            catch (Exception ex)
            {
                _logger.LogUnexpectedException(ex);
            }
        }