aprimadi/influxdb2

Failure to Query

reeslabree opened this issue · 4 comments

Like several before me, thank you for making this library! I'm happily writing to my DB, however I am struggling to read/run queries against it.

I have a query that returns:
Screen Shot 2022-09-21 at 10 58 17 AM

How do I read this? My current attempt is:

// AggregatedUsageDat definition
#[derive(Debug, FromDataPoint)]
pub struct AggregatedUsageData {
    pub device_id: String,
    pub value: i64,
}

impl Default for AggregatedUsageData {
    fn default() -> Self {
        Self {
            device_id: "".to_string(),
            value: 0_i64
        }
    }
}

// function definition
pub async fn read_and_aggregate_timeseries(
    start_time: u32,
    connection_info: InfluxConnectionInfo<'_>,
) -> Result<Vec<AggregatedUsageData>, Box<dyn std::error::Error>> {
    let client = Client::new(
        connection_info.host,
        connection_info.org,
        connection_info.token,
    );

    let qs = format!(
        "from(bucket: \"{bucket}\")
        |> range(start: {time})
        |> filter(fn: (r) => r[\"_measurement\"] == \"device-usage\")
        |> filter(fn: (r) => r[\"_field\"] == \"usage\")
        |> group(columns: [\"deviceId\"])
        |> drop(columns: [\"_start\", \"_stop\"])
        |> sum()
        |> rename(columns: {{_value: \"value\", deviceId: \"device_id\"}})
        ",
        bucket = connection_info.bucket,
        time = start_time
    );

    log::info!("Query String:\n{}", qs.to_string());

    let res: Vec<AggregatedUsageData> = client.query::<AggregatedUsageData>(Some(query)).await?;

    Ok(res)
}

This results in the error thread 'timeseries::can_read' panicked at 'called 'Option::unwrap()' on a 'None' value'

Logging my query string, I know it's correct (see above screenshot). I'm not really sure what's going on under the hood, but it seems like it would map the column values by name onto this vector? What am I doing wrong?

Have you tried removing drop(...)?

This library uses those two values to perform grouping of data point into one struct (since Influxdb treats data point with multiple tag as separate data point).

Just gave it a shot - doesn't seem to make a difference. Still getting panicked at 'called 'Option::unwrap()' on a 'None' value'. Also tried duplicating the column _value to value to retain that tag.

Any other suggestions for things to try?

Was able to get this to work - not sure why, but this query is acceptable:

let qs = format!(
    "from(bucket: \"{bucket}\") 
        |> range(start: {time})
        |> filter(fn: (r) => 
            r[\"_measurement\"] == \"device-usage\" and 
            r[\"_field\"] == \"usage\")
        |> rename(columns: {{\"deviceId\": \"device_id\"}})
        |> sum(column: \"_value\")
    ",
    bucket = connection_info.bucket,
    time = start_time
);

Curious if there's any theories why this works, however I can contently close this for now.

Yes, I forgot to mention the field _value is also assumed to exist by this lib.