Skip to content
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

Country search and rename binary as popgetter #63

Merged
merged 5 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ edition = "2021"
name = "popgetter"
path = "src/lib.rs"

[[bin]]
name = "popgetter"
path = "src/main.rs"

[dependencies]
anyhow = "1.0.75"
serde = { version = "1.0", features = ["derive"] }
Expand Down
4 changes: 2 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,13 @@ impl RunCommand for MetricsCommand {
println!("Found {} metrics.", len_requests);

if len_requests > 50 && !self.full {
display_search_results(search_results, Some(50));
display_search_results(search_results, Some(50))?;
println!(
"{} more results not shown. Use --full to show all results.",
len_requests - 50
);
} else {
display_search_results(search_results, None);
display_search_results(search_results, None)?;
}
Ok(())
}
Expand Down
57 changes: 45 additions & 12 deletions src/display.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
use comfy_table::{presets::NOTHING, *};
use itertools::izip;

use popgetter::{search::SearchResults, COL};

pub fn display_search_results(results: SearchResults, max_results: Option<usize>) {
pub fn display_search_results(
results: SearchResults,
max_results: Option<usize>,
) -> anyhow::Result<()> {
let df_to_show = match max_results {
Some(max) => results.0.head(Some(max)),
None => results.0,
};

for (metric_id, hrn, desc, hxl, level) in izip!(
df_to_show.column(COL::METRIC_ID).unwrap().iter(),
for (metric_id, hrn, desc, hxl, date, country, level, download_url) in izip!(
df_to_show.column(COL::METRIC_ID)?.str()?,
df_to_show.column(COL::METRIC_HUMAN_READABLE_NAME)?.str()?,
df_to_show.column(COL::METRIC_DESCRIPTION)?.str()?,
df_to_show.column(COL::METRIC_HXL_TAG)?.str()?,
df_to_show
.column(COL::METRIC_HUMAN_READABLE_NAME)
.unwrap()
.column(COL::SOURCE_DATA_RELEASE_COLLECTION_PERIOD_START)?
.rechunk()
.iter(),
df_to_show.column(COL::METRIC_DESCRIPTION).unwrap().iter(),
df_to_show.column(COL::METRIC_HXL_TAG).unwrap().iter(),
df_to_show.column(COL::GEOMETRY_LEVEL).unwrap().iter(),
df_to_show.column(COL::COUNTRY_NAME_SHORT_EN)?.str()?,
// Note: if using iter on an AnyValue, need to rechunk first.
df_to_show.column(COL::GEOMETRY_LEVEL)?.rechunk().iter(),
df_to_show
.column(COL::METRIC_SOURCE_DOWNLOAD_URL)?
.rechunk()
.iter()
) {
let mut table = Table::new();
table
Expand All @@ -28,28 +39,50 @@ pub fn display_search_results(results: SearchResults, max_results: Option<usize>
.set_style(comfy_table::TableComponent::TopBorderIntersections, '─')
.add_row(vec![
Cell::new("Metric ID").add_attribute(Attribute::Bold),
metric_id.get_str().unwrap().into(),
metric_id.unwrap().into(),
])
.add_row(vec![
Cell::new("Metric ID (short)").add_attribute(Attribute::Bold),
metric_id
.unwrap()
.chars()
.take(8)
.collect::<String>()
.into(),
])
.add_row(vec![
Cell::new("Human readable name").add_attribute(Attribute::Bold),
hrn.get_str().unwrap().into(),
hrn.unwrap().into(),
])
.add_row(vec![
Cell::new("Description").add_attribute(Attribute::Bold),
desc.get_str().unwrap().into(),
desc.unwrap().into(),
])
.add_row(vec![
Cell::new("HXL tag").add_attribute(Attribute::Bold),
hxl.get_str().unwrap().into(),
hxl.unwrap().into(),
])
.add_row(vec![
Cell::new("Collection date").add_attribute(Attribute::Bold),
format!("{date}").into(),
])
.add_row(vec![
Cell::new("Country").add_attribute(Attribute::Bold),
country.unwrap().into(),
])
.add_row(vec![
Cell::new("Geometry level").add_attribute(Attribute::Bold),
level.get_str().unwrap().into(),
])
.add_row(vec![
Cell::new("Source download URL").add_attribute(Attribute::Bold),
download_url.get_str().unwrap().into(),
]);

let column = table.column_mut(0).unwrap();
column.set_cell_alignment(CellAlignment::Right);

println!("\n{}", table);
}
Ok(())
}
10 changes: 8 additions & 2 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,15 @@ impl Metadata {
[col(COL::SOURCE_DATA_RELEASE_DATA_PUBLISHER_ID)],
[col(COL::DATA_PUBLISHER_ID)],
JoinArgs::new(JoinType::Inner),
)
// TODO: consider case when many countries
.explode([col(COL::DATA_PUBLISHER_COUNTRIES_OF_INTEREST)])
.join(
self.countries.clone().lazy(),
[col(COL::DATA_PUBLISHER_COUNTRIES_OF_INTEREST)],
[col(COL::COUNTRY_ID)],
JoinArgs::new(JoinType::Inner),
);
// TODO: Add a country_id column to the metadata, and merge in the countries as well. See
// https://github.com/Urban-Analytics-Technology-Platform/popgetter/issues/104

// Debug print the column names so that we know what we can access
let schema = df.schema().unwrap();
Expand Down
3 changes: 1 addition & 2 deletions src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,7 @@ impl SearchResults {

#[cfg(test)]
mod tests {
use super::*;

// use super::*;
// #[test]
// fn test_search_request() {
// let mut sr = SearchRequest{search_string: None}.with_country("a").with_country("b");
Expand Down
Loading