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
use crate::grid::Grid;
use crate::hsref::Ref;
use crate::Error;
use chrono::{DateTime, NaiveDate, SecondsFormat};
use chrono_tz::Tz;
use url::Url;
pub trait HaystackRest {
fn about(&self) -> Result<Grid, Error>;
fn formats(&self) -> Result<Grid, Error>;
fn his_read(&self, id: &Ref, range: &HisReadRange) -> Result<Grid, Error>;
fn his_write_bool(
&self,
id: &Ref,
his_data: &[(DateTime<Tz>, bool)],
) -> Result<Grid, Error>;
fn his_write_str(
&self,
id: &Ref,
his_data: &[(DateTime<Tz>, String)],
) -> Result<Grid, Error>;
fn his_write_num(
&self,
id: &Ref,
his_data: &[(DateTime<Tz>, f64)],
unit: &str,
) -> Result<Grid, Error>;
fn nav(&self, nav_id: Option<&str>) -> Result<Grid, Error>;
fn ops(&self) -> Result<Grid, Error>;
fn read(&self, filter: &str, limit: Option<u64>) -> Result<Grid, Error>;
fn read_by_ids(&self, ids: &[Ref]) -> Result<Grid, Error>;
}
pub(crate) trait HaystackUrl {
fn about_url(&self) -> Url;
fn formats_url(&self) -> Url;
fn his_read_url(&self) -> Url;
fn his_write_url(&self) -> Url;
fn nav_url(&self) -> Url;
fn ops_url(&self) -> Url;
fn read_url(&self) -> Url;
}
pub trait SkySparkRest {
fn eval(&self, axon_expr: &str) -> Result<Grid, Error>;
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum HisReadRange {
Today,
Yesterday,
Date(NaiveDate),
DateSpan { start: NaiveDate, end: NaiveDate },
DateTimeSpan {
start: DateTime<Tz>,
end: DateTime<Tz>,
},
SinceDateTime { date_time: DateTime<Tz> },
}
const DATE_FMT: &str = "%Y-%m-%d";
impl HisReadRange {
pub(crate) fn to_string(&self) -> String {
match self {
Self::Today => "today".to_owned(),
Self::Yesterday => "yesterday".to_owned(),
Self::Date(date) => date.format(DATE_FMT).to_string(),
Self::DateSpan { start, end } => {
format!("{},{}", start.format(DATE_FMT), end.format(DATE_FMT))
}
Self::DateTimeSpan { start, end } => {
let start_str = to_zinc_encoded_string(&start);
let end_str = to_zinc_encoded_string(&end);
format!("{},{}", start_str, end_str)
}
Self::SinceDateTime { date_time } => {
to_zinc_encoded_string(&date_time)
}
}
}
}
pub(crate) fn to_zinc_encoded_string(date_time: &DateTime<Tz>) -> String {
let time_zone_name = *date_time
.timezone()
.name()
.split('/')
.collect::<Vec<_>>()
.last()
.expect("timezone name is always formatted as Area/Location");
format!(
"{} {}",
date_time.to_rfc3339_opts(SecondsFormat::Secs, true),
time_zone_name,
)
}