Spaces:
Sleeping
Sleeping
File size: 1,115 Bytes
2477a46 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | from src.api.monitoring.db import get_connection
def load_crops_by_area_from_db():
query = """
SELECT area, item
FROM country_crop_mapping
ORDER BY area, item;
"""
crops_by_area = {}
with get_connection() as conn:
with conn.cursor() as cursor:
cursor.execute(query)
rows = cursor.fetchall()
for area, item in rows:
crops_by_area.setdefault(item, []).append(area)
return crops_by_area
def get_available_areas():
query = """
SELECT DISTINCT area
FROM country_crop_mapping
ORDER BY area;
"""
with get_connection() as conn:
with conn.cursor() as cursor:
cursor.execute(query)
rows = cursor.fetchall()
return [row[0] for row in rows]
def get_available_crops_by_area(area):
query = """
SELECT item
FROM country_crop_mapping
WHERE area = %s
ORDER BY item;
"""
with get_connection() as conn:
with conn.cursor() as cursor:
cursor.execute(query, (area,))
rows = cursor.fetchall()
return [row[0] for row in rows] |