Skip to content

Commit

Permalink
Query and cache "table" table
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesaoverton committed Mar 21, 2024
1 parent 783ce24 commit 1a56572
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct Config {
pub asset_path: Option<String>,
pub template_path: Option<String>,
pub actions: IndexMap<String, ActionConfig>,
pub table: Vec<SerdeMap>,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialOrd, Ord, PartialEq, Eq)]
Expand Down Expand Up @@ -353,6 +354,7 @@ impl Config {
}
},
actions: user.actions.unwrap_or_default(),
table: vec![],
};

Ok(config)
Expand Down
27 changes: 10 additions & 17 deletions src/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// we can use the valve config (which is now available) instead of running db requests. But do
// this later.

use crate::config::Config;
use crate::config::{Config, SerdeMap};
use crate::error::GetError;
use crate::sql::{
get_count_from_pool, get_message_counts_from_pool, get_table_from_pool, get_total_from_pool,
Expand All @@ -17,19 +17,16 @@ use minijinja::{Environment, Source};
use ontodev_sqlrest::{Direction, OrderByColumn, Select};
use ontodev_valve::{
toolkit,
valve::{ValveChange, ValveColumnConfig, ValveMessage, ValveTableConfig},
valve::{ValveChange, ValveColumnConfig, ValveMessage},
};
use regex::Regex;
use serde_json::{json, to_string_pretty, Map, Value};
use std::collections::HashMap;
use std::fs;
use std::io::Write;
use std::path::Path;
use tabwriter::TabWriter;
use urlencoding::decode;

pub type SerdeMap = serde_json::Map<String, serde_json::Value>;

pub async fn get_table(
config: &Config,
table: &str,
Expand Down Expand Up @@ -128,7 +125,7 @@ pub async fn get_rows(
}
}
"page" => {
let page = match get_page(&config, &select, &table_config, &column_configs).await {
let page = match get_page(&config, &select, &column_configs).await {
Ok(page) => page,
Err(e) => return Err(GetError::new(e.to_string())),
};
Expand All @@ -152,7 +149,6 @@ pub async fn get_rows(
async fn get_page(
config: &Config,
select: &Select,
table_map: &HashMap<String, ValveTableConfig>,
column_configs: &Vec<ValveColumnConfig>,
) -> Result<Value, GetError> {
let table = &unquote(&select.table).unwrap();
Expand Down Expand Up @@ -403,16 +399,13 @@ async fn get_page(
}

let end = select.offset.unwrap_or(0) + cell_rows.len();

let mut this_table = json!(table_map.get(&unquoted_table).ok_or(GetError::new(format!(
"No '{}' in {:?}",
unquoted_table, table_map
)))?);
let this_table = this_table.as_object_mut().ok_or(GetError::new(format!(
"Could not parse table config for {} as a JSON object",
unquoted_table
)))?;

let mut this_table = config
.table
.iter()
.filter(|x| x.get("table").unwrap() == &json!(unquoted_table))
.next()
.unwrap()
.clone();
this_table.insert("table".to_string(), json!(unquoted_table.clone()));
this_table.insert("href".to_string(), json!(unquoted_table.clone()));
this_table.insert("start".to_string(), json!(select.offset.unwrap_or(0) + 1));
Expand Down
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{config::Config, error::NanobotError, serve::build_app};
use crate::{config::Config, error::NanobotError, serve::build_app, sql::get_table_from_pool};
use axum_test_helper::{TestClient, TestResponse};
use clap::{arg, command, value_parser, Command};
use ontodev_sqlrest::Select;
use ontodev_valve::valve::Valve;
use std::path::Path;
use std::sync::Arc;
Expand Down Expand Up @@ -158,6 +159,11 @@ async fn build_valve(config: &mut Config) -> Result<(), NanobotError> {
(config.valve, config.pool) = {
let valve = Valve::build(&config.valve_path, &config.connection).await?;
let pool = valve.pool.clone();
let table_select = Select::new("\"table\"");
config.table = get_table_from_pool(&pool, &table_select)
.await
.unwrap()
.clone();
(Some(valve), Some(pool))
};
Ok(())
Expand Down

0 comments on commit 1a56572

Please sign in to comment.