FitTrack

Fitness, nutrition, measurements, goals, streaks
Local browser storage
Calories In
0
Net Deficit / Surplus
0
Exercise kcal
0
Steps
0
Sleep
0
Logging Streak
0

Profile & Energy

Macro Split — Last 7 Days

Calories In vs Net — Last 21 Days

Weight Trend

Workout Calories — Last 21 Days

Goal Progress

Daily Activity

Add Workout

Recent Daily Logs

Recent Workouts

Workouts per Week

Add Meal

Nutrition Summary — Today

Recent Meals

Add Measurement

Measurement Summary

Measurement History

Add Goal

Active Goals

``` --- ## File 2 — `qwen_health_analytics.html` ```html Qwen Health Analytics

Health Analytics

SQL-first analytics for fitness, nutrition, and body composition
Reads local tracker data when available
Avg Daily Net — 30d
0
Workouts — 30d
0
Weight Change
0
Current Streak
0
Longest Streak
0
Projected Target Date
—

Calorie Deficit / Surplus

Weight Trajectory

Calorie Deficit / Surplus Over Time

Workout Frequency & Intensity

Weight Loss / Gain Trajectory

Macro Distribution

Activity Streaks

Goal Progress

1. Calorie Deficit / Surplus Analysis

WITH daily_intake AS (
  SELECT user_id, meal_date AS log_date, SUM(calories) AS calories_in
  FROM meals
  WHERE user_id = :user_id
  GROUP BY user_id, meal_date
),
daily_workouts AS (
  SELECT user_id, workout_date AS log_date, SUM(calories_burned) AS exercise_kcal
  FROM workouts
  WHERE user_id = :user_id
  GROUP BY user_id, workout_date
),
profile AS (
  SELECT user_id, tdee_kcal
  FROM health_profiles
  WHERE user_id = :user_id
)
SELECT
  i.log_date,
  i.calories_in,
  COALESCE(w.exercise_kcal, 0) AS exercise_kcal,
  p.tdee_kcal + COALESCE(w.exercise_kcal, 0) AS total_out,
  p.tdee_kcal + COALESCE(w.exercise_kcal, 0) - i.calories_in AS net_deficit,
  CASE
    WHEN p.tdee_kcal + COALESCE(w.exercise_kcal, 0) - i.calories_in >= 0
    THEN 'deficit'
    ELSE 'surplus'
  END AS balance_type
FROM daily_intake i
JOIN profile p ON p.user_id = i.user_id
LEFT JOIN daily_workouts w ON w.user_id = i.user_id AND w.log_date = i.log_date
ORDER BY i.log_date;

2. Workout Frequency and Intensity Trends

SELECT
  date_trunc('week', workout_date) AS week_start,
  COUNT(*) AS sessions,
  SUM(duration_min) AS total_minutes,
  ROUND(AVG(duration_min)) AS avg_minutes,
  SUM(calories_burned) AS calories_burned,
  AVG(
    CASE intensity
      WHEN 'light' THEN 1
      WHEN 'moderate' THEN 2
      WHEN 'vigorous' THEN 3
      ELSE 0
    END
  ) AS avg_intensity_score
FROM workouts
WHERE user_id = :user_id
GROUP BY date_trunc('week', workout_date)
ORDER BY week_start;

3. Weight Trajectory and Target Date

WITH m AS (
  SELECT
    user_id,
    measurement_date,
    weight_kg,
    EXTRACT(EPOCH FROM measurement_date) / 86400 AS x
  FROM body_measurements
  WHERE user_id = :user_id
),
reg AS (
  SELECT
    user_id,
    regr_slope(weight_kg, x) AS slope_per_day,
    regr_intercept(weight_kg, x) AS intercept
  FROM m
  GROUP BY user_id
),
latest AS (
  SELECT DISTINCT ON (user_id)
    user_id,
    measurement_date AS latest_date,
    weight_kg AS latest_weight
  FROM m
  ORDER BY user_id, measurement_date DESC
),
targets AS (
  SELECT user_id, target_weight_kg
  FROM health_profiles
  WHERE user_id = :user_id
)
SELECT
  t.user_id,
  t.target_weight_kg,
  l.latest_weight,
  r.slope_per_day,
  r.slope_per_day * 7 AS weekly_change_kg,
  CASE
    WHEN r.slope_per_day IS NULL OR r.slope_per_day = 0 THEN NULL
    WHEN t.target_weight_kg < l.latest_weight AND r.slope_per_day < 0 THEN
      l.latest_date + ((t.target_weight_kg - l.latest_weight) / r.slope_per_day) * INTERVAL '1 day'
    WHEN t.target_weight_kg > l.latest_weight AND r.slope_per_day > 0 THEN
      l.latest_date + ((t.target_weight_kg - l.latest_weight) / r.slope_per_day) * INTERVAL '1 day'
    ELSE NULL
  END AS projected_target_date
FROM targets t
JOIN reg r ON r.user_id = t.user_id
JOIN latest l ON l.user_id = t.user_id;

4. Macro Distribution Pie Chart

WITH totals AS (
  SELECT
    SUM(protein_g) AS protein_g,
    SUM(carbs_g) AS carbs_g,
    SUM(fat_g) AS fat_g
  FROM meals
  WHERE user_id = :user_id
    AND meal_date >= CURRENT_DATE - INTERVAL '30 days'
)
SELECT
  protein_g,
  carbs_g,
  fat_g,
  protein_g * 4 AS protein_kcal,
  carbs_g * 4 AS carbs_kcal,
  fat_g * 9 AS fat_kcal,
  (protein_g * 4 + carbs_g * 4 + fat_g * 9) AS macro_kcal,
  ROUND((protein_g * 4) / NULLIF(protein_g * 4 + carbs_g * 4 + fat_g * 9, 0) * 100) AS protein_pct,
  ROUND((carbs_g * 4) / NULLIF(protein_g * 4 + carbs_g * 4 + fat_g * 9, 0) * 100) AS carbs_pct,
  ROUND((fat_g * 9) / NULLIF(protein_g * 4 + carbs_g * 4 + fat_g * 9, 0) * 100) AS fat_pct
FROM totals;

5. Activity Streaks

WITH logged_days AS (
  SELECT user_id, log_date FROM daily_logs WHERE steps > 0 OR sleep_hours > 0
  UNION
  SELECT user_id, meal_date FROM meals
  UNION
  SELECT user_id, workout_date FROM workouts
),
days AS (
  SELECT DISTINCT user_id, log_date
  FROM logged_days
),
islands AS (
  SELECT
    user_id,
    log_date,
    log_date - (ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY log_date) || ' days')::interval AS grp
  FROM days
)
SELECT
  user_id,
  MIN(log_date) AS streak_start,
  MAX(log_date) AS streak_end,
  COUNT(*) AS streak_length
FROM islands
GROUP BY user_id, grp
ORDER BY streak_length DESC, streak_start DESC;

6. Goal Progress Percentage

WITH metrics AS (
  SELECT
    g.id AS goal_id,
    CASE g.metric
      WHEN 'workouts' THEN (
        SELECT COUNT(*) FROM workouts w WHERE w.user_id = g.user_id
      )
      WHEN 'steps' THEN (
        SELECT COALESCE(SUM(d.steps), 0) FROM daily_logs d WHERE d.user_id = g.user_id
      )
      WHEN 'protein_g' THEN (
        SELECT ROUND(AVG(protein_g)) FROM (
          SELECT meal_date, SUM(protein_g) AS protein_g
          FROM meals
          WHERE user_id = g.user_id
          GROUP BY meal_date
        ) x
      )
      WHEN 'calories_deficit' THEN (
        SELECT COALESCE(SUM(GREATEST(0, net_deficit)), 0) FROM (
          SELECT
            i.meal_date,
            p.tdee_kcal + COALESCE(w.exercise_kcal, 0) - i.calories_in AS net_deficit
          FROM (
            SELECT meal_date, SUM(calories) AS calories_in
            FROM meals WHERE user_id = g.user_id GROUP BY meal_date
          ) i
          JOIN health_profiles p ON p.user_id = g.user_id
          LEFT JOIN (
            SELECT workout_date, SUM(calories_burned) AS exercise_kcal
            FROM workouts WHERE user_id = g.user_id GROUP BY workout_date
          ) w ON w.workout_date = i.meal_date
        ) x
      )
      ELSE NULL
    END AS metric_value
  FROM goals g
  WHERE g.user_id = :user_id
)
SELECT
  g.title,
  g.metric,
  g.target_value,
  m.metric_value,
  CASE
    WHEN g.target_value > 0 THEN
      LEAST(100, ROUND((m.metric_value / g.target_value) * 100))
    ELSE 0
  END AS progress_pct
FROM goals g
JOIN metrics m ON m.goal_id = g.id
ORDER BY progress_pct DESC;

Schema — PostgreSQL

CREATE TABLE users (
    id BIGSERIAL PRIMARY KEY,
    email VARCHAR(255) UNIQUE NOT NULL,
    name VARCHAR(255) NOT NULL,
    timezone VARCHAR(64) DEFAULT 'UTC',
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE TABLE health_profiles (
    id BIGSERIAL PRIMARY KEY,
    user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    sex VARCHAR(10),
    birth_date DATE,
    age SMALLINT,
    height_cm NUMERIC(6,2),
    current_weight_kg NUMERIC(6,2),
    target_weight_kg NUMERIC(6,2),
    target_date DATE,
    activity_level NUMERIC(4,3) DEFAULT 1.375,
    calorie_goal INTEGER,
    tdee_kcal INTEGER,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE TABLE daily_logs (
    id BIGSERIAL PRIMARY KEY,
    user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    log_date DATE NOT NULL,
    steps INTEGER DEFAULT 0,
    sleep_hours NUMERIC(4,2) DEFAULT 0,
    notes TEXT,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    UNIQUE(user_id, log_date)
);

CREATE TABLE workouts (
    id BIGSERIAL PRIMARY KEY,
    user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    workout_date DATE NOT NULL,
    workout_type VARCHAR(100) NOT NULL,
    duration_min INTEGER NOT NULL,
    intensity VARCHAR(20),
    intensity_score SMALLINT,
    calories_burned INTEGER,
    notes TEXT,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE TABLE meals (
    id BIGSERIAL PRIMARY KEY,
    user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    meal_date DATE NOT NULL,
    meal_type VARCHAR(30) NOT NULL,
    food_name VARCHAR(255) NOT NULL,
    calories INTEGER NOT NULL,
    protein_g NUMERIC(7,2) DEFAULT 0,
    carbs_g NUMERIC(7,2) DEFAULT 0,
    fat_g NUMERIC(7,2) DEFAULT 0,
    fiber_g NUMERIC(7,2) DEFAULT 0,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE TABLE body_measurements (
    id BIGSERIAL PRIMARY KEY,
    user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    measurement_date DATE NOT NULL,
    weight_kg NUMERIC(6,2),
    body_fat_pct NUMERIC(5,2),
    waist_cm NUMERIC(6,2),
    hips_cm NUMERIC(6,2),
    chest_cm NUMERIC(6,2),
    notes TEXT,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE TABLE goals (
    id BIGSERIAL PRIMARY KEY,
    user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    title VARCHAR(255) NOT NULL,
    metric VARCHAR(50) NOT NULL,
    target_value NUMERIC(10,2) NOT NULL,
    unit VARCHAR(30),
    due_date DATE,
    status VARCHAR(20) DEFAULT 'active',
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE INDEX idx_daily_logs_user_date ON daily_logs(user_id, log_date);
CREATE INDEX idx_workouts_user_date ON workouts(user_id, workout_date);
CREATE INDEX idx_meals_user_date ON meals(user_id, meal_date);
CREATE INDEX idx_measurements_user_date ON body_measurements(user_id, measurement_date);