Skip to content

Commit

Permalink
Filter scrapes by the day.
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkbyers committed Apr 11, 2024
1 parent c42fa77 commit ad9750e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/models/jobs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::models::utils::create_paginator;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Copy, Clone)]
#[derive(Serialize, Deserialize, Copy, Clone, Debug)]
pub enum JobType {
SMScrape = 0,
Embed = 1,
Expand Down Expand Up @@ -65,6 +65,7 @@ pub fn select_with_pagination(
create_paginator("jobs")(columns, q, sort_by, sort_direction, limit, offset)
}

// TODO: timestampz isn't easily comparable.
pub const INIT_TABLE: &str = r#"
CREATE TABLE IF NOT EXISTS jobs (
id uuid NOT NULL PRIMARY KEY,
Expand Down
37 changes: 35 additions & 2 deletions src/routes/index.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,48 @@
use actix_web::{get, web, HttpResponse};
use tera::{Context, Tera};

use crate::{models::sm_scrape, routes::scrapes};
use crate::{
models::{jobs, sm_scrape},
routes::scrapes,
};

#[get("/")]
async fn home(conn: web::Data<libsql::Connection>) -> HttpResponse {
let mut tera_context = Context::new();

let recent_scrape_job = jobs::select_with_pagination(
"created_at, completed_at",
&format!("job_type = {}", jobs::JobType::SMScrape.as_i32()),
"created_at",
"DESC",
1,
0,
);
println!("{}", recent_scrape_job);
let scrape_row = match conn.get_ref().query(&recent_scrape_job, ()).await {
Ok(mut row) => match row.next().await {
Ok(row) => row,
Err(e) => {
eprintln!("Error: {:?}", e);
return HttpResponse::InternalServerError().body("Error parsing db results");
}
},
Err(e) => {
eprintln!("Error: {}", e);
return HttpResponse::InternalServerError().body("Error querying the database");
}
};
let scrape_created_at: String = match scrape_row {
Some(scrape) => scrape.get::<String>(0).unwrap_or(String::new()),
None => String::new(),
};

let select_page = sm_scrape::select_with_pagination(
"id, url, arrival, lot_size, bag_size, score, packaging, cultivar_detail, spro_rec",
"score != ''",
&format!(
"score != '' AND strftime(scraped_at) > strftime('{}')",
scrape_created_at
),
"score",
"DESC",
200,
Expand Down

0 comments on commit ad9750e

Please sign in to comment.