Skip to content

Commit 0d15353

Browse files
committed
update with new rpc defined
1 parent 1db02d8 commit 0d15353

10 files changed

Lines changed: 488 additions & 1004 deletions

File tree

src/common/cloud_control/proto/billing.proto

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,67 +8,65 @@ message BillingError {
88
int32 code = 3;
99
}
1010

11-
message GetBillingHistoryDailyRequest {
11+
message GetBillingUsageDailyRequest {
1212
string tenant_id = 1;
1313
string billing_month = 2; // YYYY-MM
1414
string sql_user = 3; // audit only, empty means absent
1515
string query_id = 4; // audit only, empty means absent
1616
}
1717

18-
message BillingHistoryDailyStorageUsage {
19-
string total_bytes = 1;
20-
string day_price_per_tb = 2;
21-
uint64 read_requests = 3;
22-
string kilo_read_request_price = 4;
23-
uint64 write_requests = 5;
24-
string kilo_write_request_price = 6;
25-
}
18+
message BillingUsageDailyRow {
19+
// Billing date in YYYY-MM-DD format.
20+
string usage_date = 1;
2621

27-
message BillingHistoryDailyCloudServiceUsage {
28-
string api_requests = 1;
29-
string kilo_api_requests_price = 2;
30-
}
22+
// Top-level billing category, aligned with Snowflake naming where practical.
23+
// Examples: compute, storage, cloud services.
24+
string usage_type = 2;
3125

32-
message BillingHistoryDailyRow {
33-
string date = 1;
34-
string total_amount = 2;
35-
string warehouse_amount = 3;
36-
string storage_amount = 4;
37-
string cloud_service_amount = 5;
38-
BillingHistoryDailyStorageUsage storage_usage = 6;
39-
BillingHistoryDailyCloudServiceUsage cloud_service_usage = 7;
40-
}
26+
// More specific service category.
27+
// Examples: WAREHOUSE_METERING, STORAGE, CLOUD_SERVICES.
28+
string service_type = 3;
4129

42-
message GetBillingHistoryDailyResponse {
43-
repeated BillingHistoryDailyRow rows = 1;
44-
optional BillingError error = 2;
45-
}
30+
// Logical billable resource name when applicable, such as warehouse name.
31+
// Empty string means absent.
32+
string resource_name = 4;
4633

47-
message GetBillingHistoryWarehouseDailyRequest {
48-
string tenant_id = 1;
49-
string billing_month = 2; // YYYY-MM
50-
string sql_user = 3; // audit only, empty means absent
51-
string query_id = 4; // audit only, empty means absent
52-
}
34+
// Billable usage quantity encoded as a decimal string to avoid float precision loss.
35+
string usage = 5;
36+
37+
// Unit for usage.
38+
// Examples: second, byte, request.
39+
string usage_unit = 6;
40+
41+
// Unit price encoded as a decimal string. Empty string means absent.
42+
string rate = 7;
43+
44+
// Unit for rate.
45+
// Examples: second, tb_day, k_request.
46+
// Empty string means absent.
47+
string rate_unit = 8;
48+
49+
// Final billed amount encoded as a decimal string.
50+
string usage_in_currency = 9;
51+
52+
// Settlement currency, for example USD.
53+
string currency = 10;
54+
55+
// Resource tags when the underlying usage is associated with a tagged resource.
56+
map<string, string> tags = 11;
5357

54-
message BillingHistoryWarehouseDailyRow {
55-
string date = 1;
56-
string warehouse_name = 2;
57-
string cluster_name = 3;
58-
uint64 max_clusters = 4;
59-
string size = 5;
60-
uint64 seconds = 6;
61-
string price_per_second = 7;
62-
string credits = 8;
63-
map<string, string> tags = 9;
58+
// JSON object string for category-specific or future-compatible extension fields.
59+
// Examples:
60+
// {"cluster_name":"cl-00000","max_clusters":1,"size":"XSmall"}
61+
// {}
62+
string details = 12;
6463
}
6564

66-
message GetBillingHistoryWarehouseDailyResponse {
67-
repeated BillingHistoryWarehouseDailyRow rows = 1;
65+
message GetBillingUsageDailyResponse {
66+
repeated BillingUsageDailyRow rows = 1;
6867
optional BillingError error = 2;
6968
}
7069

7170
service BillingService {
72-
rpc GetBillingHistoryDaily(GetBillingHistoryDailyRequest) returns (GetBillingHistoryDailyResponse);
73-
rpc GetBillingHistoryWarehouseDaily(GetBillingHistoryWarehouseDailyRequest) returns (GetBillingHistoryWarehouseDailyResponse);
71+
rpc GetBillingUsageDaily(GetBillingUsageDailyRequest) returns (GetBillingUsageDailyResponse);
7472
}

src/common/cloud_control/src/billing_client.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ use databend_common_exception::Result;
1818
use tonic::Request;
1919
use tonic::transport::Channel;
2020

21-
use crate::pb::GetBillingHistoryDailyRequest;
22-
use crate::pb::GetBillingHistoryDailyResponse;
23-
use crate::pb::GetBillingHistoryWarehouseDailyRequest;
24-
use crate::pb::GetBillingHistoryWarehouseDailyResponse;
21+
use crate::pb::GetBillingUsageDailyRequest;
22+
use crate::pb::GetBillingUsageDailyResponse;
2523
use crate::pb::billing_service_client::BillingServiceClient;
2624
use crate::task_client::MAX_DECODING_SIZE;
2725
use crate::task_client::MAX_ENCODING_SIZE;
@@ -38,21 +36,12 @@ impl BillingClient {
3836
Ok(Arc::new(BillingClient { client }))
3937
}
4038

41-
pub async fn get_billing_history_daily(
39+
pub async fn get_billing_usage_daily(
4240
&self,
43-
req: Request<GetBillingHistoryDailyRequest>,
44-
) -> Result<GetBillingHistoryDailyResponse> {
41+
req: Request<GetBillingUsageDailyRequest>,
42+
) -> Result<GetBillingUsageDailyResponse> {
4543
let mut client = self.client.clone();
46-
let resp = client.get_billing_history_daily(req).await?;
47-
Ok(resp.into_inner())
48-
}
49-
50-
pub async fn get_billing_history_warehouse_daily(
51-
&self,
52-
req: Request<GetBillingHistoryWarehouseDailyRequest>,
53-
) -> Result<GetBillingHistoryWarehouseDailyResponse> {
54-
let mut client = self.client.clone();
55-
let resp = client.get_billing_history_warehouse_daily(req).await?;
44+
let resp = client.get_billing_usage_daily(req).await?;
5645
Ok(resp.into_inner())
5746
}
5847
}

src/common/cloud_control/tests/it/billing_client.rs

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use std::collections::BTreeMap;
16+
1517
use databend_common_base::runtime;
1618
use databend_common_cloud_control::billing_client::BillingClient;
17-
use databend_common_cloud_control::pb::BillingHistoryDailyRow;
18-
use databend_common_cloud_control::pb::BillingHistoryWarehouseDailyRow;
19-
use databend_common_cloud_control::pb::GetBillingHistoryDailyRequest;
20-
use databend_common_cloud_control::pb::GetBillingHistoryDailyResponse;
21-
use databend_common_cloud_control::pb::GetBillingHistoryWarehouseDailyRequest;
22-
use databend_common_cloud_control::pb::GetBillingHistoryWarehouseDailyResponse;
19+
use databend_common_cloud_control::pb::BillingUsageDailyRow;
20+
use databend_common_cloud_control::pb::GetBillingUsageDailyRequest;
21+
use databend_common_cloud_control::pb::GetBillingUsageDailyResponse;
2322
use databend_common_cloud_control::pb::billing_service_server::BillingService;
2423
use databend_common_cloud_control::pb::billing_service_server::BillingServiceServer;
2524
use hyper_util::rt::TokioIo;
@@ -37,27 +36,24 @@ pub struct MockBillingService {}
3736

3837
#[tonic::async_trait]
3938
impl BillingService for MockBillingService {
40-
async fn get_billing_history_daily(
41-
&self,
42-
request: Request<GetBillingHistoryDailyRequest>,
43-
) -> std::result::Result<Response<GetBillingHistoryDailyResponse>, Status> {
44-
Ok(Response::new(GetBillingHistoryDailyResponse {
45-
rows: vec![BillingHistoryDailyRow {
46-
date: request.into_inner().billing_month,
47-
..Default::default()
48-
}],
49-
error: None,
50-
}))
51-
}
52-
53-
async fn get_billing_history_warehouse_daily(
39+
async fn get_billing_usage_daily(
5440
&self,
55-
request: Request<GetBillingHistoryWarehouseDailyRequest>,
56-
) -> std::result::Result<Response<GetBillingHistoryWarehouseDailyResponse>, Status> {
57-
Ok(Response::new(GetBillingHistoryWarehouseDailyResponse {
58-
rows: vec![BillingHistoryWarehouseDailyRow {
59-
warehouse_name: request.into_inner().billing_month,
60-
..Default::default()
41+
request: Request<GetBillingUsageDailyRequest>,
42+
) -> std::result::Result<Response<GetBillingUsageDailyResponse>, Status> {
43+
Ok(Response::new(GetBillingUsageDailyResponse {
44+
rows: vec![BillingUsageDailyRow {
45+
usage_date: request.into_inner().billing_month,
46+
usage_type: "compute".to_string(),
47+
service_type: "WAREHOUSE_METERING".to_string(),
48+
resource_name: "default".to_string(),
49+
usage: "2653".to_string(),
50+
usage_unit: "second".to_string(),
51+
rate: "0.0002777777777778".to_string(),
52+
rate_unit: "second".to_string(),
53+
usage_in_currency: "0.737".to_string(),
54+
currency: "USD".to_string(),
55+
tags: BTreeMap::from([("env".to_string(), "test".to_string())]),
56+
details: "{\"cluster_name\":\"cl-00000\"}".to_string(),
6157
}],
6258
error: None,
6359
}))
@@ -95,27 +91,18 @@ async fn test_billing_client_success_cases() -> anyhow::Result<()> {
9591

9692
let client = BillingClient::new(channel).await?;
9793

98-
let daily_resp = client
99-
.get_billing_history_daily(Request::new(GetBillingHistoryDailyRequest {
94+
let resp = client
95+
.get_billing_usage_daily(Request::new(GetBillingUsageDailyRequest {
10096
tenant_id: "tenant".to_string(),
10197
billing_month: "2026-03".to_string(),
10298
sql_user: "root".to_string(),
10399
query_id: "query-1".to_string(),
104100
}))
105101
.await?;
106-
assert_eq!(daily_resp.rows.len(), 1);
107-
assert_eq!(daily_resp.rows[0].date, "2026-03");
108-
109-
let warehouse_resp = client
110-
.get_billing_history_warehouse_daily(Request::new(GetBillingHistoryWarehouseDailyRequest {
111-
tenant_id: "tenant".to_string(),
112-
billing_month: "2026-04".to_string(),
113-
sql_user: "root".to_string(),
114-
query_id: "query-2".to_string(),
115-
}))
116-
.await?;
117-
assert_eq!(warehouse_resp.rows.len(), 1);
118-
assert_eq!(warehouse_resp.rows[0].warehouse_name, "2026-04");
102+
assert_eq!(resp.rows.len(), 1);
103+
assert_eq!(resp.rows[0].usage_date, "2026-03");
104+
assert_eq!(resp.rows[0].usage_type, "compute");
105+
assert_eq!(resp.rows[0].resource_name, "default");
119106

120107
Ok(())
121108
}

src/query/service/src/table_functions/table_function_factory.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ use databend_common_storages_iceberg::IcebergInspectTable;
3939
use databend_common_storages_stream::stream_status_table_func::StreamStatusTable;
4040
use databend_meta_client::types::MetaId;
4141
#[cfg(feature = "task-support")]
42-
use databend_query_task_support::table_functions::BillingHistoryDailyTable;
43-
#[cfg(feature = "task-support")]
44-
use databend_query_task_support::table_functions::BillingHistoryWarehouseDailyTable;
42+
use databend_query_task_support::table_functions::BillingUsageDailyTable;
4543
#[cfg(feature = "task-support")]
4644
use databend_query_task_support::table_functions::TaskDependentsEnableTable;
4745
#[cfg(feature = "task-support")]
@@ -350,16 +348,8 @@ impl TableFunctionFactory {
350348
);
351349

352350
creators.insert(
353-
"billing_history_daily".to_string(),
354-
(next_id(), Arc::new(BillingHistoryDailyTable::create)),
355-
);
356-
357-
creators.insert(
358-
"billing_history_warehouse_daily".to_string(),
359-
(
360-
next_id(),
361-
Arc::new(BillingHistoryWarehouseDailyTable::create),
362-
),
351+
"billing_usage_daily".to_string(),
352+
(next_id(), Arc::new(BillingUsageDailyTable::create)),
363353
);
364354
}
365355

0 commit comments

Comments
 (0)