forked from parseablehq/parseable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
402 lines (355 loc) · 12 KB
/
mod.rs
File metadata and controls
402 lines (355 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
* Parseable Server (C) 2022 - 2024 Parseable, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
use std::collections::HashMap;
use actix_web::http::header::ContentType;
use chrono::Utc;
use http::StatusCode;
use itertools::Itertools;
use relative_path::RelativePathBuf;
use serde::Serialize;
use tracing::error;
use crate::{
alerts::{get_alerts_info, AlertError, AlertsInfo, ALERTS},
correlation::{CorrelationError, CORRELATIONS},
event::format::LogSource,
handlers::http::{
cluster::fetch_daily_stats_from_ingestors,
logstream::{error::StreamError, get_stats_date},
},
parseable::PARSEABLE,
rbac::{map::SessionKey, role::Action, Users},
stats::Stats,
storage::{ObjectStorageError, ObjectStoreFormat, STREAM_ROOT_DIRECTORY},
users::{dashboards::DASHBOARDS, filters::FILTERS},
};
type StreamMetadataResponse = Result<(String, Vec<ObjectStoreFormat>, DataSetType), PrismHomeError>;
#[derive(Debug, Serialize, Default)]
struct StreamInfo {
// stream_count: u32,
// log_source_count: u32,
stats_summary: Stats,
}
#[derive(Debug, Serialize, Default)]
struct DatedStats {
date: String,
events: u64,
ingestion_size: u64,
storage_size: u64,
}
#[derive(Debug, Serialize)]
struct TitleAndId {
title: String,
id: String,
}
#[derive(Debug, Serialize)]
enum DataSetType {
Logs,
Metrics,
Traces,
}
#[derive(Debug, Serialize)]
struct DataSet {
title: String,
dataset_type: DataSetType,
}
#[derive(Debug, Serialize)]
pub struct HomeResponse {
alert_titles: Vec<TitleAndId>,
alerts_info: AlertsInfo,
correlation_titles: Vec<TitleAndId>,
stream_info: StreamInfo,
stats_details: Vec<DatedStats>,
stream_titles: Vec<String>,
datasets: Vec<DataSet>,
dashboard_titles: Vec<TitleAndId>,
filter_titles: Vec<TitleAndId>,
}
pub async fn generate_home_response(key: &SessionKey) -> Result<HomeResponse, PrismHomeError> {
// Execute these operations concurrently
let (
stream_titles_result,
alert_titles_result,
correlation_titles_result,
dashboards_result,
filters_result,
alerts_info_result,
) = tokio::join!(
get_stream_titles(key),
get_alert_titles(key),
get_correlation_titles(key),
get_dashboard_titles(),
get_filter_titles(key),
get_alerts_info()
);
let stream_titles = stream_titles_result?;
let alert_titles = alert_titles_result?;
let correlation_titles = correlation_titles_result?;
let dashboard_titles = dashboards_result?;
let filter_titles = filters_result?;
let alerts_info = alerts_info_result?;
// Generate dates for date-wise stats
let mut dates = (0..7)
.map(|i| {
Utc::now()
.checked_sub_signed(chrono::Duration::days(i))
.ok_or_else(|| anyhow::Error::msg("Date conversion failed"))
.unwrap()
})
.map(|date| date.format("%Y-%m-%d").to_string())
.collect_vec();
dates.reverse();
// Process stream metadata concurrently
let stream_metadata_futures = stream_titles
.iter()
.map(|stream| get_stream_metadata(stream.clone()));
let stream_metadata_results: Vec<StreamMetadataResponse> =
futures::future::join_all(stream_metadata_futures).await;
let mut stream_wise_stream_json = HashMap::new();
let mut datasets = Vec::new();
for result in stream_metadata_results {
match result {
Ok((stream, metadata, dataset_type)) => {
stream_wise_stream_json.insert(stream.clone(), metadata);
datasets.push(DataSet {
title: stream,
dataset_type,
});
}
Err(e) => {
error!("Failed to process stream metadata: {:?}", e);
// Continue with other streams instead of failing entirely
}
}
}
// Process stats for all dates concurrently
let stats_futures = dates
.iter()
.map(|date| stats_for_date(date.clone(), stream_wise_stream_json.clone()));
let stats_results: Vec<Result<DatedStats, PrismHomeError>> =
futures::future::join_all(stats_futures).await;
let mut stream_details = Vec::new();
let mut summary = StreamInfo::default();
for result in stats_results {
match result {
Ok(dated_stats) => {
summary.stats_summary.events += dated_stats.events;
summary.stats_summary.ingestion += dated_stats.ingestion_size;
summary.stats_summary.storage += dated_stats.storage_size;
stream_details.push(dated_stats);
}
Err(e) => {
error!("Failed to process stats for date: {:?}", e);
// Continue with other dates instead of failing entirely
}
}
}
Ok(HomeResponse {
stream_info: summary,
stats_details: stream_details,
stream_titles,
datasets,
alert_titles,
correlation_titles,
dashboard_titles,
filter_titles,
alerts_info,
})
}
// Helper functions to split the work
async fn get_stream_titles(key: &SessionKey) -> Result<Vec<String>, PrismHomeError> {
let stream_titles: Vec<String> = PARSEABLE
.storage
.get_object_store()
.list_streams()
.await
.map_err(|e| PrismHomeError::Anyhow(anyhow::Error::new(e)))?
.into_iter()
.filter(|logstream| {
Users.authorize(key.clone(), Action::ListStream, Some(logstream), None)
== crate::rbac::Response::Authorized
})
.sorted()
.collect_vec();
Ok(stream_titles)
}
async fn get_alert_titles(key: &SessionKey) -> Result<Vec<TitleAndId>, PrismHomeError> {
let alert_titles = ALERTS
.list_alerts_for_user(key.clone())
.await?
.iter()
.map(|alert| TitleAndId {
title: alert.title.clone(),
id: alert.id.to_string(),
})
.collect_vec();
Ok(alert_titles)
}
async fn get_correlation_titles(key: &SessionKey) -> Result<Vec<TitleAndId>, PrismHomeError> {
let correlation_titles = CORRELATIONS
.list_correlations(key)
.await?
.iter()
.map(|corr| TitleAndId {
title: corr.title.clone(),
id: corr.id.clone(),
})
.collect_vec();
Ok(correlation_titles)
}
async fn get_dashboard_titles() -> Result<Vec<TitleAndId>, PrismHomeError> {
let dashboard_titles = DASHBOARDS
.list_dashboards()
.await
.iter()
.map(|dashboard| TitleAndId {
title: dashboard.title.clone(),
id: dashboard.dashboard_id.as_ref().unwrap().to_string(),
})
.collect_vec();
Ok(dashboard_titles)
}
async fn get_filter_titles(key: &SessionKey) -> Result<Vec<TitleAndId>, PrismHomeError> {
let filter_titles = FILTERS
.list_filters(key)
.await
.iter()
.map(|filter| TitleAndId {
title: filter.filter_name.clone(),
id: filter
.filter_id
.as_ref()
.ok_or_else(|| anyhow::Error::msg("Filter ID is null"))
.unwrap()
.clone(),
})
.collect_vec();
Ok(filter_titles)
}
async fn get_stream_metadata(
stream: String,
) -> Result<(String, Vec<ObjectStoreFormat>, DataSetType), PrismHomeError> {
let path = RelativePathBuf::from_iter([&stream, STREAM_ROOT_DIRECTORY]);
let obs = PARSEABLE
.storage
.get_object_store()
.get_objects(
Some(&path),
Box::new(|file_name| file_name.ends_with("stream.json")),
)
.await?;
let mut stream_jsons = Vec::new();
for ob in obs {
let stream_metadata: ObjectStoreFormat = match serde_json::from_slice(&ob) {
Ok(d) => d,
Err(e) => {
error!("Failed to parse stream metadata: {:?}", e);
continue;
}
};
stream_jsons.push(stream_metadata);
}
if stream_jsons.is_empty() {
return Err(PrismHomeError::Anyhow(anyhow::Error::msg(
"No stream metadata found",
)));
}
// let log_source = &stream_jsons[0].clone().log_source;
let log_source_format = stream_jsons
.iter()
.find(|sj| !sj.log_source.is_empty())
.map(|sj| sj.log_source[0].log_source_format.clone())
.unwrap_or_default();
let dataset_type = match log_source_format {
LogSource::OtelMetrics => DataSetType::Metrics,
LogSource::OtelTraces => DataSetType::Traces,
_ => DataSetType::Logs,
};
Ok((stream, stream_jsons, dataset_type))
}
async fn stats_for_date(
date: String,
stream_wise_meta: HashMap<String, Vec<ObjectStoreFormat>>,
) -> Result<DatedStats, PrismHomeError> {
// Initialize result structure
let mut details = DatedStats {
date: date.clone(),
..Default::default()
};
// Process each stream concurrently
let stream_stats_futures = stream_wise_meta.iter().map(|(stream, meta)| {
get_stream_stats_for_date(stream.clone(), date.clone(), meta.clone())
});
let stream_stats_results = futures::future::join_all(stream_stats_futures).await;
// Aggregate results
for result in stream_stats_results {
match result {
Ok((events, ingestion, storage)) => {
details.events += events;
details.ingestion_size += ingestion;
details.storage_size += storage;
}
Err(e) => {
error!("Failed to get stats for stream: {:?}", e);
// Continue with other streams
}
}
}
Ok(details)
}
async fn get_stream_stats_for_date(
stream: String,
date: String,
meta: Vec<ObjectStoreFormat>,
) -> Result<(u64, u64, u64), PrismHomeError> {
let querier_stats = get_stats_date(&stream, &date).await?;
let ingestor_stats = fetch_daily_stats_from_ingestors(&date, &meta)?;
Ok((
querier_stats.events + ingestor_stats.events,
querier_stats.ingestion + ingestor_stats.ingestion,
querier_stats.storage + ingestor_stats.storage,
))
}
#[derive(Debug, thiserror::Error)]
pub enum PrismHomeError {
#[error("Error: {0}")]
Anyhow(#[from] anyhow::Error),
#[error("AlertError: {0}")]
AlertError(#[from] AlertError),
#[error("CorrelationError: {0}")]
CorrelationError(#[from] CorrelationError),
#[error("StreamError: {0}")]
StreamError(#[from] StreamError),
#[error("ObjectStorageError: {0}")]
ObjectStorageError(#[from] ObjectStorageError),
}
impl actix_web::ResponseError for PrismHomeError {
fn status_code(&self) -> http::StatusCode {
match self {
PrismHomeError::Anyhow(_) => StatusCode::INTERNAL_SERVER_ERROR,
PrismHomeError::AlertError(e) => e.status_code(),
PrismHomeError::CorrelationError(e) => e.status_code(),
PrismHomeError::StreamError(e) => e.status_code(),
PrismHomeError::ObjectStorageError(_) => StatusCode::INTERNAL_SERVER_ERROR,
}
}
fn error_response(&self) -> actix_web::HttpResponse<actix_web::body::BoxBody> {
actix_web::HttpResponse::build(self.status_code())
.insert_header(ContentType::plaintext())
.body(self.to_string())
}
}