From 107986c95a8bdeb86618951f3347939fc77d3b03 Mon Sep 17 00:00:00 2001 From: kshitijrajsharma Date: Thu, 6 Jun 2024 19:04:00 +0545 Subject: [PATCH] fix(iso3-stats): fix bug on iso3 stats after the role check --- API/stats.py | 33 +++++++++++++++++++-------------- src/app.py | 6 +++--- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/API/stats.py b/API/stats.py index 9f3ee836..bb1522ec 100644 --- a/API/stats.py +++ b/API/stats.py @@ -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 @@ -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 = { diff --git a/src/app.py b/src/app.py index 58db1f0d..b11740cc 100644 --- a/src/app.py +++ b/src/app.py @@ -21,8 +21,8 @@ import concurrent.futures import json import os -import random import pathlib +import random import re import shutil import subprocess @@ -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...")