Skip to content

Commit

Permalink
Add stats command
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkAndshark committed Nov 3, 2023
1 parent 73b56e8 commit 3aed2fd
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 1 deletion.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions mbtiles/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ enum Commands {
/// MBTiles file to read from
file: PathBuf,
},
/// Gets tile statistics from MBTiels file
#[command(name = "stats")]
Stats { file: PathBuf },
/// Gets a single value from the MBTiles metadata table.
#[command(name = "meta-get")]
MetaGetValue {
Expand Down Expand Up @@ -114,6 +117,12 @@ async fn main_int() -> anyhow::Result<()> {
let mbt = Mbtiles::new(file.as_path())?;
mbt.validate(integrity_check, update_agg_tiles_hash).await?;
}
Commands::Stats { file } => {
let mbt = Mbtiles::new(file.as_path())?;
let mut conn = mbt.open_readonly().await?;

mbt.statistics(&mut conn).await?;
}
}

Ok(())
Expand Down
52 changes: 51 additions & 1 deletion mbtiles/src/mbtiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ pub struct Metadata {
pub json: Option<JSONValue>,
}

#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct Tileinfo {
pub zoom: u8,
pub count: u32,
pub smallest: u32,
pub largest: u32,
pub average: u32,
pub bounding_box: [u32; 4],
}

#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct Statistics {
pub file: String,
// the schema
pub structure: String,
pub page_size: u64,
pub tile_infos: Vec<Tileinfo>,
}

#[allow(clippy::trivially_copy_pass_by_ref)]
fn serialize_ti<S>(ti: &TileInfo, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down Expand Up @@ -223,7 +242,38 @@ impl Mbtiles {
self.check_agg_tiles_hashes(&mut conn).await
}
}

pub async fn statistics<T>(&self, conn: &mut T) -> MbtResult<Option<Statistics>>
where
for<'e> &'e mut T: SqliteExecutor<'e>,
{
// let page_count_query = query!("PRAGMA page_count;");
let page_size_query = query!("PRAGMA page_size;");

let tile_infos_query = query!(
r#"SELECT
zoom_level AS zoom,
count( ) AS count,
min( length( tile_data ) ) / 1024.0 AS smallest,
max( length( tile_data ) ) / 1024.0 AS largest,
avg( length( tile_data ) ) / 1024.0 AS average,
min(tile_column) as min_tile_x,
min(tile_row) as min_tile_y,
max(tile_column) as max_tile_x,
max(tile_row) as max_tile_y
FROM tiles
GROUP BY zoom_level"#
);
let file_name = &self.filename;
let page_size = page_size_query.fetch_one(&mut *conn).await?.page_size;
let mb_type = self.detect_type(&mut *conn).await?;
let tile_infos_rows = tile_infos_query.fetch_all(&mut *conn).await?;
Ok(Some(Statistics {
file: "test".to_string(),
structure: "test".to_string(),
page_size: 22,
tile_infos: Vec::new(),
}))
}
/// Get the aggregate tiles hash value from the metadata table
pub async fn get_agg_tiles_hash<T>(&self, conn: &mut T) -> MbtResult<Option<String>>
where
Expand Down

0 comments on commit 3aed2fd

Please sign in to comment.