Skip to content

Commit da606b4

Browse files
authored
Merge pull request #94 from VEAF/feat-enhance-rest-api-from-development
feat: enhanced restapi with weather information (/servers) and server attendance statistics (/server_attendance).
2 parents 267c843 + ec3a7d8 commit da606b4

File tree

7 files changed

+697
-48
lines changed

7 files changed

+697
-48
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ Scripts/net/DCSServerBot/DCSServerBotConfig.lua
1919
/logs/
2020
/config/
2121
/.emmyrc.json
22+
.copilot-docs/*

plugins/restapi/CHANGELOG.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# REST API Plugin Changelog
2+
3+
This document tracks all notable changes made to the REST API plugin.
4+
5+
## [Unreleased] - 2025-12-26
6+
7+
### 🆕 Added
8+
9+
#### Weather Integration in `/servers` Endpoint
10+
- **Comprehensive weather data**: Temperature, wind speed/direction, pressure, visibility, cloud coverage
11+
- **Real-time DCS integration**: Weather extracted from active mission weather system
12+
- **Configurable**: Control via `include_weather` setting (default: true)
13+
14+
#### New `/server_attendance` Endpoint
15+
- **Multi-period analytics**: 24h, 7d, 30d player statistics with Discord member engagement
16+
- **Enhanced server insights**: Top theatres, missions, and modules by playtime and usage
17+
- **Combat statistics**: Total sorties, kills, deaths, PvP metrics from mv_serverstats
18+
- **Daily trends**: 7-day player activity trends for graphing
19+
- **Inspired by Discord `/serverstats`**: Comprehensive data matching monitoring plugin functionality
20+
21+
#### Configuration Options
22+
- `include_weather: true/false` - Toggle weather in server responses
23+
- `server_attendance.enabled: true/false` - Enable attendance analytics endpoint
24+
25+
### 🔄 Changed
26+
27+
#### Centralized Server Name Resolution
28+
- **Unified server handling**: All endpoints now use centralized `get_resolved_server()` method
29+
- **Alias support**: Endpoints support both instance aliases (nodes.yaml) and full DCS server names (servers.yaml)
30+
- **Affected endpoints**: `/serverstats`, `/leaderboard`, `/topkills`, `/topkdr`, `/trueskill`, `/highscore`, `/weaponpk`, `/stats`, `/modulestats`, `/traps`, `/server_attendance`
31+
32+
#### Code Architecture Improvements
33+
- **Simplified SQL construction**: Removed overly complex `get_server_where_clause_and_params()` helper
34+
- **Readable patterns**: Straightforward conditional SQL with direct parameter passing
35+
- **Better maintainability**: Clear, transparent code structure while preserving functionality
36+
37+
### 🐛 Fixed
38+
39+
#### Endpoint Corrections
40+
- **`/modulestats` endpoint**: Fixed incorrect method mapping (`self.stats``self.modulestats`) and response model (`ModuleStats``list[ModuleStats]`)
41+
- **SQL syntax errors**: Resolved parameter binding issues in database queries
42+
- **Weather data extraction**: Corrected data structure parsing from DCS mission weather
43+
44+
#### Data Model Validation
45+
- **Response validation**: Fixed Pydantic model validation errors
46+
- **Type consistency**: Proper typing for all new data structures
47+
48+
### 📊 Enhanced Data Models
49+
50+
#### New Pydantic Models
51+
```python
52+
# Weather information
53+
class WeatherInfo(BaseModel):
54+
temperature: float | None
55+
wind_speed: float | None
56+
wind_direction: int | None
57+
pressure: float | None
58+
visibility: float | None
59+
cloud_coverage: str | None
60+
61+
# Top statistics for server attendance
62+
class TopTheatre(BaseModel):
63+
theatre: str
64+
playtime_hours: int
65+
66+
class TopMission(BaseModel):
67+
mission_name: str
68+
playtime_hours: int
69+
70+
class TopModule(BaseModel):
71+
module: str
72+
playtime_hours: int
73+
unique_players: int
74+
total_uses: int
75+
76+
# Enhanced server attendance with comprehensive analytics
77+
class ServerAttendanceStats(BaseModel):
78+
current_players: int
79+
unique_players_24h: int
80+
total_playtime_hours_24h: float
81+
discord_members_24h: int
82+
unique_players_7d: int
83+
total_playtime_hours_7d: float
84+
discord_members_7d: int
85+
unique_players_30d: int
86+
total_playtime_hours_30d: float
87+
discord_members_30d: int
88+
daily_trend: list[dict]
89+
top_theatres: list[TopTheatre]
90+
top_missions: list[TopMission]
91+
top_modules: list[TopModule]
92+
total_sorties: int | None
93+
total_kills: int | None
94+
total_deaths: int | None
95+
total_pvp_kills: int | None
96+
total_pvp_deaths: int | None
97+
```
98+
99+
### 🚀 Performance & Technical Details
100+
101+
#### Database Integration
102+
- **Monitoring plugin patterns**: Leverages existing monitoring infrastructure SQL queries
103+
- **Materialized views**: Efficient use of `mv_serverstats` for performance
104+
- **Optimized queries**: Following established DCS server integration patterns
105+
106+
#### Backward Compatibility
107+
- **Zero breaking changes**: All existing API consumers continue working unchanged
108+
- **Opt-in features**: New functionality enabled by default but configurable
109+
- **Migration-free**: No configuration changes required for existing setups
110+
111+
### 📈 Impact
112+
113+
#### For API Consumers
114+
- **Richer server data**: Weather and comprehensive attendance analytics
115+
- **Better server identification**: Flexible server name resolution (aliases + full names)
116+
- **Enhanced dashboards**: More detailed metrics for monitoring and visualization
117+
118+
#### For Developers
119+
- **Cleaner codebase**: Simplified, maintainable SQL construction patterns
120+
- **Consistent patterns**: Unified server resolution across all endpoints
121+
- **Better documentation**: Complete API models with examples and validation
122+
123+
---
124+
125+
**Summary**: This release significantly enhances the REST API plugin with comprehensive server analytics, weather integration, and improved code architecture while maintaining full backward compatibility.

plugins/restapi/README.md

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ DEFAULT:
1919
filter: # config parameter (in this case, the server filter list)
2020
- 'MyPrivateServer' # Do not show a server named "MyPrivateServer"
2121
- '(.*)Private(.*)' # Do not show any server that has "Private" in its name
22+
include_weather: true # Include weather information in /servers endpoint (default: true)
23+
server_attendance: # /server_attendance
24+
enabled: true # Enable the server attendance statistics endpoint (default: true)
2225
```
2326
2427
> [!WARNING]
@@ -30,7 +33,8 @@ The following commands are available through the API
3033
| API | GET / POST | Parameters | Description |
3134
|-------------------|------------|-------------------------------------------------------|---------------------------------------------------------------------------------------|
3235
| /serverstats | GET | | A comprehensive statistic for your whole setup. |
33-
| /servers | GET | | Status for each server. |
36+
| /servers | GET | | Status for each server, including weather information if enabled. |
37+
| /server_attendance| GET | [server_name: string] | Comprehensive server attendance statistics, top theatres/missions/modules, and daily trends. |
3438
| /getuser | POST | nick: string | Return a list of players ordered by last seen that match this nick. |
3539
| /stats | POST | nick: string, date: date | Statistics of this player |
3640
| /highscore | GET | [server_name: string], [period: string], [limit: int] | Highscore output |
@@ -48,3 +52,79 @@ The following commands are available through the API
4852
> [!NOTE]
4953
> To get more detailled API documentation, please enable debug in your WebService config and
5054
> access https://localhost:9876/docs.
55+
56+
## New Features
57+
58+
### Weather Information
59+
The `/servers` endpoint now includes real-time weather data for running DCS servers:
60+
```json
61+
{
62+
"weather": {
63+
"temperature": 16.0,
64+
"wind_speed": 0.968,
65+
"wind_direction": 290,
66+
"pressure": 765.0,
67+
"visibility": 5000,
68+
"clouds_base": 0,
69+
"clouds_density": 0,
70+
"precipitation": 0,
71+
"fog_enabled": false,
72+
"dust_enabled": false
73+
}
74+
}
75+
```
76+
77+
### Server Attendance Statistics
78+
The `/server_attendance` endpoint provides comprehensive server attendance analytics:
79+
80+
- **Server attendance statistics**: Current active players, unique players, total playtime hours, and Discord member engagement for different time periods (24h, 7d, 30d)
81+
- **Top statistics**: Most popular theatres, missions, and modules by playtime and usage
82+
- **Daily trends**: Daily unique player counts for the last 7 days to analyze server activity patterns
83+
- **Combat statistics**: Total sorties, kills, deaths, and PvP statistics from mv_serverstats
84+
85+
**Global statistics (no parameters):**
86+
```bash
87+
GET /server_attendance
88+
```
89+
90+
**Server-specific statistics (using DCS server name):**
91+
```bash
92+
GET /server_attendance?server_name=VEAF (www.veaf.org) [fr] - Private Foothold 2
93+
```
94+
95+
**Server-specific statistics (using instance alias):**
96+
```bash
97+
GET /server_attendance?server_name=foothold2_server
98+
```
99+
100+
**Response example:**
101+
```json
102+
{
103+
"current_players": 8,
104+
"unique_players_24h": 15,
105+
"total_playtime_hours_24h": 45.5,
106+
"discord_members_24h": 12,
107+
"unique_players_7d": 35,
108+
"total_playtime_hours_7d": 180.2,
109+
"discord_members_7d": 28,
110+
"unique_players_30d": 85,
111+
"total_playtime_hours_30d": 720.8,
112+
"discord_members_30d": 65,
113+
"daily_trend": [
114+
{"date": "2025-12-24", "unique_players": 15},
115+
{"date": "2025-12-25", "unique_players": 18}
116+
]
117+
}
118+
```
119+
120+
### Server Name Resolution
121+
All endpoints that accept a `server_name` parameter now support both server naming conventions seamlessly:
122+
- **Instance alias**: `foothold2_server` (from nodes.yaml configuration)
123+
- **DCS server name**: `VEAF (www.veaf.org) [fr] - Private Foothold 2` (from servers.yaml configuration)
124+
125+
The resolution is handled transparently by the `get_resolved_server()` method, which:
126+
1. Checks if the provided name is already a full DCS server name
127+
2. If not, searches instance aliases and returns the corresponding DCS server name
128+
3. Returns both the resolved name and server object for use in endpoints
129+
130+
This approach ensures consistent behavior across all endpoints while maintaining simple, readable SQL construction patterns.

0 commit comments

Comments
 (0)