|
| 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 | +} |
0 commit comments