Files
AcWeb/ac_web/lib/Api/Dtos/AgentTaskDto.dart
T

50 lines
1.6 KiB
Dart

import 'package:ac_web/Api/JsonReader.dart';
/// Dart counterpart of the backend `AgentTaskDto`.
///
/// A task is one piece of work an agent is currently busy with. While it runs
/// the agent appends short lines to [statusUpdates], so the list doubles as
/// the history of the task.
class AgentTaskDto {
const AgentTaskDto({
required this.title,
this.startTime,
this.statusUpdates = const <String>[],
});
final String title;
/// Moment the agent picked the task up, if the api reports it.
final DateTime? startTime;
/// Every status line the agent reported so far, oldest first.
final List<String> statusUpdates;
bool get hasStatusUpdates => statusUpdates.isNotEmpty;
/// The most recent status line, or null while the agent did not report one.
String? get latestStatus => statusUpdates.isEmpty ? null : statusUpdates.last;
factory AgentTaskDto.fromJson(Map<String, dynamic> json) {
return AgentTaskDto(
// Older builds of the api called the property "Name".
title: JsonReader.optionalString(json, 'Title') ??
JsonReader.optionalString(json, 'Name') ??
'',
startTime: JsonReader.optionalDateTime(json, 'StartTime'),
statusUpdates: JsonReader.stringList(json, 'StatusUpdates'),
);
}
Map<String, dynamic> toJson() {
return <String, dynamic>{
'Title': title,
if (startTime != null) 'StartTime': startTime!.toIso8601String(),
'StatusUpdates': statusUpdates,
};
}
@override
String toString() => 'AgentTaskDto(title: $title, updates: ${statusUpdates.length})';
}