Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adjust random fetch limit #155

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ All configuration is done via environment variables:
| DATA_FOLDER | Path to a folder where the data should be stored, needs to read/write access | `/data` (Container only) | |
| PORT | Port on which the application should listen. | `8080` | |
| SLIDESHOW_INTERVAL | Interval of the slideshow in seconds | `30` | x |
| REFRESH_INTERVAL | Interval how often the page should be reloaded in minutes | `60` | |
| REFRESH_INTERVAL | Interval how often the page should be reloaded in minutes (triggers a new slideshow) | `60` | |
| DATE_FORMAT | Date format of the image taken date (https://docs.rs/chrono/0.4.19/chrono/format/strftime/index.html) | `%d.%m.%Y` | |
| BIGDATA_CLOUD_API_KEY | To resolve geo coordinates to city name. Obtain here: https://www.bigdatacloud.com | | |
| OPEN_WEATHER_MAP_API_KEY | To receive weather live data. Obtain here: https://openweathermap.org/api | | |
Expand Down
15 changes: 15 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use std::env;

pub fn get_slideshow_interval_value() -> usize {
env::var("SLIDESHOW_INTERVAL")
.unwrap_or_else(|_| "30".to_string())
.parse()
.unwrap_or(30)
}

pub fn get_refresh_interval_value() -> usize {
env::var("REFRESH_INTERVAL")
.unwrap_or_else(|_| "60".to_string())
.parse()
.unwrap_or(60)
}
5 changes: 3 additions & 2 deletions src/config_endpoint.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
use std::env;

use crate::config;
use actix_web::get;
use actix_web::HttpResponse;

#[get("interval/slideshow")]
pub async fn get_slideshow_interval() -> HttpResponse {
HttpResponse::Ok()
.content_type("plain/text")
.body(env::var("SLIDESHOW_INTERVAL").unwrap_or_else(|_| "30".to_string()))
.body(config::get_slideshow_interval_value().to_string())
}

#[get("interval/refresh")]
pub async fn get_refresh_interval() -> HttpResponse {
HttpResponse::Ok()
.content_type("plain/text")
.body(env::var("REFRESH_INTERVAL").unwrap_or_else(|_| "60".to_string()))
.body(config::get_refresh_interval_value().to_string())
}

#[get("show-hide-button")]
Expand Down
60 changes: 0 additions & 60 deletions src/integration_test_config_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,66 +11,6 @@ use crate::{config_endpoint, resource_reader, resource_store, scheduler};

const TEST_FOLDER_NAME: &str = "integration_test_config_api";

#[actix_web::test]
async fn test_get_slideshow_interval() {
// GIVEN is a running this-week-in-past instance
let base_test_dir = create_temp_folder().await;
let app_server = test::init_service(build_app(base_test_dir.to_str().unwrap())).await;

// AND slideshow interval is set
let slideshow_interval: String = rand::thread_rng().gen::<u16>().to_string();
env::set_var("SLIDESHOW_INTERVAL", &slideshow_interval);

// WHEN requesting slideshow interval
let response: String = String::from_utf8(
test::call_and_read_body(
&app_server,
test::TestRequest::get()
.uri("/api/config/interval/slideshow")
.to_request(),
)
.await
.to_vec(),
)
.unwrap();

// THEN the response should contain the correct interval
assert_that!(response).is_equal_to(&slideshow_interval);

// cleanup
cleanup(&base_test_dir).await;
}

#[actix_web::test]
async fn test_get_refresh_interval() {
// GIVEN is a running this-week-in-past instance
let base_test_dir = create_temp_folder().await;
let app_server = test::init_service(build_app(base_test_dir.to_str().unwrap())).await;

// AND refresh interval is set
let refresh_interval: String = rand::thread_rng().gen::<u16>().to_string();
env::set_var("REFRESH_INTERVAL", &refresh_interval);

// WHEN requesting refresh interval
let response: String = String::from_utf8(
test::call_and_read_body(
&app_server,
test::TestRequest::get()
.uri("/api/config/interval/refresh")
.to_request(),
)
.await
.to_vec(),
)
.unwrap();

// THEN the response should contain the correct interval
assert_that!(response).is_equal_to(&refresh_interval);

// cleanup
cleanup(&base_test_dir).await;
}

#[actix_web::test]
async fn test_get_random_slideshow() {
// GIVEN is a running this-week-in-past instance
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use actix_web::{middleware, web, App, HttpRequest, HttpResponse, HttpServer};
use env_logger::Builder;
use log::{info, warn, LevelFilter};

mod config;
mod config_endpoint;
mod exif_reader;
mod filesystem_client;
Expand Down
25 changes: 22 additions & 3 deletions src/resource_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;

use crate::config;
use chrono::Datelike;
use log::{debug, error};
use r2d2::{Pool, PooledConnection};
Expand Down Expand Up @@ -189,14 +190,32 @@ impl ResourceStore {
/// Returns random resources, non-hidden, resource id
pub fn get_random_resources(&self) -> Vec<String> {
let connection = self.persistent_file_store_pool.get().unwrap();
// Request limit is calculated by: (60/SLIDESHOW_INTERVAL)*REFRESH_INTERVAL * 10% buffer
let request_limit =
(60 / config::get_refresh_interval_value()) * config::get_refresh_interval_value();
let request_limit = (request_limit as f32 * 1.1) as usize;
// print all variables
println!("Refresh interval: {}", config::get_refresh_interval_value());
println!(
"Slideshow interval: {}",
config::get_slideshow_interval_value()
);
// print content of env vars: SLIDESHOW_INTERVAL, REFRESH_INTERVAL
println!(
"SLIDESHOW_INTERVAL: {:?}",
std::env::var("SLIDESHOW_INTERVAL")
);
println!("REFRESH_INTERVAL: {:?}", std::env::var("REFRESH_INTERVAL"));
println!("Request limit: {}", request_limit);
let mut stmt = connection
.prepare(
.prepare(&format!(
r#"
SELECT id FROM resources
WHERE id NOT IN (SELECT id FROM hidden)
ORDER BY RANDOM()
LIMIT 1000;"#,
)
LIMIT {};"#,
request_limit
))
.unwrap();
let mut rows = stmt.query([]).unwrap();
let mut ids: Vec<String> = Vec::new();
Expand Down
Loading