public async Task ResolvesLowHealth(bool shouldResolve)
        {
            // Arrange
            var input = new List <Discrepancy> {
                new Discrepancy {
                    DateFirstOffense = DateTime.UtcNow,
                    Type             = DiscrepancyType.LowHealth,
                    MetricSource     = "the-source-1",
                    MetricType       = Metrics.Health,
                }
            };

            var metric = new Metric
            {
                Type   = Metrics.Health.AsInt(),
                Source = "the-source-1"
            };

            var context = _serviceProvider.GetRequiredService <IDataContext>();
            await context.Metrics.AddAsync(metric);

            await context.HealthReports.AddRangeAsync(
                DiscrepancyServiceTest.GenerateHealthReport(true, metric, DateTime.UtcNow.AddMinutes(-1)),
                DiscrepancyServiceTest.GenerateHealthReport(false, metric, DateTime.UtcNow.AddMinutes(1)),
                DiscrepancyServiceTest.GenerateHealthReport(false, metric, DateTime.UtcNow.AddMinutes(2)),
                DiscrepancyServiceTest.GenerateHealthReport(shouldResolve, metric, DateTime.UtcNow.AddMinutes(3))
                );

            await context.SaveChangesAsync();

            var config = new Mock <IConfiguration>();

            config
            .SetupGet(conf => conf["ServiceManager:DiscrepancyService:Health:Threshold"])
            .Returns(99.ToString());

            var discrepancyService = new DiscrepancyService(
                new Mock <ILogger <DiscrepancyService> >().Object,
                context,
                new Mock <INotificationService>().Object,
                config.Object
                );

            // Act
            var actual = await discrepancyService.FindResolvedDiscrepanciesAsync(input);

            // Assert
            if (shouldResolve)
            {
                Assert.NotEmpty(actual);
                Assert.Equal(input, actual);
            }
            else
            {
                Assert.Empty(actual);
            }
        }
Esempio n. 2
0
        public async Task LabelCritical(Metrics type)
        {
            // Arrange
            var context = _serviceProvider.GetRequiredService <IDataContext>();

            var metric = (await context.Metrics.AddAsync(new Metric
            {
                Type = type.AsInt(),
                Source = "smth.com"
            })).Entity;

            await context.AutoLabels.AddRangeAsync(new List <AutoLabel> {
                new AutoLabel {
                    Id = AutoLabels.Normal.AsInt()
                },
                new AutoLabel {
                    Id = AutoLabels.Critical.AsInt()
                },
                new AutoLabel {
                    Id = AutoLabels.Warning.AsInt()
                },
            });

            switch (type)
            {
            case Metrics.CpuLoad:
                await context.NumericDataPoints.AddRangeAsync(
                    new NumericDataPoint { Value = 90, Metric = metric },
                    new NumericDataPoint { Value = 92, Metric = metric },
                    new NumericDataPoint { Value = 88, Metric = metric },
                    new NumericDataPoint { Value = 89, Metric = metric },
                    new NumericDataPoint { Value = 100, Metric = metric }
                    );

                break;

            case Metrics.Health:
                await context.HealthReports.AddRangeAsync(
                    DiscrepancyServiceTest.GenerateHealthReport(true, metric),
                    DiscrepancyServiceTest.GenerateHealthReport(false, metric),
                    DiscrepancyServiceTest.GenerateHealthReport(false, metric),
                    DiscrepancyServiceTest.GenerateHealthReport(false, metric),
                    DiscrepancyServiceTest.GenerateHealthReport(false, metric)
                    );

                break;

            case Metrics.Ping:
                await context.PingSettings.AddAsync(
                    new PingSetting
                {
                    ServerUrl   = "https://smth.com",
                    MaxFailures = 5
                }
                    );

                await context.PingDataPoints.AddRangeAsync(
                    new PingDataPoint { Success = true, Metric = metric },
                    new PingDataPoint { Success = true, Metric = metric },
                    new PingDataPoint { Success = false, Metric = metric },
                    new PingDataPoint { Success = true, Metric = metric },
                    new PingDataPoint { Success = false, Metric = metric },
                    new PingDataPoint { Success = false, Metric = metric },
                    new PingDataPoint { Success = true, Metric = metric },
                    new PingDataPoint { Success = false, Metric = metric },
                    new PingDataPoint { Success = false, Metric = metric },
                    new PingDataPoint { Success = true, Metric = metric }
                    );

                break;
            }

            await context.SaveChangesAsync();

            var expectedLabel = AutoLabels.Critical;

            // Act
            var cachedMetric = await new CacheService(context, new Mock <ILogger <CacheService> >().Object)
                               .CacheMetricAsync(metric);

            // Assert
            Assert.Equal(expectedLabel.AsInt(), cachedMetric.AutoLabel.Id);
        }
Esempio n. 3
0
        public async Task LabelNormal(Metrics type)
        {
            // Arrange
            var context = _serviceProvider.GetRequiredService <IDataContext>();

            var metric = (await context.Metrics.AddAsync(new Metric
            {
                Type = type.AsInt(),
                Source = "smth.com"
            })).Entity;

            await context.AutoLabels.AddRangeAsync(new List <AutoLabel> {
                new AutoLabel {
                    Id = AutoLabels.Normal.AsInt()
                },
                new AutoLabel {
                    Id = AutoLabels.Critical.AsInt()
                },
                new AutoLabel {
                    Id = AutoLabels.Warning.AsInt()
                },
            });

            switch (type)
            {
            case Metrics.CpuLoad:
                await context.NumericDataPoints.AddRangeAsync(
                    new NumericDataPoint { Value = 23, Metric = metric },
                    new NumericDataPoint { Value = 65, Metric = metric },
                    new NumericDataPoint { Value = 43, Metric = metric },
                    new NumericDataPoint { Value = 45, Metric = metric },
                    new NumericDataPoint { Value = 34, Metric = metric }
                    );

                break;

            case Metrics.Health:
                await context.HealthReports.AddRangeAsync(
                    DiscrepancyServiceTest.GenerateHealthReport(true, metric),
                    DiscrepancyServiceTest.GenerateHealthReport(true, metric),
                    DiscrepancyServiceTest.GenerateHealthReport(true, metric),
                    DiscrepancyServiceTest.GenerateHealthReport(true, metric),
                    DiscrepancyServiceTest.GenerateHealthReport(true, metric)
                    );

                break;

            case Metrics.Ping:
                await context.PingSettings.AddAsync(
                    new PingSetting
                {
                    ServerUrl       = "https://smth.com",
                    MaxResponseTime = new TimeSpan(0, 0, 0, 0, 1000),
                    MaxFailures     = 5
                }
                    );

                await context.PingDataPoints.AddRangeAsync(
                    new PingDataPoint
                {
                    Success      = true,
                    ResponseTime = new TimeSpan(0, 0, 0, 0, 432),
                    Metric       = metric
                },
                    new PingDataPoint
                {
                    Success      = true,
                    ResponseTime = new TimeSpan(0, 0, 0, 0, 750),
                    Metric       = metric
                },
                    new PingDataPoint
                {
                    Success      = false,
                    ResponseTime = new TimeSpan(0, 0, 0, 0, 70),
                    Metric       = metric
                },
                    new PingDataPoint
                {
                    Success      = true,
                    ResponseTime = new TimeSpan(0, 0, 0, 0, 876),
                    Metric       = metric
                },
                    new PingDataPoint
                {
                    Success      = false,
                    ResponseTime = new TimeSpan(0, 0, 0, 0, 345),
                    Metric       = metric
                },
                    new PingDataPoint
                {
                    Success      = true,
                    ResponseTime = new TimeSpan(0, 0, 0, 0, 750),
                    Metric       = metric
                },
                    new PingDataPoint
                {
                    Success      = true,
                    ResponseTime = new TimeSpan(0, 0, 0, 0, 850),
                    Metric       = metric
                },
                    new PingDataPoint
                {
                    Success      = true,
                    ResponseTime = new TimeSpan(0, 0, 0, 0, 50),
                    Metric       = metric
                },
                    new PingDataPoint
                {
                    Success      = true,
                    ResponseTime = new TimeSpan(0, 0, 0, 0, 740),
                    Metric       = metric
                },
                    new PingDataPoint
                {
                    Success      = true,
                    ResponseTime = new TimeSpan(0, 0, 0, 0, 234),
                    Metric       = metric,
                    Timestamp    = DateTime.UtcNow.AddSeconds(5),                                  // make sure it is the most recent data point
                }
                    );

                break;
            }

            await context.SaveChangesAsync();

            var expectedLabel = AutoLabels.Normal;

            // Act
            var cachedMetric = await new CacheService(context, new Mock <ILogger <CacheService> >().Object)
                               .CacheMetricAsync(metric);

            // Assert
            Assert.Equal(expectedLabel.AsInt(), cachedMetric.AutoLabel.Id);
        }