File size: 980 Bytes
51ff9e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import time

from fastapi import FastAPI, Request

from openhands.runtime.utils.system_stats import get_system_stats

start_time = time.time()
last_execution_time = start_time


def add_health_endpoints(app: FastAPI):
    @app.get('/alive')
    async def alive():
        return {'status': 'ok'}

    @app.get('/health')
    async def health() -> str:
        return 'OK'

    @app.get('/server_info')
    async def get_server_info():
        current_time = time.time()
        uptime = current_time - start_time
        idle_time = current_time - last_execution_time

        response = {
            'uptime': uptime,
            'idle_time': idle_time,
            'resources': get_system_stats(),
        }
        return response

    @app.middleware('http')
    async def update_last_execution_time(request: Request, call_next):
        global last_execution_time
        response = await call_next(request)
        last_execution_time = time.time()
        return response