Skip to content

tdfs4ds.serve.framework_cost

tdfs4ds.serve.framework_cost

Framework cost report (Performance > Framework cost, 0.3.8.0): what tdfs4ds itself costs per call.

Three layers, like efs_map and time_manager_report:

  • :func:collect_framework_cost: the only I/O (reads FS_V_CALL_PHASES, and on demand measures the round trip with SELECT 1 on its own connection);
  • :func:build_framework_cost_model: pure, on a pandas frame;
  • :func:render_framework_cost_html (+ :func:steps_csv): inline SVG and scoped CSS, no external script, light and dark through the shell's --td-* tokens (the standalone file defines them itself).

The eight angles

  1. fixed against variable cost (cost = a + b x rows per process, robust fit, break-even rows);
  2. where the fixed cost goes (phases, safety highlighted);
  3. latency against work (statements x round trip against database time, with a what-if);
  4. volume classes; 5. per call kind; 6. trend by package version;
  5. the heaviest framework phases, each with its fix; 8. anomalies (robust z, median / MAD).

What a "step" is, and which numbers are honest, is stated in :mod:tdfs4ds.call_phases: rows are the feature rows written (an unchanged re-run writes none), a call's steps are its executions (at least 1), and the fit and the anomalies use only the calls that touched a single process (the others cannot split their call-level statements between processes).

CALL_LEVEL module-attribute

CALL_LEVEL = 'CALL_LEVEL'

Z_LIMIT module-attribute

Z_LIMIT = 3.5

ANOMALY_WINDOW module-attribute

ANOMALY_WINDOW = 60

MIN_STEPS_ANOMALY module-attribute

MIN_STEPS_ANOMALY = 8

WHAT_IF_MS module-attribute

WHAT_IF_MS = (450.0, 50.0, 5.0)

ROUND_TRIP_HELP_HTML module-attribute

ROUND_TRIP_HELP_HTML = '<details class="fc-help"><summary>What is the round trip, and what does "Measure the round trip" do?</summary><p><b>The round trip</b> is the time one SQL statement takes to travel from the machine that runs tdfs4ds (this app, or your notebook) to the database and for the answer to come back, <b>not counting the work the database does</b>. Every statement pays it once, even a tiny one. The database seconds in this report do not include it: DBQL only sees the time spent inside the database.</p><p><b>Why it matters.</b> A call that sends 200 statements spends 200 round trips on the network. At 5 ms (same data centre) that is 1 s; at 450 ms (a laptop on a VPN far from the system) it is 90 s, often more than the database work itself. The three fixed columns show these cases: <b>450 ms</b> remote over a VPN, <b>50 ms</b> same cloud region, <b>5 ms</b> next to the database.</p><p><b>"Measure the round trip"</b> runs <code>SELECT 1</code> five times on a connection of its own, from where the app runs, and keeps the median. It reads no table and costs nothing in the database. The result sets the slider and the <b>At the slider</b> column, so the table tells you how long each process step takes from here. A notebook or a scheduler running elsewhere has its own round trip.</p><p><b>Reading the Bound column.</b> <b>latency</b>: more than half of the step time is spent on the network; send fewer statements (roll_out instead of a loop of run, one clock per domain, fewer FilterManager segments) or run the job closer to the database. <b>work</b>: the database itself is the cost; look at the SQL of the process, its primary index and partitioning.</p></details>'

VOLUME_CLASSES module-attribute

VOLUME_CLASSES = ('no rows written', '< 10k rows', '10k - 1M rows', '> 1M rows')

STATE_OK module-attribute

STATE_OK = 'ok'

STATE_NO_STORE module-attribute

STATE_NO_STORE = 'no_store'

STATE_NO_TABLE module-attribute

STATE_NO_TABLE = 'no_table'

STATE_NO_ROWS module-attribute

STATE_NO_ROWS = 'no_rows'

STATE_ERROR module-attribute

STATE_ERROR = 'error'

FIXES module-attribute

FIXES = {'catalog': 'Metadata reads: fewer processes per roll_out call, or run closer to the database.', 'options': 'Settings reads: one per process per call; roll_out already reads them once.', 'upstream': 'check_upstream=True reads the lineage once per call; leave it False when the order is guaranteed elsewhere (the guard is a safety check: its cost is the price of the refusal).', 'clock': 'TimeManager reads and cursor moves: one clock per data domain (set_domain_time_manager), roll_out(data_domain=...) moves it once per date for all processes.', 'history_guard': 'The history guard reads the stored history once per run; skipped for valid_time_column processes.', 'skip_check': 'nothing_to_compute / final_runs_at: one COUNT per process-date; a date already COMPLETED costs only this check.', 'filter': 'FilterManager: fewer, larger segments (each segment is a full ingestion), or none.', 'followup': 'Follow-up rows (open, close): two statements per execution; fewer executions = fewer segments or dates.', 'entity_refresh': 'track_entity_refresh=False on processes that do not need the last-seen date per entity.', 'lineage': 'Lineage is re-recorded at registration only; register once, not per run.', 'sql_capture': 'Process SQL versions are captured at registration and per run; PROCESS_SQL_VERSIONING=False turns it off (the versions are then not kept).', 'registration': 'A registration that computes nothing still pays its metadata: register once, then run().', 'documentation': 'document=False (the default for a fast loop): documentation is an LLM step.', 'optimize': 'optimize_query=False: the optimizer is an LLM step.', 'history': 'The copy of DBQL into FS_EXECUTION_RESOURCES / FS_CALL_PHASES: throttled to once per 10 minutes per process; EXECUTION_RESOURCES=False and PHASE_TAGS=False stop the copies.', 'query_band': 'tdfs4ds.QUERY_BAND=False removes the band statements (and the profile with them).', 'other': 'Statements outside any phase block, plus CREATE/REPLACE VIEW (a TimeManager or FilterManager cursor move): they are never tagged.', 'uniqueness': 'The duplicate-key check scans the staged rows once; on a very large process it is the cost of a correct primary index.', 'null_key': 'The NULL-key check is one aggregate over the staged rows.', 'row_count': 'The staged row count is one aggregate over the staged rows.', 'truncation': 'The VARCHAR truncation check runs only for registered VARCHAR features.'}

ORIGINS module-attribute

ORIGINS = ('app', 'upload_features', 'roll_out', 'run', 'build_dataset', 'other')

_KIND_ORIGIN module-attribute

_KIND_ORIGIN = {'upload': 'upload_features', 'roll_out': 'roll_out', 'run': 'run', 'dataset': 'build_dataset'}

_METRICS module-attribute

_METRICS = (('statements', 'N_STATEMENTS'), ('db_sec', 'DB_ELAPSED_SEC'), ('cpu_sec', 'AMP_CPU_SEC'), ('io', 'TOTAL_IO'))

_CSS module-attribute

_CSS = '<style>\n.fc-wrap{--fc-wf:#1E3A8A;--fc-ink:var(--td-navy,#00233C);--fc-mute:var(--td-slate,#5A6B7B);--fc-card:var(--td-surface,#fff);\n --fc-soft:var(--td-soft,#F5F7FA);--fc-line:var(--td-border,#E2E8F0);--fc-work:#2563EB;--fc-fw:#64748B;\n --fc-sf:#FF5F00;--fc-bad:#B42318;--fc-ok:#027A48;color:var(--fc-ink);font-family:inherit}\n@media (prefers-color-scheme:dark){:root:not([data-theme="light"]) .fc-wrap{--fc-wf:#3B82F6;--fc-work:#93C5FD;--fc-fw:#94A3B8;\n --fc-sf:#FF8A3D;--fc-bad:#F97066;--fc-ok:#47CD89}}\n:root[data-theme="dark"] .fc-wrap{--fc-wf:#3B82F6;--fc-work:#93C5FD;--fc-fw:#94A3B8;--fc-sf:#FF8A3D;--fc-bad:#F97066;--fc-ok:#47CD89}\n.fc-wrap .fc-kpis{display:flex;flex-wrap:wrap;gap:10px;margin:8px 0 14px}\n.fc-wrap .fc-kpi{background:var(--fc-card);border:1px solid var(--fc-line);border-radius:10px;padding:8px 14px;min-width:130px}\n.fc-wrap .fc-kpi b{display:block;font-size:1.25rem}.fc-wrap .fc-kpi span{font-size:.78rem;color:var(--fc-mute)}\n.fc-wrap details{border:1px solid var(--fc-line);border-radius:8px;margin:8px 0;background:var(--fc-card)}\n.fc-wrap summary{cursor:pointer;padding:8px 12px;font-weight:600}\n.fc-wrap .fc-body{padding:4px 12px 12px}\n.fc-wrap table{border-collapse:collapse;width:100%;font-size:.84rem}\n.fc-wrap th{background:var(--fc-soft);text-align:left;padding:5px 8px;border-bottom:1px solid var(--fc-line)}\n.fc-wrap td{border-top:1px solid var(--fc-line);padding:5px 8px;vertical-align:top}\n.fc-wrap td.n,.fc-wrap th.n{text-align:right;font-variant-numeric:tabular-nums}\n.fc-wrap .fc-note{font-size:.82rem;color:var(--fc-mute);margin:4px 0 8px}\n.fc-wrap .fc-empty{background:var(--fc-soft);border:1px dashed var(--fc-line);border-radius:10px;padding:14px;margin:10px 0}\n.fc-wrap .fc-tag{display:inline-block;border-radius:9px;padding:0 8px;font-size:.74rem;font-weight:700;color:#fff}\n.fc-wrap .t-work{background:var(--fc-work)}.fc-wrap .t-framework{background:var(--fc-fw)}.fc-wrap .t-safety{background:var(--fc-sf)}\n.fc-wrap .fc-bad{color:var(--fc-bad);font-weight:700}.fc-wrap .fc-ok{color:var(--fc-ok)}\n.fc-wrap svg{max-width:100%;height:auto}.fc-wrap svg text{fill:var(--fc-ink);font-size:11px}\n.fc-wrap svg .ax{stroke:var(--fc-line)}.fc-wrap svg .mute{fill:var(--fc-mute)}\n.fc-wrap .s-work{fill:var(--fc-work)}.fc-wrap .s-framework{fill:var(--fc-fw)}.fc-wrap .s-safety{fill:var(--fc-sf)}\n.fc-wrap .pt{fill:var(--fc-work);opacity:.85}.fc-wrap .ln{stroke:var(--fc-sf);stroke-width:2;fill:none}\n.fc-wrap .flag{fill:var(--fc-bad)}\n.fc-wrap .s-workflow{fill:var(--fc-wf)}.fc-wrap .s-workflow-bg{background:var(--fc-wf)}\n.fc-wrap .fc-dot{display:inline-block;width:10px;height:10px;border-radius:50%;vertical-align:middle}\n.fc-wrap .s-work-bg{background:var(--fc-work)}.fc-wrap .s-framework-bg{background:var(--fc-fw)}.fc-wrap .s-safety-bg{background:var(--fc-sf)}\n.fc-wrap code{font-size:.8rem;word-break:break-all}\n.fc-wrap input[type=range]{width:260px;vertical-align:middle}\n</style>'

_STANDALONE_TOKENS module-attribute

_STANDALONE_TOKENS = '<style>\n:root{--td-navy:#00233C;--td-slate:#5A6B7B;--td-surface:#fff;--td-soft:#F5F7FA;--td-border:#E2E8F0;--td-bg:#fff}\n@media (prefers-color-scheme:dark){:root{--td-navy:#E6EDF5;--td-slate:#9AA9B8;--td-surface:#111F2E;--td-soft:#16273A;\n--td-border:#24384E;--td-bg:#0B1622}}\nbody{background:var(--td-bg);color:var(--td-navy);font-family:Segoe UI,system-ui,sans-serif;margin:0;padding:16px}\n</style>'

_ROLE_LABEL module-attribute

_ROLE_LABEL = {'workflow': 'your SQL (workflow)', 'work': 'tdfs4ds work', 'safety': 'safety checks', 'framework': 'framework'}

_SLIDER_JS module-attribute

_SLIDER_JS = "<script>(function(){var r=document.getElementById('fc-rtt');if(!r)return;\nfunction go(){var ms=parseFloat(r.value);document.getElementById('fc-rtt-v').textContent=ms+' ms';\ndocument.querySelectorAll('[data-fc-stm]').forEach(function(td){var s=parseFloat(td.dataset.fcStm),d=parseFloat(td.dataset.fcDb);\nvar w=s*ms/1000,t=d+w;td.textContent=t.toFixed(1)+' s ('+(t?Math.round(100*w/t):0)+'% wire)';});}\nr.addEventListener('input',go);go();})();</script>"

_e

_e(v) -> str

_num

_num(x, default=0.0) -> float

collect_framework_cost

collect_framework_cost(days=90, data_domains=None, *, measure_round_trip=False, run=None) -> dict

Read the facts. Never raises: {"state", "facts", "error", "hint", "round_trip_ms", "days"}.

run: run(sql) -> DataFrame (tests); default the teradataml session.

measure_round_trip_ms

measure_round_trip_ms(n=5) -> Optional[float]

Median of n SELECT 1 on a connection of our own (never the shared one). None when it cannot.

robust_line

robust_line(xs, ys) -> Optional[dict]

Theil-Sen line y = a + b x (median of the pairwise slopes), robust to outliers. Needs 3 points and 2 distinct x. {"a", "b", "n"} or None. Pure.

_median

_median(sorted_vals) -> float

break_even_rows

break_even_rows(fit) -> Optional[float]

Rows at which the variable cost b x rows equals the fixed cost a; None without a positive a and b. Pure.

robust_z

robust_z(values) -> List[float]

Robust z of each value: 0.6745 (x - median) / MAD; a zero MAD falls back to the mean absolute deviation ((x - median) / (1.2533 meanAD)); a constant series gives 0. Pure.

volume_class

volume_class(rows_per_step) -> str

what_if

what_if(stmts_per_step, db_sec_per_step, rtt_ms) -> dict

Estimated step time at a given round trip: db + statements x rtt. Pure.

_version_key

_version_key(v)

_steps_frame

_steps_frame(f)

One row per (call, process): the step table (the FS_V_FRAMEWORK_COST_RUN view, rebuilt in pandas so the model works on the facts alone).

_first

_first(series)

build_framework_cost_model

build_framework_cost_model(inputs: dict, round_trip_ms: Optional[float] = None) -> dict

The report's data. inputs is :func:collect_framework_cost's result. Pure.

origin_of

origin_of(call_kind, app_user=None) -> str

Where a call came from: app when a person of the admin app is named on it (TDFS4DS_APP_USER, set by the login gate), else its TDFS4DS_CALL_KIND (upload_features, roll_out, run, build_dataset = build_dataset and build_historical_dataset, which share the kind dataset), else other. Pure.

_annotated

_annotated(f)

The facts with ORIGIN (per call), ROLE (per phase) and numeric measures (0 when absent). Pure.

_compare_rows

_compare_rows(g, key) -> list

One row per value of key: calls, the four measures split by role, and the ratio (framework + safety) / workflow of each. ratio is None when the workflow side is 0. Pure.

build_workflow_comparison

build_workflow_comparison(f) -> dict

Framework cost against the user's workflow over the same facts (same domains, window, calls).

Workflow = the phases in phases.WORKFLOW (compute: the staging read of the process view). Roles: workflow, work (tdfs4ds' own work: MERGE, statistics, dataset builds), safety, framework. Keys: total, by_origin (ORIGINS order), by_kind (the call kinds), by_process (a process, or CALL_LEVEL for statements no process can be given), phase_by_origin (phase x origin statements and database seconds). Pure.

_phase_table

_phase_table(f, total_steps) -> list

_fits

_fits(steps) -> list

_latency

_latency(steps, rtt) -> list

_volume

_volume(steps) -> list

_kinds

_kinds(steps) -> list

_versions

_versions(steps) -> list

_heaviest

_heaviest(f) -> dict

_anomalies

_anomalies(steps, f) -> list

_fmt

_fmt(v, nd=2, unit='') -> str

_short

_short(name) -> str

A view name without its database and quotes (process ids are kept whole).

_pct

_pct(v) -> str

_tag

_tag(group) -> str

_chart_rows

_chart_rows(phases, key)

The rows a phase bar chart draws, in drawing order (largest first, at most 18).

_phase_tip

_phase_tip(name) -> str

_svg_phase_bars

_svg_phase_bars(phases, key='statements', width=640) -> str

_phase_legend_html

_phase_legend_html(phases) -> str

"What the labels mean": one row per phase drawn in either chart (statements chart order first), with its kind, what it covers and how to reduce it. Text from phases.PHASE_DESCRIPTIONS. Pure.

_svg_stack_bars

_svg_stack_bars(rows, metric, width=640, fmt=None) -> str

Horizontal stacked bars, one per row, split by role (workflow / work / safety / framework). Pure.

_ratio_txt

_ratio_txt(v) -> str

_workflow_html

_workflow_html(w) -> str

Sections 9 and 10: framework against the user's SQL, and the cost by origin. Pure.

_svg_scatter

_svg_scatter(points, line, width=420, height=220, xlabel='rows written per step', ylabel='DB seconds per step') -> str

_svg_timeline

_svg_timeline(rows, width=560) -> str

Anomaly strip: one dot per flagged step along the time of the window.

_empty_html

_empty_html(model) -> str

render_framework_cost_html

render_framework_cost_html(model: dict, standalone: bool = False) -> str

The report body (or a whole page with standalone=True: theme tokens included).

steps_csv

steps_csv(model: dict) -> str

The step table (one row per call and process) as CSV, for analytics outside the app.