openapi: 3.0.0 info: title: Analytics Service API description: | Provides aggregated statistics for the Emergency Response Platform. This service **passively records** events from RabbitMQ (it does not call other services). Every `incident.created`, `unit.dispatched`, `incident.resolved`, and `location.updated` message is stored as an `AnalyticsEvent` row. The four query endpoints then aggregate those stored events to answer questions about response times, incident distribution, and resource utilization. **Default date range:** last 30 days if `from`/`to` query params are omitted. version: 1.0.0 contact: name: Emergency Response Platform servers: - url: http://localhost:3004 description: Local development components: securitySchemes: bearerAuth: type: http scheme: bearer bearerFormat: JWT description: JWT token obtained from POST /auth/login schemas: Period: type: object description: The date range that was queried properties: from: type: string example: "2026-02-25T00:00:00.000Z" to: type: string example: "2026-03-27T00:00:00.000Z" ResponseTimesResponse: type: object properties: averageResponseTimeMinutes: type: number format: float description: Average minutes from incident creation to resolution across all resolved incidents in the period example: 8.4 totalResolved: type: integer description: Total number of resolved incidents in the period example: 42 breakdown: type: array description: Per-incident-type breakdown items: type: object properties: incidentType: type: string example: fire avgMinutes: type: string example: "6.20" count: type: integer example: 12 period: $ref: '#/components/schemas/Period' IncidentsByRegionResponse: type: object description: | Note: despite the endpoint name, grouping is currently by `incident_type`, not by geographic region. Geographic lat/lng bucketing is not yet implemented. properties: totalIncidents: type: integer example: 85 breakdown: type: array items: type: object properties: incidentType: type: string example: fire count: type: integer example: 20 period: $ref: '#/components/schemas/Period' ResourceUtilizationResponse: type: object properties: totalIncidentsCreated: type: integer example: 85 totalDispatched: type: integer example: 80 totalResolved: type: integer example: 72 resolutionRate: type: string description: Percentage of created incidents that were resolved example: "84.7%" byServiceType: type: object description: Dispatch count keyed by service type additionalProperties: type: integer example: fire: 25 police: 30 ambulance: 25 period: $ref: '#/components/schemas/Period' IncidentsSummaryResponse: type: object properties: total: type: integer description: Total incidents created example: 85 dispatched: type: integer description: Total units dispatched example: 80 resolved: type: integer description: Total incidents resolved example: 72 pending: type: integer description: total minus resolved (open/in-progress incidents) example: 13 period: $ref: '#/components/schemas/Period' ErrorResponse: type: object properties: message: type: string example: Server error security: - bearerAuth: [] paths: /analytics/response-times: get: summary: Average response times description: | Returns average response time (creation → resolution) across all resolved incidents in the queried period, plus a per-incident-type breakdown. All authenticated roles can access this endpoint. tags: [Analytics] parameters: - name: from in: query required: false schema: type: string format: date-time description: Start of date range (ISO 8601). Defaults to 30 days ago. example: "2026-02-25T00:00:00.000Z" - name: to in: query required: false schema: type: string format: date-time description: End of date range (ISO 8601). Defaults to now. example: "2026-03-27T00:00:00.000Z" - name: incidentType in: query required: false schema: type: string description: Filter to a single incident type (e.g. fire) example: fire - name: serviceType in: query required: false schema: type: string description: Filter to a single service type (e.g. ambulance) example: ambulance responses: '200': description: Response time statistics content: application/json: schema: $ref: '#/components/schemas/ResponseTimesResponse' example: averageResponseTimeMinutes: 8.4 totalResolved: 42 breakdown: - incidentType: fire avgMinutes: "6.20" count: 12 - incidentType: medical emergency avgMinutes: "10.50" count: 30 period: from: "2026-02-25T00:00:00.000Z" to: "2026-03-27T00:00:00.000Z" '401': description: Missing or invalid JWT content: application/json: schema: $ref: '#/components/schemas/ErrorResponse' '500': description: Server error content: application/json: schema: $ref: '#/components/schemas/ErrorResponse' /analytics/incidents-by-region: get: summary: Incidents grouped by type description: | Returns incident counts grouped by `incident_type` for the queried period. **Note:** The endpoint is named "by-region" but currently groups by incident type, not by geographic area. Geographic bucketing is not yet implemented. All authenticated roles can access this endpoint. tags: [Analytics] parameters: - name: from in: query required: false schema: type: string format: date-time description: Start of date range (ISO 8601). Defaults to 30 days ago. example: "2026-02-25T00:00:00.000Z" - name: to in: query required: false schema: type: string format: date-time description: End of date range (ISO 8601). Defaults to now. example: "2026-03-27T00:00:00.000Z" - name: incidentType in: query required: false schema: type: string description: Filter to a single incident type example: fire responses: '200': description: Incident counts by type content: application/json: schema: $ref: '#/components/schemas/IncidentsByRegionResponse' example: totalIncidents: 85 breakdown: - incidentType: fire count: 20 - incidentType: robbery count: 35 - incidentType: medical emergency count: 30 period: from: "2026-02-25T00:00:00.000Z" to: "2026-03-27T00:00:00.000Z" '401': description: Missing or invalid JWT content: application/json: schema: $ref: '#/components/schemas/ErrorResponse' '500': description: Server error content: application/json: schema: $ref: '#/components/schemas/ErrorResponse' /analytics/resource-utilization: get: summary: Resource utilization metrics description: | Returns total counts of incidents created, dispatched, and resolved, plus a resolution rate percentage and a per-service-type dispatch breakdown. All authenticated roles can access this endpoint. tags: [Analytics] parameters: - name: from in: query required: false schema: type: string format: date-time description: Start of date range (ISO 8601). Defaults to 30 days ago. example: "2026-02-25T00:00:00.000Z" - name: to in: query required: false schema: type: string format: date-time description: End of date range (ISO 8601). Defaults to now. example: "2026-03-27T00:00:00.000Z" responses: '200': description: Resource utilization statistics content: application/json: schema: $ref: '#/components/schemas/ResourceUtilizationResponse' example: totalIncidentsCreated: 85 totalDispatched: 80 totalResolved: 72 resolutionRate: "84.7%" byServiceType: fire: 25 police: 30 ambulance: 25 period: from: "2026-02-25T00:00:00.000Z" to: "2026-03-27T00:00:00.000Z" '401': description: Missing or invalid JWT content: application/json: schema: $ref: '#/components/schemas/ErrorResponse' '500': description: Server error content: application/json: schema: $ref: '#/components/schemas/ErrorResponse' /analytics/incidents/summary: get: summary: High-level incident summary description: | Returns a quick snapshot: total created, dispatched, resolved, and pending (= created minus resolved) for the queried period. This is the endpoint used by the frontend dashboard summary cards. All authenticated roles can access this endpoint. tags: [Analytics] parameters: - name: from in: query required: false schema: type: string format: date-time description: Start of date range (ISO 8601). Defaults to 30 days ago. example: "2026-02-25T00:00:00.000Z" - name: to in: query required: false schema: type: string format: date-time description: End of date range (ISO 8601). Defaults to now. example: "2026-03-27T00:00:00.000Z" responses: '200': description: Incident summary content: application/json: schema: $ref: '#/components/schemas/IncidentsSummaryResponse' example: total: 85 dispatched: 80 resolved: 72 pending: 13 period: from: "2026-02-25T00:00:00.000Z" to: "2026-03-27T00:00:00.000Z" '401': description: Missing or invalid JWT content: application/json: schema: $ref: '#/components/schemas/ErrorResponse' '500': description: Server error content: application/json: schema: $ref: '#/components/schemas/ErrorResponse' /health: get: summary: Health check description: Returns service status. No authentication required. tags: [Health] security: [] responses: '200': description: Service is healthy content: application/json: schema: type: object properties: status: type: string example: ok service: type: string example: analytics-service timestamp: type: string format: date-time example: "2026-03-27T10:00:00.000Z"