16
16
*
17
17
*/
18
18
19
-
20
19
use actix_web:: http:: header:: ContentType ;
21
- use chrono:: Local ;
20
+ use chrono:: Utc ;
22
21
use http:: StatusCode ;
23
22
use itertools:: Itertools ;
24
23
use serde:: Serialize ;
25
24
26
- use crate :: { alerts:: { get_alerts_info, AlertError , AlertsInfo , ALERTS } , correlation:: { CorrelationError , CORRELATIONS } , handlers:: http:: logstream:: { error:: StreamError , get_stats_date} , parseable:: PARSEABLE , rbac:: { map:: SessionKey , role:: Action , Users } , stats:: Stats , users:: { dashboards:: DASHBOARDS , filters:: FILTERS } } ;
25
+ use crate :: {
26
+ alerts:: { get_alerts_info, AlertError , AlertsInfo , ALERTS } ,
27
+ correlation:: { CorrelationError , CORRELATIONS } ,
28
+ handlers:: http:: logstream:: { error:: StreamError , get_stats_date} ,
29
+ parseable:: PARSEABLE ,
30
+ rbac:: { map:: SessionKey , role:: Action , Users } ,
31
+ stats:: Stats ,
32
+ users:: { dashboards:: DASHBOARDS , filters:: FILTERS } ,
33
+ } ;
27
34
28
35
#[ derive( Debug , Serialize , Default ) ]
29
36
struct StreamInfo {
@@ -43,7 +50,7 @@ struct DatedStats {
43
50
#[ derive( Debug , Serialize ) ]
44
51
struct TitleAndId {
45
52
title : String ,
46
- id : String
53
+ id : String ,
47
54
}
48
55
49
56
#[ derive( Debug , Serialize ) ]
@@ -54,77 +61,82 @@ pub struct HomeResponse {
54
61
stream_info : StreamInfo ,
55
62
stats_details : Vec < DatedStats > ,
56
63
stream_titles : Vec < String > ,
57
-
58
-
64
+
59
65
dashboard_titles : Vec < TitleAndId > ,
60
66
filter_titles : Vec < TitleAndId > ,
61
-
62
67
}
63
68
64
69
pub async fn generate_home_response ( key : & SessionKey ) -> Result < HomeResponse , HomeError > {
65
-
66
70
let user_id = if let Some ( user_id) = Users . get_username_from_session ( key) {
67
71
user_id
68
72
} else {
69
73
return Err ( HomeError :: Anyhow ( anyhow:: Error :: msg ( "User does not exist" ) ) ) ;
70
74
} ;
71
75
72
76
// get all stream titles
73
- let stream_titles = PARSEABLE . streams
77
+ let stream_titles: Vec < String > = PARSEABLE
78
+ . streams
74
79
. list ( )
75
- . iter ( )
80
+ . into_iter ( )
76
81
. filter ( |logstream| {
77
- Users . authorize ( key. clone ( ) , Action :: ListStream , Some ( & logstream) , None ) == crate :: rbac:: Response :: Authorized
82
+ Users . authorize ( key. clone ( ) , Action :: ListStream , Some ( logstream) , None )
83
+ == crate :: rbac:: Response :: Authorized
78
84
} )
79
- . map ( |logstream| logstream. clone ( ) )
80
- . collect_vec ( ) ;
85
+ . collect ( ) ;
81
86
82
- // get all alert titles (TODO: RBAC)
87
+ // get all alert IDs (TODO: RBAC)
83
88
// do we need to move alerts into the PARSEABLE struct?
84
89
let alert_titles = ALERTS
85
90
. list_alerts_for_user ( key. clone ( ) )
86
91
. await ?
87
92
. iter ( )
88
93
. map ( |alert| TitleAndId {
89
94
title : alert. title . clone ( ) ,
90
- id : alert. id . to_string ( )
95
+ id : alert. id . to_string ( ) ,
91
96
} )
92
97
. collect_vec ( ) ;
93
98
99
+ // get correlation IDs
94
100
let correlation_titles = CORRELATIONS
95
101
. list_correlations ( key)
96
102
. await ?
97
103
. iter ( )
98
104
. map ( |corr| TitleAndId {
99
105
title : corr. title . clone ( ) ,
100
- id : corr. id . clone ( )
106
+ id : corr. id . clone ( ) ,
101
107
} )
102
108
. collect_vec ( ) ;
103
109
110
+ // get dashboard IDs
104
111
let dashboard_titles = DASHBOARDS
105
112
. list_dashboards_by_user ( & user_id)
106
113
. iter ( )
107
114
. map ( |dashboard| TitleAndId {
108
115
title : dashboard. name . clone ( ) ,
109
- id : dashboard. dashboard_id . as_ref ( ) . unwrap ( ) . clone ( )
116
+ id : dashboard. dashboard_id . as_ref ( ) . unwrap ( ) . clone ( ) ,
110
117
} )
111
118
. collect_vec ( ) ;
112
119
120
+ // get filter IDs
113
121
let filter_titles = FILTERS
114
122
. list_filters_by_user ( & user_id)
115
123
. iter ( )
116
- . map ( |filter| {
117
- TitleAndId {
118
- title : filter. filter_name . clone ( ) ,
119
- id : filter. filter_id . as_ref ( ) . unwrap ( ) . clone ( )
120
- }
124
+ . map ( |filter| TitleAndId {
125
+ title : filter. filter_name . clone ( ) ,
126
+ id : filter. filter_id . as_ref ( ) . unwrap ( ) . clone ( ) ,
121
127
} )
122
128
. collect_vec ( ) ;
123
-
129
+
130
+ // get alerts info (distribution of alerts based on severity and state)
124
131
let alerts_info = get_alerts_info ( ) . await ?;
125
132
133
+ // generate dates for date-wise stats
126
134
let dates = ( 0 ..7 )
127
- . map ( |i| Local :: now ( ) . checked_sub_signed ( chrono:: Duration :: days ( i) ) . unwrap ( ) )
135
+ . map ( |i| {
136
+ Utc :: now ( )
137
+ . checked_sub_signed ( chrono:: Duration :: days ( i) )
138
+ . unwrap ( )
139
+ } )
128
140
. map ( |date| date. format ( "%Y-%m-%d" ) . to_string ( ) )
129
141
. collect_vec ( ) ;
130
142
@@ -133,17 +145,20 @@ pub async fn generate_home_response(key: &SessionKey) -> Result<HomeResponse, Ho
133
145
let mut summary = StreamInfo :: default ( ) ;
134
146
135
147
for date in dates. iter ( ) {
136
- let mut details = DatedStats :: default ( ) ;
137
- details. date = date. clone ( ) ;
148
+ let mut details = DatedStats {
149
+ date : date. clone ( ) ,
150
+ ..Default :: default ( )
151
+ } ;
138
152
139
153
for stream in stream_titles. iter ( ) {
140
- let stats = get_stats_date ( stream, & date)
141
- . await ?;
154
+ let stats = get_stats_date ( stream, date) . await ?;
142
155
156
+ // collect date-wise stats for all streams
143
157
details. events += stats. events ;
144
158
details. ingestion_size += stats. ingestion ;
145
159
details. storage_size += stats. storage ;
146
160
161
+ // collect all 7-day stats for all streams
147
162
summary. stats_summary . events += stats. events ;
148
163
summary. stats_summary . ingestion += stats. ingestion ;
149
164
summary. stats_summary . storage += stats. storage ;
@@ -160,7 +175,7 @@ pub async fn generate_home_response(key: &SessionKey) -> Result<HomeResponse, Ho
160
175
correlation_titles,
161
176
dashboard_titles,
162
177
filter_titles,
163
- alerts_info
178
+ alerts_info,
164
179
} )
165
180
}
166
181
@@ -173,7 +188,7 @@ pub enum HomeError {
173
188
#[ error( "CorrelationError: {0}" ) ]
174
189
CorrelationError ( #[ from] CorrelationError ) ,
175
190
#[ error( "StreamError: {0}" ) ]
176
- StreamError ( #[ from] StreamError )
191
+ StreamError ( #[ from] StreamError ) ,
177
192
}
178
193
179
194
impl actix_web:: ResponseError for HomeError {
@@ -182,7 +197,7 @@ impl actix_web::ResponseError for HomeError {
182
197
HomeError :: Anyhow ( _) => StatusCode :: INTERNAL_SERVER_ERROR ,
183
198
HomeError :: AlertError ( e) => e. status_code ( ) ,
184
199
HomeError :: CorrelationError ( e) => e. status_code ( ) ,
185
- HomeError :: StreamError ( e) => e. status_code ( )
200
+ HomeError :: StreamError ( e) => e. status_code ( ) ,
186
201
}
187
202
}
188
203
@@ -191,4 +206,4 @@ impl actix_web::ResponseError for HomeError {
191
206
. insert_header ( ContentType :: plaintext ( ) )
192
207
. body ( self . to_string ( ) )
193
208
}
194
- }
209
+ }
0 commit comments