Skip to content

Commit

Permalink
Merge pull request #242 from hotosm/hotfix/stats_endpoint
Browse files Browse the repository at this point in the history
HOTFix : Adds restriction on stats endpoint
  • Loading branch information
kshitijrajsharma authored Mar 28, 2024
2 parents 6b53fc2 + 85fb634 commit b5a1bb5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 34 deletions.
2 changes: 1 addition & 1 deletion API/raw_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ def get_osm_current_snapshot_as_plain_geojson(
"""
area_m2 = area(json.loads(params.geometry.model_dump_json()))
area_km2 = area_m2 * 1e-6
if area_km2 > 10:
if area_km2 > 5:
raise HTTPException(
status_code=400,
detail=[
Expand Down
2 changes: 1 addition & 1 deletion src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,7 @@ def get_osm_analytics_meta_stats(self):
try:
query = generate_polygon_stats_graphql_query(self.INPUT_GEOM)
payload = {"query": query}
response = requests.post(self.API_URL, json=payload, timeout=60)
response = requests.post(self.API_URL, json=payload, timeout=30)
response.raise_for_status() # Raise an HTTPError for bad responses
return response.json()
except Exception as e:
Expand Down
78 changes: 46 additions & 32 deletions src/validation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from typing import Dict, List, Optional, Union

# Third party imports
from area import area
from geojson_pydantic import Feature, FeatureCollection, MultiPolygon, Polygon
from geojson_pydantic.types import BBox
from pydantic import BaseModel as PydanticModel
Expand Down Expand Up @@ -300,22 +301,22 @@ class StatsRequestParams(BaseModel, GeometryValidatorMixin):
max_length=3,
example="NPL",
)
geometry: Optional[
Union[Polygon, MultiPolygon, Feature, FeatureCollection]
] = Field(
default=None,
example={
"type": "Polygon",
"coordinates": [
[
[83.96919250488281, 28.194446860487773],
[83.99751663208006, 28.194446860487773],
[83.99751663208006, 28.214869548073377],
[83.96919250488281, 28.214869548073377],
[83.96919250488281, 28.194446860487773],
]
],
},
geometry: Optional[Union[Polygon, MultiPolygon, Feature, FeatureCollection]] = (
Field(
default=None,
example={
"type": "Polygon",
"coordinates": [
[
[83.96919250488281, 28.194446860487773],
[83.99751663208006, 28.194446860487773],
[83.99751663208006, 28.214869548073377],
[83.96919250488281, 28.214869548073377],
[83.96919250488281, 28.194446860487773],
]
],
},
)
)

@validator("geometry", pre=True, always=True)
Expand All @@ -327,6 +328,19 @@ def set_geometry_or_iso3(cls, value, values):
raise ValueError("Either geometry or iso3 should be supplied.")
return value

@validator("geometry", pre=True, always=True)
def validate_geometry_area(cls, value):
"""Validate that the geometry area does not exceed threshold."""
if value is not None:
geometry_json = value
area_m2 = area(geometry_json)
max_area = 10000
if area_m2 * 1e-6 > max_area:
raise ValueError(
f"The area {area_m2 * 1e-6} sqkm of the geometry should not exceed {max_area} square km."
)
return value


### HDX BLock

Expand Down Expand Up @@ -612,22 +626,22 @@ class DynamicCategoriesModel(BaseModel, GeometryValidatorMixin):
}
],
)
geometry: Optional[
Union[Polygon, MultiPolygon, Feature, FeatureCollection]
] = Field(
default=None,
example={
"type": "Polygon",
"coordinates": [
[
[83.96919250488281, 28.194446860487773],
[83.99751663208006, 28.194446860487773],
[83.99751663208006, 28.214869548073377],
[83.96919250488281, 28.214869548073377],
[83.96919250488281, 28.194446860487773],
]
],
},
geometry: Optional[Union[Polygon, MultiPolygon, Feature, FeatureCollection]] = (
Field(
default=None,
example={
"type": "Polygon",
"coordinates": [
[
[83.96919250488281, 28.194446860487773],
[83.99751663208006, 28.194446860487773],
[83.99751663208006, 28.214869548073377],
[83.96919250488281, 28.214869548073377],
[83.96919250488281, 28.194446860487773],
]
],
},
)
)

@validator("geometry", pre=True, always=True)
Expand Down

0 comments on commit b5a1bb5

Please sign in to comment.