183 lines
6.9 KiB
Dart
183 lines
6.9 KiB
Dart
import '../../../core/api_gateway.dart';
|
|
import '../../graph/domain/stage_visits.dart';
|
|
import 'artifact_document.dart';
|
|
import 'artifact_record.dart';
|
|
import 'artifact_selection.dart';
|
|
|
|
/// One gallery group: the stage-produced artifacts of a recorded stage visit
|
|
/// plus that visit's turn documents, recorded or explicitly absent.
|
|
class ArtifactGroup {
|
|
const ArtifactGroup({
|
|
required this.stageKey,
|
|
required this.visit,
|
|
required this.artifacts,
|
|
this.documents = const <ArtifactDocument>[],
|
|
});
|
|
|
|
final String stageKey;
|
|
|
|
/// The display visit number: `control.visit` when positive, else the position
|
|
/// within the stage's ordered visits. Never a synthetic visit.
|
|
final int visit;
|
|
|
|
final List<ArtifactRecord> artifacts;
|
|
|
|
/// The visit's turn-document slots. Empty when the capture was unavailable,
|
|
/// so an unknown capture is never presented as four missing documents.
|
|
final List<ArtifactDocument> documents;
|
|
|
|
/// The count the gallery header reports: stage artifacts plus the turn
|
|
/// documents the server actually recorded, never an absent slot.
|
|
int get recordedCount =>
|
|
artifacts.length + documents.where((doc) => doc.record != null).length;
|
|
}
|
|
|
|
int _sequenceOf(JsonMap step) {
|
|
final sequence = step['sequence'];
|
|
return sequence is num ? sequence.toInt() : 0;
|
|
}
|
|
|
|
/// Sequence order with recorded order as the tie-break, matching the web
|
|
/// client's stable sort.
|
|
List<JsonMap> _orderedBySequence(List<JsonMap> steps) {
|
|
final indexed = <({int position, JsonMap step})>[
|
|
for (var index = 0; index < steps.length; index += 1)
|
|
(position: index, step: steps[index]),
|
|
];
|
|
indexed.sort((a, b) {
|
|
final bySequence = _sequenceOf(a.step).compareTo(_sequenceOf(b.step));
|
|
return bySequence != 0 ? bySequence : a.position.compareTo(b.position);
|
|
});
|
|
return <JsonMap>[for (final entry in indexed) entry.step];
|
|
}
|
|
|
|
/// Groups a run's stage artifacts by stage and recorded visit.
|
|
///
|
|
/// Stages keep first-appearance order and each stage's visits are ordered by
|
|
/// sequence. A shared `public/` file is attributed to the visit that first
|
|
/// recorded it, because the server records the whole public directory on every
|
|
/// model visit. Only visits with at least one presented stage artifact produce a
|
|
/// group, so an unavailable or empty capture never yields an empty group. An
|
|
/// artifact-less run yields an empty list rather than a fabricated stage.
|
|
List<ArtifactGroup> artifactGroups(List<JsonMap> steps) {
|
|
if (steps.isEmpty) return const <ArtifactGroup>[];
|
|
|
|
final attributed = <JsonMap, List<ArtifactRecord>>{};
|
|
final seenPublic = <String>{};
|
|
for (final recordedStep in _orderedBySequence(steps)) {
|
|
final repairs = repairArtifactNames(recordedStep);
|
|
final kept = <ArtifactRecord>[];
|
|
for (final artifact in stageArtifactsFrom(
|
|
artifactsOf(recordedStep),
|
|
repairs: repairs,
|
|
)) {
|
|
if (artifact.isPublicName) {
|
|
if (seenPublic.contains(artifact.name)) continue;
|
|
seenPublic.add(artifact.name);
|
|
}
|
|
kept.add(artifact);
|
|
}
|
|
attributed[recordedStep] = List<ArtifactRecord>.unmodifiable(kept);
|
|
}
|
|
|
|
final groups = <ArtifactGroup>[];
|
|
for (final entry in visitsByStage(steps).entries) {
|
|
final visits = entry.value;
|
|
for (var index = 0; index < visits.length; index += 1) {
|
|
final artifacts = attributed[visits[index]];
|
|
if (artifacts == null || artifacts.isEmpty) continue;
|
|
groups.add(
|
|
ArtifactGroup(
|
|
stageKey: entry.key,
|
|
visit: displayVisit(visits[index], index),
|
|
artifacts: artifacts,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
return List<ArtifactGroup>.unmodifiable(groups);
|
|
}
|
|
|
|
String _galleryKey(String stageKey, int visit) => '$stageKey\u0000$visit';
|
|
|
|
/// The gallery's per-visit groups: stage artifacts and turn documents in one
|
|
/// section per recorded visit.
|
|
///
|
|
/// Stage-only grouping is unchanged ([artifactGroups]); this function adds the
|
|
/// turn-document slots a visit recorded and keeps a visit whose only recorded
|
|
/// files are turn documents. A visit with neither stage artifacts nor a
|
|
/// recorded document, or whose capture is unavailable, produces no group.
|
|
List<ArtifactGroup> galleryGroups(List<JsonMap> steps) {
|
|
if (steps.isEmpty) return const <ArtifactGroup>[];
|
|
final stageGroups = <String, ArtifactGroup>{
|
|
for (final group in artifactGroups(steps))
|
|
_galleryKey(group.stageKey, group.visit): group,
|
|
};
|
|
final groups = <ArtifactGroup>[];
|
|
for (final entry in visitsByStage(steps).entries) {
|
|
for (var index = 0; index < entry.value.length; index += 1) {
|
|
final step = entry.value[index];
|
|
final visit = displayVisit(step, index);
|
|
final base = stageGroups[_galleryKey(entry.key, visit)];
|
|
final documents = visitDocuments(step);
|
|
final recordedDocuments = documents.where((doc) => doc.record != null);
|
|
if ((base?.artifacts.isEmpty ?? true) && recordedDocuments.isEmpty) {
|
|
continue;
|
|
}
|
|
groups.add(
|
|
ArtifactGroup(
|
|
stageKey: entry.key,
|
|
visit: visit,
|
|
artifacts: base?.artifacts ?? const <ArtifactRecord>[],
|
|
documents: documents,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
return List<ArtifactGroup>.unmodifiable(groups);
|
|
}
|
|
|
|
/// One visit's stage artifacts, excluding shared public files that an earlier
|
|
/// visit already recorded. A repeated non-public name in a later visit stays
|
|
/// because it is a different recorded artifact, not the same shared file.
|
|
List<ArtifactRecord> visitArtifacts(JsonMap step, List<JsonMap> runSteps) {
|
|
final all = stageArtifacts(step);
|
|
if (runSteps.isEmpty) return all;
|
|
final seen = <String>{};
|
|
for (final candidate in _orderedBySequence(runSteps)) {
|
|
if (identical(candidate, step) ||
|
|
_sequenceOf(candidate) == _sequenceOf(step)) {
|
|
break;
|
|
}
|
|
for (final artifact in stageArtifacts(candidate)) {
|
|
if (artifact.isPublicName) seen.add(artifact.name);
|
|
}
|
|
}
|
|
return <ArtifactRecord>[
|
|
for (final artifact in all)
|
|
if (!artifact.isPublicName || !seen.contains(artifact.name)) artifact,
|
|
];
|
|
}
|
|
|
|
/// The gallery group of exactly one recorded visit, addressed by its index in
|
|
/// [runSteps]. The whole run is still read so a shared `public/` file stays with
|
|
/// the visit that first recorded it and the group keeps the run's own visit
|
|
/// numbering. Null when the index is out of range or the visit recorded no
|
|
/// presentable stage artifact.
|
|
ArtifactGroup? visitArtifactGroup(List<JsonMap> runSteps, int index) {
|
|
if (index < 0 || index >= runSteps.length) return null;
|
|
final step = runSteps[index];
|
|
final stage = '${step['stage'] ?? ''}';
|
|
final visits = visitsByStage(runSteps)[stage];
|
|
if (visits == null) return null;
|
|
// The pane passes the index of the same parsed list, so the recorded map is
|
|
// the identical instance the stage grouping holds.
|
|
final position = visits.indexOf(step);
|
|
if (position < 0) return null;
|
|
final visit = displayVisit(step, position);
|
|
for (final group in artifactGroups(runSteps)) {
|
|
if (group.stageKey == stage && group.visit == visit) return group;
|
|
}
|
|
return null;
|
|
}
|