Skip to content

Fixed whisp details and added commodity #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ on:

env:
DJANGO_SETTINGS_MODULE: eudr_backend.settings
EE_ACCOUNT_NAME: ${{ secrets.EE_ACCOUNT_NAME }}
AGSTACK_API_EMAIL: ${{ secrets.AGSTACK_API_EMAIL }}
AGSTACK_API_PASSWORD: ${{ secrets.AGSTACK_API_PASSWORD }}
EMAIL_HOST_USER: ${{ secrets.EMAIL_HOST_USER }}
EMAIL_HOST_PASSWORD: ${{ secrets.EMAIL_HOST_PASSWORD }}
EMAIL_HOST_DEFAULT_USER: ${{ secrets.EMAIL_HOST_DEFAULT_USER }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_STORAGE_BUCKET_NAME: ${{ secrets.AWS_STORAGE_BUCKET_NAME }}
AWS_S3_REGION_NAME: ${{ secrets.AWS_S3_REGION_NAME }}
WHISP_API_KEY: ${{ secrets.WHISP_API_KEY }}
SERVER_USER: ${{ secrets.SERVER_USER }}
SERVER_HOST: ${{ secrets.SERVER_HOST }}

jobs:
test:
Expand Down Expand Up @@ -47,6 +60,7 @@ jobs:
echo "EE_ACCOUNT_NAME=$EE_ACCOUNT_NAME" >> .env
echo "SERVER_USER=$SERVER_USER" >> .env
echo "SERVER_HOST=$SERVER_HOST" >> .env
echo "WHISP_API_KEY=$WHISP_API_KEY" >> .env

- name: Create ee creds json file
run: |
Expand Down
12 changes: 10 additions & 2 deletions eudr_backend/async_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from eudr_backend.models import EUDRFarmModel, EUDRUploadedFilesModel, WhispAPISetting
from eudr_backend.serializers import EUDRFarmModelSerializer
from eudr_backend.utils import flatten_geojson, format_geojson_data, transform_db_data_to_geojson
from decouple import config


# Define an async function
Expand All @@ -27,7 +28,7 @@ async def async_create_farm_data(data, file_id, isSyncing=False, hasCreatedFiles
return errors, created_data
else:
err, analysis_results = await perform_analysis(data)
print(analysis_results)
# print(analysis_results)
if err:
# delete the file if there are errors
EUDRUploadedFilesModel.objects.get(id=file_id).delete()
Expand Down Expand Up @@ -59,8 +60,13 @@ async def get_existing_record(data):


async def perform_analysis(data, hasCreatedFiles=[]):
api_key = config("WHISP_API_KEY")
if not api_key:
raise ValueError("WHISP_API_KEY environment variable not set.")

# print(f"Using API Key: {api_key}")
url = "https://whisp.openforis.org/api/submit/geojson"
headers = {"X-API-KEY": "7f7aaa3a-e99e-4c53-b79c-d06143ef8c1f",
headers = {"X-API-KEY": api_key,
"Content-Type": "application/json"}
settings = await sync_to_async(WhispAPISetting.objects.first)()
chunk_size = settings.chunk_size if settings else 500
Expand Down Expand Up @@ -92,7 +98,9 @@ async def perform_analysis(data, hasCreatedFiles=[]):


async def save_farm_data(data, file_id, analysis_results=None):
# print("analysis results",analysis_results)
formatted_data = format_geojson_data(data, analysis_results, file_id)
# print("formatted data",formatted_data)
saved_records = []

for item in formatted_data:
Expand Down
1 change: 1 addition & 0 deletions eudr_backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ def initialize_earth_engine():

AGSTACK_EMAIL = config('AGSTACK_API_EMAIL')
AGSTACK_PASSWORD = config('AGSTACK_API_PASSWORD')
WHISP_API_KEY = config('WHISP_API_KEY')

# email credentials
# settings.py
Expand Down
15 changes: 13 additions & 2 deletions eudr_backend/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ def get_access_token():
response = requests.post(login_url, json=payload)
response.raise_for_status() # Raise an error for bad responses
data = response.json()
# print("login successfully",data)
return data['access_token']


@background(schedule=60) # Schedule task to run every 5 minutes
# @background(schedule=60) # Schedule task to run every 5 minutes
def update_geoid(user_id):
access_token = get_access_token()
headers = {
Expand All @@ -32,29 +33,39 @@ def update_geoid(user_id):
user_files = EUDRUploadedFilesModel.objects.filter(uploaded_by=user_id)
file_ids = user_files.values_list('id', flat=True)

# print("fileids",file_ids)

# Filter farms based on these file IDs and geoid being null
farms = EUDRFarmModel.objects.filter(
geoid__isnull=True, file_id__in=file_ids)
for farm in farms:
# print("Raw polygon value:", farm.polygon)
# check if polygon has only one ring
if len(farm.polygon) != 1:
continue

reversed_coords = [[(lat, lon) for lat, lon in ring]
for ring in farm.polygon]

# Create a Shapely Polygon
# Create a Shapely Polygon
polygon = Polygon(reversed_coords[0])

# print("converted polygon",polygon )

# Convert to WKT format
wkt_format = wkt.dumps(polygon)

# print("WKT format",wkt_format)

response = requests.post(
f'{AG_BASE_URL}/register-field-boundary',
json={"wkt": wkt_format},
headers=headers
)
# print("API response status:", response.status_code)
# print("API response text:", response.text)
data = response.json()
# print("geo id data", data)
if response.status_code == 200:
farm.geoid = data.get("Geo Id")
farm.save()
Expand Down
Loading