Overview
A fleet management platform must handle a fundamentally different data pattern from most business applications: high-frequency, high-volume time-series data from GPS devices, combined with business logic for route optimisation, driver behaviour analysis, and maintenance scheduling. This guide covers the architectural decisions behind IdeaSprout's Fleet Management module.
1. GPS Telemetry Ingestion Pipeline
Data Volume Reality
A fleet of 50 vehicles, each reporting every 30 seconds, generates:
- •50 × 2 messages/min × 60 min × 16 hours = 96,000 messages/day
- •Each message ~200 bytes → ~19 MB/day → ~7 GB/year
This is manageable, but the ingestion pipeline must handle burst scenarios (all vehicles reporting simultaneously after a network outage) without data loss.
Architecture
GPS Device → MQTT Broker (HiveMQ/EMQX) → Telemetry Consumer → TimescaleDB
↓
Event Processor → Alert Engine
↓
Route Tracker → Trip DetectorWhy MQTT
GPS devices use MQTT (not HTTP) because:
- •Low bandwidth: MQTT has a 2-byte fixed header vs. HTTP's ~500-byte overhead
- •Persistent connections: Devices maintain a single TCP connection rather than reconnecting per message
- •QoS levels: MQTT QoS 1 (at-least-once) ensures no telemetry is lost even on intermittent connectivity
Telemetry Message Schema
{
"deviceId": "GPS-001",
"vehicleId": "uuid",
"timestamp": 1712345678,
"lat": 18.5204,
"lng": 73.8567,
"speed_kmh": 45.2,
"heading": 270,
"altitude_m": 560,
"ignition": true,
"odometer_km": 45231.4,
"fuel_level_pct": 68,
"engine_rpm": 2100,
"harsh_brake": false,
"harsh_accel": false,
"signal_strength": -75
}2. Time-Series Storage with TimescaleDB
Why Not PostgreSQL Alone
Raw telemetry is a time-series workload: queries are almost always time-bounded (WHERE timestamp BETWEEN X AND Y) and the data is append-only. TimescaleDB (a PostgreSQL extension) adds:
- •Automatic partitioning by time (hypertables) - queries only scan relevant time chunks
- •Compression: 90%+ compression on older data with no query changes
- •Continuous aggregates: materialised views that auto-update as new data arrives
-- Create hypertable partitioned by hour
SELECT create_hypertable('telemetry', 'timestamp', chunk_time_interval => INTERVAL '1 hour');
-- Continuous aggregate for 5-minute summaries (used by dashboard)
CREATE MATERIALIZED VIEW telemetry_5min
WITH (timescaledb.continuous) AS
SELECT
vehicle_id,
time_bucket('5 minutes', timestamp) AS bucket,
AVG(speed_kmh) AS avg_speed,
MAX(speed_kmh) AS max_speed,
AVG(fuel_level_pct) AS avg_fuel,
COUNT(*) AS data_points
FROM telemetry
GROUP BY vehicle_id, bucket;3. Trip Detection Engine
The Problem
Raw GPS points are just coordinates. The business needs trips - discrete journeys with start/end locations, distance, duration, and fuel consumed.
Algorithm
A trip boundary is detected when:
- •Trip start: Ignition ON after ignition OFF for > 5 minutes
- •Trip end: Ignition OFF for > 5 minutes (filters brief stops)
- •Idle detection: Ignition ON + speed < 3 km/h for > 3 minutes = idle event
def detect_trips(telemetry_stream):
current_trip = None
for point in telemetry_stream:
if point.ignition and current_trip is None:
current_trip = Trip(start=point)
elif not point.ignition and current_trip is not None:
if time_since_ignition_off > 5_minutes:
current_trip.end = point
yield current_trip
current_trip = NoneDistance Calculation
Trip distance uses the Haversine formula to compute great-circle distance between consecutive GPS points, then sums them. This is more accurate than odometer delta for short trips where odometer resolution is coarse.
4. Driver Behaviour Scoring
Events Detected
| Event | Detection Method | Threshold |
|---|---|---|
| Harsh Braking | Speed drop > 20 km/h in < 3 seconds | Configurable |
| Harsh Acceleration | Speed increase > 20 km/h in < 3 seconds | Configurable |
| Speeding | Speed > road speed limit (from HERE Maps API) | Configurable |
| Excessive Idling | Ignition ON + speed < 3 km/h for > 10 minutes | Configurable |
| Night Driving | Trip between 22:00 and 05:00 | Policy-based |
| Geofence Violation | Vehicle exits defined zone | Zone-based |
Scoring Algorithm
Each driver gets a daily behaviour score (0–100):
base_score = 100
deductions:
harsh_brake_event: -3 points
harsh_accel_event: -2 points
speeding_event (per minute): -1 point
excessive_idle (per 10 min): -1 point
geofence_violation: -10 points
daily_score = max(0, base_score - total_deductions)
monthly_score = weighted_average(daily_scores, weight=distance_driven)5. Fuel Analytics Engine
Fuel Consumption Calculation
Fuel consumed per trip = (fuel_level_start - fuel_level_end) × tank_capacity / 100
This is supplemented by fuel fill events (detected as sudden fuel level increase > 10%) to handle cases where the vehicle is refuelled mid-trip.
Fuel Theft Detection
Sudden fuel level decrease (> 5%) without a corresponding trip or engine activity = potential fuel theft alert. The threshold is configurable to account for sensor noise.
Mileage Benchmarking
The platform computes expected fuel consumption based on:
- •Vehicle make/model fuel efficiency rating
- •Route terrain (flat vs. hilly - from elevation API)
- •Load weight (if integrated with weighbridge)
Actual vs. expected mileage variance > 15% triggers a maintenance alert (possible engine issue) or driver behaviour review.
6. Maintenance Scheduling Engine
Trigger Types
| Trigger | Example | Data Source |
|---|---|---|
| Distance-based | Oil change every 5,000 km | Odometer from telemetry |
| Time-based | Annual insurance renewal | Calendar |
| Engine-hours-based | Filter change every 500 engine hours | Ignition-on duration |
| Condition-based | Battery voltage < 11.8V | OBD-II integration |
Predictive Maintenance
For fleets with OBD-II integration, the platform monitors engine fault codes (DTCs) and correlates them with historical breakdown data to predict failures before they occur. This requires a minimum of 6 months of historical data to build reliable predictions.
7. Common Architectural Pitfalls
| Pitfall | Consequence | Fix |
|---|---|---|
| Storing raw GPS in PostgreSQL without partitioning | Queries slow down as data grows | TimescaleDB hypertables |
| HTTP polling instead of MQTT | High bandwidth, missed points on poor connectivity | MQTT with QoS 1 |
| Trip detection without idle filtering | Short stops create false trip boundaries | 5-minute ignition-off threshold |
| Fuel calculation without fill event detection | Negative fuel consumption on refuelled trips | Detect fill events as separate records |
| No continuous aggregates | Dashboard queries scan millions of raw points | Materialised 5-minute summaries |
Summary
A production fleet management platform requires an MQTT-based telemetry pipeline, time-series storage with automatic partitioning, a stateful trip detection engine, event-based driver behaviour scoring, and a fuel analytics engine with theft detection. The architecture must be designed for high-frequency append-only writes while supporting fast time-bounded analytical queries.