Skip to content

Commit 34a68fe

Browse files
committed
fix(server): count only each run's newest cost attempt and pass node keys to cost reads
1 parent 60eabaa commit 34a68fe

14 files changed

Lines changed: 83 additions & 64 deletions

File tree

‎benchmarks/duckshop/edge_cases/orchestration/resource_cost_history.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"observations": [
3030
{"observation_id": "build-1-run-1", "hours_ago": 2, "orchestration_run_id": "00000000-0000-0000-0000-000000001302", "resource_type": "_total", "resource_name": "build-1", "warehouse_name": "", "warehouse_size": "", "warehouse_type": null, "cluster_number": null, "status": "partial", "estimated_credits": "0.15", "estimated_usd": "0.45", "statements_observed": 2},
3131
{"observation_id": "build-1-orders-1", "hours_ago": 2, "orchestration_run_id": "00000000-0000-0000-0000-000000001302", "status": "partial", "estimated_credits": "0.10", "estimated_usd": "0.30", "statements_observed": 2},
32+
{"observation_id": "build-1-payments-1", "hours_ago": 2, "orchestration_run_id": "00000000-0000-0000-0000-000000001302", "resource_name": "payments", "status": "partial", "estimated_credits": "0.03", "estimated_usd": "0.09", "statements_observed": 2},
3233
{"observation_id": "build-1-customers-1", "hours_ago": 2, "orchestration_run_id": "00000000-0000-0000-0000-000000001302", "resource_name": "customers", "status": "partial", "estimated_credits": "0.05", "estimated_usd": "0.15", "statements_observed": 2},
3334
{"observation_id": "build-1-run-2", "hours_ago": 2, "attempt": 2, "orchestration_run_id": "00000000-0000-0000-0000-000000001302", "resource_type": "_total", "resource_name": "build-1", "warehouse_name": "", "warehouse_size": "", "warehouse_type": null, "cluster_number": null, "estimated_credits": "0.40", "estimated_usd": "1.20"},
3435
{"observation_id": "build-1-orders-2", "hours_ago": 2, "attempt": 2, "orchestration_run_id": "00000000-0000-0000-0000-000000001302", "estimated_credits": "0.25", "estimated_usd": "0.75"},

‎clients/typescript/src/schema.d.ts‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,17 +1684,14 @@ export interface components {
16841684
/** @description A target's estimated query cost over a window, from the latest observation of each run. */
16851685
CostSummaryRecord: {
16861686
by_job: components["schemas"]["CostGroup"][];
1687-
/**
1688-
* @description Every node that ran, keyed `<resource_type>:<resource_name>` like catalog node keys, so
1689-
* callers can group nodes by folder.
1690-
*/
1687+
/** @description Every node that ran, keyed `<resource_type>:<resource_name>` like catalog node keys. */
16911688
by_node: components["schemas"]["CostGroup"][];
16921689
by_warehouse_size: components["schemas"]["CostGroup"][];
16931690
/** Format: double */
16941691
credits: number;
16951692
/**
16961693
* Format: int64
1697-
* @description Runs whose cost is still pending or partial, or whose collection failed.
1694+
* @description Runs whose cost is still pending or partial, which a later build re-reads.
16981695
*/
16991696
incomplete_runs: number;
17001697
/** Format: int64 */
@@ -3450,10 +3447,7 @@ export interface components {
34503447
state_schema: string;
34513448
value: string;
34523449
};
3453-
/**
3454-
* @description One estimated query cost observation of a run, resource and warehouse; display only.
3455-
* A refresh appends a later attempt, and readers take the newest attempt per key.
3456-
*/
3450+
/** @description One estimated query cost observation of a run, resource and warehouse; refreshes append. */
34573451
WarehouseResourceCostRecord: {
34583452
/** Format: int64 */
34593453
attempt: number;

‎crates/sqlbuild-orchestration/src/warehouse_history/_helpers/costs.rs‎

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,22 @@ use crate::warehouse_metadata::models::WarehouseResourceCostRecord;
1313

1414
/// Total rows carry a build's totals; every other row is one resource on one warehouse.
1515
pub(crate) const TOTAL_RESOURCE_TYPE: &str = "_total";
16-
/// A build whose cost is final.
17-
pub(crate) const COMPLETE_STATUS: &str = "complete";
16+
/// Statuses of a build whose cost may still change: a later build re-reads it.
17+
pub(crate) const PENDING_STATUSES: [&str; 2] = ["pending", "partial"];
1818

19-
/// Newest attempt per run, resource and warehouse; a refresh appends, so older attempts stay.
20-
pub(crate) const RUN_COST_SQL: &str = "SELECT record_json FROM (SELECT record_json, ROW_NUMBER() OVER \
21-
(PARTITION BY run_id, resource_type, resource_name, record_json ->> 'warehouse_name', \
22-
record_json ->> 'warehouse_size', record_json ->> 'warehouse_type', \
23-
record_json ->> 'cluster_number' ORDER BY attempt DESC, observed_at DESC, event_id DESC) AS newest \
24-
FROM sqb_warehouse_resource_costs WHERE location_id=$1 AND target=$2 \
25-
AND orchestration_run_id=$3) costs WHERE newest=1 \
19+
/// Every row of the run's newest attempt; a refresh appends a whole attempt, so older ones stay.
20+
pub(crate) const RUN_COST_SQL: &str = "SELECT record_json FROM (SELECT record_json, attempt, \
21+
MAX(attempt) OVER (PARTITION BY run_id) AS newest FROM sqb_warehouse_resource_costs \
22+
WHERE location_id=$1 AND target=$2 AND orchestration_run_id=$3) costs WHERE attempt=newest \
2623
ORDER BY record_json ->> 'resource_type', record_json ->> 'resource_name'";
2724

28-
/// Newest attempt per run, resource and warehouse since `$3`, optionally of one node (`$4`, `$5`).
25+
/// Rows of each run's newest attempt since `$3`, optionally only a node's (`$4`, `$5`) and totals.
2926
const LATEST_COSTS_SQL: &str = "SELECT c.record_json, r.job_key FROM (SELECT record_json, \
30-
orchestration_run_id, ROW_NUMBER() OVER (PARTITION BY run_id, resource_type, resource_name, \
31-
record_json ->> 'warehouse_name', record_json ->> 'warehouse_size', \
32-
record_json ->> 'warehouse_type', record_json ->> 'cluster_number' \
33-
ORDER BY attempt DESC, observed_at DESC, event_id DESC) AS newest \
27+
orchestration_run_id, attempt, MAX(attempt) OVER (PARTITION BY run_id) AS newest \
3428
FROM sqb_warehouse_resource_costs WHERE location_id=$1 AND target=$2 AND run_started_at >= $3 \
3529
AND (CAST($4 AS TEXT) IS NULL OR resource_type IN ('_total', $4)) \
3630
AND (CAST($5 AS TEXT) IS NULL OR resource_type='_total' OR resource_name=$5)) c \
37-
LEFT JOIN sqb_runs r ON CAST(r.run_id AS TEXT)=c.orchestration_run_id WHERE c.newest=1";
31+
LEFT JOIN sqb_runs r ON CAST(r.run_id AS TEXT)=c.orchestration_run_id WHERE c.attempt=c.newest";
3832

3933
/// One latest observation and the job of the server run that built it, if any.
4034
pub(crate) struct LatestCost {

‎crates/sqlbuild-orchestration/src/warehouse_history/main/read_person_cost_summary.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::store::errors::ControlError;
1111
use crate::store::main::{begin::begin, commit::commit};
1212
use crate::store::models::{ControlStore, TransactionMode};
1313
use crate::warehouse_history::_helpers::costs::{
14-
Accumulator, COMPLETE_STATUS, TOTAL_RESOURCE_TYPE, latest_costs_in, ranked,
14+
Accumulator, PENDING_STATUSES, TOTAL_RESOURCE_TYPE, latest_costs_in, ranked,
1515
};
1616
use crate::warehouse_history::models::{CostGroup, CostSummaryRecord};
1717

@@ -42,7 +42,7 @@ pub async fn read_person_cost_summary(
4242
let record = &cost.record;
4343
if record.resource_type == TOTAL_RESOURCE_TYPE {
4444
total.add(cost);
45-
incomplete_runs += i64::from(record.status != COMPLETE_STATUS);
45+
incomplete_runs += i64::from(PENDING_STATUSES.contains(&record.status.as_str()));
4646
by_job.entry(cost.job_key.clone()).or_default().add(cost);
4747
continue;
4848
}

‎crates/sqlbuild-orchestration/src/warehouse_history/models.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ pub struct CostSummaryRecord {
209209
pub credits: f64,
210210
pub usd: Option<f64>,
211211
pub runs: i64,
212-
/// Runs whose cost is still pending or partial, or whose collection failed.
212+
/// Runs whose cost is still pending or partial, which a later build re-reads.
213213
pub incomplete_runs: i64,
214214
pub by_job: Vec<CostGroup>,
215215
pub by_warehouse_size: Vec<CostGroup>,

‎crates/sqlbuild-orchestration/tests/api/test_resource_costs.rs‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ fn given_refreshed_and_unpriced_costs_when_people_read_cost_then_latest_observat
2020
WarehouseMetadataTestCase {
2121
description: "SQLite cost reads",
2222
backend: Backend::Sqlite,
23-
expected_observations: 11,
23+
expected_observations: 12,
2424
},
2525
WarehouseMetadataTestCase {
2626
description: "PostgreSQL cost reads",
2727
backend: Backend::Postgres,
28-
expected_observations: 11,
28+
expected_observations: 12,
2929
},
3030
];
3131
for test_case in test_cases {
@@ -142,6 +142,18 @@ fn given_refreshed_and_unpriced_costs_when_people_read_cost_then_latest_observat
142142
);
143143
assert!((orders[0]["credits"].as_f64().expect("credits") - 0.25).abs() < 1e-9);
144144

145+
let (_, payments) = get(
146+
&client,
147+
(&api.url, &viewer),
148+
&format!("{path}/nodes/model:payments/cost?days=30"),
149+
)
150+
.await;
151+
assert_eq!(
152+
payments,
153+
json!([]),
154+
"a resource only the superseded attempt saw has no current cost"
155+
);
156+
145157
let (_, run_cost) = get(
146158
&client,
147159
(&api.url, &viewer),

‎crates/sqlbuild-python/src/bindings/_helpers/native_host/consumption_state.rs‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,30 @@ fn read_row(row: &Bound<'_, PyAny>) -> PyResult<ConsumptionRow> {
324324
})
325325
}
326326

327+
/// A nullable state value as a SQL literal of `declared_type`.
328+
fn state_literal(
329+
py: Python<'_>,
330+
value: Option<Bound<'_, PyAny>>,
331+
declared_type: &str,
332+
) -> PyResult<String> {
333+
let value: Bound<'_, PyAny> = value.unwrap_or_else(|| py.None().into_bound(py));
334+
render_state_sql_literal(py, &value, declared_type, None)
335+
}
336+
327337
pub(crate) fn literal(py: Python<'_>, value: Option<&str>) -> PyResult<String> {
328-
let value: Bound<'_, PyAny> =
329-
value.map_or_else(|| py.None().into_bound(py), |value| text(py, value));
330-
render_state_sql_literal(py, &value, STRING_STATE_VALUE_TYPE, None)
338+
state_literal(
339+
py,
340+
value.map(|value| text(py, value)),
341+
STRING_STATE_VALUE_TYPE,
342+
)
343+
}
344+
345+
pub(crate) fn integer_literal(py: Python<'_>, value: Option<i64>) -> PyResult<String> {
346+
state_literal(
347+
py,
348+
value.map(|value| PyInt::new(py, value).into_any()),
349+
INTEGER_STATE_VALUE_TYPE,
350+
)
331351
}
332352

333353
fn row_cells(py: Python<'_>, row: &ConsumptionAppend, recorded_at: &str) -> PyResult<Vec<String>> {
@@ -342,12 +362,7 @@ fn row_cells(py: Python<'_>, row: &ConsumptionAppend, recorded_at: &str) -> PyRe
342362
literal(py, Some(&row.relation.schema))?,
343363
literal(py, Some(&row.relation.name))?,
344364
literal(py, Some(&row.version_hash))?,
345-
render_state_sql_literal(
346-
py,
347-
&PyInt::new(py, row.input_count).into_any(),
348-
INTEGER_STATE_VALUE_TYPE,
349-
None,
350-
)?,
365+
integer_literal(py, Some(row.input_count))?,
351366
literal(py, input.map(|input| input.kind.as_str()))?,
352367
literal(py, input.map(|input| input.name.as_str()))?,
353368
literal(py, input.map(|input| input.version.as_str()))?,

‎crates/sqlbuild-python/src/bindings/_helpers/native_host/resource_costs_state.rs‎

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use pyo3::{pyfunction, wrap_pyfunction};
88
use sqlbuild_core::constants::RESOURCE_COSTS_TABLE_NAME;
99
use sqlbuild_core::cost_command::models::CostCoverage;
1010
use sqlbuild_core::cost_report::models::CostSummary;
11-
use sqlbuild_state::consumption::constants::INTEGER_STATE_VALUE_TYPE;
1211
use sqlbuild_state::events::main::resource_cost_observation_id::resource_cost_observation_id;
1312
use sqlbuild_state::resource_costs::constants::{
1413
NULLABLE_RESOURCE_COST_COLUMNS, RESOURCE_COST_COLUMN_TYPES, TOTAL_RESOURCE_TYPE,
@@ -19,11 +18,10 @@ use sqlbuild_state::resource_costs::models::{ResourceCostKey, RunCostAttempt};
1918
use sqlbuild_state::state_tables::main::values_insert_sql::values_insert_sql;
2019

2120
use crate::bindings::_helpers::boundary::python_calls::{call_with, imported};
22-
use crate::bindings::_helpers::contracts::state_literals::render_state_sql_literal;
2321
use crate::bindings::_helpers::execution_history::conversion::{parsed_iso, utc};
2422
use crate::bindings::_helpers::native_host::consumption_state::{
25-
Location, ROWS_PER_STATEMENT, execute, fixed_state_table_sql, literal, optional_text,
26-
state_table_name,
23+
Location, ROWS_PER_STATEMENT, execute, fixed_state_table_sql, integer_literal, literal,
24+
optional_text, state_table_name,
2725
};
2826
use crate::bindings::_helpers::records::fields::field as native_field;
2927

@@ -81,14 +79,6 @@ pub(crate) struct CostRunIdentity<'a> {
8179
pub(crate) completed_at: &'a str,
8280
}
8381

84-
fn integer(py: Python<'_>, value: Option<i64>) -> PyResult<String> {
85-
let value: Bound<'_, PyAny> = value.map_or_else(
86-
|| py.None().into_bound(py),
87-
|value| PyInt::new(py, value).into_any(),
88-
);
89-
render_state_sql_literal(py, &value, INTEGER_STATE_VALUE_TYPE, None)
90-
}
91-
9282
fn table(adapter: &Bound<'_, PyAny>, location: &Location) -> PyResult<Option<String>> {
9383
let keywords = location.keywords(adapter.py());
9484
state_table_name(
@@ -167,21 +157,21 @@ fn observation_cells(
167157
literal(py, Some(&observation_id))?,
168158
literal(py, Some(run.run_id))?,
169159
literal(py, run.orchestration_run_id)?,
170-
integer(py, Some(attempt))?,
160+
integer_literal(py, Some(attempt))?,
171161
literal(py, Some(row.resource_type))?,
172162
literal(py, Some(row.resource_name))?,
173163
literal(py, Some(row.warehouse_name))?,
174164
literal(py, Some(row.warehouse_size))?,
175165
literal(py, row.warehouse_type)?,
176-
integer(py, row.cluster_number)?,
177-
integer(py, Some(row.query_count))?,
166+
integer_literal(py, row.cluster_number)?,
167+
integer_literal(py, Some(row.query_count))?,
178168
literal(py, Some(row.attributed_seconds))?,
179-
integer(py, Some(row.bytes_scanned))?,
169+
integer_literal(py, Some(row.bytes_scanned))?,
180170
literal(py, Some(row.estimated_credits))?,
181171
literal(py, row.estimated_usd.as_deref())?,
182172
literal(py, Some(&summary.status))?,
183-
integer(py, Some(summary.expected_statement_count))?,
184-
integer(py, Some(summary.observed_statement_count))?,
173+
integer_literal(py, Some(summary.expected_statement_count))?,
174+
integer_literal(py, Some(summary.observed_statement_count))?,
185175
literal(py, Some(&limitations))?,
186176
literal(py, run.target_name)?,
187177
literal(py, Some(run.started_at))?,

‎fensu.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1853,7 +1853,7 @@ paths = ["src/sqlbuild/adapter/contract/classes/*", "src/sqlbuild/adapters/*/cla
18531853
reason = "tests/unit/src/sqlbuild/adapter/contract/classes/strict_adapter/test_strict_adapter.py requires every first-class adapter to define each StrictAdapter method itself instead of inheriting it from BaseAdapter."
18541854

18551855
[[dupes.allowlist]]
1856-
paths = ["crates/sqlbuild-python/src/bindings/_helpers/native_host/audit_results_state.rs", "crates/sqlbuild-python/src/bindings/_helpers/native_host/consumption_state.rs", "crates/sqlbuild-python/src/bindings/_helpers/native_host/observations_state.rs"]
1856+
paths = ["crates/sqlbuild-python/src/bindings/_helpers/native_host/audit_results_state.rs", "crates/sqlbuild-python/src/bindings/_helpers/native_host/consumption_state.rs", "crates/sqlbuild-python/src/bindings/_helpers/native_host/observations_state.rs", "crates/sqlbuild-python/src/bindings/_helpers/native_host/resource_costs_state.rs"]
18571857
reason = "Each state table has its own Python-facing create and prune bindings. The DDL and table naming live in the shared fixed_state_table_sql and state_table_name; each prune keeps its own retention rule."
18581858

18591859
[[dupes.allowlist]]

‎src/sqlbuild/.agents/skills/sqlbuild/references/docs/concepts/adapters/snowflake.md‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,23 @@ contract rate. Use `sqb cost`, `sqb cost latest`, `sqb cost <run_id>`, or
193193
provide a versioned, decimal-safe output contract. Pending detail records are refreshed from
194194
Snowflake when inspected again.
195195

196+
Under [`sqb server`](../../cli/server.md), the UI labels the same figures "estimated query cost":
197+
198+
| Page | Shows |
199+
| --- | --- |
200+
| Run | The run's total, then each model and each warehouse, most expensive first |
201+
| Model, Runs tab | The model's cost and query time in each build over the last 30 days |
202+
| Overview | The last 7 or 30 days by job, folder and warehouse size |
203+
204+
Figures are in dollars when `usd_per_credit` is set, and in credits with the default rate. A run
205+
whose cost isn't final shows "Cost pending" or "Partial", never $0.
206+
196207
The estimate is read from `INFORMATION_SCHEMA.QUERY_HISTORY` when the run finishes. If it can't be
197208
read within 30 seconds, the build is unaffected and its cost is recorded as `pending`;
198209
`sqb cost <run_id>` collects it later. Any other failure is recorded as `collection_failed` with the
199-
Snowflake error.
210+
Snowflake error. Each later build in the target also re-reads up to three pending, partial or failed
211+
runs from the last six days while its warehouse is still running, so the server's figures complete
212+
without resuming a suspended warehouse. A failed collection is tried at most three times in all.
200213

201214
The estimate is attributed compute, not your Snowflake bill. It sees only query history visible to
202215
the executing role, and leaves out warehouse resume and idle time, the 60-second minimum,

0 commit comments

Comments
 (0)