Skip to content

Commit

Permalink
feat: add key filter to default config page and API
Browse files Browse the repository at this point in the history
  • Loading branch information
Datron committed Dec 16, 2024
1 parent 0eb9086 commit 42f0c76
Show file tree
Hide file tree
Showing 17 changed files with 246 additions and 297 deletions.
157 changes: 0 additions & 157 deletions crates/context_aware_config/samples/default_config.json

This file was deleted.

18 changes: 0 additions & 18 deletions crates/context_aware_config/samples/dimensions.json

This file was deleted.

42 changes: 0 additions & 42 deletions crates/context_aware_config/samples/overrides.json

This file was deleted.

47 changes: 31 additions & 16 deletions crates/context_aware_config/src/api/default_config/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use actix_web::{
use chrono::Utc;
use diesel::{
r2d2::{ConnectionManager, PooledConnection},
ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl,
ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl, TextExpressionMethods,
};
use diesel::{Connection, SelectableHelper};
use jsonschema::{Draft, JSONSchema, ValidationError};
Expand Down Expand Up @@ -42,10 +42,13 @@ use crate::{
helpers::add_config_version,
};

use super::types::CreateReq;
use super::types::{CreateReq, DefaultConfigFilters};

pub fn endpoints() -> Scope {
Scope::new("").service(create).service(get).service(delete)
Scope::new("")
.service(create)
.service(list_default_configs)
.service(delete)
}

#[put("/{key}")]
Expand Down Expand Up @@ -213,32 +216,44 @@ fn fetch_default_key(
}

#[get("")]
async fn get(
async fn list_default_configs(
db_conn: DbConnection,
filters: Query<PaginationParams>,
pagination: Query<PaginationParams>,
filters: Query<DefaultConfigFilters>,
) -> superposition::Result<Json<PaginatedResponse<DefaultConfig>>> {
let DbConnection(mut conn) = db_conn;

if let Some(true) = filters.all {
let result: Vec<DefaultConfig> = dsl::default_configs.get_results(&mut conn)?;
let filters = filters.into_inner();

let query_builder = |filters: &DefaultConfigFilters| {
let mut builder = dsl::default_configs.into_boxed();
if let Some(ref config_name) = filters.name {
builder = builder
.filter(schema::default_configs::key.like(format!["%{}%", config_name]));
}
builder
};

if let Some(true) = pagination.all {
let result: Vec<DefaultConfig> = query_builder(&filters).get_results(&mut conn)?;
return Ok(Json(PaginatedResponse {
total_pages: 1,
total_items: result.len() as i64,
data: result,
}));
}

let n_default_configs: i64 = dsl::default_configs.count().get_result(&mut conn)?;
let limit = filters.count.unwrap_or(10);
let mut builder = dsl::default_configs
.into_boxed()
.order(dsl::created_at.desc())
.limit(limit);
if let Some(page) = filters.page {
let base_query = query_builder(&filters);
let count_query = query_builder(&filters);

let n_default_configs: i64 = count_query.count().get_result(&mut conn)?;
let limit = pagination.count.unwrap_or(10);
let mut query = base_query.order(dsl::created_at.desc()).limit(limit);
if let Some(page) = pagination.page {
let offset = (page - 1) * limit;
builder = builder.offset(offset);
query = query.offset(offset);
}
let result: Vec<DefaultConfig> = builder.load(&mut conn)?;
let result: Vec<DefaultConfig> = query.load(&mut conn)?;
let total_pages = (n_default_configs as f64 / limit as f64).ceil() as i64;
Ok(Json(PaginatedResponse {
total_pages,
Expand Down
6 changes: 6 additions & 0 deletions crates/context_aware_config/src/api/default_config/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@ where
let value: Value = Deserialize::deserialize(deserializer)?;
Ok(Some(value))
}


#[derive(Deserialize, Debug)]
pub struct DefaultConfigFilters {
pub name: Option<String>,
}
28 changes: 19 additions & 9 deletions crates/frontend/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use superposition_types::Config;

use crate::{
types::{
ConfigVersionListResponse, DefaultConfig, Dimension, ExperimentListFilters,
ExperimentResponse, FetchTypeTemplateResponse, FunctionResponse, ListFilters,
PaginatedResponse,
ConfigVersionListResponse, DefaultConfig, DefaultConfigFilters, Dimension,
ExperimentListFilters, ExperimentResponse, FetchTypeTemplateResponse,
FunctionResponse, PaginatedResponse, PaginationFilters,
},
utils::{
construct_request_headers, get_host, parse_json_response, request,
Expand All @@ -15,7 +15,7 @@ use crate::{

// #[server(GetDimensions, "/fxn", "GetJson")]
pub async fn fetch_dimensions(
filters: &ListFilters,
filters: &PaginationFilters,
tenant: String,
) -> Result<PaginatedResponse<Dimension>, ServerFnError> {
let client = reqwest::Client::new();
Expand All @@ -37,13 +37,23 @@ pub async fn fetch_dimensions(

// #[server(GetDefaultConfig, "/fxn", "GetJson")]
pub async fn fetch_default_config(
filters: &ListFilters,
pagination_filters: &PaginationFilters,
filters: &DefaultConfigFilters,
tenant: String,
) -> Result<PaginatedResponse<DefaultConfig>, ServerFnError> {
let client = reqwest::Client::new();
let host = use_host_server();

let url = format!("{}/default-config?{}", host, filters.to_string());
let filters = filters.to_string();
let url = if filters.is_empty() {
format!("{}/default-config?{}", host, pagination_filters.to_string())
} else {
format!(
"{}/default-config?{}&{}",
host,
pagination_filters.to_string(),
filters
)
};
let response: PaginatedResponse<DefaultConfig> = client
.get(url)
.header("x-tenant", tenant)
Expand Down Expand Up @@ -109,7 +119,7 @@ pub async fn delete_context(

pub async fn fetch_experiments(
filters: &ExperimentListFilters,
pagination: &ListFilters,
pagination: &PaginationFilters,
tenant: String,
) -> Result<PaginatedResponse<ExperimentResponse>, ServerFnError> {
let client = reqwest::Client::new();
Expand Down Expand Up @@ -140,7 +150,7 @@ pub async fn fetch_experiments(
}

pub async fn fetch_functions(
filters: ListFilters,
filters: PaginationFilters,
tenant: String,
) -> Result<PaginatedResponse<FunctionResponse>, ServerFnError> {
let client = reqwest::Client::new();
Expand Down
4 changes: 2 additions & 2 deletions crates/frontend/src/components/default_config_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::components::alert::AlertType;
use crate::providers::alert_provider::enqueue_alert;
use crate::providers::editor_provider::EditorProvider;
use crate::schema::EnumVariants;
use crate::types::ListFilters;
use crate::types::PaginationFilters;
use crate::{
api::{fetch_functions, fetch_types},
components::{
Expand Down Expand Up @@ -51,7 +51,7 @@ where
move || tenant_rs.get(),
|current_tenant| async move {
match fetch_functions(
ListFilters {
PaginationFilters {
page: None,
count: None,
all: Some(true),
Expand Down
Loading

0 comments on commit 42f0c76

Please sign in to comment.