| 176 | } |
| 177 | |
| 178 | pub async fn get( |
| 179 | name: &str, |
| 180 | kind: Kind, |
| 181 | a: Aggregation, |
| 182 | start: DateTime<Local>, |
| 183 | end: DateTime<Local>, |
| 184 | ) -> Result<Vec<Record>> { |
| 185 | let mut keys: Vec<String> = Vec::new(); |
| 186 | let mut timestamps: Vec<NaiveDateTime> = Vec::new(); |
| 187 | |
| 188 | match a { |
| 189 | Aggregation::MINUTE => { |
| 190 | let mut ts = NaiveDate::from_ymd_opt(start.year(), start.month(), start.day()) |
| 191 | .ok_or_else(|| anyhow!("Invalid date"))? |
| 192 | .and_hms_opt(start.hour(), start.minute(), 0) |
| 193 | .ok_or_else(|| anyhow!("Invalid time"))?; |
| 194 | let end = NaiveDate::from_ymd_opt(end.year(), end.month(), end.day()) |
| 195 | .ok_or_else(|| anyhow!("Invalid date"))? |
| 196 | .and_hms_opt(end.hour(), end.minute(), 0) |
| 197 | .ok_or_else(|| anyhow!("Invalid time"))?; |
| 198 | |
| 199 | while ts.le(&end) { |
| 200 | timestamps.push(ts); |
| 201 | keys.push(get_key(name, a, ts)); |
| 202 | ts += ChronoDuration::minutes(1); |
| 203 | } |
| 204 | } |
| 205 | Aggregation::HOUR => { |
| 206 | let mut ts = NaiveDate::from_ymd_opt(start.year(), start.month(), start.day()) |
| 207 | .ok_or_else(|| anyhow!("Invalid date"))? |
| 208 | .and_hms_opt(start.hour(), 0, 0) |
| 209 | .ok_or_else(|| anyhow!("Invalid time"))?; |
| 210 | let end = NaiveDate::from_ymd_opt(end.year(), end.month(), end.day()) |
| 211 | .ok_or_else(|| anyhow!("Invalid date"))? |
| 212 | .and_hms_opt(end.hour(), 0, 0) |
| 213 | .ok_or_else(|| anyhow!("Invalid time"))?; |
| 214 | |
| 215 | while ts.le(&end) { |
| 216 | timestamps.push(ts); |
| 217 | keys.push(get_key(name, a, ts)); |
| 218 | ts += ChronoDuration::hours(1); |
| 219 | } |
| 220 | } |
| 221 | Aggregation::DAY => { |
| 222 | let mut ts = NaiveDate::from_ymd_opt(start.year(), start.month(), start.day()) |
| 223 | .ok_or_else(|| anyhow!("Invalid date"))? |
| 224 | .and_hms_opt(0, 0, 0) |
| 225 | .ok_or_else(|| anyhow!("Invalid time"))?; |
| 226 | let end = NaiveDate::from_ymd_opt(end.year(), end.month(), end.day()) |
| 227 | .ok_or_else(|| anyhow!("Invalid date"))? |
| 228 | .and_hms_opt(0, 0, 0) |
| 229 | .ok_or_else(|| anyhow!("Invalid time"))?; |
| 230 | |
| 231 | while ts.le(&end) { |
| 232 | timestamps.push(ts); |
| 233 | keys.push(get_key(name, a, ts)); |
| 234 | ts += ChronoDuration::days(1); |
| 235 | } |