public string CreateQuery(string database, TimeSeriesRequest request) { string rangeString = CreateRangeString(request.Range); string filterString = CreateFilterString(request.MeasurementName, request.Tags); return($"from(bucket: \"{database}\") {rangeString} {filterString}"); }
public async Task GetTimeSeries_Success() { TimeSeriesRequest request = new TimeSeriesRequest() { MeasurementName = "temperature", ValueType = TimeSeriesValueType.Float, Range = TimeSeriesRange.OneDay, Tags = new Dictionary <string, string>() { ["location"] = "Room1" } }; 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) }; TimeSeriesResponse response = new TimeSeriesResponse() { Status = TimeSeriesResponseStatus.Success, DataPoints = dataPoints }; IInfluxDBProvider influxProvider = Substitute.For <IInfluxDBProvider>(); influxProvider.GetTimeSeriesAsync(request).Returns(Task.FromResult(response)); TimeSeriesController controller = new TimeSeriesController(influxProvider); var result = await controller.GetTimeSeries(request); var okResult = Assert.IsType <OkObjectResult>(result.Result); var resultList = Assert.IsType <List <DataPoint> >(okResult.Value); Assert.Equal(dataPoints, resultList); }
public async Task GetTimeSeries_BadRequest() { TimeSeriesRequest request = new TimeSeriesRequest() { MeasurementName = "temperature", ValueType = TimeSeriesValueType.Float, Range = TimeSeriesRange.OneDay, Tags = new Dictionary <string, string>() { ["location"] = "Room1" } }; TimeSeriesResponse response = new TimeSeriesResponse() { Status = TimeSeriesResponseStatus.BadRequest, DataPoints = null }; IInfluxDBProvider influxProvider = Substitute.For <IInfluxDBProvider>(); influxProvider.GetTimeSeriesAsync(request).Returns(Task.FromResult(response)); TimeSeriesController controller = new TimeSeriesController(influxProvider); var result = await controller.GetTimeSeries(request); Assert.IsType <BadRequestResult>(result.Result); }
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); }
public void CreateQuery() { string database = "TestDB"; TimeSeriesRequest request = new TimeSeriesRequest() { MeasurementName = "temperature", ValueType = TimeSeriesValueType.Float, Range = TimeSeriesRange.OneDay, Tags = new Dictionary <string, string>() { ["location"] = "Room1" } }; FluxHelper fluxHelper = new FluxHelper(); var result = fluxHelper.CreateQuery(database, request); Assert.Equal("from(bucket: \"TestDB\") |> range(start: -1d) |> filter(fn: (r) => r._measurement == \"temperature\" and r.location == \"Room1\")", result); }
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); }
public async Task <ActionResult <List <DataPoint> > > GetTimeSeries([FromBody] TimeSeriesRequest request) { if (request == null) { return(BadRequest()); } var timeSeriesResponse = await influxDbProvider.GetTimeSeriesAsync(request); var status = timeSeriesResponse.Status; if (status == TimeSeriesResponseStatus.BadRequest) { return(BadRequest()); } else if (status == TimeSeriesResponseStatus.InternalError) { return(NotFound()); } return(Ok(timeSeriesResponse.DataPoints)); }
public async Task <IEnumerable <TimeSeriesResponse> > getEventDataAsync([FromBody] TimeSeriesRequest TimeSeriesRequestData) { IEnumerable <TimeSeriesResponse> responseContent = await TimeSeriesInsightsLogic.getEventData(TimeSeriesRequestData.from, TimeSeriesRequestData.to, TimeSeriesRequestData.TagName); return(responseContent); }