Compare commits
No commits in common. "d2f4ec97c0a787b20ab88c03331fe23a852f5549" and "85e2494c3bbbd12b8a582728a88242b334f1873f" have entirely different histories.
d2f4ec97c0
...
85e2494c3b
|
@ -71,9 +71,11 @@ extern crate thiserror;
|
||||||
extern crate uuid;
|
extern crate uuid;
|
||||||
|
|
||||||
mod criteria;
|
mod criteria;
|
||||||
|
mod date_time_tz;
|
||||||
mod series;
|
mod series;
|
||||||
mod types;
|
mod types;
|
||||||
|
|
||||||
pub use criteria::*;
|
pub use criteria::*;
|
||||||
|
pub use date_time_tz::DateTimeTz;
|
||||||
pub use series::Series;
|
pub use series::Series;
|
||||||
pub use types::{EmseriesReadError, EmseriesWriteError, RecordId, Recordable, Timestamp};
|
pub use types::{EmseriesReadError, EmseriesWriteError, Recordable, Timestamp, UniqueId};
|
||||||
|
|
|
@ -24,7 +24,7 @@ use std::io::{BufRead, BufReader, LineWriter, Write};
|
||||||
use std::iter::Iterator;
|
use std::iter::Iterator;
|
||||||
|
|
||||||
use criteria::Criteria;
|
use criteria::Criteria;
|
||||||
use types::{EmseriesReadError, EmseriesWriteError, Record, RecordId, Recordable};
|
use types::{EmseriesReadError, EmseriesWriteError, Record, Recordable, UniqueId};
|
||||||
|
|
||||||
/// An open time series database.
|
/// An open time series database.
|
||||||
///
|
///
|
||||||
|
@ -33,7 +33,7 @@ use types::{EmseriesReadError, EmseriesWriteError, Record, RecordId, Recordable}
|
||||||
pub struct Series<T: Clone + Recordable + DeserializeOwned + Serialize> {
|
pub struct Series<T: Clone + Recordable + DeserializeOwned + Serialize> {
|
||||||
//path: String,
|
//path: String,
|
||||||
writer: LineWriter<File>,
|
writer: LineWriter<File>,
|
||||||
records: HashMap<RecordId, T>,
|
records: HashMap<UniqueId, T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Series<T>
|
impl<T> Series<T>
|
||||||
|
@ -62,8 +62,8 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Load a file and return all of the records in it.
|
/// Load a file and return all of the records in it.
|
||||||
fn load_file(f: &File) -> Result<HashMap<RecordId, T>, EmseriesReadError> {
|
fn load_file(f: &File) -> Result<HashMap<UniqueId, T>, EmseriesReadError> {
|
||||||
let mut records: HashMap<RecordId, T> = HashMap::new();
|
let mut records: HashMap<UniqueId, T> = HashMap::new();
|
||||||
let reader = BufReader::new(f);
|
let reader = BufReader::new(f);
|
||||||
for line in reader.lines() {
|
for line in reader.lines() {
|
||||||
match line {
|
match line {
|
||||||
|
@ -87,14 +87,14 @@ where
|
||||||
|
|
||||||
/// Put a new record into the database. A unique id will be assigned to the record and
|
/// Put a new record into the database. A unique id will be assigned to the record and
|
||||||
/// returned.
|
/// returned.
|
||||||
pub fn put(&mut self, entry: T) -> Result<RecordId, EmseriesWriteError> {
|
pub fn put(&mut self, entry: T) -> Result<UniqueId, EmseriesWriteError> {
|
||||||
let uuid = RecordId::default();
|
let uuid = UniqueId::default();
|
||||||
self.update(uuid.clone(), entry).map(|_| uuid)
|
self.update(uuid.clone(), entry).map(|_| uuid)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Update an existing record. The [RecordId] of the record passed into this function must match
|
/// Update an existing record. The `UniqueId` of the record passed into this function must match
|
||||||
/// the [RecordId] of a record already in the database.
|
/// the `UniqueId` of a record already in the database.
|
||||||
pub fn update(&mut self, uuid: RecordId, entry: T) -> Result<(), EmseriesWriteError> {
|
pub fn update(&mut self, uuid: UniqueId, entry: T) -> Result<(), EmseriesWriteError> {
|
||||||
self.records.insert(uuid.clone(), entry.clone());
|
self.records.insert(uuid.clone(), entry.clone());
|
||||||
let write_res = match serde_json::to_string(&Record {
|
let write_res = match serde_json::to_string(&Record {
|
||||||
id: uuid,
|
id: uuid,
|
||||||
|
@ -118,7 +118,7 @@ where
|
||||||
/// Future note: while this deletes a record from the view, it only adds an entry to the
|
/// Future note: while this deletes a record from the view, it only adds an entry to the
|
||||||
/// database that indicates `data: null`. If record histories ever become important, the record
|
/// database that indicates `data: null`. If record histories ever become important, the record
|
||||||
/// and its entire history (including this delete) will still be available.
|
/// and its entire history (including this delete) will still be available.
|
||||||
pub fn delete(&mut self, uuid: &RecordId) -> Result<(), EmseriesWriteError> {
|
pub fn delete(&mut self, uuid: &UniqueId) -> Result<(), EmseriesWriteError> {
|
||||||
if !self.records.contains_key(uuid) {
|
if !self.records.contains_key(uuid) {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
@ -138,7 +138,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get all of the records in the database.
|
/// Get all of the records in the database.
|
||||||
pub fn records(&self) -> impl Iterator<Item = (&RecordId, &T)> {
|
pub fn records(&self) -> impl Iterator<Item = (&UniqueId, &T)> {
|
||||||
self.records.iter()
|
self.records.iter()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,29 +148,29 @@ where
|
||||||
pub fn search<'s>(
|
pub fn search<'s>(
|
||||||
&'s self,
|
&'s self,
|
||||||
criteria: impl Criteria + 's,
|
criteria: impl Criteria + 's,
|
||||||
) -> impl Iterator<Item = (&'s RecordId, &'s T)> + 's {
|
) -> impl Iterator<Item = (&'s UniqueId, &'s T)> + 's {
|
||||||
self.records().filter(move |&tr| criteria.apply(tr.1))
|
self.records().filter(move |&tr| criteria.apply(tr.1))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Perform a search and sort the resulting records based on the comparison.
|
/// Perform a search and sort the resulting records based on the comparison.
|
||||||
pub fn search_sorted<'s, C, CMP>(&'s self, criteria: C, compare: CMP) -> Vec<(&RecordId, &T)>
|
pub fn search_sorted<'s, C, CMP>(&'s self, criteria: C, compare: CMP) -> Vec<(&UniqueId, &T)>
|
||||||
where
|
where
|
||||||
C: Criteria + 's,
|
C: Criteria + 's,
|
||||||
CMP: FnMut(&(&RecordId, &T), &(&RecordId, &T)) -> Ordering,
|
CMP: FnMut(&(&UniqueId, &T), &(&UniqueId, &T)) -> Ordering,
|
||||||
{
|
{
|
||||||
let search_iter = self.search(criteria);
|
let search_iter = self.search(criteria);
|
||||||
let mut records: Vec<(&RecordId, &T)> = search_iter.collect();
|
let mut records: Vec<(&UniqueId, &T)> = search_iter.collect();
|
||||||
records.sort_by(compare);
|
records.sort_by(compare);
|
||||||
records
|
records
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get an exact record from the database based on unique id.
|
/// Get an exact record from the database based on unique id.
|
||||||
pub fn get(&self, uuid: &RecordId) -> Option<T> {
|
pub fn get(&self, uuid: &UniqueId) -> Option<T> {
|
||||||
self.records.get(uuid).cloned()
|
self.records.get(uuid).cloned()
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
pub fn remove(&self, uuid: RecordId) -> Result<(), EmseriesError> {
|
pub fn remove(&self, uuid: UniqueId) -> Result<(), EmseriesError> {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -10,8 +10,8 @@ Luminescent Dreams Tools is distributed in the hope that it will be useful, but
|
||||||
You should have received a copy of the GNU General Public License along with Lumeto. If not, see <https://www.gnu.org/licenses/>.
|
You should have received a copy of the GNU General Public License along with Lumeto. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use chrono::{DateTime, FixedOffset, NaiveDate};
|
use chrono::NaiveDate;
|
||||||
use chrono_tz::UTC;
|
use date_time_tz::DateTimeTz;
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use serde::ser::Serialize;
|
use serde::ser::Serialize;
|
||||||
use std::{cmp::Ordering, fmt, io, str};
|
use std::{cmp::Ordering, fmt, io, str};
|
||||||
|
@ -44,79 +44,25 @@ pub enum EmseriesWriteError {
|
||||||
JSONWriteError(serde_json::error::Error),
|
JSONWriteError(serde_json::error::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
/// A Timestamp, stored with reference to human reckoning. This could be either a Naive Date or a
|
|
||||||
/// date and a time with a timezone. The idea of the "human reckoning" is that, no matter what
|
|
||||||
/// timezone the record was created in, we want to group things based on the date that the human
|
|
||||||
/// was perceiving at the time it was recorded.
|
|
||||||
pub enum Timestamp {
|
|
||||||
DateTime(DateTime<FixedOffset>),
|
|
||||||
Date(NaiveDate),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
||||||
#[serde(untagged)]
|
#[serde(untagged)]
|
||||||
pub enum TimestampJS {
|
pub enum Timestamp {
|
||||||
DateTime(String),
|
DateTime(DateTimeTz),
|
||||||
Date(String),
|
Date(NaiveDate),
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Timestamp> for TimestampJS {
|
|
||||||
fn from(s: Timestamp) -> TimestampJS {
|
|
||||||
match s {
|
|
||||||
Timestamp::DateTime(ts) => TimestampJS::DateTime(ts.to_rfc3339()),
|
|
||||||
Timestamp::Date(ts) => TimestampJS::Date(ts.to_string()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<TimestampJS> for Timestamp {
|
|
||||||
fn from(s: TimestampJS) -> Timestamp {
|
|
||||||
match s {
|
|
||||||
TimestampJS::DateTime(ts) => {
|
|
||||||
Timestamp::DateTime(DateTime::parse_from_rfc3339(&ts).unwrap())
|
|
||||||
}
|
|
||||||
TimestampJS::Date(ts) => Timestamp::Date(ts.parse::<NaiveDate>().unwrap()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl str::FromStr for Timestamp {
|
impl str::FromStr for Timestamp {
|
||||||
type Err = chrono::ParseError;
|
type Err = chrono::ParseError;
|
||||||
fn from_str(line: &str) -> Result<Self, Self::Err> {
|
fn from_str(line: &str) -> Result<Self, Self::Err> {
|
||||||
DateTime::parse_from_rfc3339(line)
|
DateTimeTz::from_str(line)
|
||||||
.map(Timestamp::DateTime)
|
.map(Timestamp::DateTime)
|
||||||
.or(NaiveDate::from_str(line).map(Timestamp::Date))
|
.or(NaiveDate::from_str(line).map(Timestamp::Date))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
impl PartialEq for Timestamp {
|
|
||||||
fn eq(&self, other: &Timestamp) -> bool {
|
|
||||||
match (self, other) {
|
|
||||||
(Timestamp::DateTime(dt1), Timestamp::DateTime(dt2)) => {
|
|
||||||
dt1.with_timezone(&UTC) == dt2.with_timezone(&UTC)
|
|
||||||
}
|
|
||||||
// It's not clear to me what would make sense when I'm comparing a date and a
|
|
||||||
// timestamp. I'm going with a naive date comparison on the idea that what I'm wanting
|
|
||||||
// here is human scale, again.
|
|
||||||
(Timestamp::DateTime(dt1), Timestamp::Date(dt2)) => dt1.date_naive() == *dt2,
|
|
||||||
(Timestamp::Date(dt1), Timestamp::DateTime(dt2)) => *dt1 == dt2.date_naive(),
|
|
||||||
(Timestamp::Date(dt1), Timestamp::Date(dt2)) => *dt1 == *dt2,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
impl PartialOrd for Timestamp {
|
impl PartialOrd for Timestamp {
|
||||||
fn partial_cmp(&self, other: &Timestamp) -> Option<Ordering> {
|
fn partial_cmp(&self, other: &Timestamp) -> Option<Ordering> {
|
||||||
// Some(self.cmp(other))
|
Some(self.cmp(other))
|
||||||
match (self, other) {
|
|
||||||
(Timestamp::DateTime(dt1), Timestamp::DateTime(dt2)) => dt1.partial_cmp(dt2),
|
|
||||||
(Timestamp::DateTime(dt1), Timestamp::Date(dt2)) => dt1.date_naive().partial_cmp(dt2),
|
|
||||||
(Timestamp::Date(dt1), Timestamp::DateTime(dt2)) => dt1.partial_cmp(&dt2.date_naive()),
|
|
||||||
(Timestamp::Date(dt1), Timestamp::Date(dt2)) => dt1.partial_cmp(dt2),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,13 +70,25 @@ impl Ord for Timestamp {
|
||||||
fn cmp(&self, other: &Timestamp) -> Ordering {
|
fn cmp(&self, other: &Timestamp) -> Ordering {
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(Timestamp::DateTime(dt1), Timestamp::DateTime(dt2)) => dt1.cmp(dt2),
|
(Timestamp::DateTime(dt1), Timestamp::DateTime(dt2)) => dt1.cmp(dt2),
|
||||||
(Timestamp::DateTime(dt1), Timestamp::Date(dt2)) => dt1.date_naive().cmp(dt2),
|
(Timestamp::DateTime(dt1), Timestamp::Date(dt2)) => dt1.0.date_naive().cmp(dt2),
|
||||||
(Timestamp::Date(dt1), Timestamp::DateTime(dt2)) => dt1.cmp(&dt2.date_naive()),
|
(Timestamp::Date(dt1), Timestamp::DateTime(dt2)) => dt1.cmp(&dt2.0.date_naive()),
|
||||||
(Timestamp::Date(dt1), Timestamp::Date(dt2)) => dt1.cmp(dt2),
|
(Timestamp::Date(dt1), Timestamp::Date(dt2)) => dt1.cmp(dt2),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<DateTimeTz> for Timestamp {
|
||||||
|
fn from(d: DateTimeTz) -> Self {
|
||||||
|
Self::DateTime(d)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<NaiveDate> for Timestamp {
|
||||||
|
fn from(d: NaiveDate) -> Self {
|
||||||
|
Self::Date(d)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Any element to be put into the database needs to be Recordable. This is the common API that
|
/// Any element to be put into the database needs to be Recordable. This is the common API that
|
||||||
/// will aid in searching and later in indexing records.
|
/// will aid in searching and later in indexing records.
|
||||||
pub trait Recordable {
|
pub trait Recordable {
|
||||||
|
@ -145,26 +103,26 @@ pub trait Recordable {
|
||||||
///
|
///
|
||||||
/// This is a wrapper around a basic uuid with some extra convenience methods.
|
/// This is a wrapper around a basic uuid with some extra convenience methods.
|
||||||
#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
|
#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
|
||||||
pub struct RecordId(Uuid);
|
pub struct UniqueId(Uuid);
|
||||||
|
|
||||||
impl Default for RecordId {
|
impl Default for UniqueId {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self(Uuid::new_v4())
|
Self(Uuid::new_v4())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl str::FromStr for RecordId {
|
impl str::FromStr for UniqueId {
|
||||||
type Err = EmseriesReadError;
|
type Err = EmseriesReadError;
|
||||||
|
|
||||||
/// Parse a RecordId from a string. Raise UUIDParseError if the parsing fails.
|
/// Parse a UniqueId from a string. Raise UUIDParseError if the parsing fails.
|
||||||
fn from_str(val: &str) -> Result<Self, Self::Err> {
|
fn from_str(val: &str) -> Result<Self, Self::Err> {
|
||||||
Uuid::parse_str(val)
|
Uuid::parse_str(val)
|
||||||
.map(RecordId)
|
.map(UniqueId)
|
||||||
.map_err(EmseriesReadError::UUIDParseError)
|
.map_err(EmseriesReadError::UUIDParseError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for RecordId {
|
impl fmt::Display for UniqueId {
|
||||||
/// Convert to a hyphenated string
|
/// Convert to a hyphenated string
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
||||||
write!(f, "{}", self.0.to_hyphenated())
|
write!(f, "{}", self.0.to_hyphenated())
|
||||||
|
@ -175,7 +133,7 @@ impl fmt::Display for RecordId {
|
||||||
/// Recordable trait.
|
/// Recordable trait.
|
||||||
#[derive(Clone, Deserialize, Serialize)]
|
#[derive(Clone, Deserialize, Serialize)]
|
||||||
pub struct Record<T: Clone + Recordable> {
|
pub struct Record<T: Clone + Recordable> {
|
||||||
pub id: RecordId,
|
pub id: UniqueId,
|
||||||
pub data: Option<T>,
|
pub data: Option<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -198,20 +156,21 @@ mod test {
|
||||||
use self::dimensioned::si::{Kilogram, KG};
|
use self::dimensioned::si::{Kilogram, KG};
|
||||||
use super::*;
|
use super::*;
|
||||||
use chrono::TimeZone;
|
use chrono::TimeZone;
|
||||||
use chrono_tz::Etc::UTC;
|
use chrono_tz::{Etc::UTC, US::Central};
|
||||||
|
use date_time_tz::DateTimeTz;
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
||||||
pub struct Weight(Kilogram<f64>);
|
pub struct Weight(Kilogram<f64>);
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
||||||
pub struct WeightRecord {
|
pub struct WeightRecord {
|
||||||
pub date: NaiveDate,
|
pub date: Timestamp,
|
||||||
pub weight: Weight,
|
pub weight: Weight,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Recordable for WeightRecord {
|
impl Recordable for WeightRecord {
|
||||||
fn timestamp(&self) -> Timestamp {
|
fn timestamp(&self) -> Timestamp {
|
||||||
Timestamp::Date(self.date.clone())
|
self.date.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tags(&self) -> Vec<String> {
|
fn tags(&self) -> Vec<String> {
|
||||||
|
@ -220,14 +179,12 @@ mod test {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn timestamp_parses_utc_time() {
|
fn timestamp_parses_datetimetz_without_timezone() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
"2003-11-10T06:00:00Z".parse::<Timestamp>().unwrap(),
|
"2003-11-10T06:00:00Z".parse::<Timestamp>().unwrap(),
|
||||||
Timestamp::DateTime(
|
Timestamp::DateTime(DateTimeTz(
|
||||||
UTC.with_ymd_and_hms(2003, 11, 10, 6, 0, 0)
|
UTC.with_ymd_and_hms(2003, 11, 10, 6, 0, 0).unwrap()
|
||||||
.unwrap()
|
)),
|
||||||
.with_timezone(&FixedOffset::east_opt(0).unwrap())
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -239,9 +196,9 @@ mod test {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[ignore]
|
#[test]
|
||||||
fn v_alpha_serialization() {
|
fn v_alpha_serialization() {
|
||||||
const WEIGHT_ENTRY: &str = "{\"data\":{\"weight\":77.79109},\"date\":\"2003-11-10\",\"id\":\"3330c5b0-783f-4919-b2c4-8169c38f65ff\"}";
|
const WEIGHT_ENTRY: &str = "{\"data\":{\"weight\":77.79109,\"date\":\"2003-11-10T06:00:00.000000000000Z\"},\"id\":\"3330c5b0-783f-4919-b2c4-8169c38f65ff\"}";
|
||||||
|
|
||||||
let rec: Record<WeightRecord> = WEIGHT_ENTRY
|
let rec: Record<WeightRecord> = WEIGHT_ENTRY
|
||||||
.parse()
|
.parse()
|
||||||
|
@ -253,7 +210,9 @@ mod test {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
rec.data,
|
rec.data,
|
||||||
Some(WeightRecord {
|
Some(WeightRecord {
|
||||||
date: NaiveDate::from_ymd_opt(2003, 11, 10).unwrap(),
|
date: Timestamp::DateTime(DateTimeTz(
|
||||||
|
UTC.with_ymd_and_hms(2003, 11, 10, 6, 0, 0).unwrap()
|
||||||
|
)),
|
||||||
weight: Weight(77.79109 * KG),
|
weight: Weight(77.79109 * KG),
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
@ -262,53 +221,54 @@ mod test {
|
||||||
#[test]
|
#[test]
|
||||||
fn serialization_output() {
|
fn serialization_output() {
|
||||||
let rec = WeightRecord {
|
let rec = WeightRecord {
|
||||||
date: NaiveDate::from_ymd_opt(2003, 11, 10).unwrap(),
|
date: Timestamp::DateTime(DateTimeTz(
|
||||||
|
UTC.with_ymd_and_hms(2003, 11, 10, 6, 0, 0).unwrap(),
|
||||||
|
)),
|
||||||
weight: Weight(77.0 * KG),
|
weight: Weight(77.0 * KG),
|
||||||
};
|
};
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
serde_json::to_string(&rec).unwrap(),
|
serde_json::to_string(&rec).unwrap(),
|
||||||
"{\"date\":\"2003-11-10\",\"weight\":77.0}"
|
"{\"date\":\"2003-11-10T06:00:00Z\",\"weight\":77.0}"
|
||||||
);
|
);
|
||||||
|
|
||||||
let rec2 = WeightRecord {
|
let rec2 = WeightRecord {
|
||||||
date: NaiveDate::from_ymd_opt(2003, 11, 10).unwrap(),
|
date: Timestamp::DateTime(
|
||||||
|
Central
|
||||||
|
.with_ymd_and_hms(2003, 11, 10, 0, 0, 0)
|
||||||
|
.unwrap()
|
||||||
|
.into(),
|
||||||
|
),
|
||||||
weight: Weight(77.0 * KG),
|
weight: Weight(77.0 * KG),
|
||||||
};
|
};
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
serde_json::to_string(&rec2).unwrap(),
|
serde_json::to_string(&rec2).unwrap(),
|
||||||
"{\"date\":\"2003-11-10\",\"weight\":77.0}"
|
"{\"date\":\"2003-11-10T06:00:00Z US/Central\",\"weight\":77.0}"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn two_datetimes_can_be_compared() {
|
fn two_datetimes_can_be_compared() {
|
||||||
let time1 = Timestamp::DateTime(
|
let time1 = Timestamp::DateTime(DateTimeTz(
|
||||||
UTC.with_ymd_and_hms(2003, 11, 10, 6, 0, 0)
|
UTC.with_ymd_and_hms(2003, 11, 10, 6, 0, 0).unwrap(),
|
||||||
.unwrap()
|
));
|
||||||
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
|
let time2 = Timestamp::DateTime(DateTimeTz(
|
||||||
);
|
UTC.with_ymd_and_hms(2003, 11, 11, 6, 0, 0).unwrap(),
|
||||||
let time2 = Timestamp::DateTime(
|
));
|
||||||
UTC.with_ymd_and_hms(2003, 11, 11, 6, 0, 0)
|
|
||||||
.unwrap()
|
|
||||||
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
|
|
||||||
);
|
|
||||||
assert!(time1 < time2);
|
assert!(time1 < time2);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn two_dates_can_be_compared() {
|
fn two_dates_can_be_compared() {
|
||||||
let time1: Timestamp = Timestamp::Date(NaiveDate::from_ymd_opt(2003, 11, 10).unwrap());
|
let time1 = Timestamp::Date(NaiveDate::from_ymd_opt(2003, 11, 10).unwrap());
|
||||||
let time2: Timestamp = Timestamp::Date(NaiveDate::from_ymd_opt(2003, 11, 11).unwrap());
|
let time2 = Timestamp::Date(NaiveDate::from_ymd_opt(2003, 11, 11).unwrap());
|
||||||
assert!(time1 < time2);
|
assert!(time1 < time2);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn datetime_and_date_can_be_compared() {
|
fn datetime_and_date_can_be_compared() {
|
||||||
let time1 = Timestamp::DateTime(
|
let time1 = Timestamp::DateTime(DateTimeTz(
|
||||||
UTC.with_ymd_and_hms(2003, 11, 10, 6, 0, 0)
|
UTC.with_ymd_and_hms(2003, 11, 10, 6, 0, 0).unwrap(),
|
||||||
.unwrap()
|
));
|
||||||
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
|
|
||||||
);
|
|
||||||
let time2 = Timestamp::Date(NaiveDate::from_ymd_opt(2003, 11, 11).unwrap());
|
let time2 = Timestamp::Date(NaiveDate::from_ymd_opt(2003, 11, 11).unwrap());
|
||||||
assert!(time1 < time2)
|
assert!(time1 < time2)
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ extern crate emseries;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use chrono::{format::Fixed, prelude::*};
|
use chrono::prelude::*;
|
||||||
use chrono_tz::Etc::UTC;
|
use chrono_tz::Etc::UTC;
|
||||||
use dimensioned::si::{Kilogram, Meter, Second, M, S};
|
use dimensioned::si::{Kilogram, Meter, Second, M, S};
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ mod test {
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
||||||
struct BikeTrip {
|
struct BikeTrip {
|
||||||
datetime: DateTime<FixedOffset>,
|
datetime: DateTimeTz,
|
||||||
distance: Distance,
|
distance: Distance,
|
||||||
duration: Duration,
|
duration: Duration,
|
||||||
comments: String,
|
comments: String,
|
||||||
|
@ -42,7 +42,7 @@ mod test {
|
||||||
|
|
||||||
impl Recordable for BikeTrip {
|
impl Recordable for BikeTrip {
|
||||||
fn timestamp(&self) -> Timestamp {
|
fn timestamp(&self) -> Timestamp {
|
||||||
Timestamp::DateTime(self.datetime.clone())
|
self.datetime.clone().into()
|
||||||
}
|
}
|
||||||
fn tags(&self) -> Vec<String> {
|
fn tags(&self) -> Vec<String> {
|
||||||
Vec::new()
|
Vec::new()
|
||||||
|
@ -52,46 +52,31 @@ mod test {
|
||||||
fn mk_trips() -> [BikeTrip; 5] {
|
fn mk_trips() -> [BikeTrip; 5] {
|
||||||
[
|
[
|
||||||
BikeTrip {
|
BikeTrip {
|
||||||
datetime: UTC
|
datetime: DateTimeTz(UTC.with_ymd_and_hms(2011, 10, 29, 0, 0, 0).unwrap()),
|
||||||
.with_ymd_and_hms(2011, 10, 29, 0, 0, 0)
|
|
||||||
.unwrap()
|
|
||||||
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
|
|
||||||
distance: Distance(58741.055 * M),
|
distance: Distance(58741.055 * M),
|
||||||
duration: Duration(11040.0 * S),
|
duration: Duration(11040.0 * S),
|
||||||
comments: String::from("long time ago"),
|
comments: String::from("long time ago"),
|
||||||
},
|
},
|
||||||
BikeTrip {
|
BikeTrip {
|
||||||
datetime: UTC
|
datetime: DateTimeTz(UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0).unwrap()),
|
||||||
.with_ymd_and_hms(2011, 10, 31, 0, 0, 0)
|
|
||||||
.unwrap()
|
|
||||||
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
|
|
||||||
distance: Distance(17702.0 * M),
|
distance: Distance(17702.0 * M),
|
||||||
duration: Duration(2880.0 * S),
|
duration: Duration(2880.0 * S),
|
||||||
comments: String::from("day 2"),
|
comments: String::from("day 2"),
|
||||||
},
|
},
|
||||||
BikeTrip {
|
BikeTrip {
|
||||||
datetime: UTC
|
datetime: DateTimeTz(UTC.with_ymd_and_hms(2011, 11, 02, 0, 0, 0).unwrap()),
|
||||||
.with_ymd_and_hms(2011, 11, 02, 0, 0, 0)
|
|
||||||
.unwrap()
|
|
||||||
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
|
|
||||||
distance: Distance(41842.945 * M),
|
distance: Distance(41842.945 * M),
|
||||||
duration: Duration(7020.0 * S),
|
duration: Duration(7020.0 * S),
|
||||||
comments: String::from("Do Some Distance!"),
|
comments: String::from("Do Some Distance!"),
|
||||||
},
|
},
|
||||||
BikeTrip {
|
BikeTrip {
|
||||||
datetime: UTC
|
datetime: DateTimeTz(UTC.with_ymd_and_hms(2011, 11, 04, 0, 0, 0).unwrap()),
|
||||||
.with_ymd_and_hms(2011, 11, 04, 0, 0, 0)
|
|
||||||
.unwrap()
|
|
||||||
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
|
|
||||||
distance: Distance(34600.895 * M),
|
distance: Distance(34600.895 * M),
|
||||||
duration: Duration(5580.0 * S),
|
duration: Duration(5580.0 * S),
|
||||||
comments: String::from("I did a lot of distance back then"),
|
comments: String::from("I did a lot of distance back then"),
|
||||||
},
|
},
|
||||||
BikeTrip {
|
BikeTrip {
|
||||||
datetime: UTC
|
datetime: DateTimeTz(UTC.with_ymd_and_hms(2011, 11, 05, 0, 0, 0).unwrap()),
|
||||||
.with_ymd_and_hms(2011, 11, 05, 0, 0, 0)
|
|
||||||
.unwrap()
|
|
||||||
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
|
|
||||||
distance: Distance(6437.376 * M),
|
distance: Distance(6437.376 * M),
|
||||||
duration: Duration(960.0 * S),
|
duration: Duration(960.0 * S),
|
||||||
comments: String::from("day 5"),
|
comments: String::from("day 5"),
|
||||||
|
@ -137,11 +122,7 @@ mod test {
|
||||||
Some(tr) => {
|
Some(tr) => {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
tr.timestamp(),
|
tr.timestamp(),
|
||||||
Timestamp::DateTime(
|
DateTimeTz(UTC.with_ymd_and_hms(2011, 10, 29, 0, 0, 0).unwrap()).into()
|
||||||
UTC.with_ymd_and_hms(2011, 10, 29, 0, 0, 0)
|
|
||||||
.unwrap()
|
|
||||||
.with_timezone(&FixedOffset::east_opt(0).unwrap())
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
assert_eq!(tr.duration, Duration(11040.0 * S));
|
assert_eq!(tr.duration, Duration(11040.0 * S));
|
||||||
assert_eq!(tr.comments, String::from("long time ago"));
|
assert_eq!(tr.comments, String::from("long time ago"));
|
||||||
|
@ -162,12 +143,10 @@ mod test {
|
||||||
ts.put(trip.clone()).expect("expect a successful put");
|
ts.put(trip.clone()).expect("expect a successful put");
|
||||||
}
|
}
|
||||||
|
|
||||||
let v: Vec<(&RecordId, &BikeTrip)> = ts
|
let v: Vec<(&UniqueId, &BikeTrip)> = ts
|
||||||
.search(exact_time(Timestamp::DateTime(
|
.search(exact_time(
|
||||||
UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0)
|
DateTimeTz(UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0).unwrap()).into(),
|
||||||
.unwrap()
|
))
|
||||||
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
|
|
||||||
)))
|
|
||||||
.collect();
|
.collect();
|
||||||
assert_eq!(v.len(), 1);
|
assert_eq!(v.len(), 1);
|
||||||
assert_eq!(*v[0].1, trips[1]);
|
assert_eq!(*v[0].1, trips[1]);
|
||||||
|
@ -185,19 +164,11 @@ mod test {
|
||||||
ts.put(trip.clone()).expect("expect a successful put");
|
ts.put(trip.clone()).expect("expect a successful put");
|
||||||
}
|
}
|
||||||
|
|
||||||
let v: Vec<(&RecordId, &BikeTrip)> = ts.search_sorted(
|
let v: Vec<(&UniqueId, &BikeTrip)> = ts.search_sorted(
|
||||||
time_range(
|
time_range(
|
||||||
Timestamp::DateTime(
|
DateTimeTz(UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0).unwrap()).into(),
|
||||||
UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0)
|
|
||||||
.unwrap()
|
|
||||||
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
|
|
||||||
),
|
|
||||||
true,
|
true,
|
||||||
Timestamp::DateTime(
|
DateTimeTz(UTC.with_ymd_and_hms(2011, 11, 04, 0, 0, 0).unwrap()).into(),
|
||||||
UTC.with_ymd_and_hms(2011, 11, 04, 0, 0, 0)
|
|
||||||
.unwrap()
|
|
||||||
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
|
|
||||||
),
|
|
||||||
true,
|
true,
|
||||||
),
|
),
|
||||||
|l, r| l.1.timestamp().cmp(&r.1.timestamp()),
|
|l, r| l.1.timestamp().cmp(&r.1.timestamp()),
|
||||||
|
@ -226,19 +197,11 @@ mod test {
|
||||||
{
|
{
|
||||||
let ts: Series<BikeTrip> =
|
let ts: Series<BikeTrip> =
|
||||||
Series::open(&path).expect("expect the time series to open correctly");
|
Series::open(&path).expect("expect the time series to open correctly");
|
||||||
let v: Vec<(&RecordId, &BikeTrip)> = ts.search_sorted(
|
let v: Vec<(&UniqueId, &BikeTrip)> = ts.search_sorted(
|
||||||
time_range(
|
time_range(
|
||||||
Timestamp::DateTime(
|
DateTimeTz(UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0).unwrap()).into(),
|
||||||
UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0)
|
|
||||||
.unwrap()
|
|
||||||
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
|
|
||||||
),
|
|
||||||
true,
|
true,
|
||||||
Timestamp::DateTime(
|
DateTimeTz(UTC.with_ymd_and_hms(2011, 11, 04, 0, 0, 0).unwrap()).into(),
|
||||||
UTC.with_ymd_and_hms(2011, 11, 04, 0, 0, 0)
|
|
||||||
.unwrap()
|
|
||||||
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
|
|
||||||
),
|
|
||||||
true,
|
true,
|
||||||
),
|
),
|
||||||
|l, r| l.1.timestamp().cmp(&r.1.timestamp()),
|
|l, r| l.1.timestamp().cmp(&r.1.timestamp()),
|
||||||
|
@ -268,20 +231,11 @@ mod test {
|
||||||
{
|
{
|
||||||
let mut ts: Series<BikeTrip> =
|
let mut ts: Series<BikeTrip> =
|
||||||
Series::open(&path).expect("expect the time series to open correctly");
|
Series::open(&path).expect("expect the time series to open correctly");
|
||||||
let v: Vec<(&RecordId, &BikeTrip)> = ts.search_sorted(
|
let v: Vec<(&UniqueId, &BikeTrip)> = ts.search_sorted(
|
||||||
time_range(
|
time_range(
|
||||||
Timestamp::DateTime(
|
DateTimeTz(UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0).unwrap()).into(),
|
||||||
UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0)
|
|
||||||
.unwrap()
|
|
||||||
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
|
|
||||||
),
|
|
||||||
true,
|
true,
|
||||||
Timestamp::DateTime(
|
DateTimeTz(UTC.with_ymd_and_hms(2011, 11, 04, 0, 0, 0).unwrap()).into(),
|
||||||
UTC.with_ymd_and_hms(2011, 11, 04, 0, 0, 0)
|
|
||||||
.unwrap()
|
|
||||||
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
|
|
||||||
)
|
|
||||||
.into(),
|
|
||||||
true,
|
true,
|
||||||
),
|
),
|
||||||
|l, r| l.1.timestamp().cmp(&r.1.timestamp()),
|
|l, r| l.1.timestamp().cmp(&r.1.timestamp()),
|
||||||
|
@ -296,19 +250,11 @@ mod test {
|
||||||
{
|
{
|
||||||
let ts: Series<BikeTrip> =
|
let ts: Series<BikeTrip> =
|
||||||
Series::open(&path).expect("expect the time series to open correctly");
|
Series::open(&path).expect("expect the time series to open correctly");
|
||||||
let v: Vec<(&RecordId, &BikeTrip)> = ts.search_sorted(
|
let v: Vec<(&UniqueId, &BikeTrip)> = ts.search_sorted(
|
||||||
time_range(
|
time_range(
|
||||||
Timestamp::DateTime(
|
DateTimeTz(UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0).unwrap()).into(),
|
||||||
UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0)
|
|
||||||
.unwrap()
|
|
||||||
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
|
|
||||||
),
|
|
||||||
true,
|
true,
|
||||||
Timestamp::DateTime(
|
DateTimeTz(UTC.with_ymd_and_hms(2011, 11, 05, 0, 0, 0).unwrap()).into(),
|
||||||
UTC.with_ymd_and_hms(2011, 11, 05, 0, 0, 0)
|
|
||||||
.unwrap()
|
|
||||||
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
|
|
||||||
),
|
|
||||||
true,
|
true,
|
||||||
),
|
),
|
||||||
|l, r| l.1.timestamp().cmp(&r.1.timestamp()),
|
|l, r| l.1.timestamp().cmp(&r.1.timestamp()),
|
||||||
|
@ -348,7 +294,7 @@ mod test {
|
||||||
Some(trip) => {
|
Some(trip) => {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
trip.datetime,
|
trip.datetime,
|
||||||
UTC.with_ymd_and_hms(2011, 11, 02, 0, 0, 0).unwrap()
|
DateTimeTz(UTC.with_ymd_and_hms(2011, 11, 02, 0, 0, 0).unwrap())
|
||||||
);
|
);
|
||||||
assert_eq!(trip.distance, Distance(50000.0 * M));
|
assert_eq!(trip.distance, Distance(50000.0 * M));
|
||||||
assert_eq!(trip.duration, Duration(7020.0 * S));
|
assert_eq!(trip.duration, Duration(7020.0 * S));
|
||||||
|
@ -384,22 +330,18 @@ mod test {
|
||||||
let ts: Series<BikeTrip> =
|
let ts: Series<BikeTrip> =
|
||||||
Series::open(&path).expect("expect the time series to open correctly");
|
Series::open(&path).expect("expect the time series to open correctly");
|
||||||
|
|
||||||
let trips: Vec<(&RecordId, &BikeTrip)> = ts.records().collect();
|
let trips: Vec<(&UniqueId, &BikeTrip)> = ts.records().collect();
|
||||||
assert_eq!(trips.len(), 3);
|
assert_eq!(trips.len(), 3);
|
||||||
|
|
||||||
let trips: Vec<(&RecordId, &BikeTrip)> = ts
|
let trips: Vec<(&UniqueId, &BikeTrip)> = ts
|
||||||
.search(exact_time(Timestamp::DateTime(
|
.search(exact_time(
|
||||||
UTC.with_ymd_and_hms(2011, 11, 02, 0, 0, 0)
|
DateTimeTz(UTC.with_ymd_and_hms(2011, 11, 02, 0, 0, 0).unwrap()).into(),
|
||||||
.unwrap()
|
))
|
||||||
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
|
|
||||||
)))
|
|
||||||
.collect();
|
.collect();
|
||||||
assert_eq!(trips.len(), 1);
|
assert_eq!(trips.len(), 1);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
trips[0].1.datetime,
|
trips[0].1.datetime,
|
||||||
UTC.with_ymd_and_hms(2011, 11, 02, 0, 0, 0)
|
DateTimeTz(UTC.with_ymd_and_hms(2011, 11, 02, 0, 0, 0).unwrap())
|
||||||
.unwrap()
|
|
||||||
.with_timezone(&FixedOffset::east_opt(0).unwrap())
|
|
||||||
);
|
);
|
||||||
assert_eq!(trips[0].1.distance, Distance(50000.0 * M));
|
assert_eq!(trips[0].1.distance, Distance(50000.0 * M));
|
||||||
assert_eq!(trips[0].1.duration, Duration(7020.0 * S));
|
assert_eq!(trips[0].1.duration, Duration(7020.0 * S));
|
||||||
|
@ -421,14 +363,14 @@ mod test {
|
||||||
ts.put(trips[2].clone()).expect("expect a successful put");
|
ts.put(trips[2].clone()).expect("expect a successful put");
|
||||||
ts.delete(&trip_id).expect("successful delete");
|
ts.delete(&trip_id).expect("successful delete");
|
||||||
|
|
||||||
let recs: Vec<(&RecordId, &BikeTrip)> = ts.records().collect();
|
let recs: Vec<(&UniqueId, &BikeTrip)> = ts.records().collect();
|
||||||
assert_eq!(recs.len(), 2);
|
assert_eq!(recs.len(), 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
let ts: Series<BikeTrip> =
|
let ts: Series<BikeTrip> =
|
||||||
Series::open(&path).expect("expect the time series to open correctly");
|
Series::open(&path).expect("expect the time series to open correctly");
|
||||||
let recs: Vec<(&RecordId, &BikeTrip)> = ts.records().collect();
|
let recs: Vec<(&UniqueId, &BikeTrip)> = ts.records().collect();
|
||||||
assert_eq!(recs.len(), 2);
|
assert_eq!(recs.len(), 2);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -445,7 +387,7 @@ mod test {
|
||||||
|
|
||||||
impl Recordable for WeightRecord {
|
impl Recordable for WeightRecord {
|
||||||
fn timestamp(&self) -> Timestamp {
|
fn timestamp(&self) -> Timestamp {
|
||||||
Timestamp::Date(self.date)
|
self.date.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tags(&self) -> Vec<String> {
|
fn tags(&self) -> Vec<String> {
|
||||||
|
|
Loading…
Reference in New Issue