Expand description

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 influxdb::{Client, Query};
use serde::Deserialize;

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

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

let client = Client::new("http://localhost:8086", "test");
let query = Query::raw_read_query(
    "SELECT temperature FROM /weather_[a-z]*$/ WHERE time > now() - 1m ORDER BY DESC",
);
let mut db_result = client.json_query(query).await?;
let _result = db_result
    .deserialize_next::<WeatherWithoutCityName>()?
    .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

Represents a returned series from InfluxDB

Represents a returned series from InfluxDB