Rifat Azad commited on
Commit
f15b29c
·
1 Parent(s): 098c02e
Files changed (1) hide show
  1. logger.py +16 -6
logger.py CHANGED
@@ -1,14 +1,19 @@
1
  from fastapi import FastAPI
2
  import psutil
3
  import httpx
 
4
 
5
  app = FastAPI()
6
 
7
  @app.get("/")
8
  async def read_root():
9
  # Get system uptime
10
- uptime = psutil.boot_time()
11
-
 
 
 
 
12
  # Get system vitals
13
  cpu_percent = psutil.cpu_percent()
14
  memory_percent = psutil.virtual_memory().percent
@@ -17,13 +22,18 @@ async def read_root():
17
  async with httpx.AsyncClient() as client:
18
  try:
19
  response = await client.get("https://www.google.com")
20
- latency = response.elapsed.total_seconds()
21
  except httpx.RequestError:
22
- latency = "Error: Unable to connect to google.com"
23
 
24
  return {
25
- "System Uptime": uptime,
 
 
 
 
 
26
  "CPU Usage": f"{cpu_percent}%",
27
  "Memory Usage": f"{memory_percent}%",
28
- "Latency to Google": latency
29
  }
 
1
  from fastapi import FastAPI
2
  import psutil
3
  import httpx
4
+ import datetime
5
 
6
  app = FastAPI()
7
 
8
  @app.get("/")
9
  async def read_root():
10
  # Get system uptime
11
+ boot_time = psutil.boot_time()
12
+ uptime_seconds = int(datetime.datetime.now().timestamp() - boot_time)
13
+ uptime_days, uptime_hours = divmod(uptime_seconds, 86400)
14
+ uptime_hours, uptime_minutes = divmod(uptime_hours, 3600)
15
+ uptime_minutes, uptime_seconds = divmod(uptime_minutes, 60)
16
+
17
  # Get system vitals
18
  cpu_percent = psutil.cpu_percent()
19
  memory_percent = psutil.virtual_memory().percent
 
22
  async with httpx.AsyncClient() as client:
23
  try:
24
  response = await client.get("https://www.google.com")
25
+ latency_ms = response.elapsed.total_seconds() * 1000
26
  except httpx.RequestError:
27
+ latency_ms = "Error: Unable to connect to google.com"
28
 
29
  return {
30
+ "System Uptime": {
31
+ "Days": uptime_days,
32
+ "Hours": uptime_hours,
33
+ "Minutes": uptime_minutes,
34
+ "Seconds": uptime_seconds
35
+ },
36
  "CPU Usage": f"{cpu_percent}%",
37
  "Memory Usage": f"{memory_percent}%",
38
+ "Latency to Google (ms)": latency_ms
39
  }