Skip to content

Commit

Permalink
improved performance for oldest meal
Browse files Browse the repository at this point in the history
  • Loading branch information
worldofjoni committed Aug 20, 2023
1 parent f18f2d7 commit c5b592a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
4 changes: 1 addition & 3 deletions backend/src/layer/data/database/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ impl DataAccessFactory {
/// Returns a object for accessing database requests for api requests.
#[must_use]
pub fn get_request_data_access(&self) -> PersistentRequestData {
PersistentRequestData {
pool: self.pool.clone(),
}
PersistentRequestData::new(self.pool.clone())
}
}

Expand Down
52 changes: 34 additions & 18 deletions backend/src/layer/data/database/request.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use async_trait::async_trait;
use chrono::{Duration, Local};
use sqlx::{Pool, Postgres};
use tokio::sync::OnceCell;

use crate::{
interface::persistent_data::{
Expand All @@ -17,7 +18,17 @@ const MAX_WEEKS_DATA: i64 = 4;
/// Class implementing all database requests arising from graphql manipulations.
#[derive(Debug)]
pub struct PersistentRequestData {
pub(super) pool: Pool<Postgres>,
pool: Pool<Postgres>,
first_meal_date: OnceCell<Option<Date>>,
}

impl PersistentRequestData {
pub(super) fn new(pool: Pool<Postgres>) -> Self {
Self {
pool,
first_meal_date: OnceCell::new(),
}
}
}

#[async_trait]
Expand Down Expand Up @@ -115,10 +126,15 @@ impl RequestDataAccess for PersistentRequestData {
}

// If date is to far in the past, return `None`.
let first_date = sqlx::query_scalar!("SELECT MIN(serve_date) FROM food_plan")
.fetch_one(&self.pool)
.await?;
if first_date.map_or(true, |first_date| first_date > date) {
// Buffer value as it does not change to improve performance.
if self
.first_meal_date
.get_or_try_init(|| {
sqlx::query_scalar!("SELECT MIN(serve_date) FROM food_plan").fetch_one(&self.pool)
})
.await?
.map_or(true, |first_date| first_date > date)
{
return Ok(None);
}

Expand Down Expand Up @@ -302,7 +318,7 @@ mod tests {

#[sqlx::test(fixtures("canteen"))]
async fn test_get_canteen(pool: PgPool) {
let request = PersistentRequestData { pool };
let request = PersistentRequestData::new(pool);

let canteen_id_strs = [
"10728cc4-1e07-4e18-a9d9-ca45b9782413",
Expand All @@ -329,7 +345,7 @@ mod tests {

#[sqlx::test(fixtures("canteen"))]
async fn test_get_canteens(pool: PgPool) {
let request = PersistentRequestData { pool };
let request = PersistentRequestData::new(pool);

let canteen = request.get_canteens().await.unwrap();
assert!(canteen.len() == 3);
Expand All @@ -340,7 +356,7 @@ mod tests {

#[sqlx::test(fixtures("canteen", "line"))]
async fn test_get_line(pool: PgPool) {
let request = PersistentRequestData { pool };
let request = PersistentRequestData::new(pool);

let lines = request
.get_lines(Uuid::parse_str("10728cc4-1e07-4e18-a9d9-ca45b9782413").unwrap())
Expand All @@ -355,7 +371,7 @@ mod tests {

#[sqlx::test(fixtures("canteen", "line"))]
async fn test_get_lines(pool: PgPool) {
let request = PersistentRequestData { pool };
let request = PersistentRequestData::new(pool);

let line_id_strs = [
"3e8c11fa-906a-4c6a-bc71-28756c6b00ae",
Expand All @@ -378,7 +394,7 @@ mod tests {

#[sqlx::test(fixtures("canteen", "line", "meal", "food_plan"))]
async fn test_get_meal(pool: PgPool) {
let request = PersistentRequestData { pool };
let request = PersistentRequestData::new(pool);

let meal_id_strs = [
"f7337122-b018-48ad-b420-6202dc3cb4ff",
Expand Down Expand Up @@ -415,7 +431,7 @@ mod tests {

#[sqlx::test(fixtures("canteen", "line", "meal", "food_plan"))]
async fn test_get_meals(pool: PgPool) {
let request = PersistentRequestData { pool };
let request = PersistentRequestData::new(pool);

let line_id = Uuid::parse_str("3e8c11fa-906a-4c6a-bc71-28756c6b00ae").unwrap();

Expand Down Expand Up @@ -448,7 +464,7 @@ mod tests {

#[sqlx::test(fixtures("canteen", "line", "meal", "food_plan"))]
async fn test_get_sides(pool: PgPool) {
let request = PersistentRequestData { pool };
let request = PersistentRequestData::new(pool);
let date = Local::now().date_naive();
let line_id = Uuid::parse_str("3e8c11fa-906a-4c6a-bc71-28756c6b00ae").unwrap();

Expand All @@ -463,7 +479,7 @@ mod tests {

#[sqlx::test(fixtures("meal", "image"))]
async fn test_get_visible_images(pool: PgPool) {
let request = PersistentRequestData { pool };
let request = PersistentRequestData::new(pool);
let meal_id = Uuid::parse_str("f7337122-b018-48ad-b420-6202dc3cb4ff").unwrap();
let client_id = Uuid::parse_str("c51d2d81-3547-4f07-af58-ed613c6ece67").unwrap();

Expand Down Expand Up @@ -506,7 +522,7 @@ mod tests {

#[sqlx::test(fixtures("meal", "image", "rating"))]
async fn test_get_personal_rating(pool: PgPool) {
let request = PersistentRequestData { pool };
let request = PersistentRequestData::new(pool);
let meal_id = Uuid::parse_str("f7337122-b018-48ad-b420-6202dc3cb4ff").unwrap();
let client_id = Uuid::parse_str("c51d2d81-3547-4f07-af58-ed613c6ece67").unwrap();

Expand All @@ -524,7 +540,7 @@ mod tests {

#[sqlx::test(fixtures("meal", "image", "rating"))]
async fn test_get_personal_upvote(pool: PgPool) {
let request = PersistentRequestData { pool };
let request = PersistentRequestData::new(pool);
let image_id = Uuid::parse_str("76b904fe-d0f1-4122-8832-d0e21acab86d").unwrap();
let client_id = Uuid::parse_str("c51d2d81-3547-4f07-af58-ed613c6ece67").unwrap();

Expand All @@ -547,7 +563,7 @@ mod tests {

#[sqlx::test(fixtures("meal", "image", "rating"))]
async fn test_get_personal_downvote(pool: PgPool) {
let request = PersistentRequestData { pool };
let request = PersistentRequestData::new(pool);
let image_id = Uuid::parse_str("76b904fe-d0f1-4122-8832-d0e21acab86d").unwrap();
let client_id = Uuid::parse_str("00adb927-8cb9-4d80-ae01-d8f2e8f2d4cf").unwrap();

Expand All @@ -570,7 +586,7 @@ mod tests {

#[sqlx::test(fixtures("meal", "additive"))]
async fn test_get_additives(pool: PgPool) {
let request = PersistentRequestData { pool };
let request = PersistentRequestData::new(pool);
let food_ids = [
"f7337122-b018-48ad-b420-6202dc3cb4ff",
"73cf367b-a536-4b49-ad0c-cb984caa9a08",
Expand Down Expand Up @@ -602,7 +618,7 @@ mod tests {

#[sqlx::test(fixtures("meal", "allergen"))]
async fn test_get_allergens(pool: PgPool) {
let request = PersistentRequestData { pool };
let request = PersistentRequestData::new(pool);
let food_ids = [
"f7337122-b018-48ad-b420-6202dc3cb4ff",
"73cf367b-a536-4b49-ad0c-cb984caa9a08",
Expand Down

0 comments on commit c5b592a

Please sign in to comment.