Skip to content

Commit

Permalink
modularized code of top search api
Browse files Browse the repository at this point in the history
  • Loading branch information
rathijitpapon committed Apr 5, 2024
1 parent 48819fe commit a1fa661
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
25 changes: 1 addition & 24 deletions server/src/search/routes.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
use crate::err::AppError;
use crate::search::services;
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 color_eyre::eyre::eyre;
use rand::Rng;
use redis::{AsyncCommands, Client as RedisClient};
use sqlx::PgPool;

Expand Down Expand Up @@ -75,27 +72,7 @@ async fn get_top_searches_handler(
.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))?;
let top_searches = services::get_top_searches(&mut connection, &query).await?;

Ok((StatusCode::OK, Json(top_searches)))
}
Expand Down
35 changes: 34 additions & 1 deletion server/src/search/services.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::err::AppError;
use crate::search::{RAGTokenResponse, SearchHistory, SearchQueryRequest, SearchResponse};
use crate::search::{
RAGTokenResponse, SearchHistory, SearchQueryRequest, SearchResponse, TopSearchRequest,
};
use crate::settings::SETTINGS;
use color_eyre::eyre::eyre;
use rand::Rng;
use redis::aio::MultiplexedConnection;
use redis::AsyncCommands;
use reqwest::Client as ReqwestClient;
Expand Down Expand Up @@ -86,3 +89,33 @@ pub async fn insert_search_history(

return Ok(search_history);
}

#[tracing::instrument(level = "debug", ret, err)]
pub async fn get_top_searches(
cache: &mut MultiplexedConnection,
top_search_request: &TopSearchRequest,
) -> crate::Result<Vec<String>> {
let random_number = rand::thread_rng().gen_range(0.0..1.0);
if random_number < 0.1 {
cache
.zremrangebyrank(
"search_history",
0,
-SETTINGS.cache_max_sorted_size as isize - 1,
)
.await
.map_err(|e| AppError::from(e))?;
}

let limit = top_search_request.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> = cache
.zrevrange("search_queries", 0, limit as isize - 1)
.await
.map_err(|e| AppError::from(e))?;

return Ok(top_searches);
}

0 comments on commit a1fa661

Please sign in to comment.