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 [], }); 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 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 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 _orderedBySequence(List 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 [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 artifactGroups(List steps) { if (steps.isEmpty) return const []; final attributed = >{}; final seenPublic = {}; for (final recordedStep in _orderedBySequence(steps)) { final repairs = repairArtifactNames(recordedStep); final kept = []; 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.unmodifiable(kept); } final groups = []; 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.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 galleryGroups(List steps) { if (steps.isEmpty) return const []; final stageGroups = { for (final group in artifactGroups(steps)) _galleryKey(group.stageKey, group.visit): group, }; final groups = []; 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 [], documents: documents, ), ); } } return List.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 visitArtifacts(JsonMap step, List runSteps) { final all = stageArtifacts(step); if (runSteps.isEmpty) return all; final seen = {}; 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 [ 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 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; }