Skip to content

Commit

Permalink
feat(cache): Refactor resources endpoint and test pipeline env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
RouHim committed Nov 12, 2022
1 parent 5a7c853 commit 433af1d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 34 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/build-image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ env:

jobs:

set-image_tag:
name: Set container image tag
runs-on: ubuntu-latest
steps:
- name: Set image tag global
run: |
GIT_BRANCH=${GITHUB_BASE_REF:-${GITHUB_REF#refs/heads/}}
if [ $GIT_BRANCH = "main" ]; then export TEST_IMAGE_TAG=latest; else export TEST_IMAGE_TAG=next; fi
echo "TEST_IMAGE_TAG=$TEST_IMAGE_TAG"
# echo "TEST_IMAGE_TAG=1234" >> $GITHUB_ENV
env

check-oci-config:
name: Check Containerfile
runs-on: ubuntu-latest
Expand Down
10 changes: 7 additions & 3 deletions src/integration_test_resources_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};

use std::{env, fs};
use std::ops::Add;
use std::{env, fs};

use actix_web::dev::{ServiceFactory, ServiceRequest, ServiceResponse};
use actix_web::{test, web, App, Error};
Expand Down Expand Up @@ -72,14 +72,18 @@ async fn test_this_week_in_past_resources() {
TEST_JPEG_URL,
)
.await;
let another_date_string = Local::now().date().add(Duration::weeks(4)).format("%Y%m%d").to_string();
let another_date_string = Local::now()
.date()
.add(Duration::weeks(4))
.format("%Y%m%d")
.to_string();
let _ = create_test_image(
&base_test_dir,
"",
format!("IMG_{}.jpg", another_date_string).as_str(),
TEST_JPEG_URL,
)
.await;
.await;

// AND a running this-week-in-past instance
let app_server = test::init_service(build_app(base_test_dir.to_str().unwrap())).await;
Expand Down
18 changes: 9 additions & 9 deletions src/resource_endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ pub async fn get_all_resources(resource_store: web::Data<ResourceStore>) -> Http

#[get("week")]
pub async fn get_this_week_resources(resource_store: web::Data<ResourceStore>) -> HttpResponse {
let keys: Vec<String> = resource_processor::get_this_week_in_past(resource_store.as_ref());
let resource_ids: Vec<String> = resource_store
.as_ref()
.get_resource_this_week_visible_random();

HttpResponse::Ok()
.content_type("application/json")
.body(serde_json::to_string(&keys).unwrap())
.body(serde_json::to_string(&resource_ids).unwrap())
}

#[get("random")]
Expand Down Expand Up @@ -51,7 +53,7 @@ pub async fn get_resource_by_id_and_resolution(
let display_width = path_params.1;
let display_height = path_params.2;

// Check cache, if successful return early
// Check cache, if successful return it
let cached_data = resource_store
.get_ref()
.get_data_cache_entry(format!("{resource_id}_{display_width}_{display_height}"));
Expand All @@ -64,13 +66,10 @@ pub async fn get_resource_by_id_and_resolution(
// if not in cache, load resource metadata from database
let remote_resource: Option<RemoteResource> = resource_store
.get_resource(resource_id)
.and_then(|resource_json_string| {
serde_json::from_str(resource_json_string.as_str())
.ok()
});
// If we can't find the requested resource by id, get out here
.and_then(|resource_json_string| serde_json::from_str(resource_json_string.as_str()).ok());
// If we can't find the requested resource by id, return with an error
if remote_resource.is_none() {
return HttpResponse::InternalServerError().finish();
return HttpResponse::NotFound().finish();
}

// If we found the requested resource, read the image data and adjust the image to the display
Expand All @@ -88,6 +87,7 @@ pub async fn get_resource_by_id_and_resolution(
},
);

// If image adjustments were successful, return the data, otherwise return with error
if let Some(resource_data) = resource_data {
resource_store.get_ref().add_data_cache_entry(
format!("{resource_id}_{display_width}_{display_height}"),
Expand Down
18 changes: 0 additions & 18 deletions src/resource_processor.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
use std::env;

use rayon::iter::{IntoParallelRefIterator, ParallelIterator};

use crate::geo_location;
use crate::resource_reader::RemoteResource;
use crate::resource_store::ResourceStore;

/// Returns resources that was taken this week in the past
/// The resources are shuffled, to the result is not deterministic
/// Excluded are hidden resources
pub fn get_this_week_in_past(resource_store: &ResourceStore) -> Vec<String> {
resource_store
.get_all_visible_resource_random()
.par_iter()
.map(|resource_json_string| {
serde_json::from_str::<RemoteResource>(resource_json_string.as_str())
.unwrap_or_else(|_| panic!("Parsing of '{resource_json_string}' failed!"))
})
.filter(|resource| resource.is_this_week())
.map(|resource| resource.id)
.collect()
}

/// Builds the display value for the specified resource
/// The display value contains the date and location of a resource
pub async fn build_display_value(
Expand Down
19 changes: 15 additions & 4 deletions src/resource_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,22 @@ impl ResourceStore {
ids
}

/// Gets a list of all visible resources in a random order
/// Returns a list of resource data
pub fn get_all_visible_resource_random(&self) -> Vec<String> {
/// Gets a list of all visible resources this week in a random order
/// Returns a list of resource IDs
pub fn get_resource_this_week_visible_random(&self) -> Vec<String> {
let connection = self.persistent_file_store_pool.get().unwrap();
let mut stmt = connection.prepare("SELECT value FROM resources WHERE id NOT IN (SELECT id FROM hidden) ORDER BY RANDOM();").unwrap();
let mut stmt = connection
.prepare(
r#"SELECT DISTINCT resources.id
FROM resources,
json_each(resources.value) json
WHERE json.key = 'taken'
AND json.value NOT NULL
AND strftime('%W', json.value) = strftime('%W', 'now')
AND resources.id NOT IN (SELECT id FROM hidden)
ORDER BY RANDOM();"#,
)
.unwrap();
let mut rows = stmt.query([]).unwrap();
let mut ids: Vec<String> = Vec::new();
while let Ok(Some(row)) = rows.next() {
Expand Down

0 comments on commit 433af1d

Please sign in to comment.