check method

Future<Map<String, dynamic>> check(
  1. RequestContext context
)

Performs health checks for all registered indicators and returns a comprehensive report. The report includes the overall status, individual indicator statuses, and any relevant details or errors

Implementation

Future<Map<String, dynamic>> check(RequestContext context) async {
  bool allHealthy = true;
  final info = <String, dynamic>{};
  final error = <String, dynamic>{};
  for (final indicator in _indicators) {
    try {
      final result = await indicator.pingCheck(context);
      if (result.status == HealthStatus.up) {
        info[indicator.name] = {'status': 'up', ...?result.details};
      } else {
        allHealthy = false;
        error[indicator.name] = {'status': 'down', ...?result.details};
        if (errorLogStyle == ErrorLogStyle.json ||
            errorLogStyle == ErrorLogStyle.pretty) {
          const red = '\x1B[31m';
          const reset = '\x1B[0m';
          final errorDetails = {
            indicator.name: {'status': 'down', ...?result.details},
          };
          if (errorLogStyle == ErrorLogStyle.json) {
            _logger.severe(
              'Health Check has failed!\n${jsonEncode(errorDetails)}',
            );
          } else if (errorLogStyle == ErrorLogStyle.pretty) {
            const cross = '✖';
            final buffer = StringBuffer();
            final lines = <String>[];
            int maxLengthLine = 0;
            for (final entry
                in result.details?.entries ?? <MapEntry<String, dynamic>>[]) {
              lines.add('${entry.key}: ${entry.value}');
              maxLengthLine =
                  entry.key.length + entry.value.toString().length >
                      maxLengthLine
                  ? entry.key.length + entry.value.toString().length
                  : maxLengthLine;
            }
            final boxWidth = maxLengthLine + 4;
            final topDashCount = max(2, boxWidth - indicator.name.length - 2);
            final topDashes = '─' * topDashCount;
            final bottomDashes = '─' * (boxWidth + 2);
            buffer.writeln(
              '$red  ┌ $cross ${indicator.name} $topDashes┐$reset',
            );
            buffer.writeln('$red  │ ${''.padRight(boxWidth)} │$reset');
            for (final line in lines) {
              buffer.writeln(
                '$red  │  ${line.padRight(boxWidth - 1)} │$reset',
              );
            }
            buffer.writeln('$red  │ ${''.padRight(boxWidth)} │$reset');
            buffer.writeln('$red  └$bottomDashes┘$reset\n');
            _logger.severe('Health Check has failed!\n${buffer.toString()}');
          }
        }
      }
    } catch (e) {
      error[indicator.name] = {'status': 'error', 'error': e.toString()};
      allHealthy = false;
    }
  }
  return {
    'status': allHealthy ? 'ok' : 'error',
    'info': info,
    'error': error,
    'details': {...error, ...info},
    'timestamp': DateTime.now().toIso8601String(),
  };
}