Skip to content

Commit 3ab14cf

Browse files
committed
Implement v3 reports endpoints
1 parent def1c70 commit 3ab14cf

6 files changed

Lines changed: 308 additions & 14 deletions

File tree

src/command/generate_reports.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub async fn run(mut conn: Connection) -> Result<()> {
2121
let today = OffsetDateTime::now_utc().date();
2222
info!(date = ?today, "Generating report");
2323

24-
let today_reports = Report::select_updated_since(&today.to_string(), None, &conn)?;
24+
let today_reports = Report::select_by_date(&today, None, &conn)?;
2525

2626
if !today_reports.is_empty() {
2727
info!("Found existing reports for today, aborting");

src/report/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub mod model;
22
pub mod v2;
33
pub use model::Report;
4+
pub mod v3;

src/report/model.rs

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rusqlite::Row;
88
use serde_json::Map;
99
use serde_json::Value;
1010
use std::sync::Arc;
11+
use time::format_description::well_known::Rfc3339;
1112
use time::macros::format_description;
1213
use time::Date;
1314
use time::OffsetDateTime;
@@ -61,10 +62,10 @@ impl ReportRepo {
6162

6263
pub async fn select_updated_since(
6364
&self,
64-
updated_since: &str,
65+
updated_since: &OffsetDateTime,
6566
limit: Option<i64>,
6667
) -> Result<Vec<Report>> {
67-
let updated_since = updated_since.to_string();
68+
let updated_since = updated_since.clone();
6869
self.pool
6970
.get()
7071
.await?
@@ -188,7 +189,7 @@ impl Report {
188189
}
189190

190191
pub fn select_updated_since(
191-
updated_since: &str,
192+
updated_since: &OffsetDateTime,
192193
limit: Option<i64>,
193194
conn: &Connection,
194195
) -> Result<Vec<Report>> {
@@ -212,7 +213,44 @@ impl Report {
212213
Ok(conn
213214
.prepare(query)?
214215
.query_map(
215-
named_params! { ":updated_since": updated_since, ":limit": limit.unwrap_or(i64::MAX) },
216+
named_params! {
217+
":updated_since": updated_since.format(&Rfc3339)?,
218+
":limit": limit.unwrap_or(i64::MAX),
219+
},
220+
mapper(),
221+
)?
222+
.collect::<Result<Vec<Report>, _>>()?)
223+
}
224+
225+
pub fn select_by_date(
226+
date: &Date,
227+
limit: Option<i64>,
228+
conn: &Connection,
229+
) -> Result<Vec<Report>> {
230+
let query = r#"
231+
SELECT
232+
r.rowid,
233+
r.area_id,
234+
json_extract(a.tags, '$.url_alias'),
235+
r.date,
236+
r.tags,
237+
r.created_at,
238+
r.updated_at,
239+
r.deleted_at
240+
FROM report r
241+
LEFT JOIN area a ON a.rowid = r.area_id
242+
WHERE r.date = :date
243+
ORDER BY r.updated_at, r.rowid
244+
LIMIT :limit
245+
"#;
246+
247+
Ok(conn
248+
.prepare(query)?
249+
.query_map(
250+
named_params! {
251+
":date": date.to_string(),
252+
":limit": limit.unwrap_or(i64::MAX),
253+
},
216254
mapper(),
217255
)?
218256
.collect::<Result<Vec<Report>, _>>()?)
@@ -384,7 +422,7 @@ mod test {
384422
.await?;
385423
let reports = state
386424
.report_repo
387-
.select_updated_since("2000-01-01", None)
425+
.select_updated_since(&datetime!(2000-01-01 00:00 UTC), None)
388426
.await?;
389427
assert_eq!(1, reports.len());
390428
Ok(())
@@ -410,7 +448,7 @@ mod test {
410448
.await?;
411449
let reports = state
412450
.report_repo
413-
.select_updated_since("2000-01-01", None)
451+
.select_updated_since(&datetime!(2000-01-01 00:00 UTC), None)
414452
.await?;
415453
assert_eq!(3, reports.len());
416454
Ok(())
@@ -450,7 +488,7 @@ mod test {
450488
2,
451489
state
452490
.report_repo
453-
.select_updated_since("2020-01-01T00:00:00Z", None)
491+
.select_updated_since(&datetime!(2020-01-01 00:00 UTC), None)
454492
.await?
455493
.len()
456494
);

src/report/v2.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ use time::OffsetDateTime;
1919

2020
#[derive(Deserialize)]
2121
pub struct GetArgs {
22-
updated_since: Option<String>,
22+
#[serde(default)]
23+
#[serde(with = "time::serde::rfc3339::option")]
24+
updated_since: Option<OffsetDateTime>,
2325
limit: Option<i64>,
2426
compress: Option<bool>,
2527
}
@@ -90,8 +92,6 @@ async fn get(
9092
.select_updated_since(
9193
&OffsetDateTime::now_utc()
9294
.checked_sub(Duration::days(7))
93-
.unwrap()
94-
.format(&Rfc3339)
9595
.unwrap(),
9696
args.limit,
9797
)
@@ -136,8 +136,6 @@ async fn get(
136136
.select_updated_since(
137137
&OffsetDateTime::now_utc()
138138
.checked_sub(Duration::days(7))
139-
.unwrap()
140-
.format(&Rfc3339)
141139
.unwrap(),
142140
args.limit,
143141
)
@@ -268,7 +266,7 @@ mod test {
268266
)
269267
.await;
270268
let req = TestRequest::get()
271-
.uri("/?updated_since=2022-01-10")
269+
.uri("/?updated_since=2022-01-10T00:00:00Z")
272270
.to_request();
273271
let res: Vec<GetItem> = test::call_and_read_body_json(&app, req).await;
274272
assert_eq!(res.len(), 1);

0 commit comments

Comments
 (0)