🔍 Code Extractor

function health_v1

Maturity: 39

Flask route handler that provides a health check endpoint returning the operational status of the application and its core components (RAG engine and document indexer).

File:
/tf/active/vicechatdev/docchat/app.py
Lines:
1508 - 1515
Complexity:
simple

Purpose

This endpoint serves as a monitoring and diagnostics tool to verify that the application is running and to check the availability status of critical components. It's typically used by load balancers, orchestration systems (like Kubernetes), or monitoring tools to determine if the service is healthy and ready to handle requests. The endpoint returns a JSON response with status information and a timestamp.

Source Code

def health():
    """Health check endpoint"""
    return jsonify({
        'status': 'healthy',
        'rag_engine': 'available' if rag_engine else 'unavailable',
        'document_indexer': 'available' if document_indexer else 'unavailable',
        'timestamp': datetime.now().isoformat()
    })

Return Value

Returns a Flask JSON response object containing a dictionary with four keys: 'status' (string, always 'healthy'), 'rag_engine' (string, either 'available' or 'unavailable' based on whether the rag_engine object exists), 'document_indexer' (string, either 'available' or 'unavailable' based on whether the document_indexer object exists), and 'timestamp' (string, ISO 8601 formatted current datetime).

Dependencies

  • flask
  • datetime

Required Imports

from flask import jsonify
from datetime import datetime

Usage Example

from flask import Flask, jsonify
from datetime import datetime

app = Flask(__name__)

# Initialize these as None or with actual instances
rag_engine = None
document_indexer = None

@app.route('/health')
def health():
    """Health check endpoint"""
    return jsonify({
        'status': 'healthy',
        'rag_engine': 'available' if rag_engine else 'unavailable',
        'document_indexer': 'available' if document_indexer else 'unavailable',
        'timestamp': datetime.now().isoformat()
    })

if __name__ == '__main__':
    app.run(debug=True)

# Access the endpoint:
# GET http://localhost:5000/health
# Response: {"status": "healthy", "rag_engine": "unavailable", "document_indexer": "unavailable", "timestamp": "2024-01-15T10:30:45.123456"}

Best Practices

  • This endpoint should always return a 200 status code when the application is running, even if dependent components are unavailable
  • The endpoint performs only lightweight checks (object existence) to ensure fast response times
  • Consider adding more detailed health checks for production environments (database connectivity, external API availability, etc.)
  • The timestamp helps identify when the health check was performed, useful for debugging time-sensitive issues
  • This endpoint should not require authentication to allow monitoring systems easy access
  • For production use, consider adding response caching with a short TTL to reduce load from frequent health checks
  • The global variables 'rag_engine' and 'document_indexer' should be properly initialized before the Flask app starts accepting requests

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function health 92.4% similar

    Flask route handler that provides a health check endpoint returning the application's operational status, chat engine availability, and current timestamp.

    From: /tf/active/vicechatdev/vice_ai/app.py
  • function health_check 88.4% similar

    A Flask route handler that provides a health check endpoint returning the application's status and current timestamp.

    From: /tf/active/vicechatdev/leexi/app.py
  • function system_status 74.3% similar

    Flask API endpoint that returns comprehensive system status information including database connectivity, authentication state, and feature availability.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function test_flask_routes 60.3% similar

    A test function that validates Flask application routes are properly configured by checking for required endpoints.

    From: /tf/active/vicechatdev/docchat/test_model_selection.py
  • function admin_status 60.1% similar

    Flask route handler that retrieves comprehensive system status information including disk usage, session counts, analysis counts, and orphaned file detection for an admin dashboard.

    From: /tf/active/vicechatdev/full_smartstat/app.py
← Back to Browse