[][src]Module influxdb::integrations::serde_integration

Serde Integration for InfluxDB. Provides deserialization of query returns.

When querying multiple series in the same query (e.g. with a regex query), it might be desirable to flat map the resulting series into a single Vec like so. The example assumes, that there are weather readings in multiple series named weather_<city_name> (e.g. weather_berlin, or weather_london). Since we're using a Regex query, we don't actually know which series will be returned. To assign the city name to the series, we can use the series name, InfluxDB provides alongside query results.

use futures::prelude::*;
use influxdb::client::InfluxDbClient;
use influxdb::query::InfluxDbQuery;
use serde::Deserialize;

#[derive(Deserialize)]
struct WeatherWithoutCityName {
    temperature: i32,
}

#[derive(Deserialize)]
struct Weather {
    city_name: String,
    weather: WeatherWithoutCityName,
}

let mut rt = tokio::runtime::current_thread::Runtime::new().unwrap();
let client = InfluxDbClient::new("http://localhost:8086", "test");
let query = InfluxDbQuery::raw_read_query(
    "SELECT temperature FROM /weather_[a-z]*$/ WHERE time > now() - 1m ORDER BY DESC",
);
let _result = rt
    .block_on(client.json_query(query))
    .map(|mut db_result| db_result.deserialize_next::<WeatherWithoutCityName>())
    .map(|it| {
        it.map(|series_vec| {
            series_vec
                .series
                .into_iter()
                .map(|mut city_series| {
                    let city_name =
                        city_series.name.split("_").collect::<Vec<&str>>().remove(2);
                    Weather {
                        weather: city_series.values.remove(0),
                        city_name: city_name.to_string(),
                    }
                })
                .collect::<Vec<Weather>>()
        })
    });

Structs

InfluxDbSeries

Represents a returned series from InfluxDB