146 lines
5.0 KiB
Dart
146 lines
5.0 KiB
Dart
import '../../../core/api_gateway.dart';
|
|
import 'file_change.dart';
|
|
|
|
/// One step's recorded `file_changes` capture. A null capture on a
|
|
/// [StageCapture] means the step recorded no capture at all - it must never be
|
|
/// read as "the workspace was unchanged".
|
|
class ChangeCapture {
|
|
ChangeCapture({
|
|
required this.status,
|
|
this.scope = '',
|
|
required this.files,
|
|
this.skipped = 0,
|
|
this.omitted = 0,
|
|
this.diagnostic = '',
|
|
});
|
|
|
|
final String status;
|
|
final String scope;
|
|
final List<FileChange> files;
|
|
final int skipped;
|
|
final int omitted;
|
|
final String diagnostic;
|
|
|
|
String get _state => status.trim().toLowerCase();
|
|
bool get unavailable => _state == 'unavailable';
|
|
bool get partial => _state == 'partial';
|
|
bool get complete => _state == 'complete' || _state.isEmpty;
|
|
bool get hasFiles => files.isNotEmpty;
|
|
|
|
factory ChangeCapture.fromJson(JsonMap json) {
|
|
final rawFiles = json['files'];
|
|
final files = <FileChange>[];
|
|
if (rawFiles is List) {
|
|
for (final value in rawFiles) {
|
|
final file = FileChange.tryParse(value);
|
|
if (file != null) files.add(file);
|
|
}
|
|
}
|
|
return ChangeCapture(
|
|
status: '${json['status'] ?? ''}'.trim(),
|
|
scope: '${json['scope'] ?? ''}'.trim(),
|
|
files: List.unmodifiable(files),
|
|
skipped: _count(json['skipped']),
|
|
omitted: _count(json['omitted']),
|
|
diagnostic: '${json['diagnostic'] ?? ''}'.trim(),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// The honest wording for a `file_changes` value that exists but is not an
|
|
/// object. Such a record is unreadable evidence, not an observed no-change.
|
|
const malformedCaptureDetail =
|
|
'The recorded change capture could not be read, so no changes can be '
|
|
'shown. This does not mean the workspace was unchanged.';
|
|
|
|
/// One stage visit and the capture it recorded, in run order. `capture == null`
|
|
/// is missing evidence, not an observed no-change.
|
|
class StageCapture {
|
|
const StageCapture({
|
|
required this.sequence,
|
|
required this.visit,
|
|
required this.stage,
|
|
required this.stepStatus,
|
|
required this.capture,
|
|
});
|
|
|
|
/// The run-wide step ordinal, used for ordering and widget keys.
|
|
final int sequence;
|
|
|
|
/// The display visit number within [stage]: the recorded `control.visit` when
|
|
/// positive, otherwise the visit's 1-based position in its stage. Mirrors the
|
|
/// web client's `displayVisit`; [sequence] is not the visit number.
|
|
final int visit;
|
|
final String stage;
|
|
final String stepStatus;
|
|
final ChangeCapture? capture;
|
|
|
|
bool get missing => capture == null;
|
|
|
|
/// Parses one step. [position] is the step's 1-based position among the
|
|
/// steps of its own stage, used when the step recorded no `control.visit`.
|
|
factory StageCapture.fromStep(JsonMap step, {int position = 1}) {
|
|
final raw = step['file_changes'];
|
|
return StageCapture(
|
|
sequence: _count(step['sequence']),
|
|
visit: _displayVisit(step, position),
|
|
stage: _stageName(step),
|
|
stepStatus: '${step['status'] ?? ''}'.trim(),
|
|
// A non-null but malformed value still records that a capture existed;
|
|
// only null is missing evidence. A value we cannot read is unavailable,
|
|
// never a fabricated empty capture that would claim "no changes".
|
|
capture: raw == null
|
|
? null
|
|
: raw is Map
|
|
? ChangeCapture.fromJson(Map<String, dynamic>.from(raw))
|
|
: ChangeCapture(
|
|
status: 'unavailable',
|
|
files: const [],
|
|
diagnostic: malformedCaptureDetail,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Groups a run's steps into stage/visit captures ordered by numeric sequence,
|
|
/// stable for ties. A step with a null `file_changes` still appears so its
|
|
/// missing evidence is visible next to the captures that did record.
|
|
List<StageCapture> stageCaptures(List<JsonMap> steps) {
|
|
final indexed = <({JsonMap step, int index})>[];
|
|
for (var i = 0; i < steps.length; i++) {
|
|
indexed.add((step: steps[i], index: i));
|
|
}
|
|
indexed.sort((a, b) {
|
|
final bySequence = _count(
|
|
a.step['sequence'],
|
|
).compareTo(_count(b.step['sequence']));
|
|
return bySequence != 0 ? bySequence : a.index.compareTo(b.index);
|
|
});
|
|
// The visit position is per stage, in the same sequence order (the web
|
|
// client's displayVisit), not the run-wide step ordinal.
|
|
final positions = <String, int>{};
|
|
final captures = <StageCapture>[];
|
|
for (final entry in indexed) {
|
|
final stage = _stageName(entry.step);
|
|
final position = (positions[stage] ?? 0) + 1;
|
|
positions[stage] = position;
|
|
captures.add(StageCapture.fromStep(entry.step, position: position));
|
|
}
|
|
return captures;
|
|
}
|
|
|
|
/// The recorded `control.visit` when positive, else the stage position.
|
|
int _displayVisit(JsonMap step, int position) {
|
|
final control = step['control'];
|
|
final recorded = control is Map ? control['visit'] : null;
|
|
if (recorded is num && recorded > 0) return recorded.toInt();
|
|
return position < 1 ? 1 : position;
|
|
}
|
|
|
|
String _stageName(JsonMap step) {
|
|
final name = '${step['stage'] ?? ''}'.trim();
|
|
return name.isEmpty ? 'stage' : name;
|
|
}
|
|
|
|
int _count(dynamic value) => value is num ? value.toInt() : 0;
|