import '../../../core/api_gateway.dart'; import '../../graph/domain/stage_visits.dart'; import 'artifact_record.dart'; import 'artifact_selection.dart'; /// One gallery group: the stage-produced artifacts of a recorded stage visit. class ArtifactGroup { const ArtifactGroup({ required this.stageKey, required this.visit, required this.artifacts, }); 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; } 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); } /// 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, ]; }