|
19 | 19 | use actix_web::http::StatusCode; |
20 | 20 | use actix_web::{web, HttpRequest, HttpResponse, Responder}; |
21 | 21 |
|
22 | | -use crate::metadata; |
| 22 | +use crate::alerts::Alerts; |
23 | 23 | use crate::response; |
24 | 24 | use crate::s3::S3; |
25 | 25 | use crate::storage::ObjectStorage; |
26 | | -use crate::validator; |
| 26 | +use crate::{metadata, validator}; |
27 | 27 |
|
28 | 28 | pub async fn delete(req: HttpRequest) -> HttpResponse { |
29 | 29 | let stream_name: String = req.match_info().get("logstream").unwrap().parse().unwrap(); |
@@ -116,26 +116,23 @@ pub async fn schema(req: HttpRequest) -> HttpResponse { |
116 | 116 | pub async fn get_alert(req: HttpRequest) -> HttpResponse { |
117 | 117 | let stream_name: String = req.match_info().get("logstream").unwrap().parse().unwrap(); |
118 | 118 |
|
119 | | - match metadata::STREAM_INFO.alert(&stream_name) { |
120 | | - Ok(alert) => response::ServerResponse { |
121 | | - msg: alert, |
| 119 | + match metadata::STREAM_INFO.alert(stream_name.clone()) { |
| 120 | + Ok(alerts) => response::ServerResponse { |
| 121 | + msg: serde_json::to_string(&alerts).unwrap(), |
122 | 122 | code: StatusCode::OK, |
123 | 123 | } |
124 | 124 | .to_http(), |
125 | | - Err(_) => match S3::new().get_alert(&stream_name).await { |
126 | | - Ok(alert) if alert.is_empty() => response::ServerResponse { |
| 125 | + Err(_) => match S3::new().get_alerts(&stream_name).await { |
| 126 | + Ok(alerts) if alerts.alerts.is_empty() => response::ServerResponse { |
127 | 127 | msg: "alert configuration not set for log stream {}".to_string(), |
128 | 128 | code: StatusCode::BAD_REQUEST, |
129 | 129 | } |
130 | 130 | .to_http(), |
131 | | - Ok(alert) => { |
132 | | - let buf = alert.as_ref(); |
133 | | - response::ServerResponse { |
134 | | - msg: String::from_utf8(buf.to_vec()).unwrap(), |
135 | | - code: StatusCode::OK, |
136 | | - } |
137 | | - .to_http() |
| 131 | + Ok(alerts) => response::ServerResponse { |
| 132 | + msg: serde_json::to_string(&alerts).unwrap(), |
| 133 | + code: StatusCode::OK, |
138 | 134 | } |
| 135 | + .to_http(), |
139 | 136 | Err(_) => response::ServerResponse { |
140 | 137 | msg: "alert doesn't exist".to_string(), |
141 | 138 | code: StatusCode::BAD_REQUEST, |
@@ -164,7 +161,7 @@ pub async fn put(req: HttpRequest) -> HttpResponse { |
164 | 161 | if let Err(e) = metadata::STREAM_INFO.add_stream( |
165 | 162 | stream_name.to_string(), |
166 | 163 | "".to_string(), |
167 | | - "".to_string(), |
| 164 | + Default::default(), |
168 | 165 | ) { |
169 | 166 | return response::ServerResponse { |
170 | 167 | msg: format!( |
@@ -208,47 +205,56 @@ pub async fn put(req: HttpRequest) -> HttpResponse { |
208 | 205 |
|
209 | 206 | pub async fn put_alert(req: HttpRequest, body: web::Json<serde_json::Value>) -> HttpResponse { |
210 | 207 | let stream_name: String = req.match_info().get("logstream").unwrap().parse().unwrap(); |
211 | | - let alert_config = body.clone(); |
212 | | - match validator::alert(serde_json::to_string(&body.as_object()).unwrap()) { |
213 | | - Ok(_) => match S3::new() |
214 | | - .create_alert(&stream_name, alert_config.to_string()) |
215 | | - .await |
216 | | - { |
217 | | - Ok(_) => { |
218 | | - if let Err(e) = |
219 | | - metadata::STREAM_INFO.set_alert(stream_name.clone(), alert_config.to_string()) |
220 | | - { |
221 | | - return response::ServerResponse { |
222 | | - msg: format!( |
223 | | - "failed to set alert configuration for log stream {} due to err: {}", |
224 | | - stream_name, e |
225 | | - ), |
226 | | - code: StatusCode::INTERNAL_SERVER_ERROR, |
227 | | - } |
228 | | - .to_http(); |
229 | | - } |
230 | | - response::ServerResponse { |
231 | | - msg: format!("set alert configuration for log stream {}", stream_name), |
232 | | - code: StatusCode::OK, |
233 | | - } |
234 | | - .to_http() |
235 | | - } |
236 | | - Err(e) => response::ServerResponse { |
| 208 | + let alerts: Alerts = match serde_json::from_value(body.into_inner()) { |
| 209 | + Ok(alerts) => alerts, |
| 210 | + Err(e) => { |
| 211 | + return response::ServerResponse { |
237 | 212 | msg: format!( |
238 | 213 | "failed to set alert configuration for log stream {} due to err: {}", |
239 | 214 | stream_name, e |
240 | 215 | ), |
241 | | - code: StatusCode::INTERNAL_SERVER_ERROR, |
| 216 | + code: StatusCode::BAD_REQUEST, |
242 | 217 | } |
243 | | - .to_http(), |
244 | | - }, |
245 | | - Err(e) => response::ServerResponse { |
| 218 | + .to_http() |
| 219 | + } |
| 220 | + }; |
| 221 | + |
| 222 | + if let Err(e) = validator::alert(serde_json::to_string(&alerts).unwrap()) { |
| 223 | + return response::ServerResponse { |
246 | 224 | msg: format!( |
247 | 225 | "failed to set alert configuration for log stream {} due to err: {}", |
248 | 226 | stream_name, e |
249 | 227 | ), |
250 | 228 | code: StatusCode::BAD_REQUEST, |
251 | 229 | } |
252 | | - .to_http(), |
| 230 | + .to_http(); |
| 231 | + } |
| 232 | + |
| 233 | + if let Err(e) = S3::new().put_alerts(&stream_name, alerts.clone()).await { |
| 234 | + return response::ServerResponse { |
| 235 | + msg: format!( |
| 236 | + "failed to set alert configuration for log stream {} due to err: {}", |
| 237 | + stream_name, e |
| 238 | + ), |
| 239 | + code: StatusCode::INTERNAL_SERVER_ERROR, |
| 240 | + } |
| 241 | + .to_http(); |
| 242 | + } |
| 243 | + |
| 244 | + if let Err(e) = metadata::STREAM_INFO.set_alert(stream_name.to_string(), alerts) { |
| 245 | + return response::ServerResponse { |
| 246 | + msg: format!( |
| 247 | + "failed to set alert configuration for log stream {} due to err: {}", |
| 248 | + stream_name, e |
| 249 | + ), |
| 250 | + code: StatusCode::INTERNAL_SERVER_ERROR, |
| 251 | + } |
| 252 | + .to_http(); |
| 253 | + } |
| 254 | + |
| 255 | + response::ServerResponse { |
| 256 | + msg: format!("set alert configuration for log stream {}", stream_name), |
| 257 | + code: StatusCode::OK, |
253 | 258 | } |
| 259 | + .to_http() |
254 | 260 | } |
0 commit comments