Skip to content

Safety Engagement & Predictive Analytics

Last Updated: 2025-02-03


Overview

The Safety Engagement & Predictive Analytics module provides comprehensive insights into safety participation, activity trends, and early warning systems based on historical data analysis.

Key Features:

  • ✅ Activity timeline with participation metrics
  • ✅ Role-aware data visibility (Admin/Worker/Driver)
  • ✅ Predictive warnings based on historical patterns
  • ✅ Explainable heuristics (no AI black boxes)
  • ✅ Near-miss pattern detection

Engagement Views

Activity Timeline

The Activity Timeline shows safety-related activities over time with role-specific views:

For Workers/Drivers:

  • Personal activity metrics
  • Own incidents, near-misses, checklists, training
  • Progress tracking

For Admins:

  • Aggregated team metrics
  • Participation rates
  • Average activities per user
  • Total vs. active users

Metrics Tracked:

  • Incidents: Reported safety incidents
  • Near Misses: Near-miss reports
  • Checklists: Completed safety checklists
  • Training: Completed training modules
  • Total: Combined activity count

Time Range:

  • Default: Last 30 days
  • Configurable: 7, 14, 30, 60, 90 days

Predictive Warnings (v1)

Warning Types

  1. High-Risk Zones

    • Based on historical incident frequency
    • Risk score calculation (0-100)
    • 7/30 day prediction window
    • Zone-specific recommendations
  2. Repeating Incident Patterns

    • Category-based pattern detection
    • Frequency analysis (last 90 days)
    • Trend identification (increasing/decreasing)
    • Severity scoring
  3. Near-Miss Pattern Detection

    • Location/category clustering
    • Pattern threshold: 3+ occurrences
    • 90-day historical window
    • Last occurrence tracking
  4. SLA Breach Risks

    • Workflow step performance
    • Time-to-completion analysis
    • Risk percentage calculation

How It Works

Deterministic Heuristics:

  • No machine learning
  • Transparent calculations
  • Explainable results
  • Historical data analysis

Data Sources:

  • safety_incidents table
  • near_miss_reports table
  • safety_checklist_submissions table
  • safety_risk_trends view
  • workflow_instances table

Calculation Methods:

  • Frequency counting
  • Trend analysis (simple linear regression)
  • Risk score aggregation
  • Pattern matching

Role-Based Access

Admin Access

  • Full engagement analytics
  • Team/zone leaderboards
  • Aggregated participation metrics
  • All predictive warnings
  • Historical pattern analysis

Worker/Driver Access

  • Personal activity timeline
  • Own safety score
  • Personal badges and points
  • Relevant warnings (zone-specific)
  • Limited historical view

Permissions

  • Controlled via FeatureGate component
  • Module: safety_culture (engagement)
  • Module: safety_intelligence (predictive warnings)
  • RLS policies enforce tenant isolation

Back Button Behavior

The improved back navigation:

  • Preserves route history: Uses browser history API
  • Prevents dashboard redirects: Smart fallback logic
  • Maintains context: Preserves location state
  • Role-aware fallbacks: Defaults to appropriate dashboard

Implementation:

  • BackButton component with history tracking
  • Multiple heuristics for history detection
  • Safe fallback routes per role
  • State preservation option

UI Components

ActivityTimeline Component

Location: src/components/safety/ActivityTimeline.tsx

Props:

  • days?: number - Time range (default: 30)
  • role?: "admin" | "worker" | "driver" - Override role

Features:

  • Line chart visualization
  • Participation metrics cards (admin only)
  • Responsive design
  • Loading states

PredictiveWarningsView Component

Location: src/pages/safety/PredictiveWarningsView.tsx

Features:

  • Warning cards with severity indicators
  • Explainable explanations
  • Action links
  • Empty state handling

Data Queries

Engagement Data

Edge Function: safety-engagement

  • Aggregates scores, points, badges
  • 60s cache TTL
  • Per-tenant isolation

Direct Queries:

  • safety_scores table
  • safety_points_log table
  • safety_badges table

Activity Data

Queries:

  • safety_incidents (grouped by date)
  • near_miss_reports (grouped by date)
  • safety_checklist_submissions (grouped by date)
  • safety_training_completions (grouped by date, optional)

Predictive Data

Queries:

  • useHighRiskZones() hook
  • useDetectRiskTrends() hook
  • useWorkflowMetrics() hook
  • near_miss_reports (pattern analysis)

Performance

Optimizations:

  • Edge Functions for aggregation
  • Caching (30-60s TTL)
  • Parallel query execution
  • Date-range filtering
  • Indexed queries

Expected Performance:

  • Activity timeline: < 500ms
  • Predictive warnings: < 800ms
  • Engagement metrics: < 300ms (cached)

Future Enhancements

Planned Features:

  • Custom date range picker
  • Export to CSV/PDF
  • Email notifications for warnings
  • Mobile-optimized views
  • Real-time updates (WebSocket)

Troubleshooting

No Activity Data

Possible Causes:

  • No data in date range
  • RLS policy restrictions
  • Missing tenant association

Solutions:

  • Check date range
  • Verify user tenant membership
  • Review RLS policies

Warnings Not Appearing

Possible Causes:

  • Insufficient historical data (< 90 days)
  • Risk scores below thresholds
  • Pattern thresholds not met

Solutions:

  • Wait for data accumulation
  • Adjust thresholds (admin config)
  • Review calculation logic

Released under Commercial License