Files
pipelines/app/lib/features/changes/domain/run_changes.dart
T

110 lines
3.7 KiB
Dart

import '../../../core/api_gateway.dart';
import 'change_capture.dart';
import 'file_change.dart';
/// The run-level final-state aggregate of recorded file captures. Ported from
/// `web/src/changes.js` `runChanges`:
///
/// * a null `file_changes` is missing evidence (pending while the step is
/// running/waiting), never "no changes";
/// * a present capture with no files is a real, observed no-change;
/// * a path recorded by several visits is one entry, in first-seen order, whose
/// per-file facts come from the last recorded visit (the run's final state);
/// * capture counters and caveats describe every contributing step.
class RunChanges {
const RunChanges({
required this.files,
required this.stepsWithChanges,
required this.stepsPending,
required this.added,
required this.removed,
required this.partial,
required this.unavailable,
required this.pending,
required this.missingCaptures,
required this.skipped,
required this.omitted,
required this.diagnostic,
});
final List<FileChange> files;
final int stepsWithChanges;
final int stepsPending;
final int added;
final int removed;
final bool partial;
final bool unavailable;
final bool pending;
final int missingCaptures;
final int skipped;
final int omitted;
final String diagnostic;
}
RunChanges runChanges(List<JsonMap> steps) =>
runChangesFor(stageCaptures(steps));
/// Whether the run-wide final-state aggregate may be presented. Ported from
/// `web/src/changes.js` `showRunChangesSummary`: only a settled succeeded run
/// has a final workspace state. A failed or running run keeps its evidence in
/// each visit's own captured section instead of claiming a settled summary.
bool showRunChangesSummary(Object? status) =>
status != null && '$status'.trim().toLowerCase() == 'succeeded';
/// The same aggregate over already-derived [stageCaptures], so a caller that
/// shows both the summary and the sections parses each capture only once.
RunChanges runChangesFor(List<StageCapture> visits) {
final entries = <String, FileChange>{};
var stepsWithChanges = 0;
var stepsPending = 0;
var partial = false;
var unavailable = false;
var missingCaptures = 0;
var skipped = 0;
var omitted = 0;
var truncatedSeen = false;
final diagnostics = <String>{};
for (final visit in visits) {
final capture = visit.capture;
if (capture == null) {
final status = visit.stepStatus.trim().toLowerCase();
if (status == 'running' || status == 'waiting') {
stepsPending++;
} else {
missingCaptures++;
}
continue;
}
stepsWithChanges++;
if (capture.unavailable) unavailable = true;
if (capture.partial) partial = true;
if (capture.diagnostic.isNotEmpty) diagnostics.add(capture.diagnostic);
skipped += capture.skipped;
omitted += capture.omitted;
for (final file in capture.files) {
if (file.truncated) truncatedSeen = true;
final existing = entries[file.path];
final names = existing == null ? <String>[] : [...existing.steps];
if (!names.contains(visit.stage)) names.add(visit.stage);
entries[file.path] = file.withSteps(names);
}
}
final files = entries.values.toList(growable: false);
return RunChanges(
files: List.unmodifiable(files),
stepsWithChanges: stepsWithChanges,
stepsPending: stepsPending,
added: files.fold(0, (sum, file) => sum + file.added),
removed: files.fold(0, (sum, file) => sum + file.removed),
partial: partial || truncatedSeen || omitted > 0,
unavailable: unavailable,
pending: stepsWithChanges == 0 && stepsPending > 0,
missingCaptures: missingCaptures,
skipped: skipped,
omitted: omitted,
diagnostic: diagnostics.join(' '),
);
}