Jitendra12421 commited on
Commit
bc06848
·
verified ·
1 Parent(s): bf4b95a

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -5
app.py CHANGED
@@ -1,7 +1,7 @@
1
  from __future__ import annotations
2
 
3
  import asyncio
4
- from datetime import date
5
 
6
  import sys
7
  from pathlib import Path
@@ -10,7 +10,7 @@ from fastapi import FastAPI, Query
10
  from fastapi.middleware.cors import CORSMiddleware
11
 
12
  sys.path.insert(0, str(Path(__file__).resolve().parent))
13
- from nifty_backend.runtime import dashboard_payload, latest_saved_prediction, refresh_daily_data, refresh_first5_prediction, seconds_until_next_ist_run
14
 
15
 
16
  app = FastAPI(title="NIFTY 50 Forecaster Backend")
@@ -23,21 +23,52 @@ app.add_middleware(
23
  )
24
 
25
 
 
 
26
  async def daily_ist_refresh_loop() -> None:
 
27
  while True:
28
- await asyncio.sleep(seconds_until_next_ist_run())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  try:
30
  await asyncio.to_thread(refresh_first5_prediction)
 
31
  except Exception as exc:
32
  print(f"[scheduler] first5 refresh failed: {exc}", flush=True)
 
 
33
  try:
34
  await asyncio.to_thread(refresh_daily_data)
35
  except Exception as exc:
36
  print(f"[scheduler] daily refresh failed: {exc}", flush=True)
37
 
38
-
39
  @app.on_event("startup")
40
  async def start_scheduler() -> None:
 
 
 
 
 
 
 
 
 
 
 
 
41
  asyncio.create_task(daily_ist_refresh_loop())
42
 
43
 
@@ -53,7 +84,12 @@ def root() -> dict[str, str]:
53
 
54
  @app.get("/dashboard")
55
  def dashboard() -> dict:
56
- return dashboard_payload()
 
 
 
 
 
57
 
58
 
59
  @app.get("/cron/keepalive")
 
1
  from __future__ import annotations
2
 
3
  import asyncio
4
+ from datetime import date, datetime, time
5
 
6
  import sys
7
  from pathlib import Path
 
10
  from fastapi.middleware.cors import CORSMiddleware
11
 
12
  sys.path.insert(0, str(Path(__file__).resolve().parent))
13
+ from nifty_backend.runtime import dashboard_payload, latest_saved_prediction, refresh_daily_data, refresh_first5_prediction, seconds_until_next_ist_run, IST
14
 
15
 
16
  app = FastAPI(title="NIFTY 50 Forecaster Backend")
 
23
  )
24
 
25
 
26
+ market_status = "Waiting for next session"
27
+
28
  async def daily_ist_refresh_loop() -> None:
29
+ global market_status
30
  while True:
31
+ # Wait until 9:00 AM IST
32
+ await asyncio.sleep(seconds_until_next_ist_run(time(9, 0)))
33
+ market_status = "Market Pre-Open"
34
+ print("[scheduler] 9:00 AM IST - Market Pre-Open", flush=True)
35
+
36
+ # Wait until 9:15 AM IST
37
+ await asyncio.sleep(seconds_until_next_ist_run(time(9, 15)))
38
+ market_status = "Market Officially Opened"
39
+ print("[scheduler] 9:15 AM IST - Market Officially Opened", flush=True)
40
+
41
+ # Wait until 9:20 AM IST
42
+ await asyncio.sleep(seconds_until_next_ist_run(time(9, 20)))
43
+ market_status = "Fetching T+5 Prediction Data..."
44
+ print("[scheduler] 9:20 AM IST - Fetching Data", flush=True)
45
+
46
  try:
47
  await asyncio.to_thread(refresh_first5_prediction)
48
+ market_status = "Prediction Ready"
49
  except Exception as exc:
50
  print(f"[scheduler] first5 refresh failed: {exc}", flush=True)
51
+ market_status = "Prediction Failed"
52
+
53
  try:
54
  await asyncio.to_thread(refresh_daily_data)
55
  except Exception as exc:
56
  print(f"[scheduler] daily refresh failed: {exc}", flush=True)
57
 
 
58
  @app.on_event("startup")
59
  async def start_scheduler() -> None:
60
+ global market_status
61
+ # Initialize correct status on startup based on current time
62
+ now = datetime.now(IST).time()
63
+ if now < time(9, 0):
64
+ market_status = "Waiting for 9:00 AM"
65
+ elif now < time(9, 15):
66
+ market_status = "Market Pre-Open"
67
+ elif now < time(9, 20):
68
+ market_status = "Market Officially Opened"
69
+ else:
70
+ market_status = "Prediction Ready"
71
+
72
  asyncio.create_task(daily_ist_refresh_loop())
73
 
74
 
 
84
 
85
  @app.get("/dashboard")
86
  def dashboard() -> dict:
87
+ global market_status
88
+ payload = dashboard_payload()
89
+ if "data_status" not in payload:
90
+ payload["data_status"] = {}
91
+ payload["data_status"]["market_status"] = market_status
92
+ return payload
93
 
94
 
95
  @app.get("/cron/keepalive")