1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright Claudio Mattera 2021.
// Distributed under the MIT License or Apache 2.0 License at your option.
// See accompanying files License-MIT.txt and License-Apache-2.0, or online at
// https://opensource.org/licenses/MIT
// https://opensource.org/licenses/Apache-2.0

//! Types used by other modules

use std::fmt;

use tracing::*;

use thiserror::Error;

use chrono::{DateTime, SecondsFormat, Utc};

/// Value types supported by InfluxDB
#[derive(Clone, Debug, PartialEq)]
pub enum Value {
    /// A floating point value
    Float(f64),

    /// An integer value
    Integer(i64),

    /// An unsigned integer value
    UnsignedInteger(u64),

    /// A string value
    String(String),

    /// A boolean value
    Boolean(bool),

    /// A datetime value (as nanosecond epoch)
    Timestamp(DateTime<Utc>),
}

impl Value {
    pub fn into_f64(self) -> f64 {
        if let Value::Float(value) = self {
            value
        } else if let Value::Integer(value) = self {
            value as f64
        } else if let Value::UnsignedInteger(value) = self {
            value as f64
        } else {
            panic!("Not a f64: {:?}", self);
        }
    }

    pub fn into_i64(self) -> i64 {
        if let Value::Integer(value) = self {
            value
        } else if let Value::UnsignedInteger(value) = self {
            value as i64
        } else if let Value::Float(value) = self {
            warn!("Casting float to integer");
            value as i64
        } else {
            panic!("Not a i64: {:?}", self);
        }
    }

    pub fn into_u64(self) -> u64 {
        if let Value::UnsignedInteger(value) = self {
            value
        } else {
            panic!("Not a u64: {:?}", self);
        }
    }

    pub fn into_boolean(self) -> bool {
        if let Value::Boolean(value) = self {
            value
        } else {
            panic!("Not a boolean: {:?}", self);
        }
    }

    pub fn into_string(self) -> String {
        if let Value::String(value) = self {
            value
        } else {
            panic!("Not a string: {:?}", self);
        }
    }

    pub fn into_timestamp(self) -> DateTime<Utc> {
        if let Value::Timestamp(value) = self {
            value
        } else {
            panic!("Not a timestamp: {:?}", self);
        }
    }
}

impl fmt::Display for Value {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match &self {
            Value::Float(value) => {
                write!(f, "{}", value)?;
            }
            Value::Integer(value) => {
                write!(f, "{}", value)?;
            }
            Value::UnsignedInteger(value) => {
                write!(f, "{}", value)?;
            }
            Value::String(value) => {
                write!(f, "{}", value)?;
            }
            Value::Boolean(value) => {
                write!(f, "{}", value)?;
            }
            Value::Timestamp(value) => {
                write!(f, "{}", value)?;
            }
        }

        Ok(())
    }
}

/// A duration
///
/// Note: this type is almost entirely equivalent to `chrono::Duration`, but
/// it also supports infinite duration in addition.
///
/// Since `chrono::Duration` implements `Into<Duration>`, the former can be
/// used everywhere the latter is expected.
#[derive(Debug)]
pub enum Duration {
    /// A duration expressed in nanoseconds
    Nanoseconds(i64),

    /// A duration expressed in microseconds
    Microseconds(i64),

    /// A duration expressed in milliseconds
    Milliseconds(i64),

    /// A duration expressed in seconds
    Seconds(i64),

    /// A duration expressed in minutes
    Minutes(i64),

    /// A duration expressed in hours
    Hours(i64),

    /// A duration expressed in days
    Days(i64),

    /// An infinite duration
    Infinity,
}

impl ToString for Duration {
    fn to_string(&self) -> String {
        match self {
            Duration::Nanoseconds(seconds) => format!("{}ns", seconds),
            Duration::Microseconds(seconds) => format!("{}us", seconds),
            Duration::Milliseconds(seconds) => format!("{}ms", seconds),
            Duration::Seconds(seconds) => format!("{}s", seconds),
            Duration::Minutes(minutes) => format!("{}m", minutes),
            Duration::Hours(hours) => format!("{}h", hours),
            Duration::Days(days) => format!("{}d", days),
            Duration::Infinity => "inf".to_string(),
        }
    }
}

impl From<chrono::Duration> for Duration {
    fn from(duration: chrono::Duration) -> Self {
        Duration::Seconds(duration.num_seconds())
    }
}

/// An entity which is either an instant or a duration
///
/// InfluxDB allows to use durations where instants are expected, and
/// interprets them as the point in time relative to the current instant.
/// E.g. if now is `2021-03-10T22:43:32Z`, the duration `Duration::Minutes(-4)`
/// is interpreted as the instant `2021-03-10T22:39:32Z`.
#[derive(Debug)]
pub enum InstantOrDuration {
    /// An instant in time
    Instant(DateTime<Utc>),

    /// The instant corresponding to the current time plus a duration
    Duration(Duration),
}

impl ToString for InstantOrDuration {
    fn to_string(&self) -> String {
        match self {
            InstantOrDuration::Instant(instant) => format!(
                "'{}'",
                instant.to_rfc3339_opts(SecondsFormat::AutoSi, true),
            ),
            InstantOrDuration::Duration(duration) => duration.to_string(),
        }
    }
}

impl From<DateTime<Utc>> for InstantOrDuration {
    fn from(instant: DateTime<Utc>) -> Self {
        InstantOrDuration::Instant(instant)
    }
}

impl From<Duration> for InstantOrDuration {
    fn from(duration: Duration) -> Self {
        InstantOrDuration::Duration(duration)
    }
}

impl From<chrono::Duration> for InstantOrDuration {
    fn from(duration: chrono::Duration) -> Self {
        InstantOrDuration::Duration(duration.into())
    }
}

/// An error occurred while creating the dataframe
#[derive(Error, Debug)]
pub enum DataFrameError {
    /// Error while creating the dataframe
    #[error("Error while creating the dataframe")]
    Creation,
}