Sadeep Sachintha commited on
Commit
d65b565
·
1 Parent(s): f53ddba

feat: add in-memory diagnostic logs endpoint to track production errors

Browse files
Files changed (2) hide show
  1. main.py +24 -0
  2. 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": 324.7184,
4
- "timestamp": 1779042576.8926218
5
  },
6
  "LKR_USD": {
7
- "rate": 0.003079591424446536,
8
- "timestamp": 1779042576.8926272
9
  },
10
  "EUR_LKR": {
11
- "rate": 378.3294,
12
- "timestamp": 1779042576.8926556
13
  },
14
  "LKR_EUR": {
15
- "rate": 0.0026431992861247365,
16
- "timestamp": 1779042576.8926578
17
  },
18
  "GBP_LKR": {
19
- "rate": 434.116,
20
- "timestamp": 1779042576.8926651
21
  },
22
  "LKR_GBP": {
23
- "rate": 0.0023035317749173032,
24
- "timestamp": 1779042576.8926666
25
  },
26
  "AUD_LKR": {
27
- "rate": 233.4401,
28
- "timestamp": 1779042576.8926735
29
  },
30
  "LKR_AUD": {
31
- "rate": 0.0042837541622026375,
32
- "timestamp": 1779042576.892675
33
  },
34
  "JPY_LKR": {
35
- "rate": 2.0486,
36
- "timestamp": 1779042576.892681
37
  },
38
  "LKR_JPY": {
39
- "rate": 0.48813824074978035,
40
- "timestamp": 1779042576.8926826
41
  },
42
  "AED_LKR": {
43
- "rate": 88.4045,
44
- "timestamp": 1779042576.8926892
45
  },
46
  "LKR_AED": {
47
- "rate": 0.011311641375721824,
48
- "timestamp": 1779042576.892691
49
  },
50
  "SAR_LKR": {
51
- "rate": 86.5316,
52
- "timestamp": 1779042576.8926976
53
  },
54
  "LKR_SAR": {
55
- "rate": 0.011556471855368443,
56
- "timestamp": 1779042576.8926988
57
  },
58
  "INR_LKR": {
59
- "rate": 3.3908,
60
- "timestamp": 1779042576.8927054
61
  },
62
  "LKR_INR": {
63
- "rate": 0.29491565412292087,
64
- "timestamp": 1779042576.8927069
65
  },
66
  "CNY_LKR": {
67
- "rate": 47.7682,
68
- "timestamp": 1779042576.8927135
69
  },
70
  "LKR_CNY": {
71
- "rate": 0.020934429180919523,
72
- "timestamp": 1779042576.892715
73
  },
74
  "QAR_LKR": {
75
- "rate": 89.0738,
76
- "timestamp": 1779042576.8927214
77
  },
78
  "LKR_QAR": {
79
- "rate": 0.011226645770136672,
80
- "timestamp": 1779042576.8927226
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
  }