RuntimeSnapshot.fromScheduler constructor

RuntimeSnapshot.fromScheduler(
  1. Scheduler scheduler
)

Builds a snapshot from a Scheduler.

Implementation

factory RuntimeSnapshot.fromScheduler(Scheduler scheduler) {
  final now = DateTime.now();
  final totalsJson = scheduler.totals.toJson();
  // Add live elapsed time for active runs to the runtime aggregate so the
  // dashboard reflects in-flight work without continuously updating timers.
  var liveSeconds = (totalsJson['seconds_running'] as num).toDouble();
  for (final entry in scheduler.running.values) {
    liveSeconds += now.difference(entry.startedAt).inMilliseconds / 1000.0;
  }
  totalsJson['seconds_running'] = liveSeconds;

  return RuntimeSnapshot(
    generatedAt: now,
    running: scheduler.running.values
        .map((e) => e.toJson())
        .toList(growable: false),
    retrying: scheduler.retryAttempts.values
        .map((r) => r.toJson())
        .toList(growable: false),
    claimed: scheduler.claimed.toList(growable: false),
    completed: scheduler.completed.toList(growable: false),
    codexTotals: totalsJson,
    recentEvents: scheduler.recentEvents
        .map((e) => e.toJson())
        .toList(growable: false),
    validationErrors: scheduler.validationErrors,
    proofOfWork: scheduler.proofPaths,
  );
}