"""SOURCE render logic for TI-Cal-06 (REFERENCE ONLY). This is the exact function from our pipeline. It is DB-coupled — you cannot run it without our database. The query result it produces is materialized for you in data.csv, and the rendered output is figure.json (load that to display the chart). This file is provided so you can understand / re-theme the logic. """ def render_TI_Cal_06(reg: dict) -> dict: """Calendar Competition Load - releases per day in last 90 days.""" chart_id = "TI-Cal-06" output_dir = THIS_DIR / chart_id / ROLE conn = lib.connect_readonly() t0 = time.time() # Reframed (user: "is day-of-week more interesting?") - yes: release day-of-week has a strong, clean signal. df = lib.run_query(conn, """ SELECT EXTRACT(DOW FROM release_date)::integer AS dow, COUNT(DISTINCT imdb_id) AS releases, MAX(release_date) AS max_rd FROM categorization_release_dates_all WHERE release_date >= DATE '2022-01-01' AND release_date <= CURRENT_DATE GROUP BY EXTRACT(DOW FROM release_date) ORDER BY dow; """) q_sec = time.time() - t0 conn.close() if df.empty: lib.needs_spec_review(output_dir, chart_id, reg["spec_file"], reg["pc_id"], "No release-DOW data") write_shim(output_dir, chart_id); return {"status": "needs_spec_review"} df["releases"] = df["releases"].astype(int) freshness = str(df["max_rd"].max()) names = {0: "Sunday", 1: "Monday", 2: "Tuesday", 3: "Wednesday", 4: "Thursday", 5: "Friday", 6: "Saturday"} order = [1, 2, 3, 4, 5, 6, 0] # Mon → Sun df = df.set_index("dow").reindex(order).reset_index() df["day"] = df["dow"].map(names) df["releases"] = df["releases"].fillna(0).astype(int) fri = df[df["dow"] == 5]["releases"].iloc[0]; total = df["releases"].sum() colors = [lib.NAVY if d == 5 else lib.PALE_BLUE for d in df["dow"]] fig = go.Figure() fig.add_trace(go.Bar(x=df["day"], y=df["releases"], marker=dict(color=colors), showlegend=False, text=[f"{v:,}" for v in df["releases"]], textposition="outside", textfont=dict(size=11, color=lib.INK), cliponaxis=False, hovertemplate="%{x}
%{y:,} titles released")) title_lesson = f"New titles drop on Fridays: {fri/total:.0%} of all releases land on a single weekday" what = ("How many titles are released on each day of the week (2022-present). Friday (highlighted) carries nearly a third of all " "releases - the streaming and theatrical default release slot.") why = ("Release day is a crowded-room problem. Everything competes for the same Friday attention - and Thursday is nearly as " "crowded - while the weekend and Monday are comparatively empty. A counter-programmed launch, dropping when nobody else " "does, can own the conversation instead of splitting it.") fig.update_layout(**lib.layout_v2(title_lesson, what, width=1150, height=560), xaxis=lib.standard_xaxis("Day of week released"), yaxis=lib.standard_yaxis("Titles released (2022-present)", tickformat=",d")) fig.update_yaxes(range=[0, float(df["releases"].max()) * 1.16]) lib.add_why_and_footer(fig, why, lib.assemble_footer("PC-Cal-06", ["CL-014 external data (release dates)"], freshness, ["categorization_release_dates_all"], "all releases since 2022")) lib.save_csv(df, output_dir); lib.export_chart(fig, output_dir, width=1150, height=560) lib.write_lesson_design(output_dir, chart_id, f"# {chart_id}\n\n{title_lesson}\n\n{what}\n") write_shim(output_dir, chart_id) lib.write_manifest(output_dir, chart_id, "ok", reg["spec_file"], pc_id="PC-Cal-06", intended_lesson_from_spec="Release day-of-week (reframed from 90-day load)", rendered_lesson={"reader_title": title_lesson, "reader_subtitle": what, "claim_strength": "strong", "rules_applied": "DOW release counts 2022+"}, row_count=len(df), freshness_date=freshness, query_seconds=q_sec, render_seconds=time.time() - t0 - q_sec) return {"status": "ok", "rows": len(df)}