@@ -59,7 +59,7 @@ mod users;
5959use crate :: {
6060 AdminError , AdminService , AuthConfig , KeyStore , MAX_DASHBOARD_BODY_BYTES ,
6161 account:: { AccountError , AccountService } ,
62- admin:: { AdminOperation , DashboardSignIn , SignedInAdmin } ,
62+ admin:: { AdminOperation , DashboardSignIn , Overview , Problem , SignedInAdmin } ,
6363 assets:: EmbeddedAssets ,
6464 config:: { DashboardConfig , PolicyLinks } ,
6565 federation:: ReturnTo ,
@@ -103,9 +103,59 @@ use topcoat::{
103103/// shown: showing it asks for a confirmation this much newer than the service
104104/// needs.
105105const FORM_TIME : Duration = Duration :: minutes ( 2 ) ;
106- /// How long every page shows the health the overview last found, before a page
106+ /// How long every page shows what the overview last found, before a page
107107/// reads it again: the status bar must not cost each page the overview's queries.
108- const HEALTH_FOR : StdDuration = StdDuration :: from_secs ( 60 ) ;
108+ const ATTENTION_FOR : StdDuration = StdDuration :: from_secs ( 60 ) ;
109+
110+ /// What the overview last found, as every page shows it: the status bar's
111+ /// count, and the tabs the sidebar marks.
112+ #[ derive( Clone , Copy , Default ) ]
113+ pub ( super ) struct Attention {
114+ /// How many things need attention: each open alert, and each other problem.
115+ count : u32 ,
116+ /// Whether alerts are open, which marks the Alerts tab.
117+ alerts : bool ,
118+ /// Whether the overview found another problem, which marks its own tab.
119+ overview : bool ,
120+ }
121+
122+ impl Attention {
123+ /// What `overview` found.
124+ fn of ( overview : & Overview ) -> Self {
125+ let others = overview
126+ . problems ( )
127+ . iter ( )
128+ . filter ( |problem| !matches ! ( problem, Problem :: OpenAlerts ( _) ) )
129+ . count ( ) ;
130+ let count = u32:: try_from ( overview. open_alerts )
131+ . unwrap_or ( u32:: MAX )
132+ . saturating_add ( u32:: try_from ( others) . unwrap_or ( u32:: MAX ) ) ;
133+ Self {
134+ count,
135+ alerts : overview. open_alerts > 0 ,
136+ overview : others > 0 ,
137+ }
138+ }
139+
140+ /// The status bar's health.
141+ const fn health ( self ) -> Health {
142+ match self . count {
143+ 0 => Health :: Normal ,
144+ count => Health :: Warnings ( count) ,
145+ }
146+ }
147+
148+ /// The tabs the sidebar marks.
149+ fn tabs ( self ) -> Vec < DashboardTab > {
150+ [
151+ ( self . overview , DashboardTab :: Overview ) ,
152+ ( self . alerts , DashboardTab :: Alerts ) ,
153+ ]
154+ . into_iter ( )
155+ . filter_map ( |( marked, tab) | marked. then_some ( tab) )
156+ . collect ( )
157+ }
158+ }
109159
110160/// The dashboard's pages, and what every one of them needs.
111161#[ derive( Clone ) ]
@@ -136,8 +186,8 @@ pub(crate) struct AdminApplication {
136186 policy : HeaderValue ,
137187 /// Piquel's policies on piquel.fr, which every page's footer links.
138188 policies : PolicyLinks ,
139- /// The health the overview last found, and when, shared by every page.
140- health : Arc < Mutex < Option < ( Instant , Health ) > > > ,
189+ /// What the overview last found, and when, shared by every page.
190+ attention : Arc < Mutex < Option < ( Instant , Attention ) > > > ,
141191}
142192
143193/// What a page answers in its place, such as the redirect through auth's handoff
@@ -247,7 +297,7 @@ impl AdminApplication {
247297 kek : config. key_encryption_key . clone ( ) ,
248298 policy : HeaderValue :: try_from ( policy) ?,
249299 policies : config. oidc . policies . clone ( ) ,
250- health : Arc :: default ( ) ,
300+ attention : Arc :: default ( ) ,
251301 } )
252302 }
253303
@@ -572,44 +622,50 @@ struct SearchQuery {
572622
573623impl AdminApplication {
574624 /// Whether the service needs attention, as the overview last found, read
575- /// again once that is [`HEALTH_FOR `] old. A browser that is not signed in
625+ /// again once that is [`ATTENTION_FOR `] old. A browser that is not signed in
576626 /// learns nothing, and neither does a page whose read fails: the status bar
577627 /// never costs the page, so a failed read is logged and shows as normal.
578- async fn health ( cx : & Cx ) -> Health {
628+ async fn attention ( cx : & Cx ) -> Attention {
579629 let app = Self :: of ( cx) ;
580630 let Ok ( admin) = Self :: admin ( cx) . await else {
581- return Health :: Normal ;
631+ return Attention :: default ( ) ;
582632 } ;
583633 let cached = app
584- . health
634+ . attention
585635 . lock ( )
586636 . unwrap_or_else ( PoisonError :: into_inner)
587- . filter ( |( read, _) | read. elapsed ( ) < HEALTH_FOR )
588- . map ( |( _, health ) | health ) ;
589- if let Some ( health ) = cached {
590- return health ;
637+ . filter ( |( read, _) | read. elapsed ( ) < ATTENTION_FOR )
638+ . map ( |( _, attention ) | attention ) ;
639+ if let Some ( attention ) = cached {
640+ return attention ;
591641 }
592642 match app. service . overview ( & admin. operator ) . await {
593- Ok ( overview) => Self :: remember_health ( cx, & overview) ,
643+ Ok ( overview) => Self :: remember_attention ( cx, & overview) ,
594644 Err ( error) => {
595645 tracing:: warn!( error = %ErrorChain ( & error) , "the health check failed" ) ;
596- Health :: Normal
646+ Attention :: default ( )
597647 }
598648 }
599649 }
600650
601- /// Keeps what `overview` found as the health every page shows for the next
602- /// [`HEALTH_FOR`], and says what that is.
603- pub ( super ) fn remember_health ( cx : & Cx , overview : & crate :: admin:: Overview ) -> Health {
604- let health = match u32:: try_from ( overview. problems ( ) . len ( ) ) . unwrap_or ( u32:: MAX ) {
605- 0 => Health :: Normal ,
606- count => Health :: Warnings ( count) ,
607- } ;
651+ /// Keeps what `overview` found as what every page shows for the next
652+ /// [`ATTENTION_FOR`], and says what that is.
653+ pub ( super ) fn remember_attention ( cx : & Cx , overview : & Overview ) -> Attention {
654+ let attention = Attention :: of ( overview) ;
655+ * Self :: of ( cx)
656+ . attention
657+ . lock ( )
658+ . unwrap_or_else ( PoisonError :: into_inner) = Some ( ( Instant :: now ( ) , attention) ) ;
659+ attention
660+ }
661+
662+ /// Forgets what every page shows, so the next page reads it again: an
663+ /// administrator's own change, such as an update on an alert, shows at once.
664+ pub ( super ) fn forget_attention ( cx : & Cx ) {
608665 * Self :: of ( cx)
609- . health
666+ . attention
610667 . lock ( )
611- . unwrap_or_else ( PoisonError :: into_inner) = Some ( ( Instant :: now ( ) , health) ) ;
612- health
668+ . unwrap_or_else ( PoisonError :: into_inner) = None ;
613669 }
614670
615671 /// Each tab's page, in the sidebar's order.
@@ -750,7 +806,8 @@ impl AdminApplication {
750806 } ) ;
751807 // The avatar leads to the administrator's own settings, on auth.
752808 let me = app. auth . join ( routes:: page:: settings:: HOME ) ;
753- let health = Self :: health ( cx) . await ;
809+ let attention = Self :: attention ( cx) . await ;
810+ let marked = attention. tabs ( ) ;
754811 let environment = app. auth . host ( ) ;
755812 let dashboard = Dashboard {
756813 title,
@@ -766,7 +823,8 @@ impl AdminApplication {
766823 preferences : BrowserCookie :: read_preferences ( headers ( cx) ) ,
767824 preferences_action : prefer,
768825 here,
769- health : Some ( health) ,
826+ health : Some ( attention. health ( ) ) ,
827+ attention : & marked,
770828 environment,
771829 policies : app. policies . links ( ) ,
772830 search : Search {
0 commit comments