Skip to content

Commit 675e0f5

Browse files
authored
feat(query): add catalog stats api (#17602)
* feat(query): add catalog stats api * z * z * z * z
1 parent fdb4b7d commit 675e0f5

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

src/query/service/src/servers/http/v1/catalog/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ pub mod list_database_tables;
1818
pub mod list_databases;
1919
pub mod search_databases;
2020
pub mod search_tables;
21+
pub mod stats;
2122

2223
pub use get_database_table::get_database_table_handler;
2324
pub use list_database_table_fields::list_database_table_fields_handler;
2425
pub use list_database_tables::list_database_tables_handler;
2526
pub use list_databases::list_databases_handler;
2627
pub use search_databases::search_databases_handler;
2728
pub use search_tables::search_tables_handler;
29+
pub use stats::catalog_stats_handler;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2021 Datafuse Labs
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use databend_common_catalog::catalog::CatalogManager;
16+
use databend_common_exception::Result;
17+
use poem::error::InternalServerError;
18+
use poem::error::Result as PoemResult;
19+
use poem::web::Json;
20+
use poem::IntoResponse;
21+
use serde::Deserialize;
22+
use serde::Serialize;
23+
24+
use crate::servers::http::v1::HttpQueryContext;
25+
26+
#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Default)]
27+
pub struct CatalogStatsResponse {
28+
pub tables: u64,
29+
pub databases: u64,
30+
}
31+
32+
#[async_backtrace::framed]
33+
async fn handle(ctx: &HttpQueryContext) -> Result<CatalogStatsResponse> {
34+
let tenant = ctx.session.get_current_tenant();
35+
let visibility_checker = ctx.session.get_visibility_checker(false).await?;
36+
37+
let catalog = CatalogManager::instance().get_default_catalog(Default::default())?;
38+
39+
let mut tables: u64 = 0;
40+
let mut databases: u64 = 0;
41+
42+
for db in catalog.list_databases(&tenant).await? {
43+
if !visibility_checker.check_database_visibility(
44+
catalog.name().as_str(),
45+
db.name(),
46+
db.get_db_info().database_id.db_id,
47+
) {
48+
continue;
49+
}
50+
databases += 1;
51+
for table in db.list_tables().await? {
52+
if !visibility_checker.check_table_visibility(
53+
catalog.name().as_str(),
54+
db.name(),
55+
table.name(),
56+
db.get_db_info().database_id.db_id,
57+
table.get_table_info().ident.table_id,
58+
) {
59+
continue;
60+
}
61+
tables += 1;
62+
}
63+
}
64+
65+
Ok(CatalogStatsResponse { tables, databases })
66+
}
67+
68+
#[poem::handler]
69+
#[async_backtrace::framed]
70+
pub async fn catalog_stats_handler(ctx: &HttpQueryContext) -> PoemResult<impl IntoResponse> {
71+
let resp = handle(ctx).await.map_err(InternalServerError)?;
72+
Ok(Json(resp))
73+
}

src/query/service/src/servers/http/v1/http_query_handlers.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use crate::servers::http::error::QueryError;
5050
use crate::servers::http::middleware::EndpointKind;
5151
use crate::servers::http::middleware::HTTPSessionMiddleware;
5252
use crate::servers::http::middleware::MetricsMiddleware;
53+
use crate::servers::http::v1::catalog::catalog_stats_handler;
5354
use crate::servers::http::v1::catalog::get_database_table_handler;
5455
use crate::servers::http::v1::catalog::list_database_table_fields_handler;
5556
use crate::servers::http::v1::catalog::list_database_tables_handler;
@@ -515,6 +516,11 @@ pub fn query_route() -> Route {
515516
post(search_databases_handler),
516517
EndpointKind::Catalog,
517518
),
519+
(
520+
"/catalog/stats",
521+
get(catalog_stats_handler),
522+
EndpointKind::Catalog,
523+
),
518524
(
519525
"/users",
520526
get(list_users_handler).post(create_user_handler),

src/query/service/tests/it/servers/http/http_query_handlers.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,13 @@ async fn test_catalog_apis() -> Result<()> {
870870
assert_eq!(body.fields.len(), 1);
871871
assert_eq!(body.fields[0].name, "a");
872872

873+
let response = get_uri(&ep, "/v1/catalog/stats").await;
874+
assert_eq!(response.status(), StatusCode::OK);
875+
let body = response.into_body().into_string().await.unwrap();
876+
let body: catalog::stats::CatalogStatsResponse = serde_json::from_str(&body).unwrap();
877+
assert_eq!(body.tables, 1);
878+
assert_eq!(body.databases, 3);
879+
873880
Ok(())
874881
}
875882

0 commit comments

Comments
 (0)