Skip to content

Commit

Permalink
Configurable default number of results per page
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesaoverton committed Feb 12, 2024
1 parent 8a87194 commit b80518e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use toml;
pub struct Config {
pub config_version: u16,
pub port: u16,
pub results_per_page: u16,
pub logging_level: LoggingLevel,
pub connection: String,
pub pool: Option<AnyPool>,
Expand Down Expand Up @@ -122,13 +123,15 @@ impl TomlConfig {
pub struct NanobotConfig {
pub config_version: u16,
pub port: Option<u16>,
pub results_per_page: Option<u16>,
}

impl Default for NanobotConfig {
fn default() -> NanobotConfig {
NanobotConfig {
config_version: DEFAULT_CONFIG_VERSION,
port: Some(DEFAULT_PORT),
results_per_page: Some(DEFAULT_RESULTS_PER_PAGE),
}
}
}
Expand Down Expand Up @@ -280,6 +283,7 @@ pub type SerdeMap = serde_json::Map<String, SerdeValue>;

pub const DEFAULT_CONFIG_VERSION: u16 = 1;
pub const DEFAULT_PORT: u16 = 3000;
pub const DEFAULT_RESULTS_PER_PAGE: u16 = 20;
lazy_static! {
pub static ref DEFAULT_TOML: String =
format!("[nanobot]\nconfig_version = {}", DEFAULT_CONFIG_VERSION);
Expand All @@ -296,6 +300,10 @@ impl Config {
let config = Config {
config_version: user.nanobot.config_version,
port: user.nanobot.port.unwrap_or(DEFAULT_PORT),
results_per_page: user
.nanobot
.results_per_page
.unwrap_or(DEFAULT_RESULTS_PER_PAGE),
logging_level: user.logging.unwrap_or_default().level.unwrap_or_default(),
connection: user
.database
Expand Down Expand Up @@ -381,6 +389,7 @@ pub fn to_toml(config: &Config) -> TomlConfig {
nanobot: NanobotConfig {
config_version: config.config_version.clone(),
port: Some(config.port.clone()),
results_per_page: Some(config.results_per_page.clone()),
},
logging: Some(LoggingConfig {
level: Some(config.logging_level.clone()),
Expand Down
6 changes: 3 additions & 3 deletions src/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::config::Config;
use crate::error::GetError;
use crate::sql::{
get_count_from_pool, get_message_counts_from_pool, get_table_from_pool, get_total_from_pool,
LIMIT_DEFAULT, LIMIT_MAX,
LIMIT_MAX,
};
use chrono::prelude::{DateTime, Utc};
use csv::WriterBuilder;
Expand Down Expand Up @@ -55,7 +55,7 @@ pub async fn get_table(
) -> Result<String, GetError> {
let table = unquote(table).unwrap_or(table.to_string());
let mut select = Select::new(format!("\"{}\"", table));
select.limit(LIMIT_DEFAULT);
select.limit(usize::from(config.results_per_page));
get_rows(config, &select, shape, format).await
}

Expand Down Expand Up @@ -123,7 +123,7 @@ pub async fn get_rows(
match select.limit {
Some(l) if l > LIMIT_MAX => select.limit(LIMIT_MAX),
Some(l) if l > 0 => select.limit(l),
_ => select.limit(LIMIT_DEFAULT),
_ => select.limit(usize::from(config.results_per_page)),
};

match shape {
Expand Down
6 changes: 3 additions & 3 deletions src/serve.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{config::Config, get, ldtab, sql::LIMIT_DEFAULT, tree_view};
use crate::{config::Config, get, ldtab, tree_view};
use ansi_to_html;
use axum::{
extract::{Form, Path, Query, State},
Expand Down Expand Up @@ -859,8 +859,8 @@ async fn table(
Ok(n) => n,
Err(e) => return Err(e.to_string().into()),
};
let pages = row_number / LIMIT_DEFAULT as u32;
pages * LIMIT_DEFAULT as u32
let pages = row_number / state.config.results_per_page as u32;
pages * state.config.results_per_page as u32
};
let html = format!(
r#"<script>
Expand Down
1 change: 0 additions & 1 deletion src/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use std::collections::HashMap;
use std::error::Error;

pub const LIMIT_MAX: usize = 10000;
pub const LIMIT_DEFAULT: usize = 20; // TODO: 100?

// TODO: Possibly delete this function since it seems like it is superseded by the Valve API?
pub async fn save_table(
Expand Down

0 comments on commit b80518e

Please sign in to comment.