Dashboard API¶
The Dashboard API provides endpoints for retrieving admin panel statistics, recent activity, and custom widget data.
All dashboard endpoints are under /admin/api/dashboard.
Endpoints¶
Get Dashboard Statistics¶
Get statistics for all registered models including record counts.
GET /api/dashboard/stats
Headers
Header |
Required |
Description |
|---|---|---|
|
Yes |
|
Example Request
GET /admin/api/dashboard/stats HTTP/1.1
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Success Response (200 OK)
{
"models": [
{
"name": "User",
"model_name": "User",
"count": 150,
"icon": "users",
"category": "Authentication"
},
{
"name": "Product",
"model_name": "Product",
"count": 42,
"icon": "box",
"category": "Inventory"
},
{
"name": "Order",
"model_name": "Order",
"count": 328,
"icon": "shopping-cart",
"category": "Sales"
}
],
"total_records": 520,
"total_models": 3,
"widgets": []
}
Response Fields
Field |
Type |
Description |
|---|---|---|
|
array |
Statistics for each registered model |
|
string |
Display name of the model |
|
string |
Internal model class name |
|
integer |
Total number of records |
|
string |
Icon identifier for UI display |
|
string or null |
Category for grouping |
|
integer |
Sum of all records across models |
|
integer |
Number of registered models |
|
array |
Custom dashboard widget data |
Get Recent Activity¶
Get recent admin activity from the audit log.
GET /api/dashboard/activity
Headers
Header |
Required |
Description |
|---|---|---|
|
Yes |
|
Query Parameters
Parameter |
Type |
Default |
Description |
|---|---|---|---|
|
integer |
50 |
Maximum number of entries to return |
Example Request
GET /admin/api/dashboard/activity?limit=20 HTTP/1.1
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Success Response (200 OK)
[
{
"action": "create",
"model": "User",
"record_id": 151,
"timestamp": "2024-01-16T14:30:00",
"user": "admin@example.com",
"details": {
"fields": ["email", "name", "is_active"]
}
},
{
"action": "update",
"model": "Product",
"record_id": 42,
"timestamp": "2024-01-16T14:25:00",
"user": "editor@example.com",
"details": {
"changed_fields": ["price", "description"]
}
},
{
"action": "delete",
"model": "Order",
"record_id": 100,
"timestamp": "2024-01-16T14:20:00",
"user": "admin@example.com",
"details": {}
}
]
Response Fields
Field |
Type |
Description |
|---|---|---|
|
string |
Action performed: “create”, “update”, “delete”, etc. |
|
string |
Name of the affected model |
|
string, integer, or null |
ID of the affected record |
|
string |
ISO 8601 timestamp of the action |
|
string or null |
User who performed the action |
|
object |
Additional action-specific information |
Available Action Types
Action |
Description |
|---|---|
|
A new record was created |
|
An existing record was updated |
|
A record was deleted |
|
Multiple records were deleted |
|
Data was exported |
|
User logged in |
|
User logged out |
Note
The activity endpoint returns an empty array if audit logging is not configured. Configure an AuditLogger in your AdminConfig to enable activity tracking.
Get Custom Widgets¶
Get custom dashboard widget configurations.
GET /api/dashboard/widgets
Headers
Header |
Required |
Description |
|---|---|---|
|
Yes |
|
Example Request
GET /admin/api/dashboard/widgets HTTP/1.1
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Success Response (200 OK)
[
{
"id": "revenue-chart",
"type": "chart",
"title": "Monthly Revenue",
"data": {
"labels": ["Jan", "Feb", "Mar", "Apr"],
"values": [12500, 15000, 14200, 18000]
},
"config": {
"chartType": "line",
"color": "#3B82F6"
}
},
{
"id": "active-users",
"type": "metric",
"title": "Active Users",
"data": {
"value": 1250,
"change": 12.5,
"period": "last 7 days"
},
"config": {
"format": "number",
"showTrend": true
}
}
]
Response Fields
Field |
Type |
Description |
|---|---|---|
|
string |
Unique identifier for the widget |
|
string |
Widget type (see types below) |
|
string |
Display title |
|
object |
Widget-specific data payload |
|
object |
Optional rendering configuration |
Widget Types
Type |
Description |
|---|---|
|
Single value with optional trend indicator |
|
Line, bar, or pie chart |
|
List of items |
|
Custom component type |
Configuring Custom Widgets¶
Custom widgets are configured via AdminConfig.extra["widgets"]:
from litestar_admin import AdminConfig, AdminPlugin
config = AdminConfig(
title="My Admin",
extra={
"widgets": [
{
"id": "sales-metric",
"type": "metric",
"title": "Total Sales",
"data": {
"value": 45000,
"change": 8.3,
},
"config": {
"format": "currency",
"currency": "USD",
},
},
{
"id": "orders-chart",
"type": "chart",
"title": "Orders This Week",
"data": {
"labels": ["Mon", "Tue", "Wed", "Thu", "Fri"],
"values": [45, 52, 48, 61, 55],
},
"config": {
"chartType": "bar",
},
},
]
}
)
For dynamic widget data, you can create a custom endpoint that fetches data and updates the widget configuration at runtime:
from litestar import get
from litestar.di import Provide
@get("/api/dashboard/custom-widgets")
async def get_custom_widgets(db_session: AsyncSession) -> list[dict]:
# Fetch dynamic data
sales = await get_total_sales(db_session)
orders = await get_weekly_orders(db_session)
return [
{
"id": "sales-metric",
"type": "metric",
"title": "Total Sales",
"data": {"value": sales, "change": calculate_change(sales)},
"config": {"format": "currency"},
},
# ... more widgets
]
Dashboard Data Flow¶
Here’s how the dashboard fetches and displays data:
Frontend Dashboard API Database
| | |
|-- GET /dashboard/stats --->| |
| |-- COUNT each model ---->|
| |<-- record counts -------|
|<-- stats with counts ------| |
| | |
|-- GET /dashboard/activity->| |
| |-- query audit log ----->|
| |<-- activity entries ----|
|<-- activity feed ----------| |
| | |
|-- GET /dashboard/widgets ->| |
|<-- widget configurations --| |
Performance Considerations¶
The /dashboard/stats endpoint queries the database for each registered model’s count. For large databases with many models, consider:
Caching: Cache the counts with a short TTL (e.g., 60 seconds)
Approximate Counts: Use database-specific approximate count functions
Background Updates: Precompute counts in a background task
Example with caching:
from litestar.stores.redis import RedisStore
# In your custom stats endpoint
@get("/api/dashboard/stats")
async def get_cached_stats(
cache: RedisStore,
db_session: AsyncSession,
) -> DashboardStats:
cached = await cache.get("dashboard_stats")
if cached:
return DashboardStats(**cached)
stats = await compute_stats(db_session)
await cache.set("dashboard_stats", stats.dict(), expires_in=60)
return stats