HealthResponse.fromResults constructor

HealthResponse.fromResults(
  1. List<HealthCheckResult> results
)

Creates a health response from a list of check results.

Implementation

factory HealthResponse.fromResults(List<HealthCheckResult> results) {
  final checks = <String, List<HealthCheckResult>>{};
  for (final result in results) {
    checks.putIfAbsent(result.name, () => []).add(result);
  }

  final hasFailure = results.any((r) => r.status == HealthStatus.fail);
  final failedChecks = results.where((r) => r.status == HealthStatus.fail);

  String? notes;
  String? output;
  if (hasFailure) {
    final failedNames = failedChecks.map((r) => r.name).toSet().join(', ');
    notes = 'Failed checks: $failedNames';
    output = failedChecks
        .where((r) => r.output != null)
        .map((r) => '${r.name}: ${r.output}')
        .join('; ');
    if (output.isEmpty) output = null;
  }

  return HealthResponse(
    status: hasFailure ? HealthStatus.fail : HealthStatus.pass,
    checks: checks,
    notes: notes,
    output: output,
    time: DateTime.now().toUtc(),
  );
}