ntdservices commited on
Commit
d2180f1
Β·
verified Β·
1 Parent(s): 1320d43

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -15
app.py CHANGED
@@ -4,7 +4,8 @@
4
  # β€’ Pulls the last 40 Treasury β€œDebt to the Penny” points
5
  # β€’ Derives a $/sec rate from point-0 and point-30 (or falls back to the last
6
  # two distinct points when needed)
7
- # β€’ Projects the current debt from the timestamp of the latest record
 
8
  #
9
  # Endpoints
10
  # / – simple text β€œrunning” check
@@ -21,7 +22,7 @@ app = Flask(__name__)
21
  # ─────────────────── runtime state ────────────────────
22
  DEBT_STATE = {
23
  "debt_at_record": 0.0, # value at latest record date
24
- "record_time": 0.0, # epoch seconds for that record date (00:00 UTC)
25
  "rate_per_sec": 0.0, # $/s derived from chosen points
26
  "last_refresh": 0.0
27
  }
@@ -45,9 +46,10 @@ def load_debt_records():
45
  for item in r.json()["data"]
46
  ] # newest first
47
 
48
- def epoch(date_str):
49
- # record_date is YYYY-MM-DD (interpreted as 00:00 UTC)
50
- return time.mktime(time.strptime(date_str, "%Y-%m-%d"))
 
51
 
52
  # ─────────────────── background refresher ────────────
53
  def refresher():
@@ -57,22 +59,20 @@ def refresher():
57
  (d0_date, d0_val) = recs[0]
58
 
59
  # attempt the long-horizon slope
60
- candidate = None
61
- if len(recs) > HORIZON:
62
- candidate = recs[HORIZON]
63
 
64
  if candidate and candidate[1] != d0_val:
65
- (dk_date, dk_val) = candidate # point-30 (β‰ˆ one month ago)
66
  else:
67
  # fall back to previous distinct point
68
  dk_date, dk_val = next(
69
  (d for d in recs[1:] if d[1] != d0_val),
70
  (None, None)
71
  )
72
- if dk_date is None: # still identical, give up
73
  raise ValueError("Could not find two distinct points")
74
 
75
- t0, tk = epoch(d0_date), epoch(dk_date)
76
  rate = (d0_val - dk_val) / (t0 - tk) # $/s (may be negative)
77
 
78
  DEBT_STATE.update(
@@ -83,7 +83,7 @@ def refresher():
83
  )
84
 
85
  print(
86
- f"[refresh] {d0_date=} debt={d0_val:,.2f} "
87
  f"rate={rate:,.2f}$/s horizon={t0 - tk:.0f}s"
88
  )
89
 
@@ -102,9 +102,7 @@ def root():
102
  @app.route("/api/debt")
103
  def api_debt():
104
  now = time.time()
105
- # subtract 12 h guard so we never β€œproject into the future” for the
106
- # current calendar day, yet keep full projection when record is >1 d old
107
- elapsed = max(0.0, now - DEBT_STATE["record_time"] - 12*3600)
108
  current = DEBT_STATE["debt_at_record"] + DEBT_STATE["rate_per_sec"] * elapsed
109
  return jsonify(
110
  startingDebt = current,
 
4
  # β€’ Pulls the last 40 Treasury β€œDebt to the Penny” points
5
  # β€’ Derives a $/sec rate from point-0 and point-30 (or falls back to the last
6
  # two distinct points when needed)
7
+ # β€’ Projects the current debt from the timestamp (00:01 UTC) of the latest
8
+ # record all the way to *now*β€”no 12-hour guard
9
  #
10
  # Endpoints
11
  # / – simple text β€œrunning” check
 
22
  # ─────────────────── runtime state ────────────────────
23
  DEBT_STATE = {
24
  "debt_at_record": 0.0, # value at latest record date
25
+ "record_time": 0.0, # epoch seconds for that record date (00:01 UTC)
26
  "rate_per_sec": 0.0, # $/s derived from chosen points
27
  "last_refresh": 0.0
28
  }
 
46
  for item in r.json()["data"]
47
  ] # newest first
48
 
49
+ def epoch_0001utc(date_str: str) -> float:
50
+ """Convert YYYY-MM-DD to seconds since epoch at 00:01 UTC of that day."""
51
+ tm = time.strptime(date_str, "%Y-%m-%d")
52
+ return time.mktime(tm) + 60 # add 60 s β†’ 00:01 UTC
53
 
54
  # ─────────────────── background refresher ────────────
55
  def refresher():
 
59
  (d0_date, d0_val) = recs[0]
60
 
61
  # attempt the long-horizon slope
62
+ candidate = recs[HORIZON] if len(recs) > HORIZON else None
 
 
63
 
64
  if candidate and candidate[1] != d0_val:
65
+ (dk_date, dk_val) = candidate # point-30
66
  else:
67
  # fall back to previous distinct point
68
  dk_date, dk_val = next(
69
  (d for d in recs[1:] if d[1] != d0_val),
70
  (None, None)
71
  )
72
+ if dk_date is None:
73
  raise ValueError("Could not find two distinct points")
74
 
75
+ t0, tk = epoch_0001utc(d0_date), epoch_0001utc(dk_date)
76
  rate = (d0_val - dk_val) / (t0 - tk) # $/s (may be negative)
77
 
78
  DEBT_STATE.update(
 
83
  )
84
 
85
  print(
86
+ f"[refresh] latest={d0_date} debt={d0_val:,.2f} "
87
  f"rate={rate:,.2f}$/s horizon={t0 - tk:.0f}s"
88
  )
89
 
 
102
  @app.route("/api/debt")
103
  def api_debt():
104
  now = time.time()
105
+ elapsed = max(0.0, now - DEBT_STATE["record_time"]) # **no 12-h guard**
 
 
106
  current = DEBT_STATE["debt_at_record"] + DEBT_STATE["rate_per_sec"] * elapsed
107
  return jsonify(
108
  startingDebt = current,