Sadeep Sachintha commited on
Commit ·
d65b565
1
Parent(s): f53ddba
feat: add in-memory diagnostic logs endpoint to track production errors
Browse files- main.py +24 -0
- rate_cache.json +40 -40
main.py
CHANGED
|
@@ -35,6 +35,25 @@ from services.fx_service import fx_service
|
|
| 35 |
logging.basicConfig(level=getattr(logging, settings.log_level))
|
| 36 |
logger = logging.getLogger(__name__)
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
# Global diagnostic results storage
|
| 39 |
diagnostic_results = []
|
| 40 |
|
|
@@ -175,6 +194,11 @@ async def health_check():
|
|
| 175 |
"""Health check endpoint required by Hugging Face Spaces."""
|
| 176 |
return {"status": "ok"}
|
| 177 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
@app.get("/api/stats")
|
| 179 |
async def get_system_stats():
|
| 180 |
"""Retrieve database metrics and live LKR exchange rates."""
|
|
|
|
| 35 |
logging.basicConfig(level=getattr(logging, settings.log_level))
|
| 36 |
logger = logging.getLogger(__name__)
|
| 37 |
|
| 38 |
+
# InMemoryLogHandler to store logs for web diagnostics
|
| 39 |
+
class InMemoryLogHandler(logging.Handler):
|
| 40 |
+
def __init__(self, limit=300):
|
| 41 |
+
super().__init__()
|
| 42 |
+
self.limit = limit
|
| 43 |
+
self.records = []
|
| 44 |
+
|
| 45 |
+
def emit(self, record):
|
| 46 |
+
try:
|
| 47 |
+
self.records.append(self.format(record))
|
| 48 |
+
if len(self.records) > self.limit:
|
| 49 |
+
self.records.pop(0)
|
| 50 |
+
except Exception:
|
| 51 |
+
pass
|
| 52 |
+
|
| 53 |
+
in_memory_logs = InMemoryLogHandler()
|
| 54 |
+
in_memory_logs.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
|
| 55 |
+
logging.getLogger().addHandler(in_memory_logs)
|
| 56 |
+
|
| 57 |
# Global diagnostic results storage
|
| 58 |
diagnostic_results = []
|
| 59 |
|
|
|
|
| 194 |
"""Health check endpoint required by Hugging Face Spaces."""
|
| 195 |
return {"status": "ok"}
|
| 196 |
|
| 197 |
+
@app.get("/api/logs")
|
| 198 |
+
async def get_logs():
|
| 199 |
+
"""Retrieve in-memory logs for diagnostics."""
|
| 200 |
+
return {"logs": in_memory_logs.records}
|
| 201 |
+
|
| 202 |
@app.get("/api/stats")
|
| 203 |
async def get_system_stats():
|
| 204 |
"""Retrieve database metrics and live LKR exchange rates."""
|
rate_cache.json
CHANGED
|
@@ -1,82 +1,82 @@
|
|
| 1 |
{
|
| 2 |
"USD_LKR": {
|
| 3 |
-
"rate":
|
| 4 |
-
"timestamp":
|
| 5 |
},
|
| 6 |
"LKR_USD": {
|
| 7 |
-
"rate": 0.
|
| 8 |
-
"timestamp":
|
| 9 |
},
|
| 10 |
"EUR_LKR": {
|
| 11 |
-
"rate": 378.
|
| 12 |
-
"timestamp":
|
| 13 |
},
|
| 14 |
"LKR_EUR": {
|
| 15 |
-
"rate": 0.
|
| 16 |
-
"timestamp":
|
| 17 |
},
|
| 18 |
"GBP_LKR": {
|
| 19 |
-
"rate":
|
| 20 |
-
"timestamp":
|
| 21 |
},
|
| 22 |
"LKR_GBP": {
|
| 23 |
-
"rate": 0.
|
| 24 |
-
"timestamp":
|
| 25 |
},
|
| 26 |
"AUD_LKR": {
|
| 27 |
-
"rate":
|
| 28 |
-
"timestamp":
|
| 29 |
},
|
| 30 |
"LKR_AUD": {
|
| 31 |
-
"rate": 0.
|
| 32 |
-
"timestamp":
|
| 33 |
},
|
| 34 |
"JPY_LKR": {
|
| 35 |
-
"rate": 2.
|
| 36 |
-
"timestamp":
|
| 37 |
},
|
| 38 |
"LKR_JPY": {
|
| 39 |
-
"rate": 0.
|
| 40 |
-
"timestamp":
|
| 41 |
},
|
| 42 |
"AED_LKR": {
|
| 43 |
-
"rate": 88.
|
| 44 |
-
"timestamp":
|
| 45 |
},
|
| 46 |
"LKR_AED": {
|
| 47 |
-
"rate": 0.
|
| 48 |
-
"timestamp":
|
| 49 |
},
|
| 50 |
"SAR_LKR": {
|
| 51 |
-
"rate": 86.
|
| 52 |
-
"timestamp":
|
| 53 |
},
|
| 54 |
"LKR_SAR": {
|
| 55 |
-
"rate": 0.
|
| 56 |
-
"timestamp":
|
| 57 |
},
|
| 58 |
"INR_LKR": {
|
| 59 |
-
"rate": 3.
|
| 60 |
-
"timestamp":
|
| 61 |
},
|
| 62 |
"LKR_INR": {
|
| 63 |
-
"rate": 0.
|
| 64 |
-
"timestamp":
|
| 65 |
},
|
| 66 |
"CNY_LKR": {
|
| 67 |
-
"rate": 47.
|
| 68 |
-
"timestamp":
|
| 69 |
},
|
| 70 |
"LKR_CNY": {
|
| 71 |
-
"rate": 0.
|
| 72 |
-
"timestamp":
|
| 73 |
},
|
| 74 |
"QAR_LKR": {
|
| 75 |
-
"rate": 89.
|
| 76 |
-
"timestamp":
|
| 77 |
},
|
| 78 |
"LKR_QAR": {
|
| 79 |
-
"rate": 0.
|
| 80 |
-
"timestamp":
|
| 81 |
}
|
| 82 |
}
|
|
|
|
| 1 |
{
|
| 2 |
"USD_LKR": {
|
| 3 |
+
"rate": 325.6208,
|
| 4 |
+
"timestamp": 1779075945.4562404
|
| 5 |
},
|
| 6 |
"LKR_USD": {
|
| 7 |
+
"rate": 0.0030710568858009074,
|
| 8 |
+
"timestamp": 1779075945.4562442
|
| 9 |
},
|
| 10 |
"EUR_LKR": {
|
| 11 |
+
"rate": 378.3062,
|
| 12 |
+
"timestamp": 1779075945.456252
|
| 13 |
},
|
| 14 |
"LKR_EUR": {
|
| 15 |
+
"rate": 0.002643361382922088,
|
| 16 |
+
"timestamp": 1779075945.4562538
|
| 17 |
},
|
| 18 |
"GBP_LKR": {
|
| 19 |
+
"rate": 433.499,
|
| 20 |
+
"timestamp": 1779075945.4562578
|
| 21 |
},
|
| 22 |
"LKR_GBP": {
|
| 23 |
+
"rate": 0.0023068103963330942,
|
| 24 |
+
"timestamp": 1779075945.4562588
|
| 25 |
},
|
| 26 |
"AUD_LKR": {
|
| 27 |
+
"rate": 232.1025,
|
| 28 |
+
"timestamp": 1779075945.4562616
|
| 29 |
},
|
| 30 |
"LKR_AUD": {
|
| 31 |
+
"rate": 0.0043084413136437565,
|
| 32 |
+
"timestamp": 1779075945.456262
|
| 33 |
},
|
| 34 |
"JPY_LKR": {
|
| 35 |
+
"rate": 2.0491,
|
| 36 |
+
"timestamp": 1779075945.4562647
|
| 37 |
},
|
| 38 |
"LKR_JPY": {
|
| 39 |
+
"rate": 0.4880191303499097,
|
| 40 |
+
"timestamp": 1779075945.4562657
|
| 41 |
},
|
| 42 |
"AED_LKR": {
|
| 43 |
+
"rate": 88.6525,
|
| 44 |
+
"timestamp": 1779075945.4562683
|
| 45 |
},
|
| 46 |
"LKR_AED": {
|
| 47 |
+
"rate": 0.011279997744000451,
|
| 48 |
+
"timestamp": 1779075945.456269
|
| 49 |
},
|
| 50 |
"SAR_LKR": {
|
| 51 |
+
"rate": 86.7697,
|
| 52 |
+
"timestamp": 1779075945.4562716
|
| 53 |
},
|
| 54 |
"LKR_SAR": {
|
| 55 |
+
"rate": 0.011524760371419977,
|
| 56 |
+
"timestamp": 1779075945.4562721
|
| 57 |
},
|
| 58 |
"INR_LKR": {
|
| 59 |
+
"rate": 3.3929,
|
| 60 |
+
"timestamp": 1779075945.4562747
|
| 61 |
},
|
| 62 |
"LKR_INR": {
|
| 63 |
+
"rate": 0.2947331191606001,
|
| 64 |
+
"timestamp": 1779075945.4562752
|
| 65 |
},
|
| 66 |
"CNY_LKR": {
|
| 67 |
+
"rate": 47.773,
|
| 68 |
+
"timestamp": 1779075945.4562776
|
| 69 |
},
|
| 70 |
"LKR_CNY": {
|
| 71 |
+
"rate": 0.020932325790718607,
|
| 72 |
+
"timestamp": 1779075945.456278
|
| 73 |
},
|
| 74 |
"QAR_LKR": {
|
| 75 |
+
"rate": 89.3091,
|
| 76 |
+
"timestamp": 1779075945.4562805
|
| 77 |
},
|
| 78 |
"LKR_QAR": {
|
| 79 |
+
"rate": 0.011197067264142175,
|
| 80 |
+
"timestamp": 1779075945.4562812
|
| 81 |
}
|
| 82 |
}
|