Skip to content

Commit 07c73d1

Browse files
authored
Merge pull request #316 from Rustmail/315-panel-statistics
feat(panel): add panel statistics
2 parents fe0527e + 79fa920 commit 07c73d1

13 files changed

Lines changed: 1242 additions & 12 deletions

File tree

.run/Run.run.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<option name="isRedirectInput" value="false" />
1717
<option name="redirectInputPath" value="" />
1818
<method v="2">
19-
<option name="ToolBeforeRunTask" enabled="true" actionId="Tool_External Tools_trunk" />
19+
<option name="ToolBeforeRunTask" enabled="true" actionId="Tool_External Tools_Trunk" />
2020
<option name="CARGO.BUILD_TASK_PROVIDER" enabled="true" />
2121
</method>
2222
</configuration>

docs/guides/panel.md

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,7 @@ Panel access requires one of:
6363

6464
### Dashboard
6565

66-
The home view displays:
67-
68-
- Bot status (online/offline)
69-
- Active ticket count
70-
- Quick statistics
66+
The home view displays a statistics dashboard with key metrics about your support activity. See [Statistics](#statistics) for details.
7167

7268
### Tickets
7369

@@ -107,6 +103,74 @@ For super administrators:
107103

108104
---
109105

106+
## Statistics
107+
108+
The statistics dashboard provides insights into your support team's performance. It is visible to all users with the **View Panel** permission.
109+
110+
### Period Selector
111+
112+
Use the period selector to view statistics for different time ranges:
113+
114+
- **7 days** - Recent activity
115+
- **30 days** - Monthly overview (default)
116+
- **90 days** - Quarterly trends
117+
118+
### Overview Cards
119+
120+
Key metrics displayed at the top:
121+
122+
| Metric | Description |
123+
|---------------------|--------------------------------------------------------|
124+
| Open Tickets | Number of currently open tickets |
125+
| Closed Today | Tickets closed since midnight (server time) |
126+
| Closed This Week | Tickets closed in the last 7 days |
127+
| Total Closed | All-time closed ticket count |
128+
| Avg Response Time | Average time from ticket creation to first staff reply |
129+
| Avg Resolution Time | Average time from ticket creation to closure |
130+
131+
**Note:** Response time is calculated from when a user creates a ticket until a staff member sends their first message in that ticket.
132+
133+
### Activity Chart
134+
135+
A bar chart showing daily ticket activity:
136+
137+
- **Blue bars** - Tickets created
138+
- **Green bars** - Tickets closed
139+
140+
Hover over bars to see exact counts for each day. The chart is scrollable horizontally for longer periods (90 days).
141+
142+
### Categories
143+
144+
Breakdown of closed tickets by category. Shows:
145+
146+
- Category name
147+
- Number of tickets
148+
- Percentage of total
149+
150+
Categories are defined when tickets are moved to specific channels or assigned categories through commands.
151+
152+
### Top Performers
153+
154+
Highlights the top-performing staff members:
155+
156+
| Award | Criteria |
157+
|---------------------|--------------------------------------------------|
158+
| Fastest Responder | Lowest average response time (minimum 5 tickets) |
159+
| Most Messages | Highest message count in tickets |
160+
| Most Tickets Closed | Highest number of closed tickets |
161+
162+
### Staff Leaderboard
163+
164+
Ranks staff members by activity within the selected period:
165+
166+
- Username
167+
- Messages sent in tickets
168+
- Tickets closed
169+
170+
Click **Show all** to expand beyond the top 5.
171+
172+
---
173+
110174
## Permission System
111175

112176
### Super Administrators
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
pub mod config;
22
pub mod restart;
33
pub mod start;
4+
pub mod statistics;
45
pub mod status;
56
pub mod stop;
67
pub mod tickets;
78

89
pub use config::*;
910
pub use restart::*;
1011
pub use start::*;
12+
pub use statistics::*;
1113
pub use status::*;
1214
pub use stop::*;
1315
pub use tickets::*;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use crate::prelude::db::*;
2+
use crate::prelude::types::*;
3+
use axum::Json;
4+
use axum::extract::{Query, State};
5+
use axum::http::StatusCode;
6+
use axum::response::IntoResponse;
7+
use std::sync::Arc;
8+
use tokio::sync::Mutex;
9+
10+
#[derive(serde::Deserialize)]
11+
pub struct StatisticsQuery {
12+
#[serde(default = "default_days")]
13+
pub days: i64,
14+
}
15+
16+
fn default_days() -> i64 {
17+
30
18+
}
19+
20+
pub async fn handle_statistics(
21+
State(bot_state): State<Arc<Mutex<BotState>>>,
22+
Query(query): Query<StatisticsQuery>,
23+
) -> impl IntoResponse {
24+
let days = match query.days {
25+
7 | 30 | 90 => query.days,
26+
_ => 30,
27+
};
28+
29+
let state_lock = bot_state.lock().await;
30+
31+
let pool = match &state_lock.db_pool {
32+
Some(p) => p.clone(),
33+
None => {
34+
return (
35+
StatusCode::SERVICE_UNAVAILABLE,
36+
Json(serde_json::json!({"error": "Database not available"})),
37+
);
38+
}
39+
};
40+
41+
drop(state_lock);
42+
43+
match get_statistics(&pool, days).await {
44+
Ok(stats) => (StatusCode::OK, Json(serde_json::to_value(stats).unwrap())),
45+
Err(e) => (
46+
StatusCode::INTERNAL_SERVER_ERROR,
47+
Json(serde_json::json!({"error": format!("Failed to fetch statistics: {}", e)})),
48+
),
49+
}
50+
}

rustmail/src/api/routes/bot.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub fn create_bot_router(bot_state: Arc<Mutex<BotState>>) -> Router<Arc<Mutex<Bo
3232
.route("/status", get(handle_status_bot))
3333
.route("/tickets", get(handle_tickets_bot))
3434
.route("/config", get(handle_get_config))
35+
.route("/statistics", get(handle_statistics))
3536
.layer(axum::middleware::from_fn_with_state(
3637
bot_state.clone(),
3738
move |state, jar, req, next| {

rustmail/src/db/operations/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub mod messages;
66
pub mod reminders;
77
pub mod scheduled;
88
pub mod snippets;
9+
pub mod statistics;
910
pub mod threads;
1011

1112
pub use api_keys::*;
@@ -16,4 +17,5 @@ pub use messages::*;
1617
pub use reminders::*;
1718
pub use scheduled::*;
1819
pub use snippets::*;
20+
pub use statistics::*;
1921
pub use threads::*;

0 commit comments

Comments
 (0)