Spaces:
Sleeping
Sleeping
Update app.py
Browse files3 unmodified lines4567891010 unmodified lines2122232425262717 unmodified lines4546474849505152533 unmodified lines575859606162636465666768697071727374757677784 unmodified lines8384858687888912 unmodified lines1021031041051061071081091103 unmodified lines# β’ Pulls the last 40 Treasury βDebt to the Pennyβ points# β’ Derives a $/sec rate from point-0 and point-30 (or falls back to the last# two distinct points when needed)# β’ Projects the current debt from the timestamp of the latest record## Endpoints# / β simple text βrunningβ check10 unmodified lines# βββββββββββββββββββ runtime state ββββββββββββββββββββDEBT_STATE = {"debt_at_record": 0.0, # value at latest record date"record_time": 0.0, # epoch seconds for that record date (00:00 UTC)"rate_per_sec": 0.0, # $/s derived from chosen points"last_refresh": 0.0}17 unmodified linesfor item in r.json()["data"]] # newest firstdef epoch(date_str):# record_date is YYYY-MM-DD (interpreted as 00:00 UTC)return time.mktime(time.strptime(date_str, "%Y-%m-%d"))# βββββββββββββββββββ background refresher ββββββββββββdef refresher():3 unmodified lines(d0_date, d0_val) = recs[0]# attempt the long-horizon slopecandidate = Noneif len(recs) > HORIZON:candidate = recs[HORIZON]if candidate and candidate[1] != d0_val:(dk_date, dk_val) = candidate # point-30 (β one month ago)else:# fall back to previous distinct pointdk_date, dk_val = next((d for d in recs[1:] if d[1] != d0_val),(None, None))if dk_date is None: # still identical, give upraise ValueError("Could not find two distinct points")t0, tk = epoch(d0_date), epoch(dk_date)rate = (d0_val - dk_val) / (t0 - tk) # $/s (may be negative)DEBT_STATE.update(4 unmodified lines)print(f"[refresh] {d0_date=} debt={d0_val:,.2f} "f"rate={rate:,.2f}$/s horizon={t0 - tk:.0f}s")12 unmodified lines@app.route("/api/debt")def api_debt():now = time.time()# subtract 12 h guard so we never βproject into the futureβ for the# current calendar day, yet keep full projection when record is >1 d oldelapsed = max(0.0, now - DEBT_STATE["record_time"] - 12*3600)current = DEBT_STATE["debt_at_record"] + DEBT_STATE["rate_per_sec"] * elapsedreturn jsonify(startingDebt = current,3 unmodified lines456789101110 unmodified lines2223242526272817 unmodified lines464748495051525354553 unmodified lines59606162636465666768697071727374757677784 unmodified lines8384858687888912 unmodified lines1021031041051061071083 unmodified lines# β’ Pulls the last 40 Treasury βDebt to the Pennyβ points# β’ Derives a $/sec rate from point-0 and point-30 (or falls back to the last# two distinct points when needed)# β’ Projects the current debt from the timestamp (00:01 UTC) of the latest# record all the way to *now*βno 12-hour guard## Endpoints# / β simple text βrunningβ check10 unmodified lines# βββββββββββββββββββ runtime state ββββββββββββββββββββDEBT_STATE = {"debt_at_record": 0.0, # value at latest record date"record_time": 0.0, # epoch seconds for that record date (00:01 UTC)"rate_per_sec": 0.0, # $/s derived from chosen points"last_refresh": 0.0}17 unmodified linesfor item in r.json()["data"]] # newest firstdef epoch_0001utc(date_str: str) -> float:"""Convert YYYY-MM-DD to seconds since epoch at 00:01 UTC of that day."""tm = time.strptime(date_str, "%Y-%m-%d")return time.mktime(tm) + 60 # add 60 s β 00:01 UTC# βββββββββββββββββββ background refresher ββββββββββββdef refresher():3 unmodified lines(d0_date, d0_val) = recs[0]# attempt the long-horizon slopecandidate = recs[HORIZON] if len(recs) > HORIZON else Noneif candidate and candidate[1] != d0_val:(dk_date, dk_val) = candidate # point-30else:# fall back to previous distinct pointdk_date, dk_val = next((d for d in recs[1:] if d[1] != d0_val),(None, None))if dk_date is None:raise ValueError("Could not find two distinct points")t0, tk = epoch_0001utc(d0_date), epoch_0001utc(dk_date)rate = (d0_val - dk_val) / (t0 - tk) # $/s (may be negative)DEBT_STATE.update(4 unmodified lines)print(f"[refresh] latest={d0_date} debt={d0_val:,.2f} "f"rate={rate:,.2f}$/s horizon={t0 - tk:.0f}s")12 unmodified lines@app.route("/api/debt")def api_debt():now = time.time()elapsed = max(0.0, now - DEBT_STATE["record_time"]) # **no 12-h guard**current = DEBT_STATE["debt_at_record"] + DEBT_STATE["rate_per_sec"] * elapsedreturn jsonify(startingDebt = current,
app.py+13-15