335 lines
10 KiB
Dart
335 lines
10 KiB
Dart
import '../../pipelines/pipeline_format.dart';
|
|
import '../../../core/api_gateway.dart';
|
|
|
|
const runStates = [
|
|
'queued',
|
|
'running',
|
|
'waiting',
|
|
'succeeded',
|
|
'failed',
|
|
'canceled',
|
|
'interrupted',
|
|
];
|
|
|
|
/// The largest millisecond value [DateTime] supports, and also exactly
|
|
/// representable by the web's JavaScript numbers. The previous
|
|
/// `0x7fffffffffffffff` literals failed dart2js compilation.
|
|
const int _maxMillis = 8640000000000000;
|
|
|
|
String localDayKey(DateTime date) {
|
|
final local = date.toLocal();
|
|
String two(int value) => value.toString().padLeft(2, '0');
|
|
return '${local.year}-${two(local.month)}-${two(local.day)}';
|
|
}
|
|
|
|
String runDayLabel(String createdAt, DateTime today) {
|
|
final created = DateTime.tryParse(createdAt)?.toLocal();
|
|
if (created == null) return 'Unknown date';
|
|
final day = localDayKey(created);
|
|
if (day == localDayKey(today)) return 'Today';
|
|
final yesterday = DateTime(
|
|
today.year,
|
|
today.month,
|
|
today.day,
|
|
).subtract(const Duration(days: 1));
|
|
if (day == localDayKey(yesterday)) return 'Yesterday';
|
|
const months = [
|
|
'Jan',
|
|
'Feb',
|
|
'Mar',
|
|
'Apr',
|
|
'May',
|
|
'Jun',
|
|
'Jul',
|
|
'Aug',
|
|
'Sep',
|
|
'Oct',
|
|
'Nov',
|
|
'Dec',
|
|
];
|
|
return '${created.day} ${months[created.month - 1]} ${created.year}';
|
|
}
|
|
|
|
List<({String label, List<JsonMap> runs})> groupRunsByDay(
|
|
List<JsonMap> sortedRuns,
|
|
DateTime today,
|
|
) {
|
|
final groups = <({String label, List<JsonMap> runs})>[];
|
|
for (final run in sortedRuns) {
|
|
final label = runDayLabel('${run['created_at'] ?? ''}', today);
|
|
if (groups.isEmpty || groups.last.label != label) {
|
|
groups.add((label: label, runs: <JsonMap>[]));
|
|
}
|
|
groups.last.runs.add(run);
|
|
}
|
|
return groups;
|
|
}
|
|
|
|
({String label, String? pipeline}) runIdentity(dynamic title, dynamic name) {
|
|
final stored = '${title ?? ''}'.trim();
|
|
return (
|
|
label: stored.isNotEmpty ? stored : '${name ?? ''}',
|
|
pipeline: stored.isNotEmpty ? '${name ?? ''}' : null,
|
|
);
|
|
}
|
|
|
|
String runStateOf(dynamic status) =>
|
|
switch ('${status ?? ''}'.trim().toLowerCase()) {
|
|
'queued' ||
|
|
'running' ||
|
|
'waiting' ||
|
|
'active' ||
|
|
'blocked' ||
|
|
'skipped' => '${status ?? ''}'.trim().toLowerCase(),
|
|
'succeeded' || 'success' || 'completed' || 'done' => 'succeeded',
|
|
'failed' || 'error' => 'failed',
|
|
'canceled' || 'cancelled' => 'canceled',
|
|
'interrupted' => 'interrupted',
|
|
_ => 'unknown',
|
|
};
|
|
|
|
List<JsonMap> filterRuns(
|
|
List<JsonMap> runs, {
|
|
Set<String> statuses = const {},
|
|
int? pipelineId,
|
|
String timeRange = 'any_time',
|
|
String scheduled = 'any',
|
|
DateTime? today,
|
|
}) => runs.where((run) {
|
|
if (statuses.isNotEmpty && !statuses.contains(runStateOf(run['status']))) {
|
|
return false;
|
|
}
|
|
if (pipelineId != null && run['pipeline_id'] != pipelineId) {
|
|
return false;
|
|
}
|
|
if (scheduled == 'scheduled' && !_isScheduled(run)) {
|
|
return false;
|
|
}
|
|
if (scheduled == 'unscheduled' &&
|
|
(run['scheduled'] != false || _isScheduled(run))) {
|
|
return false;
|
|
}
|
|
if (timeRange == 'any_time' || timeRange.isEmpty) {
|
|
return true;
|
|
}
|
|
final created = DateTime.tryParse('${run['created_at'] ?? ''}')?.toLocal();
|
|
if (created == null) {
|
|
return false;
|
|
}
|
|
final now = today ?? DateTime.now();
|
|
final start = DateTime(now.year, now.month, now.day);
|
|
final date = DateTime(created.year, created.month, created.day);
|
|
if (date.isAfter(start)) {
|
|
return false;
|
|
}
|
|
final days = switch (timeRange) {
|
|
'today' => 0,
|
|
'last_7_days' => 6,
|
|
'last_30_days' => 29,
|
|
_ => null,
|
|
};
|
|
return days == null || !date.isBefore(start.subtract(Duration(days: days)));
|
|
}).toList();
|
|
|
|
bool _isScheduled(JsonMap run) =>
|
|
run['scheduled'] == true ||
|
|
(run['schedule_id'] is num && (run['schedule_id'] as num) > 0);
|
|
|
|
/// The run row's meta line as the web's `metaText`: `run 7 · 21 Oct 12:00 ·
|
|
/// 4m 12s` for a finished run, `run 7 · 3m 5s` (elapsed) while it executes.
|
|
/// Unusable timestamps are omitted, never printed blank.
|
|
String runRowMetaText(JsonMap run, DateTime now) {
|
|
final parts = ['run ${run['id']}'];
|
|
final created = '${run['created_at'] ?? ''}';
|
|
if (run['executing'] == true) {
|
|
final start = parseInstant(created);
|
|
if (start != null) {
|
|
parts.add(formatDuration(now.difference(start).inSeconds));
|
|
}
|
|
} else {
|
|
final stamp = formatLocalTimestamp(created);
|
|
if (stamp != null) parts.add(stamp);
|
|
final duration = durationSeconds(created, '${run['finished_at'] ?? ''}');
|
|
if (duration != null) parts.add(formatDuration(duration));
|
|
}
|
|
return parts.join(' · ');
|
|
}
|
|
|
|
/// The facts a run row lays out in its corners: when it ran, for how long and
|
|
/// how far it got. Absent facts are null, never invented.
|
|
typedef RunRowFacts = ({String? when, String? duration, String? progress});
|
|
|
|
RunRowFacts runRowFacts(JsonMap run, DateTime now) {
|
|
final created = '${run['created_at'] ?? ''}';
|
|
final finished = '${run['finished_at'] ?? ''}';
|
|
final start = formatLocalTimestamp(created);
|
|
final executing = run['executing'] == true;
|
|
String? when = start;
|
|
int? seconds;
|
|
if (executing) {
|
|
final instant = parseInstant(created);
|
|
if (instant != null) seconds = now.difference(instant).inSeconds;
|
|
} else {
|
|
seconds = durationSeconds(created, finished);
|
|
final end = parseInstant(finished)?.toLocal();
|
|
final begin = parseInstant(created)?.toLocal();
|
|
if (start != null && end != null && begin != null) {
|
|
// Same day: the end is just its time.
|
|
final sameDay =
|
|
end.year == begin.year &&
|
|
end.month == begin.month &&
|
|
end.day == begin.day;
|
|
final endText = sameDay
|
|
? '${end.hour.toString().padLeft(2, '0')}:'
|
|
'${end.minute.toString().padLeft(2, '0')}'
|
|
: formatLocalTimestamp(finished);
|
|
when = '$start → $endText';
|
|
}
|
|
}
|
|
final steps = run['completed_steps'];
|
|
final stage = '${run['current_stage'] ?? ''}'.trim();
|
|
final progress = [
|
|
if (steps is num && steps > 0)
|
|
steps == 1 ? '1 step' : '${steps.toInt()} steps',
|
|
if (executing && stage.isNotEmpty) 'at $stage',
|
|
].join(' · ');
|
|
return (
|
|
when: when,
|
|
duration: seconds == null ? null : formatDuration(seconds),
|
|
progress: progress.isEmpty ? null : progress,
|
|
);
|
|
}
|
|
|
|
String runStatusLabel(JsonMap run) {
|
|
final status = '${run['status'] ?? ''}';
|
|
if (!{'queued', 'running', 'waiting'}.contains(status)) {
|
|
return status.isEmpty ? 'unknown' : status;
|
|
}
|
|
if (run['cleanup_unconfirmed'] == true) {
|
|
return 'Cleanup unconfirmed — workspace reserved';
|
|
}
|
|
if (run['cancel_requested'] == true) {
|
|
return 'Stopping — waiting for execution to stop';
|
|
}
|
|
if (status == 'waiting' &&
|
|
(run['export_pending_sequence'] as num? ?? 0) > 0) {
|
|
return 'Waiting for session export';
|
|
}
|
|
return status;
|
|
}
|
|
|
|
List<JsonMap> sortRuns(List<JsonMap> runs, {String sort = 'newest_first'}) {
|
|
final result = [...runs];
|
|
int? instant(dynamic value) =>
|
|
DateTime.tryParse('${value ?? ''}')?.millisecondsSinceEpoch;
|
|
int? duration(JsonMap run) {
|
|
final from = instant(run['created_at']);
|
|
final to = instant(run['finished_at']);
|
|
if (from == null || to == null || to < from) return null;
|
|
return (to - from) ~/ 1000;
|
|
}
|
|
|
|
int cmp<T extends Comparable<Object>>(T a, T b) => a.compareTo(b);
|
|
result.sort((a, b) {
|
|
final at = instant(a['created_at']);
|
|
final bt = instant(b['created_at']);
|
|
int primary;
|
|
switch (sort) {
|
|
case 'oldest_first':
|
|
primary = cmp(at ?? _maxMillis, bt ?? _maxMillis);
|
|
break;
|
|
case 'longest_duration':
|
|
primary = cmp(duration(a) == null ? 1 : 0, duration(b) == null ? 1 : 0);
|
|
if (primary == 0) primary = cmp(duration(b) ?? -1, duration(a) ?? -1);
|
|
if (primary == 0) {
|
|
primary = cmp(bt ?? -_maxMillis, at ?? -_maxMillis);
|
|
}
|
|
break;
|
|
case 'shortest_duration':
|
|
primary = cmp(duration(a) == null ? 1 : 0, duration(b) == null ? 1 : 0);
|
|
if (primary == 0) {
|
|
primary = cmp(duration(a) ?? _maxMillis, duration(b) ?? _maxMillis);
|
|
}
|
|
if (primary == 0) {
|
|
primary = cmp(bt ?? -_maxMillis, at ?? -_maxMillis);
|
|
}
|
|
break;
|
|
case 'pipeline_name':
|
|
primary = '${a['name'] ?? ''}'.toLowerCase().compareTo(
|
|
'${b['name'] ?? ''}'.toLowerCase(),
|
|
);
|
|
if (primary == 0) {
|
|
primary = cmp(bt ?? -_maxMillis, at ?? -_maxMillis);
|
|
}
|
|
break;
|
|
case 'status':
|
|
primary = '${a['status'] ?? ''}'.toLowerCase().compareTo(
|
|
'${b['status'] ?? ''}'.toLowerCase(),
|
|
);
|
|
if (primary == 0) {
|
|
primary = cmp(bt ?? -_maxMillis, at ?? -_maxMillis);
|
|
}
|
|
break;
|
|
default:
|
|
primary = cmp(bt ?? -_maxMillis, at ?? -_maxMillis);
|
|
}
|
|
return primary == 0
|
|
? ((b['id'] as num?) ?? 0).compareTo((a['id'] as num?) ?? 0)
|
|
: primary;
|
|
});
|
|
return result;
|
|
}
|
|
|
|
/// Null usage means unsupported/unavailable, not zero.
|
|
class RunListResult {
|
|
const RunListResult(this.runs, this.usage);
|
|
final List<JsonMap> runs;
|
|
final JsonMap? usage;
|
|
}
|
|
|
|
class DecisionOption {
|
|
const DecisionOption(this.key, {this.target = ''});
|
|
final String key;
|
|
final String target;
|
|
}
|
|
|
|
class DecisionDraft {
|
|
const DecisionDraft({this.selected, this.feedback = ''});
|
|
final String? selected;
|
|
final String feedback;
|
|
bool get canSubmit => selected != null && selected!.trim().isNotEmpty;
|
|
DecisionDraft select(String option) =>
|
|
DecisionDraft(selected: option, feedback: feedback);
|
|
DecisionDraft withFeedback(String value) =>
|
|
DecisionDraft(selected: selected, feedback: value);
|
|
}
|
|
|
|
String attentionKey(num runId, String kind) => '$runId:$kind';
|
|
|
|
List<JsonMap> orderAttention(
|
|
List<JsonMap> runs, {
|
|
Set<String> dismissed = const {},
|
|
}) {
|
|
final result = runs.where((run) {
|
|
final waiting = run['status'] == 'waiting';
|
|
if (waiting && (run['export_pending_sequence'] as num? ?? 0) > 0) {
|
|
return false;
|
|
}
|
|
final kind = waiting ? 'Decision' : 'Failed';
|
|
return (waiting || run['status'] == 'failed') &&
|
|
!dismissed.contains(attentionKey(run['id'] as num, kind));
|
|
}).toList();
|
|
String recency(JsonMap run) => '${run['finished_at'] ?? ''}'.trim().isNotEmpty
|
|
? '${run['finished_at']}'
|
|
: '${run['created_at'] ?? ''}';
|
|
result.sort((a, b) {
|
|
final rank = (a['status'] == 'waiting' ? 0 : 1).compareTo(
|
|
b['status'] == 'waiting' ? 0 : 1,
|
|
);
|
|
if (rank != 0) return rank;
|
|
final date = recency(b).compareTo(recency(a));
|
|
return date != 0 ? date : (b['id'] as num).compareTo(a['id'] as num);
|
|
});
|
|
return result;
|
|
}
|