influxdb-rs / influxdb-rust

Rust Client for the InfluxDB Time Series Database
https://crates.io/crates/influxdb
MIT License
244 stars 79 forks source link

invalid precision #61

Open jayhuang75 opened 4 years ago

jayhuang75 commented 4 years ago

Hello @Empty2k12

Great crate, thank you. Recently I used this crate.

let write_query = Timestamp::Now.into_query("weather").add_field("humidity", 82); let write_result = influxdb.query(&write_query).await; println!("write_result : {:?}", write_result.expect("write into influxdb failed"));

The error message as follow:

DatabaseError { error: "influxdb error: \"{\"error\":\"invalid precision \\\"rfc3339\\\" (use n, u, ms, s, m or h)\"}\n\"" }

Any suggestion? or guidance?

Thank you

Empty2k12 commented 4 years ago

Hello @jayhuang75

rfc3339 is the most precise time precision. You can override it by setting Timestamp::Hours(11).into_query("weather"), where 11 is the hours since the epoch. Timestamp also has other types for smaller precision such as second or nanosecond.

Which InfluxDB version are you using? I am not aware of a change that removes setting the rfc3339 precision.

jayhuang75 commented 4 years ago

@Empty2k12 , appreciated your quick response. I don't have the challenge with Timestamp::Hours(11).into_query("weather"), or with any other such as Seconds. The only challenge is the Now(), which leads to the error as above.

The version influxdb is the docker images influxdb:latest (1.8.0).

Thank you so much again for your help here. Appreciated.

papey commented 3 years ago

Same here, with 1.8+, Timestramp::Now() causes the following error

DatabaseError { error: "influxdb error: \"{\"error\":\"invalid precision \\\"rfc3339\\\" (use n, u, ms, s, m or h)\"}\n\"" }

Edit, after some experimentations, with v0.3.0,

Here is a quickfix :

// timestamp will compute from EPOCH timestamp
fn ts() -> u128 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("Time goes backward ?")
        .as_millis()
}

Then

    // init query
    let mut query = Timestamp::Microseconds(ts()).into_query(&measurement.key);