Files
pipelines/app/lib/features/pipelines/pipeline_models.dart
T

243 lines
7.4 KiB
Dart

import 'package:flutter/foundation.dart';
/// The shared decoded-envelope type from the transport contract.
typedef PipelineJson = Map<String, dynamic>;
/// Whether a pipeline currently has configured schedules.
///
/// Configuration is not run provenance: an unknown older record stays
/// [unclassified] rather than being reported as unscheduled.
enum PipelineScheduleState { scheduled, unscheduled, unclassified }
/// One row of `list_pipelines`.
@immutable
class PipelineSummary {
const PipelineSummary({
required this.id,
required this.name,
required this.version,
required this.stages,
required this.createdAt,
required this.updatedAt,
required this.scheduled,
required this.scheduleCount,
required this.enabledScheduleCount,
});
factory PipelineSummary.fromJson(PipelineJson json) {
final scheduleCount = _optionalInt(json['schedule_count']);
final scheduled = json['scheduled'];
return PipelineSummary(
id: _int(json['id']),
name: _string(json['name']),
version: _int(json['version']),
stages: _int(json['stages']),
createdAt: _string(json['created_at']),
updatedAt: _string(json['updated_at']),
scheduled: scheduled is bool ? scheduled : null,
scheduleCount: scheduleCount,
enabledScheduleCount: _optionalInt(json['enabled_schedule_count']),
);
}
final int id;
final String name;
final int version;
final int stages;
final String createdAt;
final String updatedAt;
final bool? scheduled;
final int? scheduleCount;
final int? enabledScheduleCount;
String get displayName => name.isEmpty ? 'pipeline $id' : name;
/// Mirrors the web's schedule configuration rule, including its fallbacks
/// for peers that predate the schedule fields.
PipelineScheduleState get scheduleState {
if (scheduleCount != null && scheduleCount! > 0) {
return PipelineScheduleState.scheduled;
}
if (scheduled != null) {
return scheduled!
? PipelineScheduleState.scheduled
: PipelineScheduleState.unscheduled;
}
if (scheduleCount == 0) return PipelineScheduleState.unscheduled;
return PipelineScheduleState.unclassified;
}
/// The configured-schedule label, or an empty string when unscheduled.
String get scheduleLabel {
if (scheduleState != PipelineScheduleState.scheduled) return '';
final count = scheduleCount;
if (count != null && count > 0) {
return '$count ${count == 1 ? 'schedule' : 'schedules'}';
}
return 'Has schedules';
}
}
/// One entry of a pipeline detail's version history.
@immutable
class PipelineVersion {
const PipelineVersion({
required this.version,
required this.stages,
required this.createdAt,
});
factory PipelineVersion.fromJson(PipelineJson json) => PipelineVersion(
version: _int(json['version']),
stages: _int(json['stages']),
createdAt: _string(json['created_at']),
);
final int version;
final int stages;
final String createdAt;
}
/// The `detail` payload of a `get_pipeline` reply.
@immutable
class PipelineDetail extends PipelineSummary {
const PipelineDetail({
required super.id,
required super.name,
required super.version,
required super.stages,
required super.createdAt,
required super.updatedAt,
required super.scheduled,
required super.scheduleCount,
required super.enabledScheduleCount,
required this.latestVersion,
required this.versionCreatedAt,
required this.contentHash,
required this.source,
required this.versions,
this.graph,
this.shape,
});
factory PipelineDetail.fromJson(PipelineJson json) {
final summary = PipelineSummary.fromJson(json);
final versions = json['versions'];
return PipelineDetail(
id: summary.id,
name: summary.name,
version: summary.version,
stages: summary.stages,
createdAt: summary.createdAt,
updatedAt: summary.updatedAt,
scheduled: summary.scheduled,
scheduleCount: summary.scheduleCount,
enabledScheduleCount: summary.enabledScheduleCount,
latestVersion: _int(json['latest_version']),
versionCreatedAt: _string(json['version_created_at']),
contentHash: _string(json['content_hash']),
source: _string(json['source']),
versions: versions is List
? List.unmodifiable(
versions.whereType<Map>().map(
(version) => PipelineVersion.fromJson(
Map<String, dynamic>.from(version),
),
),
)
: const <PipelineVersion>[],
graph: _jsonObject(json['graph']),
shape: _jsonObject(json['shape']),
);
}
final int latestVersion;
final String versionCreatedAt;
final String contentHash;
final String source;
final List<PipelineVersion> versions;
/// The exact definition graph this reply pinned for [resolvedVersion].
///
/// Raw JSON: its fields are never normalised here. Null means the reply
/// carried no graph, which is not the same as a definition with no stages.
final PipelineJson? graph;
/// The optional compact graph summary some replies add, kept verbatim.
final PipelineJson? shape;
/// The concrete version this detail describes. A request for version 0 asks
/// for latest, so the reply's own `version` is authoritative for display.
int get resolvedVersion => version > 0 ? version : latestVersion;
}
/// The subset of a run summary the pipeline list needs for its last-run join.
@immutable
class PipelineRun {
const PipelineRun({
required this.id,
required this.pipelineId,
required this.status,
required this.createdAt,
required this.finishedAt,
required this.executing,
});
factory PipelineRun.fromJson(PipelineJson json) => PipelineRun(
id: _int(json['id']),
pipelineId: _int(json['pipeline_id']),
status: _string(json['status']),
createdAt: _string(json['created_at']),
finishedAt: _string(json['finished_at']),
executing: json['executing'] == true,
);
final int id;
final int pipelineId;
final String status;
final String createdAt;
final String finishedAt;
final bool executing;
}
/// Timestamps decide recency; a higher run id breaks an exact tie or two bad
/// timestamps. This is the one recency rule shared by the list's join and the
/// "Most recent run" ordering.
bool isMoreRecent(PipelineRun candidate, PipelineRun current) {
final a = DateTime.tryParse(candidate.createdAt);
final b = DateTime.tryParse(current.createdAt);
if (a != null && b != null && a != b) return a.isAfter(b);
return candidate.id > current.id;
}
/// The latest run per pipeline id.
Map<int, PipelineRun> latestRunByPipeline(Iterable<PipelineRun> runs) {
final latest = <int, PipelineRun>{};
for (final run in runs) {
final current = latest[run.pipelineId];
if (current == null || isMoreRecent(run, current)) {
latest[run.pipelineId] = run;
}
}
return latest;
}
int _int(Object? value) {
if (value is int) return value;
if (value is num) return value.toInt();
return int.tryParse('${value ?? ''}') ?? 0;
}
int? _optionalInt(Object? value) {
if (value == null) return null;
if (value is int) return value;
if (value is num) return value.toInt();
return int.tryParse('$value');
}
String _string(Object? value) => value is String ? value : '';
/// A raw JSON object exactly as a reply carried it, or null when absent.
PipelineJson? _jsonObject(Object? value) =>
value is Map ? Map<String, dynamic>.from(value) : null;