Update app.py
Browse files
app.py
CHANGED
|
@@ -8,8 +8,6 @@ import requests
|
|
| 8 |
from dataclasses import dataclass
|
| 9 |
from tenacity import retry, stop_after_attempt, wait_fixed
|
| 10 |
import plotly.express as px
|
| 11 |
-
import redis
|
| 12 |
-
import os
|
| 13 |
|
| 14 |
# Configuration du logging
|
| 15 |
logging.basicConfig(
|
|
@@ -18,11 +16,6 @@ logging.basicConfig(
|
|
| 18 |
)
|
| 19 |
logger = logging.getLogger(__name__)
|
| 20 |
|
| 21 |
-
# Configuration du cache Redis
|
| 22 |
-
REDIS_HOST = os.getenv('REDIS_HOST', 'localhost')
|
| 23 |
-
REDIS_PORT = int(os.getenv('REDIS_PORT', 6379))
|
| 24 |
-
redis_client = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT, db=0)
|
| 25 |
-
|
| 26 |
@dataclass
|
| 27 |
class PesticideRecord:
|
| 28 |
"""Structure de données pour les enregistrements de pesticides."""
|
|
@@ -98,11 +91,11 @@ class PesticideDataFetcher:
|
|
| 98 |
if not response or "value" not in response:
|
| 99 |
break
|
| 100 |
all_products.extend(response["value"])
|
| 101 |
-
|
| 102 |
-
if
|
| 103 |
-
url =
|
| 104 |
-
|
| 105 |
-
|
| 106 |
self._product_cache = all_products
|
| 107 |
logger.info(f"Récupéré {len(all_products)} produits au total")
|
| 108 |
return all_products
|
|
@@ -152,11 +145,32 @@ class PesticideDataFetcher:
|
|
| 152 |
return f"Substance {substance_id}"
|
| 153 |
|
| 154 |
def get_all_substances(self) -> List[str]:
|
| 155 |
-
"""Récupère la liste complète des substances actives."""
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
class PesticideInterface:
|
| 162 |
"""Classe pour gérer l'interface utilisateur Gradio."""
|
|
@@ -401,5 +415,4 @@ def main():
|
|
| 401 |
)
|
| 402 |
|
| 403 |
if __name__ == "__main__":
|
| 404 |
-
main()
|
| 405 |
-
|
|
|
|
| 8 |
from dataclasses import dataclass
|
| 9 |
from tenacity import retry, stop_after_attempt, wait_fixed
|
| 10 |
import plotly.express as px
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Configuration du logging
|
| 13 |
logging.basicConfig(
|
|
|
|
| 16 |
)
|
| 17 |
logger = logging.getLogger(__name__)
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
@dataclass
|
| 20 |
class PesticideRecord:
|
| 21 |
"""Structure de données pour les enregistrements de pesticides."""
|
|
|
|
| 91 |
if not response or "value" not in response:
|
| 92 |
break
|
| 93 |
all_products.extend(response["value"])
|
| 94 |
+
next_link = response.get("@odata.nextLink")
|
| 95 |
+
if next_link:
|
| 96 |
+
url = next_link
|
| 97 |
+
else:
|
| 98 |
+
break
|
| 99 |
self._product_cache = all_products
|
| 100 |
logger.info(f"Récupéré {len(all_products)} produits au total")
|
| 101 |
return all_products
|
|
|
|
| 145 |
return f"Substance {substance_id}"
|
| 146 |
|
| 147 |
def get_all_substances(self) -> List[str]:
|
| 148 |
+
"""Récupère la liste complète des substances actives avec pagination."""
|
| 149 |
+
if self._substance_cache:
|
| 150 |
+
return list(self._substance_cache.keys())
|
| 151 |
+
all_substances = set()
|
| 152 |
+
base_url = f"{self.BASE_URL}/active_substances?format=json&api-version=v2.0"
|
| 153 |
+
url = base_url
|
| 154 |
+
while url:
|
| 155 |
+
response = self.fetch_data(url)
|
| 156 |
+
if not response or "value" not in response:
|
| 157 |
+
break
|
| 158 |
+
for item in response.get("value", []):
|
| 159 |
+
substance_name = item.get("substance_name")
|
| 160 |
+
if substance_name:
|
| 161 |
+
all_substances.add(substance_name)
|
| 162 |
+
self._substance_cache[substance_name] = {
|
| 163 |
+
"status": item.get("substance_status"),
|
| 164 |
+
"approval_date": item.get("approval_date"),
|
| 165 |
+
"expiry_date": item.get("expiry_date")
|
| 166 |
+
}
|
| 167 |
+
next_link = response.get("@odata.nextLink")
|
| 168 |
+
if next_link:
|
| 169 |
+
url = next_link
|
| 170 |
+
else:
|
| 171 |
+
break
|
| 172 |
+
logger.info(f"Récupéré {len(all_substances)} substances au total")
|
| 173 |
+
return sorted(all_substances)
|
| 174 |
|
| 175 |
class PesticideInterface:
|
| 176 |
"""Classe pour gérer l'interface utilisateur Gradio."""
|
|
|
|
| 415 |
)
|
| 416 |
|
| 417 |
if __name__ == "__main__":
|
| 418 |
+
main()
|
|
|