4
4
import click
5
5
import importlib_resources
6
6
from datacube import Datacube
7
+ from datacube .model import Dataset
7
8
from datacube .index .hl import Doc2Dataset
8
9
from datacube .utils import changes
9
10
from datadog import initialize , statsd
@@ -183,14 +184,15 @@ def get_esri_list():
183
184
184
185
185
186
def index_update_dataset (
186
- metadata : dict ,
187
+ dataset : dict | Dataset ,
187
188
uri : str ,
188
189
dc : Datacube ,
189
- doc2ds : Doc2Dataset ,
190
+ doc2ds : Doc2Dataset | None ,
190
191
update : bool = False ,
191
192
update_if_exists : bool = False ,
192
193
allow_unsafe : bool = False ,
193
194
archive_less_mature : Optional [int ] = None ,
195
+ auto_add_lineage : Optional [bool ] = False ,
194
196
publish_action : Optional [str ] = None ,
195
197
stac_doc : Optional [dict ] = None ,
196
198
) -> int :
@@ -215,19 +217,17 @@ def index_update_dataset(
215
217
:param stac_doc: STAC document for publication to SNS topic.
216
218
:return: Returns nothing. Raises an exception if anything goes wrong.
217
219
"""
218
- if uri is None :
219
- raise IndexingException ("Failed to get URI from metadata doc" )
220
220
# Make sure we can create a dataset first
221
- try :
222
- ds , err = doc2ds ( metadata , uri )
223
- except ValueError as e :
224
- raise IndexingException (
225
- f"Exception thrown when trying to create dataset: ' { e } ' \n The URI was { uri } "
226
- ) from e
227
- if ds is None :
228
- raise IndexingException (
229
- f"Failed to create dataset with error { err } \n The URI was { uri } "
230
- )
221
+ if not isinstance ( dataset , Dataset ) :
222
+ print ( "Not a dataset: " , dataset )
223
+ try :
224
+ if doc2ds is None :
225
+ doc2ds = Doc2Dataset ( dc . index )
226
+ dataset , _ = doc2ds ( dataset , uri )
227
+ except ValueError as e :
228
+ raise IndexingException (
229
+ f"Exception thrown when trying to create dataset: ' { e } ' \n The URI was { uri } "
230
+ ) from e
231
231
232
232
with dc .index .transaction ():
233
233
# Process in a transaction
@@ -236,13 +236,13 @@ def index_update_dataset(
236
236
updated = False
237
237
238
238
if isinstance (archive_less_mature , int ) and publish_action :
239
- dupes = dc .index .datasets .find_less_mature (ds , archive_less_mature )
239
+ dupes = dc .index .datasets .find_less_mature (dataset , archive_less_mature )
240
240
for dupe in dupes :
241
241
archive_stacs .append (ds_to_stac (dupe ))
242
242
243
243
# Now do something with the dataset
244
244
# Note that any of the exceptions raised below will rollback any archiving performed above.
245
- if dc .index .datasets .has (metadata . get ( "id" ) ):
245
+ if dc .index .datasets .has (dataset . id ):
246
246
# Update
247
247
if update or update_if_exists :
248
248
# Set up update fields
@@ -252,7 +252,7 @@ def index_update_dataset(
252
252
# Do the updating
253
253
try :
254
254
dc .index .datasets .update (
255
- ds ,
255
+ dataset ,
256
256
updates_allowed = updates ,
257
257
archive_less_mature = archive_less_mature ,
258
258
)
@@ -262,9 +262,8 @@ def index_update_dataset(
262
262
f"Updating the dataset raised an exception: { e } "
263
263
)
264
264
else :
265
- logging .warning ("Dataset already exists, not indexing" )
266
265
raise SkippedException (
267
- f"Dataset { metadata . get ( 'id' ) } already exists, not indexing"
266
+ f"Dataset { dataset . id } already exists, not indexing"
268
267
)
269
268
else :
270
269
if update :
@@ -273,22 +272,26 @@ def index_update_dataset(
273
272
"Can't update dataset because it doesn't exist."
274
273
)
275
274
# Everything is working as expected, add the dataset
276
- dc .index .datasets .add (ds , archive_less_mature = archive_less_mature )
275
+ dc .index .datasets .add (
276
+ dataset ,
277
+ with_lineage = auto_add_lineage ,
278
+ archive_less_mature = archive_less_mature ,
279
+ )
277
280
added = True
278
281
279
282
if publish_action :
280
283
for arch_stac in archive_stacs :
281
284
publish_to_topic (arn = publish_action , action = "ARCHIVED" , stac = arch_stac )
282
285
283
286
if added :
284
- logging .info ("New Dataset Added: %s" , ds .id )
287
+ logging .info ("New Dataset Added: %s" , dataset .id )
285
288
if publish_action :
286
289
# if STAC was not provided, generate from dataset
287
- stac_doc = stac_doc if stac_doc else ds_to_stac (ds )
290
+ stac_doc = stac_doc if stac_doc else ds_to_stac (dataset )
288
291
publish_to_topic (arn = publish_action , action = "ADDED" , stac = stac_doc )
289
292
290
293
if updated :
291
- logging .info ("Existing Dataset Updated: %s" , ds .id )
294
+ logging .info ("Existing Dataset Updated: %s" , dataset .id )
292
295
293
296
294
297
def statsd_gauge_reporting (value , tags = None , statsd_setting = "localhost:8125" ):
0 commit comments