public async System.Threading.Tasks.Task GetTimeSeriesAsync_Success()
        {
            TimeSeriesRequest request = new TimeSeriesRequest()
            {
                MeasurementName = "temperature",
                ValueType       = TimeSeriesValueType.Float,
                Range           = TimeSeriesRange.OneDay,
                Tags            = new Dictionary <string, string>()
                {
                    ["location"] = "Room1"
                }
            };
            string         httpResponseContent = "testHttpQueryResult";
            HttpStatusCode statusCode          = HttpStatusCode.OK;
            var            messageHandlerMock  = new HttpMessageHandlerMock(httpResponseContent, statusCode);
            var            httpClient          = new HttpClient(messageHandlerMock);
            var            clientFactory       = Substitute.For <IHttpClientFactory>();

            clientFactory.CreateClient().Returns(httpClient);
            var optionsMonitor = Substitute.For <IOptionsMonitor <InfluxDBOptions> >();
            var influxOptions  = new InfluxDBOptions()
            {
                Ip       = "192.168.0.10",
                Port     = 8086,
                Database = "TestDB"
            };

            optionsMonitor.CurrentValue.Returns(influxOptions);
            var dataPoints = new List <DataPoint>()
            {
                new DataPoint <float>(new DateTime(2020, 10, 30), 3.14f),
                new DataPoint <float>(new DateTime(2020, 10, 31), 3.14f)
            };
            IFluxHelper fluxHelper = Substitute.For <IFluxHelper>();
            IInfluxDBQueryResultParser queryResultParser = Substitute.For <IInfluxDBQueryResultParser>();

            queryResultParser.ParseQueryResult(httpResponseContent, TimeSeriesValueType.Float).Returns(dataPoints);
            InfluxDBProvider provider = new InfluxDBProvider(clientFactory, optionsMonitor, fluxHelper, queryResultParser);

            var result = await provider.GetTimeSeriesAsync(request);

            Assert.NotNull(result);
            Assert.Equal(TimeSeriesResponseStatus.Success, result.Status);
            Assert.Equal(dataPoints, result.DataPoints);
        }
Example #2
0
        public async Task <TimeSeriesResponse> GetTimeSeriesAsync(TimeSeriesRequest request)
        {
            TimeSeriesResponse response = new TimeSeriesResponse();

            try
            {
                string url = $"{apiUrl}/query";

                var httpClient = clientFactory.CreateClient();

                var httpRequest = new HttpRequestMessage()
                {
                    RequestUri = new Uri(url),
                    Method     = HttpMethod.Post
                };
                httpRequest.Headers.Add("Accept", "application/csv");

                string flux = fluxHelper.CreateQuery(options.Database, request);

                httpRequest.Content = new StringContent(
                    flux,
                    Encoding.UTF8,
                    "application/vnd.flux"
                    );

                var httpResponseMessage = await httpClient.SendAsync(httpRequest);

                string queryResult = await httpResponseMessage.Content.ReadAsStringAsync();

                response.DataPoints = queryResultParser.ParseQueryResult(queryResult, request.ValueType);
                response.Status     = TimeSeriesResponseStatus.Success;
            }
            catch
            {
                response.DataPoints = null;
                response.Status     = TimeSeriesResponseStatus.InternalError;
            }

            return(response);
        }