33
33
def main (
34
34
gcp_service_acct : gcp_service_account ,
35
35
alerts_bucket : str ,
36
+ alerts_provider : str ,
36
37
territory_id : int ,
37
38
db : postgresql ,
38
39
db_table_name : str ,
@@ -49,6 +50,7 @@ def main(
49
50
return _main (
50
51
storage_client ,
51
52
alerts_bucket ,
53
+ alerts_provider ,
52
54
territory_id ,
53
55
db ,
54
56
db_table_name ,
@@ -59,6 +61,7 @@ def main(
59
61
def _main (
60
62
storage_client : gcs .Client ,
61
63
alerts_bucket : str ,
64
+ alerts_provider : str ,
62
65
territory_id : int ,
63
66
db : postgresql ,
64
67
db_table_name : str ,
@@ -71,6 +74,8 @@ def _main(
71
74
storage_client : google.cloud.storage.Client
72
75
alerts_bucket : str
73
76
The name of the GCS bucket containing alerts.
77
+ alerts_provider : str
78
+ The name of the alerts provider.
74
79
territory_id : int
75
80
The ID of the territory for which alerts are being processed.
76
81
db : postgresql
@@ -97,10 +102,12 @@ def _main(
97
102
convert_tiffs_to_jpg (tiff_files )
98
103
99
104
prepared_alerts_metadata , alerts_statistics = prepare_alerts_metadata (
100
- alerts_metadata , territory_id
105
+ alerts_metadata , territory_id , alerts_provider
101
106
)
102
107
103
- prepared_alerts_data = prepare_alerts_data (destination_path , geojson_files )
108
+ prepared_alerts_data = prepare_alerts_data (
109
+ destination_path , geojson_files , alerts_provider
110
+ )
104
111
105
112
logger .info (f"Writing alerts to the database table [{ db_table_name } ]." )
106
113
alerts_writer = StructuredDBWriter (
@@ -317,7 +324,7 @@ def convert_tiffs_to_jpg(tiff_files):
317
324
logger .info ("Successfully converted TIFF files to JPEG." )
318
325
319
326
320
- def prepare_alerts_metadata (alerts_metadata , territory_id ):
327
+ def prepare_alerts_metadata (alerts_metadata , territory_id , alerts_provider ):
321
328
"""
322
329
Prepare alerts metadata by filtering and processing CSV data.
323
330
@@ -335,6 +342,7 @@ def prepare_alerts_metadata(alerts_metadata, territory_id):
335
342
CSV data as a string containing alerts metadata.
336
343
territory_id : int
337
344
The identifier for the territory used to filter the metadata.
345
+ alerts_provider : str
338
346
339
347
Returns
340
348
-------
@@ -377,9 +385,7 @@ def prepare_alerts_metadata(alerts_metadata, territory_id):
377
385
index = False ,
378
386
)
379
387
380
- # TODO: Currently, this script is only used for Terras alerts. Let's discuss a more sustainable approach with the alerts provider(s).
381
- # Also, if this changes for future alerts, we will need to ensure that existing records are not overwritten.
382
- filtered_df ["data_source" ] = "terras"
388
+ filtered_df ["data_source" ] = alerts_provider
383
389
384
390
# Replace all NaN values with None
385
391
filtered_df .replace ({float ("nan" ): None }, inplace = True )
@@ -419,7 +425,7 @@ def prepare_alerts_metadata(alerts_metadata, territory_id):
419
425
return prepared_alerts_metadata , alerts_statistics
420
426
421
427
422
- def prepare_alerts_data (local_directory , geojson_files ):
428
+ def prepare_alerts_data (local_directory , geojson_files , alerts_provider ):
423
429
"""
424
430
Prepare alerts data by reading GeoJSON files from a local directory.
425
431
@@ -432,7 +438,8 @@ def prepare_alerts_data(local_directory, geojson_files):
432
438
The local directory where GeoJSON files are stored.
433
439
geojson_files : list of str
434
440
A list of GeoJSON file names to be processed.
435
-
441
+ alerts_provider : str
442
+ The name of the alerts provider.
436
443
Returns
437
444
-------
438
445
list of dict
@@ -485,7 +492,7 @@ def prepare_alerts_data(local_directory, geojson_files):
485
492
"g__type" : geom .get ("type" ),
486
493
"g__coordinates" : json .dumps (geom .get ("coordinates" )),
487
494
# Metadata
488
- "data_source" : "terras" ,
495
+ "data_source" : alerts_provider ,
489
496
"source_file_name" : file_path ,
490
497
}
491
498
)
0 commit comments