Skip to content

Commit

Permalink
added top search queries api
Browse files Browse the repository at this point in the history
  • Loading branch information
rathijitpapon committed Apr 5, 2024
1 parent 4e494bf commit 48819fe
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions server/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ name = "server"
path = "src/main.rs"

[dependencies]


anyhow = { version = "1.0.81", features = ["backtrace"] }
async-trait = "0.1.78"
axum = { version = "0.7.4", features = ["macros"] }
Expand Down Expand Up @@ -51,3 +53,4 @@ tracing-subscriber = { version = "0.3.18", features = [
uuid = { version = "1.8.0", features = ["serde", "v4"] }
log = "0.4.21"
redis = { version = "0.25.2", features = ["tokio-comp", "json"] }
rand = "0.8.5"
1 change: 1 addition & 0 deletions server/config/dev.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
db = "postgresql://postgres:postgres@localhost/curieo"
cache = "redis://127.0.0.1/"
cache_max_sorted_size = 100
rag_api = "http://127.0.0.1:8000"
rag_api_username = "curieo"
rag_api_password = "curieo"
Expand Down
5 changes: 5 additions & 0 deletions server/src/search/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ use sqlx::types::time;
use sqlx::FromRow;
use std::fmt::Debug;

#[derive(Serialize, Deserialize, Debug)]
pub struct TopSearchRequest {
pub limit: Option<i64>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct RAGTokenResponse {
pub access_token: String,
Expand Down
48 changes: 46 additions & 2 deletions server/src/search/routes.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use crate::err::AppError;
use crate::search::services;
use crate::search::{SearchHistory, SearchHistoryRequest, SearchQueryRequest};
use crate::search::{SearchHistory, SearchHistoryRequest, SearchQueryRequest, TopSearchRequest};
use crate::settings::SETTINGS;
use crate::startup::AppState;
use axum::extract::{Query, State};
use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::routing::get;
use axum::{Json, Router};
use redis::Client as RedisClient;
use color_eyre::eyre::eyre;
use rand::Rng;
use redis::{AsyncCommands, Client as RedisClient};
use sqlx::PgPool;

#[tracing::instrument(level = "debug", skip_all, ret, err(Debug))]
Expand All @@ -33,6 +36,11 @@ async fn get_search_handler(
)
.await?;

connection
.zincr("search_queries", &search_query.query, 1)
.await
.map_err(|e| AppError::from(e))?;

Ok((StatusCode::OK, Json(search_response)))
}

Expand All @@ -57,8 +65,44 @@ async fn get_search_history_handler(
Ok((StatusCode::OK, Json(search_history)))
}

#[tracing::instrument(level = "debug", skip_all, ret, err(Debug))]
async fn get_top_searches_handler(
State(cache): State<RedisClient>,
Query(query): Query<TopSearchRequest>,
) -> crate::Result<impl IntoResponse> {
let mut connection = cache
.get_multiplexed_async_connection()
.await
.map_err(|e| AppError::from(e))?;

let random_number = rand::thread_rng().gen_range(0.0..1.0);
if random_number < 0.1 {
connection
.zremrangebyrank(
"search_history",
0,
-SETTINGS.cache_max_sorted_size as isize - 1,
)
.await
.map_err(|e| AppError::from(e))?;
}

let limit = query.limit.unwrap_or(10);
if limit < 1 || limit > 100 {
Err(eyre!("limit must be a number between 1 and 100"))?;
}

let top_searches: Vec<String> = connection
.zrevrange("search_queries", 0, limit as isize - 1)
.await
.map_err(|e| AppError::from(e))?;

Ok((StatusCode::OK, Json(top_searches)))
}

pub fn routes() -> Router<AppState> {
Router::new()
.route("/", get(get_search_handler))
.route("/history", get(get_search_history_handler))
.route("/top", get(get_top_searches_handler))
}
1 change: 1 addition & 0 deletions server/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub struct Settings {
pub rag_api: String,
pub rag_api_username: Secret<String>,
pub rag_api_password: Secret<String>,
pub cache_max_sorted_size: i64,
}

impl Settings {
Expand Down

0 comments on commit 48819fe

Please sign in to comment.