Skip to content

Commit

Permalink
memory optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
Id405 authored and erlewa committed Aug 2, 2024
1 parent 52e46f0 commit 9761f9c
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 328 deletions.
23 changes: 14 additions & 9 deletions heatmap-service/src/geo_assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@ use geojson::{FeatureCollection, GeoJson};

use crate::assets::Assets;
use crate::config::Config;
use crate::granule::Granule;

#[derive(Clone)]
pub struct GeoAssets {
pub heatmap_features: FeatureCollection,
pub outline_features: FeatureCollection,
pub heatmap_features: Vec<Granule>,
pub outline_features: Vec<Granule>,
}

impl GeoAssets {
pub fn from_config(config: Config) -> Self {
Self {
heatmap_features: config.heatmap_geo_json_path.try_into().unwrap(),
outline_features: std::str::from_utf8(
Assets::get("outline.geojson").unwrap().data.as_ref(),
heatmap_features: Granule::from_feature_collection(
config.heatmap_geo_json_path.try_into().unwrap(),
)
.unwrap(),
outline_features: Granule::from_feature_collection(
std::str::from_utf8(Assets::get("outline.geojson").unwrap().data.as_ref())
.unwrap()
.parse::<GeoJson>()
.unwrap()
.try_into()
.unwrap(),
)
.unwrap()
.parse::<GeoJson>()
.unwrap()
.try_into()
.unwrap(),
}
}
Expand Down
16 changes: 5 additions & 11 deletions heatmap-service/src/granule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,18 @@ impl TryFrom<&Feature> for Granule {
type Error = Box<dyn std::error::Error>;

fn try_from(feature: &Feature) -> Result<Granule, Self::Error> {
let mut ancestors: Vec<serde_json::Map<String, serde_json::Value>> = Vec::new();
let mut ancestors: Vec<&serde_json::Map<String, serde_json::Value>> = Vec::new();
if let Some(ancestors_properties) = feature
.properties
.clone()
.as_ref()
.ok_or("feature has no properties associated with it")?
.get("ancestors")
{
ancestors = ancestors_properties
.clone()
.as_array()
.expect("failed to convert to array")
.iter()
.map(|feature| {
feature
.as_object()
.expect("failed to map ancestors")
.clone()
})
.map(|feature| feature.as_object().expect("failed to map ancestors"))
.collect();
}

Expand Down Expand Up @@ -66,7 +60,7 @@ impl TryFrom<&Feature> for Granule {
polygons,
ancestors: ancestors
.iter()
.map(|feature| feature.try_into().expect("failed to map ancestors"))
.map(|feature| (*feature).try_into().expect("failed to map ancestors"))
.collect(),
})
}
Expand Down Expand Up @@ -134,7 +128,7 @@ impl TryFrom<&serde_json::Map<String, serde_json::Value>> for Ancestor {

impl Granule {
pub fn from_feature_collection(
feature_collection: &FeatureCollection,
feature_collection: FeatureCollection,
) -> Result<Vec<Granule>, Box<dyn std::error::Error>> {
feature_collection
.features
Expand Down
9 changes: 3 additions & 6 deletions heatmap-service/src/heatmap_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ pub struct HeatmapResponse {
}

impl HeatmapResponse {
pub fn from_geojson(
filter: heatmap_api::Filter,
feature_collection: &FeatureCollection,
) -> Self {
let mut granules = Granule::from_feature_collection(feature_collection).unwrap();
pub fn from_geojson(filter: heatmap_api::Filter, granules: &[Granule]) -> Self {
let mut granules = granules.to_owned();

let data_type = filter.product_type;
let platform_type = filter.platform_type;
Expand All @@ -37,7 +34,7 @@ impl HeatmapResponse {

granules.retain(|granule| !granule.ancestors.is_empty());

Self::from_granules(granules.clone())
Self::from_granules(granules)
}

fn from_granules(granules: Vec<Granule>) -> Self {
Expand Down
5 changes: 2 additions & 3 deletions heatmap-service/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ mod middleware;
mod outline_response;
mod query;
mod redis;
mod tests;

#[actix_web::main]
async fn main() -> std::io::Result<()> {
Expand All @@ -47,10 +46,10 @@ async fn main() -> std::io::Result<()> {

config.redis = None;

let geo_assets = GeoAssets::from_config(config.clone());

let bind_address = config.server_address.clone();

let geo_assets = GeoAssets::from_config(config.clone());

HttpServer::new(move || {
let mut app = App::new()
.wrap(Logger::default())
Expand Down
8 changes: 3 additions & 5 deletions heatmap-service/src/outline_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct OutlineData {
}

impl OutlineData {
pub fn from_granules(granules: Vec<Granule>) -> Self {
pub fn from_granules(granules: &Vec<Granule>) -> Self {
let mut positions = Vec::new();

for granule in granules.iter() {
Expand All @@ -31,13 +31,11 @@ pub struct OutlineResponse {
}

impl OutlineResponse {
pub fn from_geojson(feature_collection: &FeatureCollection) -> Self {
let granules = Granule::from_feature_collection(feature_collection).unwrap();

pub fn from_geojson(granules: &Vec<Granule>) -> Self {
Self::from_granules(granules)
}

fn from_granules(granules: Vec<Granule>) -> Self {
fn from_granules(granules: &Vec<Granule>) -> Self {
Self {
data: OutlineData::from_granules(granules),
}
Expand Down
2 changes: 0 additions & 2 deletions heatmap-service/src/tests/add_granule.sql

This file was deleted.

Loading

0 comments on commit 9761f9c

Please sign in to comment.