Add option for generating a mod search index for fuse.js

This commit is contained in:
Tyler Hallada 2022-01-24 23:20:51 -05:00
parent 4875d2d764
commit 240349cf1a
2 changed files with 64 additions and 0 deletions

View File

@ -6,6 +6,7 @@ use humansize::{file_size_opts, FileSize};
use models::file::File;
use models::game_mod::Mod;
use reqwest::StatusCode;
use serde::Serialize;
use sqlx::postgres::PgPoolOptions;
use std::collections::{HashMap, HashSet};
use std::env;
@ -55,6 +56,10 @@ struct Args {
/// folder to output all mod data as json files
#[argh(option, short = 'm')]
mod_data: Option<String>,
/// file to output all mod titles and ids as a json search index
#[argh(option, short = 's')]
mod_search_index: Option<String>,
}
async fn extract_with_compress_tools(
@ -267,6 +272,33 @@ pub async fn main() -> Result<()> {
return Ok(());
}
if let Some(mod_search_index) = args.mod_search_index {
#[derive(Serialize)]
struct ModForSearchIdTranslated {
name: String,
id: i32,
}
let mut search_index = vec![];
let page_size = 20;
let mut last_id = None;
loop {
let mods = game_mod::batched_get_for_search(&pool, page_size, last_id).await?;
if mods.is_empty() {
break;
}
for mod_for_search in mods {
search_index.push(ModForSearchIdTranslated {
name: mod_for_search.name,
id: mod_for_search.nexus_mod_id,
});
last_id = Some(mod_for_search.id);
}
}
let mut file = std::fs::File::create(mod_search_index)?;
write!(file, "{}", serde_json::to_string(&search_index)?)?;
return Ok(());
}
let mut page = args.page;
let mut has_next_page = true;

View File

@ -42,6 +42,13 @@ pub struct UnsavedMod<'a> {
pub first_upload_at: NaiveDateTime,
}
#[derive(Debug, Serialize, Deserialize, FromRow)]
pub struct ModForSearch {
pub id: i32,
pub name: String,
pub nexus_mod_id: i32,
}
#[derive(Debug, Serialize, Deserialize, FromRow)]
pub struct ModWithCells {
pub id: i32,
@ -318,6 +325,31 @@ pub async fn update_from_api_response<'a>(
Ok(ret)
}
#[instrument(level = "debug", skip(pool))]
pub async fn batched_get_for_search(
pool: &sqlx::Pool<sqlx::Postgres>,
page_size: i64,
last_id: Option<i32>,
) -> Result<Vec<ModForSearch>> {
let last_id = last_id.unwrap_or(0);
sqlx::query_as!(
ModForSearch,
"SELECT
id,
name,
nexus_mod_id
FROM mods
WHERE id > $2
ORDER BY mods.id ASC
LIMIT $1",
page_size,
last_id,
)
.fetch_all(pool)
.await
.context("Failed to batch get for search")
}
#[instrument(level = "debug", skip(pool))]
pub async fn batched_get_with_cells(
pool: &sqlx::Pool<sqlx::Postgres>,