|
| 1 | +from datetime import datetime |
| 2 | +from typing import Any, Dict, Optional |
| 3 | + |
| 4 | +import pystac |
| 5 | +from pydantic import Field, validator |
| 6 | + |
| 7 | +from pctasks.core.models.record import Record |
| 8 | +from pctasks.core.utils import StrEnum |
| 9 | + |
| 10 | + |
| 11 | +class ItemRecordType(StrEnum): |
| 12 | + STAC_ITEM = "StacItem" |
| 13 | + ITEM_UPDATED = "ItemUpdated" |
| 14 | + |
| 15 | + |
| 16 | +class ItemRecord(Record): |
| 17 | + type: ItemRecordType |
| 18 | + stac_id: str |
| 19 | + |
| 20 | + @validator("stac_id") |
| 21 | + def _stac_id_validator(cls, v: str) -> str: |
| 22 | + # Ensure a single forward slash in the string |
| 23 | + if v.count("/") != 1: |
| 24 | + raise ValueError("stac_id must contain a single forward slash") |
| 25 | + return v |
| 26 | + |
| 27 | + def get_id(self) -> str: |
| 28 | + version = getattr(self, "version", "") |
| 29 | + return f"{self.collection_id}:{self.item_id}:{version}:{self.type}" |
| 30 | + |
| 31 | + @property |
| 32 | + def collection_id(self) -> str: |
| 33 | + return self.stac_id.split("/")[0] |
| 34 | + |
| 35 | + @property |
| 36 | + def item_id(self) -> str: |
| 37 | + return self.stac_id.split("/")[1] |
| 38 | + |
| 39 | + |
| 40 | +class StacItemRecord(ItemRecord): |
| 41 | + """ |
| 42 | + Record for STAC items. |
| 43 | +
|
| 44 | + These records are used in the items container of the Cosmos DB database. |
| 45 | +
|
| 46 | + Parameters |
| 47 | + ---------- |
| 48 | + type: ItemRecordType, 'StacItem' |
| 49 | + This is always 'StacItem' |
| 50 | + stac_id: str |
| 51 | + The "STAC identifier" which is the STAC collection ID and Item ID joined |
| 52 | + by a single ``/`` |
| 53 | + version: str, optional |
| 54 | + The STAC version identifier. |
| 55 | + """ |
| 56 | + |
| 57 | + type: ItemRecordType = Field(default=ItemRecordType.STAC_ITEM, const=True) |
| 58 | + item: Dict[str, Any] |
| 59 | + deleted: bool = False |
| 60 | + |
| 61 | + @classmethod |
| 62 | + def from_item(cls, item: pystac.Item) -> "StacItemRecord": |
| 63 | + collection_id = item.collection_id |
| 64 | + item_id = item.id |
| 65 | + stac_id = f"{collection_id}/{item_id}" |
| 66 | + return cls( |
| 67 | + stac_id=stac_id, version=item.properties.get("version"), item=item.to_dict() |
| 68 | + ) |
| 69 | + |
| 70 | + @property |
| 71 | + def version(self) -> str: |
| 72 | + return self.item.get("properties", {}).get("version", "") |
| 73 | + |
| 74 | + |
| 75 | +class ItemUpdatedRecord(ItemRecord): |
| 76 | + """Record that records an item update. |
| 77 | +
|
| 78 | + Does not specify if the item was created or updated. |
| 79 | + """ |
| 80 | + |
| 81 | + type: ItemRecordType = Field(default=ItemRecordType.ITEM_UPDATED, const=True) |
| 82 | + |
| 83 | + run_id: str |
| 84 | + """The run ID of the workflow that updated this Item version""" |
| 85 | + |
| 86 | + delete: bool = False |
| 87 | + """True if the update was to delete this Item version""" |
| 88 | + |
| 89 | + storage_event_time: Optional[datetime] = None |
| 90 | + message_inserted_time: Optional[datetime] = None |
| 91 | + version: Optional[str] |
| 92 | + |
| 93 | + @validator("version") |
| 94 | + def _version_validator(cls, v: Optional[str]) -> str: |
| 95 | + return v or "" |
0 commit comments