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
use super::Archive;
use crate::{
errors::BufkitDataErr,
models::Model,
site::{StateProv, StationNumber},
};
use chrono::FixedOffset;
use std::{collections::HashMap, str::FromStr};
pub struct StationSummary {
pub station_num: StationNumber,
pub ids: Vec<String>,
pub models: Vec<Model>,
pub name: Option<String>,
pub notes: Option<String>,
pub state: Option<StateProv>,
pub time_zone: Option<FixedOffset>,
pub auto_download: bool,
pub number_of_files: u32,
}
struct StationEntry {
station_num: StationNumber,
id: String,
model: Model,
name: Option<String>,
notes: Option<String>,
state: Option<StateProv>,
time_zone: Option<FixedOffset>,
auto_download: bool,
number_of_files: u32,
}
impl StationSummary {
pub fn ids_as_string(&self) -> String {
self.ids.join(", ")
}
pub fn models_as_string(&self) -> String {
self.models
.iter()
.map(|m| m.as_static_str().to_owned())
.collect::<Vec<_>>()
.join(", ")
}
}
impl From<StationEntry> for StationSummary {
fn from(entry: StationEntry) -> Self {
let StationEntry {
station_num,
id,
model,
name,
notes,
state,
time_zone,
auto_download,
number_of_files,
} = entry;
StationSummary {
station_num,
ids: vec![id],
models: vec![model],
name,
notes,
state,
time_zone,
auto_download,
number_of_files,
}
}
}
impl Archive {
pub fn station_summaries(&self) -> Result<Vec<StationSummary>, BufkitDataErr> {
let mut vals: HashMap<StationNumber, StationSummary> = HashMap::new();
let mut stmt = self.db_conn.prepare(include_str!("station_summary.sql"))?;
stmt.query_and_then(rusqlite::NO_PARAMS, Self::parse_row_to_entry)?
.for_each(|stn_entry| {
if let Ok(stn_entry) = stn_entry {
if let Some(summary) = vals.get_mut(&stn_entry.station_num) {
summary.ids.push(stn_entry.id);
summary.models.push(stn_entry.model);
summary.number_of_files += stn_entry.number_of_files;
} else {
vals.insert(stn_entry.station_num, StationSummary::from(stn_entry));
}
}
});
let mut vals: Vec<StationSummary> = vals.into_iter().map(|(_, v)| v).collect();
vals.iter_mut().for_each(|summary| {
summary.ids.sort_unstable();
summary.ids.dedup();
summary.models.sort_unstable();
summary.models.dedup();
});
Ok(vals)
}
fn parse_row_to_entry(row: &rusqlite::Row) -> Result<StationEntry, rusqlite::Error> {
let station_num: StationNumber = row.get::<_, u32>(0).map(StationNumber::from)?;
let id: String = row.get(1)?;
let model: Model = row.get::<_, String>(2).and_then(|a_string| {
Model::from_str(&a_string).map_err(|_| rusqlite::Error::InvalidQuery)
})?;
let name: Option<String> = row.get(3)?;
let state: Option<StateProv> = row
.get::<_, String>(4)
.ok()
.and_then(|a_string| StateProv::from_str(&a_string).ok());
let notes: Option<String> = row.get(5)?;
let time_zone: Option<chrono::FixedOffset> =
row.get::<_, i32>(6).ok().map(|offset: i32| {
if offset < 0 {
chrono::FixedOffset::west(offset.abs())
} else {
chrono::FixedOffset::east(offset)
}
});
let auto_download: bool = row.get(7)?;
let number_of_files: u32 = row.get(8)?;
Ok(StationEntry {
station_num,
id,
model,
name,
state,
notes,
time_zone,
auto_download,
number_of_files,
})
}
}