Skip to content

Commit 4154bbc

Browse files
committed
refactor: Restructured the correlation module
1 parent 54d76b9 commit 4154bbc

File tree

4 files changed

+41
-21
lines changed

4 files changed

+41
-21
lines changed

src/correlation/mod.rs

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,14 @@ use once_cell::sync::Lazy;
2828
use serde::{Deserialize, Serialize};
2929
use serde_json::Error as SerdeError;
3030
use tokio::sync::RwLock;
31-
use tracing::{trace, warn};
31+
use tracing::{error, trace, warn};
3232

3333
use crate::{
3434
handlers::http::rbac::RBACError, option::CONFIG, query::QUERY_SESSION, rbac::map::SessionKey,
3535
storage::ObjectStorageError, users::filters::FilterQuery, utils::get_hash,
3636
};
3737

3838
pub mod correlation_utils;
39-
pub mod http_handlers;
4039

4140
pub static CORRELATIONS: Lazy<Correlation> = Lazy::new(Correlation::default);
4241

@@ -55,7 +54,13 @@ impl Correlation {
5554
continue;
5655
}
5756

58-
let correlation: CorrelationConfig = serde_json::from_slice(&corr)?;
57+
let correlation: CorrelationConfig = match serde_json::from_slice(&corr) {
58+
Ok(c) => c,
59+
Err(e) => {
60+
error!("Unable to load correlation- {e}");
61+
continue;
62+
},
63+
};
5964

6065
correlations.push(correlation);
6166
}
@@ -192,7 +197,12 @@ impl CorrelationRequest {
192197
let ctx = &QUERY_SESSION;
193198

194199
let h1: HashSet<&String> = self.table_configs.iter().map(|t| &t.table_name).collect();
195-
let h2 = HashSet::from([&self.join_config.table_one, &self.join_config.table_two]);
200+
let h2: HashSet<&String> = self
201+
.join_config
202+
.join_conditions
203+
.iter()
204+
.map(|j| &j.table_name)
205+
.collect();
196206

197207
// check if table config tables are the same
198208
if h1.len() != 2 {
@@ -223,13 +233,19 @@ impl CorrelationRequest {
223233
.iter()
224234
.map(|c| c.as_str())
225235
.collect_vec();
226-
let join_field = if table_config.table_name == self.join_config.table_one {
227-
&self.join_config.field_one
228-
} else {
229-
&self.join_config.field_two
230-
};
231236

232-
selected_fields.push(join_field.as_str());
237+
// unwrap because we have determined that the tables in table config are the same as those in join config
238+
let condition = self
239+
.join_config
240+
.join_conditions
241+
.iter()
242+
.find(|j| j.table_name == table_config.table_name)
243+
.unwrap();
244+
let join_field = condition.field.as_str();
245+
246+
if !selected_fields.contains(&join_field) {
247+
selected_fields.push(join_field);
248+
}
233249

234250
// if this errors out then the table config is incorrect or join config is incorrect
235251
df.select_columns(selected_fields.as_slice())?;
@@ -284,11 +300,15 @@ pub struct TableConfig {
284300
pub table_name: String,
285301
}
286302

303+
#[derive(Debug, Clone, Serialize, Deserialize)]
304+
#[serde(rename_all = "camelCase")]
305+
pub struct JoinCondition {
306+
pub table_name: String,
307+
pub field: String,
308+
}
309+
287310
#[derive(Debug, Clone, Serialize, Deserialize)]
288311
#[serde(rename_all = "camelCase")]
289312
pub struct JoinConfig {
290-
pub table_one: String,
291-
pub field_one: String,
292-
pub table_two: String,
293-
pub field_two: String,
313+
pub join_conditions: Vec<JoinCondition>,
294314
}

src/correlation/http_handlers.rs renamed to src/handlers/http/correlation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::{
2626
utils::actix::extract_session_key_from_req,
2727
};
2828

29-
use super::{
29+
use crate::correlation::{
3030
correlation_utils::user_auth_for_query, CorrelationConfig, CorrelationError,
3131
CorrelationRequest, CORRELATIONS,
3232
};

src/handlers/http/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use self::{cluster::get_ingestor_info, query::Query};
2929

3030
pub mod about;
3131
pub mod cluster;
32+
pub mod correlation;
3233
pub mod health_check;
3334
pub mod ingest;
3435
mod kinesis;

src/handlers/http/modal/server.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
*/
1818

1919
use crate::analytics;
20-
use crate::correlation;
2120
use crate::correlation::CORRELATIONS;
2221
use crate::handlers;
2322
use crate::handlers::http::about;
@@ -184,30 +183,30 @@ impl Server {
184183
web::resource("")
185184
.route(
186185
web::get()
187-
.to(correlation::http_handlers::list)
186+
.to(http::correlation::list)
188187
.authorize(Action::GetCorrelation),
189188
)
190189
.route(
191190
web::post()
192-
.to(correlation::http_handlers::post)
191+
.to(http::correlation::post)
193192
.authorize(Action::CreateCorrelation),
194193
),
195194
)
196195
.service(
197196
web::resource("/{correlation_id}")
198197
.route(
199198
web::get()
200-
.to(correlation::http_handlers::get)
199+
.to(http::correlation::get)
201200
.authorize(Action::GetCorrelation),
202201
)
203202
.route(
204203
web::put()
205-
.to(correlation::http_handlers::modify)
204+
.to(http::correlation::modify)
206205
.authorize(Action::PutCorrelation),
207206
)
208207
.route(
209208
web::delete()
210-
.to(correlation::http_handlers::delete)
209+
.to(http::correlation::delete)
211210
.authorize(Action::DeleteCorrelation),
212211
),
213212
)

0 commit comments

Comments
 (0)