UNPKG

@datalayer/core

Version:

[![Datalayer](https://assets.datalayer.tech/datalayer-25.svg)](https://datalayer.io)

108 lines (107 loc) 2.94 kB
/* * Copyright (c) 2023-2025 Datalayer, Inc. * Distributed under the terms of the Modified BSD License. */ import { validateJSON } from '../api/utils/validation'; /** * Represents a health check response from a Datalayer service. * Provides standardized health status information across all services. */ export class HealthCheck { // Properties healthy; status; responseTime; errors; timestamp; /** * Create a HealthCheck instance. * @param data - The health check data * @param sdk - Reference to the SDK instance (unused but kept for consistency) */ constructor(data, sdk) { // Initialize properties this.healthy = data.healthy || false; this.status = data.status || 'unknown'; this.responseTime = data.responseTime || 0; this.errors = data.errors || []; this.timestamp = data.timestamp ? new Date(data.timestamp) : new Date(); } /** * Check if the service is healthy. * @returns True if the service is healthy */ isHealthy() { return this.healthy; } /** * Get the service status. * @returns The status string */ getStatus() { return this.status; } /** * Get the response time in milliseconds. * @returns The response time */ getResponseTime() { return this.responseTime; } /** * Get any errors reported during the health check. * @returns Array of error messages */ getErrors() { return this.errors; } /** * Get the timestamp of the health check. * @returns The timestamp */ getTimestamp() { return this.timestamp; } /** * Check if there are any errors. * @returns True if there are errors */ hasErrors() { return this.errors.length > 0; } /** * Get a human-readable summary of the health check. * @returns Summary string */ getSummary() { if (this.healthy) { return `Service is healthy (${this.responseTime}ms response time)`; } else { const errorCount = this.errors.length; return `Service is unhealthy: ${this.status} (${errorCount} error${errorCount !== 1 ? 's' : ''})`; } } /** * Convert to a plain object. * @returns Plain object representation */ async toJSON() { const obj = { healthy: this.healthy, status: this.status, responseTime: this.responseTime, errors: this.errors, timestamp: this.timestamp.toISOString(), }; validateJSON(obj, 'HealthCheck'); return obj; } /** * Get a string representation. * @returns String representation */ toString() { return `HealthCheck(${this.status}, healthy=${this.healthy}, responseTime=${this.responseTime}ms)`; } }