284 lines
9.1 KiB
Dart
284 lines
9.1 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,
|
|
this.recordedStages,
|
|
this.flowStages,
|
|
this.shape,
|
|
});
|
|
|
|
factory PipelineSummary.fromJson(PipelineJson json) {
|
|
final scheduleCount = _optionalInt(json['schedule_count']);
|
|
final scheduled = json['scheduled'];
|
|
final recordedStages = _optionalInt(json['stages']);
|
|
return PipelineSummary(
|
|
id: _int(json['id']),
|
|
name: _string(json['name']),
|
|
version: _int(json['version']),
|
|
stages: recordedStages ?? 0,
|
|
recordedStages: recordedStages,
|
|
// A summary may omit `stages` while still carrying the current
|
|
// version's flow (`shape`); keep that recorded count for the list row
|
|
// without changing the summary's own field or its sorting.
|
|
flowStages: _flowStageCount(json['shape']),
|
|
createdAt: _string(json['created_at']),
|
|
updatedAt: _string(json['updated_at']),
|
|
scheduled: scheduled is bool ? scheduled : null,
|
|
scheduleCount: scheduleCount,
|
|
enabledScheduleCount: _optionalInt(json['enabled_schedule_count']),
|
|
shape: _jsonObject(json['shape']),
|
|
);
|
|
}
|
|
|
|
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;
|
|
|
|
/// The stage count the summary itself recorded, or null when it omitted it.
|
|
///
|
|
/// Kept apart from [stages] so sorting keeps the web's rule (its own
|
|
/// `stages` field, never the flow) while the list row can use the recorded
|
|
/// flow below.
|
|
final int? recordedStages;
|
|
|
|
/// The stage count recorded by the current version's optional `shape` flow.
|
|
final int? flowStages;
|
|
|
|
/// The current version's recorded flow, when the summary carries one.
|
|
///
|
|
/// Raw JSON like a detail's graph: the row's miniature draws it without
|
|
/// normalising, fetching or inventing a stage. Null is "not recorded", not an
|
|
/// empty flow.
|
|
final PipelineJson? shape;
|
|
|
|
/// The list row's stage count from recorded values only: the summary's own
|
|
/// count, else the recorded flow's count, else its plain [stages] field.
|
|
/// Zero only when none was recorded, which is the web row's own fallback.
|
|
int get rowStageCount => recordedStages ?? flowStages ?? stages;
|
|
|
|
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,
|
|
super.recordedStages,
|
|
super.flowStages,
|
|
super.shape,
|
|
required this.latestVersion,
|
|
required this.versionCreatedAt,
|
|
required this.contentHash,
|
|
required this.source,
|
|
required this.versions,
|
|
this.graph,
|
|
});
|
|
|
|
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,
|
|
recordedStages: summary.recordedStages,
|
|
flowStages: summary.flowStages,
|
|
shape: summary.shape,
|
|
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']),
|
|
);
|
|
}
|
|
|
|
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 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 : '';
|
|
|
|
/// The stage count recorded by a list row's optional flow summary (`shape`),
|
|
/// or null when that flow was not recorded with a stage list.
|
|
int? _flowStageCount(Object? value) {
|
|
if (value is! Map) return null;
|
|
final stages = value['stages'];
|
|
return stages is List ? stages.length : null;
|
|
}
|
|
|
|
/// 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;
|