# Class HealthService

Health Check Service

Aggregates status from the rate limiter and metrics service to produce a structured HealthStatus snapshot.

The check is intentionally synchronous-safe – it never makes network calls. Network reachability is inferred from recent metrics (EP API error rate).

#### Example

    const healthService = new HealthService(rateLimiter, metricsService);
    const status = healthService.checkHealth();
    console.log(status.status); // 'healthy' | 'degraded' | 'unhealthy'

- Defined in services/HealthService.ts:90

##### Index

### Constructors

constructor

### Properties

metricsService
rateLimiter
startTime

### Methods

buildCacheStatus
buildRateLimiterStatus
checkHealth
deriveOverallStatus
isEpApiReachable

## Constructors

### constructor

- new HealthService(
  rateLimiter: RateLimiter,
  metricsService: MetricsService,
  ): HealthService

  Creates a new HealthService instance.

  #### Parameters

  - rateLimiter: RateLimiter

    Rate limiter whose token availability is checked as part of the degraded-state heuristic

  - metricsService: MetricsService

    Metrics service providing EP API call and error counters used to infer reachability

  #### Returns HealthService

  #### Example

      const healthService = new HealthService(
        createStandardRateLimiter(),
        new MetricsService()
      );

  #### Since

  0.8.0

  - Defined in services/HealthService.ts:111

## Properties

### `Private` `Readonly` metricsService

metricsService: MetricsService

Metrics service providing EP API call and error counters used to infer reachability

- Defined in services/HealthService.ts:113

### `Private` `Readonly` rateLimiter

rateLimiter: RateLimiter

Rate limiter whose token availability is checked as part of the degraded-state heuristic

- Defined in services/HealthService.ts:112

### `Private` `Readonly` startTime

startTime: number

- Defined in services/HealthService.ts:91

## Methods

### `Private` buildCacheStatus

- buildCacheStatus(): CacheHealthStatus

  Build a descriptive cache status object. Cyclomatic complexity: 2

  #### Returns CacheHealthStatus

  - Defined in services/HealthService.ts:191

### `Private` buildRateLimiterStatus

- buildRateLimiterStatus(): RateLimiterStatus

  Build rate-limiter snapshot by delegating to RateLimiter.getStatus(). Cyclomatic complexity: 1

  #### Returns RateLimiterStatus

  - Defined in services/HealthService.ts:164

### checkHealth

- checkHealth(): HealthStatus

  Produces a health status snapshot.

  Checks:

  1. Rate-limiter token availability (degraded if < 10 %)
  2. EP API error counter (unhealthy if recent errors > 0 with no successes)

  The check is synchronous-safe — it never makes network calls. Reachability is inferred from cached metric counters.

  #### Returns HealthStatus

  Structured HealthStatus object with `status`, `epApiReachable`, `cache`, `rateLimiter`, `timestamp`, and `uptimeMs`

  #### Throws

  If the underlying metrics or rate-limiter service throws unexpectedly (should not occur under normal operating conditions)

  #### Example

      const healthService = new HealthService(rateLimiter, metricsService);
      const status = healthService.checkHealth();
      if (status.status !== 'healthy') {
        console.warn('Server degraded:', status);
      }

  #### Since

  0.8.0

  - Defined in services/HealthService.ts:144

### `Private` deriveOverallStatus

- deriveOverallStatus(
  rateLimiter: RateLimiterStatus,
  epApiReachable: boolean | null,
  ): HealthStatusLevel

  Derive the overall health verdict from sub-system checks. Cyclomatic complexity: 3

  #### Parameters

  - rateLimiter: RateLimiterStatus
  - epApiReachable: boolean | null

  #### Returns HealthStatusLevel

  - Defined in services/HealthService.ts:208

### `Private` isEpApiReachable

- isEpApiReachable(): boolean | null

  Determine EP API reachability from recorded metrics. Cyclomatic complexity: 2

  Returns `null` when no API calls have been recorded yet (unknown state), `false` when all calls failed (error count >= call count > 0), `true` when at least some calls succeeded.

  #### Returns boolean | null

  - Defined in services/HealthService.ts:176