Skip to content

Commit afed517

Browse files
committed
restructured code
1 parent cc46126 commit afed517

File tree

11 files changed

+44
-93
lines changed

11 files changed

+44
-93
lines changed

src/handlers/http/alerts/alerts_utils.rs renamed to src/alerts/alerts_utils.rs

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use datafusion::{
2929
use tracing::trace;
3030

3131
use crate::{
32-
handlers::http::alerts::{AlertState, ALERTS},
3332
query::{TableScanVisitor, QUERY_SESSION},
3433
rbac::{
3534
map::SessionKey,
@@ -39,7 +38,9 @@ use crate::{
3938
utils::time::TimeRange,
4039
};
4140

42-
use super::{AlertConfig, AlertError, ThresholdConfig};
41+
use super::{
42+
Aggregate, AlertConfig, AlertError, AlertOperator, AlertState, ThresholdConfig, ALERTS,
43+
};
4344

4445
async fn get_tables_from_query(query: &str) -> Result<TableScanVisitor, AlertError> {
4546
let session_state = QUERY_SESSION.state();
@@ -152,48 +153,22 @@ fn get_exprs(thresholds: &Vec<ThresholdConfig>) -> (Vec<Expr>, Vec<Expr>, Expr)
152153
let mut expr = Expr::Literal(datafusion::scalar::ScalarValue::Boolean(Some(true)));
153154
for threshold in thresholds {
154155
let res = match threshold.operator {
155-
crate::handlers::http::alerts::AlertOperator::GreaterThan => {
156-
col(&threshold.column).gt(lit(threshold.value))
157-
}
158-
crate::handlers::http::alerts::AlertOperator::LessThan => {
159-
col(&threshold.column).lt(lit(threshold.value))
160-
}
161-
crate::handlers::http::alerts::AlertOperator::EqualTo => {
162-
col(&threshold.column).eq(lit(threshold.value))
163-
}
164-
crate::handlers::http::alerts::AlertOperator::NotEqualTo => {
165-
col(&threshold.column).not_eq(lit(threshold.value))
166-
}
167-
crate::handlers::http::alerts::AlertOperator::GreaterThanEqualTo => {
168-
col(&threshold.column).gt_eq(lit(threshold.value))
169-
}
170-
crate::handlers::http::alerts::AlertOperator::LessThanEqualTo => {
171-
col(&threshold.column).lt_eq(lit(threshold.value))
172-
}
173-
crate::handlers::http::alerts::AlertOperator::Like => {
174-
col(&threshold.column).like(lit(threshold.value))
175-
}
176-
crate::handlers::http::alerts::AlertOperator::NotLike => {
177-
col(&threshold.column).not_like(lit(threshold.value))
178-
}
156+
AlertOperator::GreaterThan => col(&threshold.column).gt(lit(threshold.value)),
157+
AlertOperator::LessThan => col(&threshold.column).lt(lit(threshold.value)),
158+
AlertOperator::EqualTo => col(&threshold.column).eq(lit(threshold.value)),
159+
AlertOperator::NotEqualTo => col(&threshold.column).not_eq(lit(threshold.value)),
160+
AlertOperator::GreaterThanEqualTo => col(&threshold.column).gt_eq(lit(threshold.value)),
161+
AlertOperator::LessThanEqualTo => col(&threshold.column).lt_eq(lit(threshold.value)),
162+
AlertOperator::Like => col(&threshold.column).like(lit(threshold.value)),
163+
AlertOperator::NotLike => col(&threshold.column).not_like(lit(threshold.value)),
179164
};
180165

181166
aggr_expr.push(match threshold.agg {
182-
crate::handlers::http::alerts::Aggregate::Avg => {
183-
avg(col(&threshold.column)).alias(&threshold.column)
184-
}
185-
crate::handlers::http::alerts::Aggregate::Count => {
186-
count(col(&threshold.column)).alias(&threshold.column)
187-
}
188-
crate::handlers::http::alerts::Aggregate::Min => {
189-
min(col(&threshold.column)).alias(&threshold.column)
190-
}
191-
crate::handlers::http::alerts::Aggregate::Max => {
192-
max(col(&threshold.column)).alias(&threshold.column)
193-
}
194-
crate::handlers::http::alerts::Aggregate::Sum => {
195-
sum(col(&threshold.column)).alias(&threshold.column)
196-
}
167+
Aggregate::Avg => avg(col(&threshold.column)).alias(&threshold.column),
168+
Aggregate::Count => count(col(&threshold.column)).alias(&threshold.column),
169+
Aggregate::Min => min(col(&threshold.column)).alias(&threshold.column),
170+
Aggregate::Max => max(col(&threshold.column)).alias(&threshold.column),
171+
Aggregate::Sum => sum(col(&threshold.column)).alias(&threshold.column),
197172
});
198173
expr = expr.and(res);
199174
}

src/handlers/http/alerts/mod.rs renamed to src/alerts/mod.rs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,11 @@ use tracing::{trace, warn};
3535
use ulid::Ulid;
3636

3737
pub mod alerts_utils;
38-
pub mod http_handlers;
3938
pub mod target;
4039

4140
use crate::option::CONFIG;
4241
use crate::rbac::map::SessionKey;
4342
use crate::storage;
44-
use crate::storage::object_storage::alert_json_path;
4543
use crate::storage::ObjectStorageError;
4644
use crate::sync::schedule_alert_task;
4745
use crate::utils::uid;
@@ -461,14 +459,12 @@ impl Alerts {
461459
trigger_notif: bool,
462460
) -> Result<(), AlertError> {
463461
let store = CONFIG.storage().get_object_store();
464-
// let alert_path = alert_json_path(alert_id);
465462

466-
// // read and modify alert
467-
// let mut alert: AlertConfig = serde_json::from_slice(&store.get_object(&alert_path).await?)?;
468-
// alert.state = new_state;
463+
// read and modify alert
469464
let mut alert = self.get_alert_by_id(alert_id).await?;
470465

471466
alert.state = new_state;
467+
472468
// save to disk
473469
store.put_alert(alert_id, &alert).await?;
474470

@@ -482,12 +478,6 @@ impl Alerts {
482478
};
483479
drop(writer);
484480

485-
// self.alerts.write().await.iter_mut().for_each(|alert| {
486-
// if alert.id.to_string() == alert_id {
487-
// alert.state = new_state;
488-
// }
489-
// });
490-
491481
if trigger_notif {
492482
alert.trigger_notifications().await?;
493483
}
@@ -497,17 +487,7 @@ impl Alerts {
497487

498488
/// Remove alert and scheduled task from disk and memory
499489
pub async fn delete(&self, alert_id: &str) -> Result<(), AlertError> {
500-
let store = CONFIG.storage().get_object_store();
501-
let alert_path = alert_json_path(alert_id);
502-
503-
// delete from disk
504-
store
505-
.delete_object(&alert_path)
506-
.await
507-
.map_err(AlertError::ObjectStorage)?;
508-
trace!("Deleted from disk");
509-
510-
// now delete from memory
490+
// delete from memory
511491
let read_access = self.alerts.read().await;
512492

513493
let index = read_access
File renamed without changes.
File renamed without changes.

src/handlers/http/alerts/target.rs renamed to src/alerts/target.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use humantime_serde::re::humantime;
3030
use reqwest::ClientBuilder;
3131
use tracing::{error, trace, warn};
3232

33-
use crate::handlers::http::alerts::ALERTS;
33+
use super::ALERTS;
3434

3535
use super::{AlertState, CallableTarget, Context};
3636

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use actix_web::{web, HttpRequest, Responder};
2626
use bytes::Bytes;
2727
use relative_path::RelativePathBuf;
2828

29-
use super::{
29+
use crate::alerts::{
3030
alerts_utils::user_auth_for_query, AlertConfig, AlertError, AlertRequest, AlertState, ALERTS,
3131
};
3232

@@ -95,6 +95,15 @@ pub async fn delete(req: HttpRequest) -> Result<impl Responder, AlertError> {
9595
// validate that the user has access to the tables mentioned
9696
user_auth_for_query(&session_key, &alert.query).await?;
9797

98+
let store = CONFIG.storage().get_object_store();
99+
let alert_path = alert_json_path(alert_id);
100+
101+
// delete from disk
102+
store
103+
.delete_object(&alert_path)
104+
.await
105+
.map_err(AlertError::ObjectStorage)?;
106+
98107
// delete from disk and memory
99108
ALERTS.delete(alert_id).await?;
100109

@@ -115,9 +124,11 @@ pub async fn modify(req: HttpRequest, alert: AlertRequest) -> Result<impl Respon
115124
.ok_or(AlertError::Metadata("No alert ID Provided"))?;
116125

117126
// check if alert id exists in map
118-
ALERTS.get_alert_by_id(alert_id).await?;
127+
let old_alert = ALERTS.get_alert_by_id(alert_id).await?;
119128

120129
// validate that the user has access to the tables mentioned
130+
// in the old as well as the modified alert
131+
user_auth_for_query(&session_key, &old_alert.query).await?;
121132
user_auth_for_query(&session_key, &alert.query).await?;
122133

123134
let store = CONFIG.storage().get_object_store();

src/handlers/http/modal/query_server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
*
1717
*/
1818

19+
use crate::alerts::ALERTS;
1920
use crate::handlers::airplane;
20-
use crate::handlers::http::alerts::ALERTS;
2121
use crate::handlers::http::base_path;
2222
use crate::handlers::http::caching_removed;
2323
use crate::handlers::http::cluster::{self, init_cluster_metrics_schedular};

src/handlers/http/modal/server.rs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
*
1717
*/
1818

19+
use crate::alerts::ALERTS;
1920
use crate::analytics;
2021
use crate::handlers;
2122
use crate::handlers::http::about;
2223
use crate::handlers::http::alerts;
23-
use crate::handlers::http::alerts::ALERTS;
2424
use crate::handlers::http::base_path;
2525
use crate::handlers::http::caching_removed;
2626
use crate::handlers::http::health_check;
@@ -188,39 +188,23 @@ impl Server {
188188
web::scope("/alerts")
189189
.service(
190190
web::resource("")
191-
.route(
192-
web::get()
193-
.to(alerts::http_handlers::list)
194-
.authorize(Action::GetAlert),
195-
)
196-
.route(
197-
web::post()
198-
.to(alerts::http_handlers::post)
199-
.authorize(Action::PutAlert),
200-
),
191+
.route(web::get().to(alerts::list).authorize(Action::GetAlert))
192+
.route(web::post().to(alerts::post).authorize(Action::PutAlert)),
201193
)
202194
.service(
203195
web::resource("/{alert_id}")
204-
.route(
205-
web::get()
206-
.to(alerts::http_handlers::get)
207-
.authorize(Action::GetAlert),
208-
)
209-
.route(
210-
web::put()
211-
.to(alerts::http_handlers::modify)
212-
.authorize(Action::PutAlert),
213-
)
196+
.route(web::get().to(alerts::get).authorize(Action::GetAlert))
197+
.route(web::put().to(alerts::modify).authorize(Action::PutAlert))
214198
.route(
215199
web::delete()
216-
.to(alerts::http_handlers::delete)
200+
.to(alerts::delete)
217201
.authorize(Action::DeleteAlert),
218202
),
219203
)
220204
.service(
221205
web::resource("/{alert_id}/update_state").route(
222206
web::put()
223-
.to(alerts::http_handlers::update_state)
207+
.to(alerts::update_state)
224208
.authorize(Action::PutAlert),
225209
),
226210
)

src/lib.rs

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

1919
mod about;
20+
pub mod alerts;
2021
pub mod analytics;
2122
pub mod banner;
2223
mod catalog;

src/storage/object_storage.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use super::{
2525
SCHEMA_FILE_NAME, STREAM_METADATA_FILE_NAME, STREAM_ROOT_DIRECTORY,
2626
};
2727

28-
use crate::handlers::http::alerts::AlertConfig;
28+
use crate::alerts::AlertConfig;
2929
use crate::handlers::http::modal::ingest_server::INGESTOR_META;
3030
use crate::handlers::http::users::{DASHBOARDS_DIR, FILTER_DIR, USERS_ROOT_DIR};
3131
use crate::metrics::{EVENTS_STORAGE_SIZE_DATE, LIFETIME_EVENTS_STORAGE_SIZE};

0 commit comments

Comments
 (0)