Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

6.9 KB
Raw Blame
/**
 * MeatSpace Logging Stats
 *
 * Aggregates the daily-dated health logs (alcohol, nicotine, workouts, body,
 * blood pressure) into a single glanceable summary for the dashboard streak
 * widget: a cross-domain logging streak, per-domain this-week counts, and a
 * 7-day sparkline. Pure read — composes the existing per-domain getters rather
 * than reaching into their storage files, so MortalLoom-backed installs are
 * covered for free.
 */

import { getDateString } from '../lib/fileUtils.js';
import { getDailyAlcohol } from './meatspaceAlcohol.js';
import { getDailyNicotine } from './meatspaceNicotine.js';
import { getWorkouts, getBodyHistory, getBloodPressureHistory } from './meatspaceHealth.js';

// Each domain contributes an array of `{ date: 'YYYY-MM-DD', ... }` records.
// `label` is the human-facing name shown per-domain in the widget.
// `load(options)` forwards `{ strict }` so a caller that counts these can refuse an
// unreadable file rather than a fake empty one (#2726).
const DOMAINS = [
  { key: 'alcohol', label: 'Alcohol', load: (o) => getDailyAlcohol(undefined, undefined, o) },
  { key: 'nicotine', label: 'Nicotine', load: (o) => getDailyNicotine(undefined, undefined, o) },
  { key: 'workouts', label: 'Workouts', load: (o) => getWorkouts(o) },
  { key: 'body', label: 'Body', load: (o) => getBodyHistory(o) },
  { key: 'bloodPressure', label: 'Blood Pressure', load: (o) => getBloodPressureHistory(o) },
];

// Consecutive days (ending today, or yesterday if today is empty) that have at
// least one log in any domain. Mirrors usage.js's calculateStreak so the two
// dashboard streaks read the same way.
function calculateStreak(loggedDates) {
  let streak = 0;
  const checkDate = new Date();
  // Normalize to midnight so day arithmetic is stable across DST.
  checkDate.setHours(0, 0, 0, 0);
  while (true) {
    const dateStr = getDateString(checkDate);
    if (loggedDates.has(dateStr)) {
      streak++;
      checkDate.setDate(checkDate.getDate() - 1);
    } else if (streak === 0) {
      // Grace: a gap today doesn't break a streak that ran through yesterday.
      checkDate.setDate(checkDate.getDate() - 1);
      if (!loggedDates.has(getDateString(checkDate))) break;
    } else {
      break;
    }
  }
  return streak;
}

// Longest run of consecutive logged days anywhere in history.
function calculateLongestStreak(loggedDates) {
  const dates = [...loggedDates].sort();
  let longest = 0;
  let run = 0;
  let prev = null;
  for (const dateStr of dates) {
    if (prev) {
      const diffDays = Math.round(
        (new Date(dateStr) - new Date(prev)) / (1000 * 60 * 60 * 24)
      );
      run = diffDays === 1 ? run + 1 : 1;
    } else {
      run = 1;
    }
    longest = Math.max(longest, run);
    prev = dateStr;
  }
  return longest;
}

/**
 * @param {{ strict?: boolean, withActiveDayKeys?: boolean }} [options]
 *
 * `withActiveDayKeys: true` additionally returns `activeDayKeys` — the sorted union of every
 * stored day key across the five domains, i.e. the raw `loggedDates` set the aggregates below
 * are derived from. Opt-in rather than always-on because `GET /api/meatspace/logging-stats`
 * ships this whole object to a dashboard widget that polls it, and the key list grows without
 * bound with install age; the only caller that needs the raw days is the Character sheet's
 * cross-domain `daysActive` tile, which must UNION them with POST's days rather than sum two
 * per-domain counts (#4120). The keys are returned AS STORED (server-local `getDateString()`
 * day labels) — see `server/lib/activeDays.js` for the day-boundary reconciliation the union
 * applies on read.
 *
 * `strict: true` propagates a domain's read failure instead of substituting an empty entry
 * list for it (#2726).
 *
 *   The default per-domain `.catch(() => [])` is right for the dashboard widget: one
 *   unreadable file shouldn't blank the whole streak card. It is wrong for anything
 *   that COUNTS these — all five domains can fail and still produce a perfectly
 *   plausible `totalLogged: 0`, which reads as "you have never logged anything"
 *   rather than "we could not tell". Under strict the first failure rejects, and the
 *   caller renders that as explicitly unavailable.
 */
export async function getLoggingStats({ strict = false, withActiveDayKeys = false } = {}) {
  const loadEntries = async (d) => {
    if (!strict) return (await d.load().catch(() => [])) || [];
    const entries = await d.load({ strict: true });
    // Validate, don't coerce: a non-array here means the loader returned something
    // uncountable, which under strict must not silently become a zero.
    if (!Array.isArray(entries)) throw new Error(`${d.key} logs unreadable`);
    return entries;
  };
  const perDomain = await Promise.all(
    DOMAINS.map(async (d) => ({ ...d, entries: await loadEntries(d) }))
  );

  const today = new Date();
  today.setHours(0, 0, 0, 0);
  const weekAgo = new Date(today);
  weekAgo.setDate(weekAgo.getDate() - 6); // inclusive 7-day window (today + 6 prior)
  const weekAgoStr = getDateString(weekAgo);
  const todayStr = getDateString(today);

  // Union of every logged date across domains → drives the streak + sparkline.
  const loggedDates = new Set();
  const domains = [];
  let totalLogged = 0;

  for (const d of perDomain) {
    let thisWeek = 0;
    for (const entry of d.entries) {
      if (!entry?.date) continue;
      loggedDates.add(entry.date);
      totalLogged++;
      if (entry.date >= weekAgoStr && entry.date <= todayStr) thisWeek++;
    }
    domains.push({ key: d.key, label: d.label, total: d.entries.length, thisWeek });
  }

  // 7-day sparkline: count of distinct domains logged each day (0..DOMAINS.length).
  const perDayDomainCount = new Map();
  for (const d of perDomain) {
    const daysSeen = new Set();
    for (const entry of d.entries) {
      if (entry?.date && entry.date >= weekAgoStr && entry.date <= todayStr) daysSeen.add(entry.date);
    }
    for (const date of daysSeen) perDayDomainCount.set(date, (perDayDomainCount.get(date) || 0) + 1);
  }

  const last7Days = [];
  for (let i = 6; i >= 0; i--) {
    const date = new Date(today);
    date.setDate(date.getDate() - i);
    const dateStr = getDateString(date);
    last7Days.push({
      date: dateStr,
      label: date.toLocaleDateString('en-US', { weekday: 'short' }),
      domains: perDayDomainCount.get(dateStr) || 0,
      logged: loggedDates.has(dateStr),
    });
  }

  return {
    currentStreak: calculateStreak(loggedDates),
    longestStreak: calculateLongestStreak(loggedDates),
    weekTotal: domains.reduce((sum, d) => sum + d.thisWeek, 0),
    totalLogged,
    domains,
    last7Days,
    // Sorted so the array is stable and diffable; a Set would not survive the JSON round-trip
    // this object takes through the route. Absent (not `[]`) when the caller did not ask —
    // "you did not request the day keys" must stay distinguishable from "there are none".
    ...(withActiveDayKeys ? { activeDayKeys: [...loggedDates].sort() } : {}),
  };
}