Skip to content

Commit

Permalink
refactor: cleaning resolve page + utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ShubhranshuSanjeev committed Oct 4, 2024
1 parent ac910ce commit c9549da
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 224 deletions.
61 changes: 54 additions & 7 deletions crates/frontend/src/components/context_form/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use std::fmt::Display;
use serde::{Deserialize, Serialize};
use serde_json::{json, Map, Value};

use crate::{schema::SchemaType, types::Context};
use crate::{
schema::{HtmlDisplay, SchemaType},
types::Context,
};
use derive_more::{Deref, DerefMut};

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand All @@ -12,6 +15,14 @@ pub enum Operand {
Dimension(serde_json::Value),
}

impl Operand {
pub fn inner(self) -> Value {
match self {
Operand::Value(v) | Operand::Dimension(v) => v,
}
}
}

impl Operand {
pub fn from_operand_json(value: Value) -> Self {
match value {
Expand Down Expand Up @@ -81,6 +92,16 @@ impl Operator {
other => Operator::Other(other.to_string()),
}
}

pub fn to_condition_json_operator(self) -> String {
match self {
Self::Has => "has".to_owned(),
Self::Is => "is".to_owned(),
Self::In => "in".to_owned(),
Self::Between => "between".to_owned(),
Self::Other(o) => o,
}
}
}

impl Display for Operator {
Expand Down Expand Up @@ -167,12 +188,7 @@ impl Condition {
}

pub fn to_condition_json(self) -> Value {
let operator = match self.operator {
Operator::In | Operator::Has => "in".to_owned(),
Operator::Is => "==".to_owned(),
Operator::Between => "<=".to_owned(),
Operator::Other(op) => op,
};
let operator = self.operator.to_condition_json_operator();

let operands = self
.operands
Expand All @@ -185,6 +201,25 @@ impl Condition {

json!({ operator: operands })
}

pub fn to_condition_query_str(self) -> String {
let operator = self.operator.to_condition_json_operator();
let dimension = self.dimension;

let value = self
.operands
.iter()
.filter_map(|operand| {
if let Operand::Value(v) = operand {
return Some(v.to_owned().html_display());
}
None
})
.collect::<Vec<String>>()
.join(",");

format!("{}{}{}", dimension, operator, value)
}
}

impl TryFrom<(Operator, String, SchemaType)> for Condition {
Expand Down Expand Up @@ -235,6 +270,18 @@ impl Conditions {
"and": self.iter().map(|v| Condition::to_condition_json(v.clone())).collect::<Vec<Value>>()
})
}
pub fn to_query_string(self) -> String {
self.iter()
.map(|condition| condition.clone().to_condition_query_str())
.collect::<Vec<String>>()
.join("&")
}
}

impl FromIterator<Condition> for Conditions {
fn from_iter<T: IntoIterator<Item = Condition>>(iter: T) -> Self {
Conditions(iter.into_iter().collect::<Vec<Condition>>())
}
}

impl TryFrom<&Context> for Conditions {
Expand Down
27 changes: 14 additions & 13 deletions crates/frontend/src/pages/context_override.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ use crate::api::fetch_config;
use crate::api::{delete_context, fetch_default_config, fetch_dimensions};
use crate::components::alert::AlertType;
use crate::components::button::Button;
use crate::components::condition_pills::types::{Condition, ConditionOperator};
use crate::components::condition_pills::utils::extract_conditions;
use crate::components::context_card::ContextCard;
use crate::components::context_form::types::Conditions;
use crate::components::context_form::types::{Condition, Conditions, Operator};
use crate::components::context_form::utils::{create_context, update_context};
use crate::components::context_form::ContextForm;
use crate::components::delete_modal::DeleteModal;
Expand All @@ -15,6 +13,7 @@ use crate::components::skeleton::{Skeleton, SkeletonVariant};
use crate::providers::alert_provider::enqueue_alert;
use crate::providers::condition_collapse_provider::ConditionCollapseProvider;
use crate::providers::editor_provider::EditorProvider;
use crate::schema::SchemaType;
use crate::types::{Config, Context, DefaultConfig, Dimension};
use futures::join;
use leptos::*;
Expand Down Expand Up @@ -164,18 +163,20 @@ pub fn context_override() -> impl IntoView {
let PageResource { dimensions, .. } = page_resource.get().unwrap_or_default();
let context_with_mandatory_dimensions = dimensions
.into_iter()
.filter_map(|dim| {
if dim.mandatory {
Some(Condition {
left_operand: dim.dimension,
operator: ConditionOperator::Other(String::from("")),
right_operand: vec![],
})
.filter_map(|v| {
let dimension_name = v.dimension;
let r#type = SchemaType::try_from(v.schema).unwrap();

if v.mandatory {
Some(
Condition::try_from((Operator::Is, dimension_name, r#type))
.unwrap(),
)
} else {
None
}
})
.collect::<Vec<Condition>>();
.collect::<Conditions>();
set_selected_data.set(Some(Data {
context: context_with_mandatory_dimensions,
overrides: vec![],
Expand All @@ -193,7 +194,7 @@ pub fn context_override() -> impl IntoView {
let handle_context_edit =
Callback::new(move |data: (Context, Map<String, Value>)| {
let (context, overrides) = data;
let conditions = extract_conditions(&context.condition);
let conditions = Conditions::from_context_json(context.condition).unwrap();

set_selected_data.set(Some(Data {
context: conditions,
Expand All @@ -207,7 +208,7 @@ pub fn context_override() -> impl IntoView {
let handle_context_clone =
Callback::new(move |data: (Context, Map<String, Value>)| {
let (context, overrides) = data;
let conditions = extract_conditions(&context.condition);
let conditions = Conditions::from_context_json(context.condition).unwrap();

set_selected_data.set(Some(Data {
context: conditions,
Expand Down
3 changes: 2 additions & 1 deletion crates/frontend/src/pages/experiment_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use leptos::*;
use chrono::{prelude::Utc, TimeZone};
use serde::{Deserialize, Serialize};

use crate::components::context_form::types::Conditions;
use crate::components::drawer::{close_drawer, Drawer, DrawerBtn};
use crate::components::skeleton::Skeleton;
use crate::components::table::types::TablePaginationProps;
Expand Down Expand Up @@ -228,7 +229,7 @@ pub fn experiment_list() -> impl IntoView {
<EditorProvider>
<ExperimentForm
name="".to_string()
context=vec![]
context=Conditions::default()
variants=vec![]
dimensions=dim.clone()
default_config=def_conf.clone()
Expand Down
51 changes: 7 additions & 44 deletions crates/frontend/src/pages/home.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::borrow::Cow;
use std::time::Duration;

use crate::components::condition_pills::{
types::{Condition, ConditionOperator},
Condition as ConditionComponent,
};
use crate::components::skeleton::{Skeleton, SkeletonVariant};
use crate::components::{
condition_pills::{types::Condition, Condition as ConditionComponent},
context_form::types::Conditions,
};
use crate::providers::condition_collapse_provider::ConditionCollapseProvider;
use crate::types::Config;
use crate::{
Expand Down Expand Up @@ -183,7 +182,7 @@ pub fn home() -> impl IntoView {
},
);

let (context_rs, context_ws) = create_signal::<Vec<Condition>>(vec![]);
let (context_rs, context_ws) = create_signal::<Conditions>(Conditions::default());
let (selected_tab_rs, selected_tab_ws) = create_signal(ResolveTab::AllConfig);
let (req_inprogess_rs, req_inprogress_ws) = create_signal(false);

Expand Down Expand Up @@ -241,42 +240,6 @@ pub fn home() -> impl IntoView {
}
};

let gen_query_context = |query: Vec<Condition>| -> String {
let mut context: Vec<String> = vec![];
for condition in query.iter() {
let dimension = condition.left_operand.clone();
let op = match condition.operator.clone() {
ConditionOperator::Is => Cow::Borrowed("="),
ConditionOperator::In => Cow::Borrowed("IN"),
ConditionOperator::Has => Cow::Borrowed("HAS"),
ConditionOperator::Between => Cow::Borrowed("BETWEEN"),
ConditionOperator::Other(op) => Cow::Owned(op),
};
let value = condition
.right_operand
.clone()
.into_iter()
.filter_map(|value| {
if value.is_object() && value.get("var").is_some() {
None
} else {
Some(value)
}
})
.map(|value| match value {
Value::String(s) => s.clone(),
Value::Number(n) => n.to_string(),
Value::Bool(b) => b.to_string(),
Value::Null => String::from("null"),
_ => format!("{}", value),
})
.collect::<Vec<String>>()
.join(",");
context.push(format!("{}{op}{}", dimension, value));
}
context.join("&").to_string()
};

let resolve_click = move |ev: MouseEvent| {
ev.prevent_default();
req_inprogress_ws.set(true);
Expand Down Expand Up @@ -304,7 +267,7 @@ pub fn home() -> impl IntoView {
let context_updated = context_rs.get();
// resolve the context and get the config that would apply
spawn_local(async move {
let context = gen_query_context(context_updated);
let context = context_updated.to_query_string();
let mut config = match resolve_config(tenant_rs.get_untracked(), context)
.await
.unwrap()
Expand Down Expand Up @@ -382,7 +345,7 @@ pub fn home() -> impl IntoView {

<ContextForm
dimensions=dimension.to_owned().unwrap_or_default()
context=vec![]
context=Conditions::default()
heading_sub_text="Query your configs".to_string()
dropdown_direction=DropdownDirection::Right
is_standalone=false
Expand Down
Loading

0 comments on commit c9549da

Please sign in to comment.