| 1 | //! 告警中心(0005)。**跨库**:一个顶栏面板,不挂在某个 KB 下面。 |
| 2 | //! |
| 3 | //! 可见性不在这里判——它在 `utopia_store::alerts` 的那几条 SQL 里判且只判一次。 |
| 4 | //! 路由层只负责取当前用户。 |
| 5 | |
| 6 | use axum::extract::{Query, State}; |
| 7 | use axum::response::sse::{Event, KeepAlive, Sse}; |
| 8 | use axum::Json; |
| 9 | use chrono::{DateTime, Utc}; |
| 10 | use futures_util::Stream; |
| 11 | use serde::{Deserialize, Serialize}; |
| 12 | use serde_json::json; |
| 13 | use std::convert::Infallible; |
| 14 | use tokio::sync::broadcast; |
| 15 | use uuid::Uuid; |
| 16 | |
| 17 | use crate::auth::AuthUser; |
| 18 | use crate::error::ApiResult; |
| 19 | use crate::state::AppState; |
| 20 | |
| 21 | /// 一页几组。弹窗里放得下的量——再多就该翻页,而不是让人滚一屏。 |
| 22 | const PAGE: i64 = 8; |
| 23 | /// 一页最多能要多少:防的是有人把 limit 写成 100000 让服务端去数全表 |
| 24 | const MAX_PAGE: i64 = 50; |
| 25 | |
| 26 | #[derive(Deserialize)] |
| 27 | pub struct ListQuery { |
| 28 | /// 搜库名、对象详情、kind 代号。**搜不到界面上那句标题**—— |
| 29 | /// 措辞在客户端,服务端没有它(见 store 里 SEARCH 的注释) |
| 30 | #[serde(default)] |
| 31 | pub q: Option<String>, |
| 32 | #[serde(default)] |
| 33 | pub limit: Option<i64>, |
| 34 | #[serde(default)] |
| 35 | pub offset: Option<i64>, |
| 36 | } |
| 37 | |
| 38 | /// 一组连着的同类故障。折叠是**读**出来的,存储那边仍是一次故障一行。 |
| 39 | #[derive(Serialize)] |
| 40 | pub struct GroupView { |
| 41 | pub kb_id: Option<Uuid>, |
| 42 | pub kb_name: Option<String>, |
| 43 | pub kind: String, |
| 44 | pub severity: String, |
| 45 | pub count: i64, |
| 46 | pub unread: i64, |
| 47 | pub latest_at: DateTime<Utc>, |
| 48 | /// 跟 `latest_at` 一起圈出这一组,标已读时原样发回来 |
| 49 | pub earliest_at: DateTime<Utc>, |
| 50 | /// 明细,最多几条,新的在前 |
| 51 | pub lines: Vec<serde_json::Value>, |
| 52 | } |
| 53 | |
| 54 | #[derive(Serialize)] |
| 55 | pub struct ListResponse { |
| 56 | pub items: Vec<GroupView>, |
| 57 | /// 总**组**数——翻页控件数的是组,不是行 |
| 58 | pub total: i64, |
| 59 | } |
| 60 | |
| 61 | pub async fn list( |
| 62 | State(state): State<AppState>, |
| 63 | AuthUser(user): AuthUser, |
| 64 | Query(q): Query<ListQuery>, |
| 65 | ) -> ApiResult<Json<ListResponse>> { |
| 66 | let limit = q.limit.unwrap_or(PAGE).clamp(1, MAX_PAGE); |
| 67 | let offset = q.offset.unwrap_or(0).max(0); |
| 68 | let page = utopia_store::alerts::list_groups(&state.pool, &user, q.q.as_deref(), limit, offset) |
| 69 | .await?; |
| 70 | Ok(Json(ListResponse { |
| 71 | items: page |
| 72 | .items |
| 73 | .into_iter() |
| 74 | .map(|g| GroupView { |
| 75 | kb_id: g.kb_id, |
| 76 | kb_name: g.kb_name, |
| 77 | kind: g.kind, |
| 78 | severity: g.severity, |
| 79 | count: g.count, |
| 80 | unread: g.unread, |
| 81 | latest_at: g.latest_at, |
| 82 | earliest_at: g.earliest_at, |
| 83 | lines: g.lines, |
| 84 | }) |
| 85 | .collect(), |
| 86 | total: page.total, |
| 87 | })) |
| 88 | } |
| 89 | |
| 90 | /// 角标。单独一条路由而不是从列表里数:这个每开一页都要。 |
| 91 | pub async fn unread( |
| 92 | State(state): State<AppState>, |
| 93 | AuthUser(user): AuthUser, |
| 94 | ) -> ApiResult<Json<serde_json::Value>> { |
| 95 | let n = utopia_store::alerts::unread_count(&state.pool, &user).await?; |
| 96 | Ok(Json(json!({ "unread": n }))) |
| 97 | } |
| 98 | |
| 99 | #[derive(Deserialize)] |
| 100 | pub struct ReadGroupBody { |
| 101 | pub kb_id: Option<Uuid>, |
| 102 | pub kind: String, |
| 103 | /// 组的时间区间,原样来自列表返回的 `earliest_at` / `latest_at` |
| 104 | pub from: DateTime<Utc>, |
| 105 | pub to: DateTime<Utc>, |
| 106 | } |
| 107 | |
| 108 | /// 把一整组标已读。**逐人**——读过不等于问题没了,别人的未读不受影响。 |
| 109 | /// |
| 110 | /// 按时间区间圈而不是发 id 列表:一组可能有几百条。可见性在 store 里照查, |
| 111 | /// 所以猜一个 kind 也标不掉自己看不见的东西。 |
| 112 | pub async fn mark_group_read( |
| 113 | State(state): State<AppState>, |
| 114 | AuthUser(user): AuthUser, |
| 115 | Json(b): Json<ReadGroupBody>, |
| 116 | ) -> ApiResult<Json<serde_json::Value>> { |
| 117 | let n = |
| 118 | utopia_store::alerts::mark_group_read(&state.pool, &user, b.kb_id, &b.kind, b.from, b.to) |
| 119 | .await?; |
| 120 | Ok(Json(json!({ "marked": n }))) |
| 121 | } |
| 122 | |
| 123 | /// 全部已读。 |
| 124 | pub async fn mark_all_read( |
| 125 | State(state): State<AppState>, |
| 126 | AuthUser(user): AuthUser, |
| 127 | ) -> ApiResult<Json<serde_json::Value>> { |
| 128 | let n = utopia_store::alerts::mark_all_read(&state.pool, &user).await?; |
| 129 | Ok(Json(json!({ "marked": n }))) |
| 130 | } |
| 131 | |
| 132 | /// 全局事件流。KB 那条是 `/kbs/{id}/events`,按库过滤;角标是跨库的, |
| 133 | /// 挂不上去。 |
| 134 | /// |
| 135 | /// **这里不做任何权限过滤**:事件不带数据,收到的人一律回头重取列表, |
| 136 | /// 而列表那条查询会把不该看的挡掉。代价是没权限的人也被叫醒一次, |
| 137 | /// 换来的是推送这条路上一行权限逻辑都没有——不会出现"推送判得比列表松"。 |
| 138 | pub async fn stream( |
| 139 | State(state): State<AppState>, |
| 140 | AuthUser(_user): AuthUser, |
| 141 | ) -> ApiResult<Sse<impl Stream<Item = Result<Event, Infallible>>>> { |
| 142 | let mut rx = state.events.subscribe(); |
| 143 | let stream = async_stream::stream! { |
| 144 | loop { |
| 145 | match rx.recv().await { |
| 146 | Ok(ev) if ev.kind == "alert" => { |
| 147 | yield Ok(Event::default().event("alert").data("{}")); |
| 148 | } |
| 149 | Ok(_) => continue, |
| 150 | Err(broadcast::error::RecvError::Lagged(_)) => continue, |
| 151 | Err(broadcast::error::RecvError::Closed) => return, |
| 152 | } |
| 153 | } |
| 154 | }; |
| 155 | Ok(Sse::new(stream).keep_alive(KeepAlive::default())) |
| 156 | } |