diff --git a/harvester/src/mapbox_tree_update.py b/harvester/src/mapbox_tree_update.py index bc1a40c5..a51a1085 100644 --- a/harvester/src/mapbox_tree_update.py +++ b/harvester/src/mapbox_tree_update.py @@ -75,6 +75,16 @@ def generate_trees_csv(temp_dir, db_conn): """ ) trees = cur.fetchall() + trees = [] + + # Get all waterings that are included in the amount of waterings for the last 30 days + cur.execute( + """ + SELECT * FROM trees_watered w WHERE w.timestamp >= CURRENT_DATE - INTERVAL '30 days' AND DATE_TRUNC('day', w.timestamp) < CURRENT_DATE; + """ + ) + trees_watered = cur.fetchall() + logging.info(f"Creating trees.csv file for {len(trees)} trees...") # Build CSV file with all trees in it @@ -109,7 +119,7 @@ def generate_trees_csv(temp_dir, db_conn): with open(trees_csv_full_path, "w") as out: out.write(trees_csv) - return trees_csv_full_path + return (trees_csv_full_path, trees_watered) def update_mapbox_tree_layer( @@ -125,7 +135,7 @@ def update_mapbox_tree_layer( with tempfile.TemporaryDirectory() as temp_dir: # Generate trees.csv from trees in database - trees_csv_full_path = generate_trees_csv(temp_dir, db_conn) + (trees_csv_full_path, trees_watered) = generate_trees_csv(temp_dir, db_conn) # Preprocess trees.csv with tippecanoe trees_preprocessed_full_path = preprocess_trees_csv( @@ -164,3 +174,28 @@ def update_mapbox_tree_layer( if tileset_creation_error is not None: logging.error("Could not create Mapbox tileset") + + return trees_watered + + +def update_tree_waterings(trees_watered, db_conn): + print(f"Set included_in_map_layer = TRUE for {len(trees_watered)} watered trees...") + tree_ids = [tree_watered[4] for tree_watered in trees_watered] + with db_conn.cursor() as cur: + # Set included_in_map_layer = FALSE for all waterings + cur.execute( + """ + UPDATE trees_watered SET included_in_map_layer = FALSE WHERE TRUE; + """, + (tree_ids,), + ) + db_conn.commit() + + # Set included_in_map_layer = FALSE for the waterings that are included in this round of the harvester + cur.execute( + """ + UPDATE trees_watered SET included_in_map_layer = TRUE WHERE id = ANY (%s); + """, + (tree_ids,), + ) + db_conn.commit() diff --git a/harvester/src/run_harvester.py b/harvester/src/run_harvester.py index 9eceb3bd..f8201952 100644 --- a/harvester/src/run_harvester.py +++ b/harvester/src/run_harvester.py @@ -9,7 +9,7 @@ from radolan_db_utils import ( get_start_end_harvest_dates, ) -from mapbox_tree_update import update_mapbox_tree_layer +from mapbox_tree_update import update_mapbox_tree_layer, update_tree_waterings # Set up logging logging.basicConfig() @@ -79,7 +79,7 @@ # Update Mapbox layer if not SKIP_MAPBOX: - update_mapbox_tree_layer( + trees_watered = update_mapbox_tree_layer( MAPBOX_USERNAME, MAPBOX_TOKEN, MAPBOX_TILESET, @@ -89,3 +89,6 @@ SUPABASE_SERVICE_ROLE_KEY, database_connection, ) + + # Update the tree waterings and flag the waterings which are now included in the mapbox layer with included_in_map_layer = TRUE + update_tree_waterings(trees_watered, database_connection)