-
Notifications
You must be signed in to change notification settings - Fork 870
feat(query): add billing_usage_daily table function #19784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
smallfish
wants to merge
6
commits into
databendlabs:main
Choose a base branch
from
smallfish:feat/add-billing-history-table-functions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b89528c
feat(query): add billing usage daily table function
smallfish 4e8752d
update: tags/details with jsonb, add permission check
smallfish e855d46
refactor args with start/end date (remove month)
smallfish 1fda1e5
updated with review
smallfish d9315f1
fix ci check
smallfish 2a56f86
Merge branch 'main' of github.com:databendlabs/databend into feat/add…
smallfish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| syntax = "proto3"; | ||
|
|
||
| package billingproto; | ||
|
|
||
| message BillingError { | ||
| string kind = 1; | ||
| string message = 2; | ||
| int32 code = 3; | ||
| } | ||
|
|
||
| message GetBillingUsageDailyRequest { | ||
| string tenant_id = 1; | ||
| string start_date = 2; // YYYY-MM-DD | ||
| string end_date = 3; // YYYY-MM-DD, empty means start_date | ||
| string sql_user = 4; // audit only, empty means absent | ||
| string query_id = 5; // audit only, empty means absent | ||
| } | ||
|
|
||
| message BillingUsageDailyRow { | ||
| // Billing date in YYYY-MM-DD format. | ||
| string usage_date = 1; | ||
|
|
||
| // Top-level billing category, aligned with Snowflake naming where practical. | ||
| // Examples: compute, storage, cloud services. | ||
| string usage_type = 2; | ||
|
|
||
| // More specific service category. | ||
| // Examples: WAREHOUSE_METERING, STORAGE, CLOUD_SERVICES. | ||
| string service_type = 3; | ||
|
|
||
| // Logical billable resource name when applicable, such as warehouse name. | ||
| // Empty string means absent. | ||
| string resource_name = 4; | ||
|
|
||
| // Billable usage quantity encoded as a decimal string to avoid float precision loss. | ||
| string usage = 5; | ||
|
|
||
| // Unit for usage. | ||
| // Examples: second, byte, request. | ||
| string usage_unit = 6; | ||
|
|
||
| // Reserved billing unit price field. | ||
| // Empty string means the rate is intentionally undisclosed. | ||
| string rate = 7; | ||
|
|
||
| // Billing dimension unit for the reserved rate field. | ||
| // Examples: second, tb_day, k_request. | ||
| // Returned when the billing dimension is known. | ||
| string rate_unit = 8; | ||
|
|
||
| // Final billed amount encoded as a decimal string. | ||
| string usage_in_currency = 9; | ||
|
|
||
| // Settlement currency, for example USD. | ||
| string currency = 10; | ||
|
|
||
| // Resource tags when the underlying usage is associated with a tagged resource. | ||
| map<string, string> tags = 11; | ||
|
|
||
| // JSON object string for category-specific or future-compatible extension fields. | ||
| // Examples: | ||
| // {"cluster_name":"cl-00000","max_clusters":1,"size":"XSmall"} | ||
| // {} | ||
| string details = 12; | ||
| } | ||
|
|
||
| message GetBillingUsageDailyResponse { | ||
| repeated BillingUsageDailyRow rows = 1; | ||
| optional BillingError error = 2; | ||
| } | ||
|
|
||
| service BillingService { | ||
| rpc GetBillingUsageDaily(GetBillingUsageDailyRequest) returns (GetBillingUsageDailyResponse); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // Copyright 2021 Datafuse Labs | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| use std::sync::Arc; | ||
|
|
||
| use databend_common_exception::Result; | ||
| use tonic::Request; | ||
| use tonic::transport::Channel; | ||
|
|
||
| use crate::pb::GetBillingUsageDailyRequest; | ||
| use crate::pb::GetBillingUsageDailyResponse; | ||
| use crate::pb::billing_service_client::BillingServiceClient; | ||
| use crate::task_client::MAX_DECODING_SIZE; | ||
| use crate::task_client::MAX_ENCODING_SIZE; | ||
|
|
||
| pub struct BillingClient { | ||
| pub client: BillingServiceClient<Channel>, | ||
| } | ||
|
|
||
| impl BillingClient { | ||
| pub async fn new(channel: Channel) -> Result<Arc<BillingClient>> { | ||
| let client = BillingServiceClient::new(channel) | ||
| .max_decoding_message_size(MAX_DECODING_SIZE) | ||
| .max_encoding_message_size(MAX_ENCODING_SIZE); | ||
| Ok(Arc::new(BillingClient { client })) | ||
| } | ||
|
|
||
| pub async fn get_billing_usage_daily( | ||
| &self, | ||
| req: Request<GetBillingUsageDailyRequest>, | ||
| ) -> Result<GetBillingUsageDailyResponse> { | ||
| let mut client = self.client.clone(); | ||
| let resp = client.get_billing_usage_daily(req).await?; | ||
| Ok(resp.into_inner()) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| // Copyright 2022 Datafuse Labs. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| use std::collections::BTreeMap; | ||
|
|
||
| use databend_common_base::runtime; | ||
| use databend_common_cloud_control::billing_client::BillingClient; | ||
| use databend_common_cloud_control::pb::BillingUsageDailyRow; | ||
| use databend_common_cloud_control::pb::GetBillingUsageDailyRequest; | ||
| use databend_common_cloud_control::pb::GetBillingUsageDailyResponse; | ||
| use databend_common_cloud_control::pb::billing_service_server::BillingService; | ||
| use databend_common_cloud_control::pb::billing_service_server::BillingServiceServer; | ||
| use hyper_util::rt::TokioIo; | ||
| use tonic::Request; | ||
| use tonic::Response; | ||
| use tonic::Status; | ||
| use tonic::codegen::tokio_stream; | ||
| use tonic::transport::Endpoint; | ||
| use tonic::transport::Server; | ||
| use tonic::transport::Uri; | ||
| use tower::service_fn; | ||
|
|
||
| #[derive(Default)] | ||
| pub struct MockBillingService {} | ||
|
|
||
| #[tonic::async_trait] | ||
| impl BillingService for MockBillingService { | ||
| async fn get_billing_usage_daily( | ||
| &self, | ||
| request: Request<GetBillingUsageDailyRequest>, | ||
| ) -> std::result::Result<Response<GetBillingUsageDailyResponse>, Status> { | ||
| Ok(Response::new(GetBillingUsageDailyResponse { | ||
| rows: vec![BillingUsageDailyRow { | ||
| usage_date: request.into_inner().start_date, | ||
| usage_type: "compute".to_string(), | ||
| service_type: "WAREHOUSE_METERING".to_string(), | ||
| resource_name: "default".to_string(), | ||
| usage: "2653".to_string(), | ||
| usage_unit: "second".to_string(), | ||
| rate: "".to_string(), | ||
| rate_unit: "second".to_string(), | ||
| usage_in_currency: "0.737".to_string(), | ||
| currency: "USD".to_string(), | ||
| tags: BTreeMap::from([("env".to_string(), "test".to_string())]), | ||
| details: "{\"cluster_name\":\"cl-00000\"}".to_string(), | ||
| }], | ||
| error: None, | ||
| })) | ||
| } | ||
| } | ||
|
|
||
| #[tokio::test(flavor = "current_thread")] | ||
| async fn test_billing_client_success_cases() -> anyhow::Result<()> { | ||
| let (client, server) = tokio::io::duplex(1024); | ||
| let client = TokioIo::new(client); | ||
|
|
||
| runtime::spawn(async move { | ||
| Server::builder() | ||
| .add_service(BillingServiceServer::new(MockBillingService::default())) | ||
| .serve_with_incoming(tokio_stream::iter(vec![Ok::<_, std::io::Error>(server)])) | ||
| .await | ||
| }); | ||
|
|
||
| let mut client_io = Some(client); | ||
| let channel = Endpoint::try_from("http://[::]:0") | ||
| .unwrap() | ||
| .connect_with_connector(service_fn(move |_: Uri| { | ||
| let client = client_io.take(); | ||
|
|
||
| async move { | ||
| if let Some(client) = client { | ||
| Ok(client) | ||
| } else { | ||
| Err(std::io::Error::other("Client already taken")) | ||
| } | ||
| } | ||
| })) | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| let client = BillingClient::new(channel).await?; | ||
|
|
||
| let resp = client | ||
| .get_billing_usage_daily(Request::new(GetBillingUsageDailyRequest { | ||
| tenant_id: "tenant".to_string(), | ||
| start_date: "2026-03-01".to_string(), | ||
| end_date: "2026-03-31".to_string(), | ||
| sql_user: "root".to_string(), | ||
| query_id: "query-1".to_string(), | ||
| })) | ||
| .await?; | ||
| assert_eq!(resp.rows.len(), 1); | ||
| assert_eq!(resp.rows[0].usage_date, "2026-03-01"); | ||
| assert_eq!(resp.rows[0].usage_type, "compute"); | ||
| assert_eq!(resp.rows[0].resource_name, "default"); | ||
|
|
||
| Ok(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.