fix price history missing entries

This commit is contained in:
Moritz Hölting 2025-12-17 20:02:10 +01:00
parent 83026cfcac
commit 6f4700e021
4 changed files with 21 additions and 12 deletions

View File

@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "SELECT date, canteen, price_students, price_employees, price_guests FROM meals WHERE LOWER(\"name\") = $1 AND is_latest = TRUE",
"query": "SELECT date, canteen, price_students, price_employees, price_guests FROM meals WHERE LOWER(\"name\") = $1 AND is_latest = TRUE ORDER BY date DESC LIMIT $2;",
"describe": {
"columns": [
{
@ -31,7 +31,8 @@
],
"parameters": {
"Left": [
"Text"
"Text",
"Int8"
]
},
"nullable": [
@ -42,5 +43,5 @@
false
]
},
"hash": "56b320a7188c5ee88c869e763f6d23ee37461894a3b20c850908923460c3d3a0"
"hash": "48944b01f74bc796d58c26eedf9025b641910dc475b48556855f10f59112e367"
}

View File

@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "SELECT date, price_students, price_employees, price_guests FROM meals WHERE canteen = $1 AND LOWER(\"name\") = $2 AND is_latest = TRUE",
"query": "SELECT date, price_students, price_employees, price_guests FROM meals WHERE canteen = $1 AND LOWER(\"name\") = $2 AND is_latest = TRUE ORDER BY date DESC LIMIT $3;",
"describe": {
"columns": [
{
@ -27,7 +27,8 @@
"parameters": {
"Left": [
"Text",
"Text"
"Text",
"Int8"
]
},
"nullable": [
@ -37,5 +38,5 @@
false
]
},
"hash": "1477fded8be083c2d5e29a92f9e558c9855ad90020dc1199bce0d20cca02799d"
"hash": "781e98dce280715896a347808d891ff02e2c609c161e9c935a76fa9d63e61696"
}

View File

@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "SELECT date, canteen, price_students, price_employees, price_guests FROM meals WHERE canteen = ANY($1) AND LOWER(\"name\") = $2 AND is_latest = TRUE",
"query": "SELECT date, canteen, price_students, price_employees, price_guests FROM meals WHERE canteen = ANY($1) AND LOWER(\"name\") = $2 AND is_latest = TRUE ORDER BY date DESC LIMIT $3;",
"describe": {
"columns": [
{
@ -32,7 +32,8 @@
"parameters": {
"Left": [
"TextArray",
"Text"
"Text",
"Int8"
]
},
"nullable": [
@ -43,5 +44,5 @@
false
]
},
"hash": "cdf76d5e7d5d61d3cc61411d526816996885ccbd07237847c57f62cc3b6357db"
"hash": "da9537a50cbe6a27836af42e7e333581826a3f87131f001f2db3ef39b88b746e"
}

View File

@ -22,6 +22,7 @@ pub fn configure(cfg: &mut ServiceConfig) {
#[serde(rename_all = "camelCase")]
struct PriceHistoryQuery {
canteens: Option<String>,
limit: Option<u32>,
}
#[derive(Debug, Clone, Deserialize, FromRow)]
@ -45,6 +46,7 @@ async fn price_history(
.as_deref()
.map(util::parse_canteens_comma_separated);
let dish_name = path.into_inner();
let limit = query.limit.unwrap_or(1000) as i64;
if let Some(canteens) = canteens {
if canteens.iter().all(Result::is_ok) {
@ -54,9 +56,10 @@ async fn price_history(
let canteen = canteens.into_iter().next().expect("length is 1");
let res = sqlx::query!(
r#"SELECT date, price_students, price_employees, price_guests FROM meals WHERE canteen = $1 AND LOWER("name") = $2 AND is_latest = TRUE"#,
r#"SELECT date, price_students, price_employees, price_guests FROM meals WHERE canteen = $1 AND LOWER("name") = $2 AND is_latest = TRUE ORDER BY date DESC LIMIT $3;"#,
canteen.get_identifier(),
dish_name.to_lowercase(),
limit,
)
.fetch_all(db)
.await;
@ -89,9 +92,10 @@ async fn price_history(
}
} else {
let res = sqlx::query_as!(PriceHistoryRow,
r#"SELECT date, canteen, price_students, price_employees, price_guests FROM meals WHERE canteen = ANY($1) AND LOWER("name") = $2 AND is_latest = TRUE"#,
r#"SELECT date, canteen, price_students, price_employees, price_guests FROM meals WHERE canteen = ANY($1) AND LOWER("name") = $2 AND is_latest = TRUE ORDER BY date DESC LIMIT $3;"#,
&canteens.iter().map(|c| c.get_identifier().to_string()).collect_vec(),
dish_name.to_lowercase(),
limit
)
.fetch_all(db)
.await;
@ -118,8 +122,9 @@ async fn price_history(
}
} else {
let res = sqlx::query_as!(PriceHistoryRow,
r#"SELECT date, canteen, price_students, price_employees, price_guests FROM meals WHERE LOWER("name") = $1 AND is_latest = TRUE"#,
r#"SELECT date, canteen, price_students, price_employees, price_guests FROM meals WHERE LOWER("name") = $1 AND is_latest = TRUE ORDER BY date DESC LIMIT $2;"#,
dish_name.to_lowercase(),
limit as i64,
)
.fetch_all(db)
.await;
@ -144,6 +149,7 @@ fn structure_multiple_canteens(
v: Vec<PriceHistoryRow>,
) -> BTreeMap<String, BTreeMap<NaiveDate, DishPrices>> {
v.into_iter()
.sorted_by_cached_key(|r| r.canteen.clone())
.chunk_by(|r| r.canteen.clone())
.into_iter()
.map(|(d, g)| {