diet-guard/app/lib/models/period_average.dart
Krzysztof kuhy Rudnicki e07284d3e1 Show weekly and monthly average intake on both devices
The calendar already said which individual days went over budget, but not
whether the week as a whole did. A run of small overages reads as four
harmless yellow squares; averaged, it is the thing that actually moves the
needle.

Three rules are load-bearing, and are documented in CLAUDE.md because getting
any of them wrong silently changes what "under budget" means:

- The denominator is logged days, not elapsed days. An unlogged day is not a
  zero-kcal day, and averaging gaps in as zeros would make a badly-tracked
  week look like the best week of the year.
- The yardstick is the mean of the per-day budgets over those same days,
  resolved through BudgetSchedule -- never today's budget, which would
  retroactively reclassify past weeks on every budget edit.
- Today is excluded. A half-logged today drags the mean far enough to flip
  the band, so every period ends at yesterday.

Band boundaries reuse the calendar's own OVER_BUDGET_YELLOW_CEILING by
import rather than by copy, so a period and its days can never disagree
about what "over" means; that constant loses its underscore to say so.

The MCP get_averages tool returns the average and the band but drops the
average budget, keeping the module's existing invariant that the budget
number never leaves this machine. The CLI, the gate and the phone all show
both, since it is hidden from the network, not from the user.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DjJqpcEsaV5WRhPXyxCThd
2026-08-09 14:59:40 +02:00

105 lines
2.7 KiB
Dart

/// Value types for a period's average intake and its budget band.
library;
import 'package:flutter/foundation.dart';
/// How a period's average intake compared with its average budget.
///
/// Kept in sync with `diet_guard/_averages.py`'s `AverageBand`.
enum AverageBand {
/// Average intake at or below the average budget.
under,
/// Above budget, but no more than 20% above.
slightlyOver,
/// More than 20% above budget.
veryOver,
}
/// The human phrase for [band], or `'no data'` when it is null.
///
/// Byte-identical to `_averages.band_label` so the phone and the PC gate never
/// describe the same week with different words.
String bandLabel(AverageBand? band) => switch (band) {
null => 'no data',
AverageBand.under => 'under',
AverageBand.slightlyOver => 'slightly over',
AverageBand.veryOver => 'very over',
};
/// One period's mean intake and how it compared with the mean budget.
@immutable
class PeriodAverage {
/// Creates a [PeriodAverage].
const PeriodAverage({
required this.start,
required this.end,
required this.loggedDays,
required this.elapsedDays,
required this.avgKcal,
required this.avgBudget,
required this.band,
});
/// An entirely empty period, for a screen that has not loaded yet.
static const empty = PeriodAverage(
start: '',
end: '',
loggedDays: 0,
elapsedDays: 0,
avgKcal: null,
avgBudget: null,
band: null,
);
/// `YYYY-MM-DD`, the period's first day (inclusive).
final String start;
/// `YYYY-MM-DD`, the period's last *complete* day (inclusive); earlier than
/// [start] when the period has none yet.
final String end;
/// Days in the range carrying at least one valid, non-tombstoned entry.
final int loggedDays;
/// Complete days in the range, logged or not.
final int elapsedDays;
/// Mean kcal across [loggedDays], or null when that is zero.
final double? avgKcal;
/// Mean daily budget across those same days, or null.
final double? avgBudget;
/// The band, or null when there is nothing to judge.
final AverageBand? band;
@override
bool operator ==(Object other) =>
other is PeriodAverage &&
other.start == start &&
other.end == end &&
other.loggedDays == loggedDays &&
other.elapsedDays == elapsedDays &&
other.avgKcal == avgKcal &&
other.avgBudget == avgBudget &&
other.band == band;
@override
int get hashCode => Object.hash(
start,
end,
loggedDays,
elapsedDays,
avgKcal,
avgBudget,
band,
);
@override
String toString() =>
'PeriodAverage($start..$end, $avgKcal kcal over $loggedDays/'
'$elapsedDays days, ${bandLabel(band)})';
}