Skip to content

Commit

Permalink
Add MetricText for search params from data request spec
Browse files Browse the repository at this point in the history
  • Loading branch information
sgreenbury committed Jul 26, 2024
1 parent 7a39e7b commit f57460f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
35 changes: 25 additions & 10 deletions src/data_request_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// See [#67](https://github.com/Urban-Analytics-Technology-Platform/popgetter-cli/issues/67)

use itertools::Itertools;
use nonempty::nonempty;
use serde::{Deserialize, Serialize};

use crate::geo::BBox;
use crate::search::{GeometryLevel, MetricId, SearchParams, YearRange};
use crate::search::{GeometryLevel, MetricId, SearchContext, SearchParams, SearchText, YearRange};

#[derive(Serialize, Deserialize, Debug, Default)]
pub struct DataRequestSpec {
Expand All @@ -18,12 +19,28 @@ pub struct DataRequestSpec {
impl TryFrom<DataRequestSpec> for SearchParams {
type Error = anyhow::Error;
fn try_from(value: DataRequestSpec) -> Result<Self, Self::Error> {
// TODO: handle MetricSpec::DataProduct variant
Ok(Self {
text: vec![],
// TODO: consider updating for regex field following [#66](https://github.com/Urban-Analytics-Technology-Platform/popgetter-cli/issues/66)
text: value
.metrics
.iter()
.filter_map(|metric| match metric {
MetricSpec::MetricText(text) => Some(SearchText {
text: text.clone(),
context: nonempty![
SearchContext::HumanReadableName,
SearchContext::Hxl,
SearchContext::Description
],
}),
_ => None,
})
.collect_vec(),
year_range: if let Some(v) = value.years {
Some(
v.iter()
.map(|year| format!("{year}...{year}").parse::<YearRange>())
.map(|year| year.parse::<YearRange>())
.collect::<Result<Vec<_>, anyhow::Error>>()?,
)
} else {
Expand All @@ -32,12 +49,9 @@ impl TryFrom<DataRequestSpec> for SearchParams {
metric_id: value
.metrics
.iter()
.filter_map(|metric| {
match metric {
MetricSpec::Metric(m) => Some(m.clone()),
// TODO: handle DataProduct variant
MetricSpec::DataProduct(_) => None,
}
.filter_map(|metric| match metric {
MetricSpec::MetricId(m) => Some(m.clone()),
_ => None,
})
.collect_vec(),
geometry_level: value
Expand Down Expand Up @@ -96,7 +110,8 @@ impl TryFrom<DataRequestSpec> for SearchParams {

#[derive(Serialize, Deserialize, Debug)]
pub enum MetricSpec {
Metric(MetricId),
MetricId(MetricId),
MetricText(String),
DataProduct(String),
}

Expand Down
23 changes: 13 additions & 10 deletions test_recipe.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
{
"region": [
{
"BoundingBox": [
-74.251785,
40.647043,
-73.673286,
40.91014
]
"BoundingBox": [-74.251785, 40.647043, -73.673286, 40.91014]
}
],
"metrics": [
{
"Metric": "f29c1976"
"MetricId": "f29c1976"
},
{
"Metric": "079f3ba3"
"MetricId": "079f3ba3"
},
{
"Metric": "81cae95d"
"MetricId": "81cae95d"
},
{
"MetricText": "Key: uniqueID, Value: B01001_001;"
}
]
],
"years": ["2021"],
"geometry": {
"geometry_level": "tract",
"include_geoms": true
}
}

0 comments on commit f57460f

Please sign in to comment.