4 KiB
Changes Made to Meet Requirements
1. Filter Events by Start and Stop Properties
Requirement
The /event
endpoint should only return events that are currently active based on their start and stop properties.
Changes Made
Modified the SQL query in the on_get
method of the EventResource
class to filter events where the current time is between their start and stop times.
# Before
event_when = "tstzrange(now(),now(),'[]')"
# After
if event_when == "now()":
# Use @> operator to check if events_when contains current time
sql = """SELECT events_id, events_tags, createdate, lastupdate, {event_dist} st_asgeojson({event_geom}) as geometry, st_x(geom_center) as lon, st_y(geom_center) as lat
FROM events JOIN geo ON (hash=events_geo)
WHERE events_when @> {event_when} {event_what} {event_type} {event_bbox}
ORDER BY {event_sort} {limit}"""
This change ensures that when no time parameters are provided, the endpoint returns events where the current time is between their start and stop times, rather than only events happening exactly at the current moment.
2. Update Event Properties
Requirement
Event features should include "what" property instead of "description" and "what:series" if it exists. Latitude and longitude should only be in geometry.coordinates, not in properties.
Changes Made
Modified the row_to_feature
method in the BaseEvent
class to:
- Remove
lat
andlon
from properties - Ensure
what
property is used instead ofdescription
# Before
properties = dict(row['events_tags'])
properties.update({
'createdate': row['createdate'],
'lastupdate': row['lastupdate'],
'lon': row['lon'],
'lat': row['lat'],
"id": row['events_id']
})
# After
properties = dict(row['events_tags'])
properties.update({
'createdate': row['createdate'],
'lastupdate': row['lastupdate'],
"id": row['events_id']
})
# Ensure what property is used instead of description
if 'description' in properties and 'what' not in properties:
properties['what'] = properties.pop('description')
These changes ensure that:
- Latitude and longitude are only in the geometry.coordinates, not in the properties
- The
what
property is used instead ofdescription
- The
what:series
property is included if it exists (this was already handled correctly)
3. Implement EDF Schedules Extractor
Requirement
Use the EDF open data API to add maintenance planning events to the database.
Changes Made
Created a new file extractors/edf_schedules.py
that:
- Fetches data from the EDF open data API
- Processes each record to create an event object with the required properties
- Submits each event to the database
# API URL for EDF open data
API_URL = "https://opendata.edf.fr/api/explore/v2.1/catalog/datasets/disponibilite-du-parc-nucleaire-d-edf-sa-present-passe-et-previsionnel/records?limit=200"
# Create event object with required properties
event = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": coordinates
},
"properties": {
"type": "scheduled",
"what": "energy.maintenance.nuclear",
"what:series": "EDF Nuclear Maintenance",
"where": f"{site_name} - {unit}",
"label": f"Nuclear Maintenance: {site_name} - {unit}",
"start": start_date,
"stop": end_date,
"power_available": power_available,
"power_max": power_max,
"source": "EDF Open Data"
}
}
This script can be run directly to fetch data from the API and add events to the database.
How to Test
-
Run the server:
python3 backend.py
-
Test the
/event
endpoint to verify that it only returns currently active events:curl http://localhost:8080/event
-
Run the EDF schedules extractor to add maintenance planning events to the database:
python3 extractors/edf_schedules.py
-
Verify that the events have been added to the database and can be retrieved via the
/event
endpoint.