Skip to content

Commit

Permalink
fix(iso3-stats): fix bug on iso3 stats after the role check
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitijrajsharma committed Jun 6, 2024
1 parent c127bc1 commit 107986c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
33 changes: 19 additions & 14 deletions API/stats.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Standard library imports
import json

from fastapi import APIRouter, Body, Request, Depends, HTTPException
from fastapi_versioning import version
# Third party imports
from area import area
from fastapi import APIRouter, Body, Depends, HTTPException, Request
from fastapi_versioning import version

# Reader imports
from src.app import PolygonStats
from src.config import LIMITER as limiter
from src.config import POLYGON_STATISTICS_API_RATE_LIMIT
Expand Down Expand Up @@ -58,18 +62,19 @@ async def get_polygon_stats(
dict: A dictionary containing statistics for the specified polygon.
"""
if not (user.role is UserRole.STAFF.value or user.role is UserRole.ADMIN.value):
area_m2 = area(json.loads(params.geometry.model_dump_json()))
area_km2 = area_m2 * 1e-6
limit = 10000
if area_km2 > limit:
raise HTTPException(
status_code=400,
detail=[
{
"msg": f"""Polygon Area {int(area_km2)} Sq.KM is higher than Threshold : {limit} Sq.KM"""
}
],
)
if params.geometry:
area_m2 = area(json.loads(params.geometry.model_dump_json()))
area_km2 = area_m2 * 1e-6
limit = 10000
if area_km2 > limit:
raise HTTPException(
status_code=400,
detail=[
{
"msg": f"""Polygon Area {int(area_km2)} Sq.KM is higher than Threshold : {limit} Sq.KM"""
}
],
)
feature = None
if params.geometry:
feature = {
Expand Down
6 changes: 3 additions & 3 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import concurrent.futures
import json
import os
import random
import pathlib
import random
import re
import shutil
import subprocess
Expand Down Expand Up @@ -1062,13 +1062,13 @@ 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=30)
response = requests.post(self.API_URL, json=payload, timeout=20)
response.raise_for_status()
return response.json()
except Exception as e:
print(f"Request failed: {e}")
retries += 1
delay = min(delay * 2, MAX_DELAY) # Exponential backoff
delay = min(delay * 0.5, MAX_DELAY) # Exponential backoff
jitter = random.uniform(0, 1) # jitter to avoid simultaneous retries
sleep_time = delay * (1 + jitter)
print(f"Retrying in {sleep_time} seconds...")
Expand Down

0 comments on commit 107986c

Please sign in to comment.