Files
pipelines/app/lib/features/runs/ui/run_detail_visit_facts.dart
T

106 lines
4.3 KiB
Dart

import '../../../core/api_gateway.dart';
import '../../graph/domain/stage_definition.dart';
import '../../pipelines/pipeline_format.dart';
import 'run_detail_content.dart';
/// The exact empty wording for a visit's recorded Input and Output. They are
/// the run tabs' own sentences, so a visit never says something different for
/// the same absent fact.
const String visitEmptyInput = 'No input recorded.';
const String visitEmptyOutput = 'No output recorded.';
/// One always-visible fact of a recorded visit.
typedef VisitFact = ({String label, String value});
/// The detail's own recorded steps as maps, in recorded order.
List<JsonMap> detailRunSteps(JsonMap run) {
final steps = run['steps'];
if (steps is! List) return const <JsonMap>[];
return steps.whereType<Map>().map(JsonMap.from).toList();
}
/// The raw step at [index] of the detail's own `steps`, or null when the reply
/// has no such recorded visit.
///
/// The order is the server's recorded order, the same order [detailTimeline]
/// uses, so a row and its pane always describe one visit.
JsonMap? detailVisitStepAt(JsonMap run, int index) {
final steps = detailRunSteps(run);
if (index < 0 || index >= steps.length) return null;
return steps[index];
}
/// The row index of [stageKey]'s latest recorded visit, or null when the run
/// never recorded that stage. It reads the same ordered timeline the rows do,
/// so a Graph click opens exactly the row the operator would have selected.
int? latestDetailVisitIndex(dynamic steps, String stageKey) {
final visits = detailTimeline(steps);
int? latest;
for (var index = 0; index < visits.length; index += 1) {
if (visits[index].stage == stageKey) latest = index;
}
return latest;
}
/// The visit's recorded facts, in the pane's order. A field the server did not
/// record is absent rather than zero, false or a guessed value.
List<VisitFact> detailVisitFacts(JsonMap step) {
final facts = <VisitFact>[];
final status = _text(step['status']);
if (status != null) facts.add((label: 'Status', value: status));
final outcome = _text(step['outcome']);
if (outcome != null) facts.add((label: 'Outcome', value: outcome));
final duration = detailVisitDuration(step);
if (duration != null) facts.add((label: 'Duration', value: duration));
final model = detailVisitProviderModel(step);
if (model != null) facts.add((label: 'Provider/model', value: model));
final target = _text(step['target']);
if (target != null) facts.add((label: 'Target', value: target));
return facts;
}
/// The visit's duration, only when the server recorded both timestamps and the
/// finish is not before the start. One stamp is not a zero-length visit.
String? detailVisitDuration(JsonMap step) {
final seconds = durationSeconds(
_text(step['started_at']),
_text(step['finished_at']),
);
return seconds == null ? null : formatDuration(seconds);
}
/// The effective provider/model this visit ran with, joined as
/// `provider / model` when both were recorded. A visit that recorded neither
/// has no row; one recorded part is shown alone, never completed locally.
String? detailVisitProviderModel(JsonMap step) {
final provider = _text(step['provider']);
final model = _text(step['model']);
if (provider != null && model != null) return '$provider / $model';
return provider ?? model;
}
/// A recorded verdict, or null when the server recorded no option and no
/// reason. An empty recorded verdict is not rendered as a decision.
({String? option, String? reason})? detailVisitVerdict(JsonMap step) {
final verdict = step['verdict'];
if (verdict is! Map) return null;
final option = _text(verdict['option']);
final reason = _text(verdict['reason']);
if (option == null && reason == null) return null;
return (option: option, reason: reason);
}
/// The visit's diagnostic exactly as recorded, or null when it is blank.
String? detailVisitDiagnostic(JsonMap step) => _text(step['diagnostic']);
/// Whether the reply's pinned graph declares [stageKey], so the Definition
/// section shows the authored definition rather than its missing wording.
bool detailVisitHasDefinition(JsonMap? graph, String stageKey) =>
graph != null && stageDefinitionFor(graph, stageKey) != null;
String? _text(dynamic value) {
if (value == null) return null;
final text = '$value'.trim();
return text.isEmpty ? null : text;
}