218 lines
6.6 KiB
Dart
218 lines
6.6 KiB
Dart
import '../../../core/api_gateway.dart';
|
|
|
|
const runStates = [
|
|
'queued',
|
|
'running',
|
|
'waiting',
|
|
'succeeded',
|
|
'failed',
|
|
'canceled',
|
|
'interrupted',
|
|
];
|
|
|
|
({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);
|
|
|
|
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 ?? 0x7fffffffffffffff, bt ?? 0x7fffffffffffffff);
|
|
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 ?? -0x7fffffffffffffff, at ?? -0x7fffffffffffffff);
|
|
}
|
|
break;
|
|
case 'shortest_duration':
|
|
primary = cmp(duration(a) == null ? 1 : 0, duration(b) == null ? 1 : 0);
|
|
if (primary == 0) {
|
|
primary = cmp(
|
|
duration(a) ?? 0x7fffffffffffffff,
|
|
duration(b) ?? 0x7fffffffffffffff,
|
|
);
|
|
}
|
|
if (primary == 0) {
|
|
primary = cmp(bt ?? -0x7fffffffffffffff, at ?? -0x7fffffffffffffff);
|
|
}
|
|
break;
|
|
case 'pipeline_name':
|
|
primary = '${a['name'] ?? ''}'.toLowerCase().compareTo(
|
|
'${b['name'] ?? ''}'.toLowerCase(),
|
|
);
|
|
if (primary == 0) {
|
|
primary = cmp(bt ?? -0x7fffffffffffffff, at ?? -0x7fffffffffffffff);
|
|
}
|
|
break;
|
|
case 'status':
|
|
primary = '${a['status'] ?? ''}'.toLowerCase().compareTo(
|
|
'${b['status'] ?? ''}'.toLowerCase(),
|
|
);
|
|
if (primary == 0) {
|
|
primary = cmp(bt ?? -0x7fffffffffffffff, at ?? -0x7fffffffffffffff);
|
|
}
|
|
break;
|
|
default:
|
|
primary = cmp(bt ?? -0x7fffffffffffffff, at ?? -0x7fffffffffffffff);
|
|
}
|
|
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;
|
|
}
|