Calories

1,840 / 2,200

Steps

8,432

Sleep

7h 20m

Water

1.5 / 3L

Weight Trajectory

Log Nutrition

Log Activity

Active Goals

  • Lose 5kg 60% Done
  • 10k Steps Daily On Track
  • Workout 4x/week 2/4 Done

Badges & Achievements

``` #### FILE 2: `nexus_health_analytics.html` This file serves as the "Back-end Logic & Analytics Documentation" view, presenting the SQL intelligence required to drive the dashboard. ```html Nexus Analytics Engine

Nexus Analytics Engine

Advanced SQL Logic for Health Insights

1. Calorie Deficit/Surplus Analysis (Daily)

Calculates net energy balance by subtracting burned calories from intake.

SELECT 
    DATE(n.timestamp) as date,
    SUM(n.calories) as total_intake,
    (SELECT SUM(value_numeric) FROM activities a 
     WHERE a.type = 'BURNED_CALORIES' 
     AND DATE(a.timestamp) = DATE(n.timestamp)) as total_burned,
    SUM(n.calories) - (SELECT SUM(value_numeric) FROM activities a 
                      WHERE a.type = 'BURNED_CALORIES' 
                      AND DATE(a.timestamp) = DATE(n.timestamp)) as net_balance
FROM nutrition_logs n
GROUP BY DATE(n.timestamp)
ORDER BY date DESC;
2. Workout Frequency & Intensity Trends

Aggregates workout volume and average perceived intensity per week.

SELECT 
    DATE_TRUNC('week', timestamp) as week_commencing,
    COUNT(activity_id) as sessions_per_week,
    AVG(intensity_score) as avg_intensity
FROM activities
WHERE type = 'WORKOUT'
GROUP BY week_commencing
ORDER BY week_commencing DESC;
3. Weight Loss Trajectory & Target Prediction

Calculates current rate of change to estimate target date.

WITH WeightStats AS (
    SELECT 
        weight_kg,
        recorded_at,
        LAG(weight_kg) OVER (ORDER BY recorded_at) as prev_weight,
        LAG(recorded_at) OVER (ORDER BY recorded_at) as prev_date
    FROM weight_history
),
RateOfChange AS (
    SELECT 
        (weight_kg - prev_weight) / 
        EXTRACT(DAY FROM (recorded_at - prev_date)) as daily_change
    FROM WeightStats
    WHERE prev_weight IS NOT NULL
)
SELECT 
    AVG(daily_change) as avg_daily_loss,
    ( (SELECT current_weight FROM users WHERE id = 1) - (SELECT target_weight FROM users WHERE id = 1) ) / AVG(daily_change) as days_to_goal
FROM RateOfChange;
4. Macro Distribution (Pie Chart Data)

Calculates the percentage of total calories from each macronutrient.

SELECT 
    ROUND((SUM(protein_g * 4) / SUM(calories)) * 100, 1) as protein_pct,
    ROUND((SUM(carbs_g * 4) / SUM(calories)) * 100, 1) as carbs_pct,
    ROUND((SUM(fat_g * 9) / SUM(calories)) * 100, 1) as fat_pct
FROM nutrition_logs
WHERE DATE(timestamp) = CURRENT_DATE;
5. Activity Streaks (Consecutive Days)

Uses window functions to identify consecutive logging days.

WITH DailyLogs AS (
    SELECT DISTINCT DATE(timestamp) as log_date
    FROM activities
    WHERE type IN ('WORKOUT', 'STEPS', 'SLEEP')
),
GroupedLogs AS (
    SELECT 
        log_date,
        log_date - (ROW_NUMBER() OVER (ORDER BY log_date) * INTERVAL '1 day') as grp
    FROM DailyLogs
)
SELECT 
    MIN(log_date) as streak_start,
    MAX(log_date) as streak_end,
    COUNT(*) as consecutive_days
FROM GroupedLogs
GROUP BY grp
ORDER BY consecutive_days DESC
LIMIT 1;
6. Goal Progress Percentage

Calculates how close a user is to reaching a numeric goal.

SELECT 
    goal_type,
    target_value,
    current_value,
    ((current_value / target_value) * 100) as percent_complete
FROM goals
WHERE user_id = 1 AND is_completed = FALSE;