public async Task GivenAPreviousAlert_WhenGettingLastFiredAlert_ThenReturnDate()
        {
            var options          = BuildOptions();
            var influxManagement = new InfluxManagement(options);
            var authToken        = await influxManagement.Setup();

            options = BuildOptions(authToken);

            var alertsStore = BuildAlertsStore(options);
            var sut         = new LastFiredAlertQuery(BuildFluxQueryBuilder(options));

            var date     = DateTimeOffset.UtcNow.AddMinutes(-1);
            var location = "testlastalert" + DateTimeOffset.Now.ToUnixTimeMilliseconds().ToString();

            await alertsStore.WriteMeasure(new Db.Entities.AlertMeasure
            {
                Location = location,
                Value    = 1,
                Time     = date
            });

            var lastFiredAlert = await sut.CheckLastFiredAlert();

            Assert.NotNull(lastFiredAlert);
            Assert.Equal(location, lastFiredAlert.Location);
        }
        public async Task GivenNewTempMeasure_WhenWritten_ThenCanBeRetrieved()
        {
            var options          = BuildOptions();
            var influxManagement = new InfluxManagement(options);
            var authToken        = await influxManagement.Setup();

            options = BuildOptions(authToken);
            var fluxQueryBuilder = BuildFluxQueryBuilder(options);

            var envSensorsStore = BuildEnvSensorsStore(options);

            DateTime     utcNow       = DateTime.UtcNow;
            var          expectedTemp = 25D + (1D / (double)utcNow.Millisecond);
            const string location     = "kitchen";

            await envSensorsStore.WriteMeasure(new Db.Entities.Temperature
            {
                Location = location,
                Value    = expectedTemp,
                Time     = utcNow
            });

            var tempMeasures = await fluxQueryBuilder
                               .From()
                               .Range(utcNow.AddSeconds(-2), utcNow.AddSeconds(2))
                               .FilterMeasurement("temperature")
                               .AggregateWindow("mean", "1h")
                               .Query();

            Assert.NotEmpty(tempMeasures);

            var actualTemp = tempMeasures
                             .SingleOrDefault(t => t.Records.Any(c => c.GetValueByKey("location") as string == location))
                             ?.Records
                             .Select(fluxRecord => fluxRecord.GetValue())
                             .SingleOrDefault();

            Assert.Equal(expectedTemp, actualTemp);
        }
Esempio n. 3
0
        public async Task GivenNewAlert_WhenWritten_ThenCanBeRetrieved()
        {
            var options          = BuildOptions();
            var influxManagement = new InfluxManagement(options);
            var authToken        = await influxManagement.Setup();

            options = BuildOptions(authToken);
            var fluxQueryBuilder = BuildFluxQueryBuilder(options);

            var alertsStore = BuildAlertsStore(options);

            var          utcNow   = DateTimeOffset.UtcNow;
            const string location = "AlertsStoreTests";

            await alertsStore.WriteMeasure(new Db.Entities.AlertMeasure
            {
                Location = location,
                Value    = 1,
                Time     = utcNow
            });

            var tempMeasures = await fluxQueryBuilder
                               .From()
                               .Range(utcNow.AddSeconds(-2), utcNow.AddSeconds(2))
                               .FilterMeasurement("alert")
                               .Aggregate("max()")
                               .Query();

            Assert.NotEmpty(tempMeasures);

            var actualSumAlerts = tempMeasures
                                  .SingleOrDefault(t => t.Records.Any(c => c.GetValueByKey("location") as string == location))
                                  ?.Records
                                  .Select(fluxRecord => fluxRecord.GetValue())
                                  .SingleOrDefault();

            Assert.Equal(1d, actualSumAlerts);
        }