|
1 | 1 | import decimal |
2 | 2 | import json |
3 | | -from enum import Enum |
4 | | -from typing import Any, Dict, Sequence, Union |
| 3 | +from typing import Any, Dict, Sequence |
5 | 4 |
|
6 | 5 | import boto3 |
7 | 6 | import orjson |
8 | 7 | import pydantic |
9 | 8 | from pypgstac.db import PgstacDB |
10 | 9 | from pypgstac.load import Methods |
11 | 10 |
|
12 | | -from .schemas import AccessibleItem, StacCollection |
| 11 | +from .schemas import Ingestion |
13 | 12 | from .vedaloader import VEDALoader |
14 | 13 |
|
15 | 14 |
|
16 | | -class IngestionType(str, Enum): |
17 | | - collections = "collections" |
18 | | - items = "items" |
19 | | - |
20 | | - |
21 | 15 | class DbCreds(pydantic.BaseModel): |
22 | 16 | username: str |
23 | 17 | password: str |
@@ -62,47 +56,29 @@ def decimal_to_float(obj): |
62 | 56 | ) |
63 | 57 |
|
64 | 58 |
|
65 | | -def load_items(items: Sequence[AccessibleItem], loader): |
| 59 | +def load_items(creds: DbCreds, ingestions: Sequence[Ingestion]): |
66 | 60 | """ |
67 | | - Loads items into the PgSTAC database and |
68 | | - updates the summaries and extent for the collections involved |
69 | | - """ |
70 | | - loading_result = loader.load_items( |
71 | | - file=items, |
72 | | - # use insert_ignore to avoid overwritting existing items or upsert to replace |
73 | | - insert_mode=Methods.upsert, |
74 | | - ) |
75 | | - |
76 | | - # Trigger update on summaries and extents |
77 | | - collections = set([item["collection"] for item in items]) |
78 | | - for collection in collections: |
79 | | - loader.update_collection_summaries(collection) |
80 | | - |
81 | | - return loading_result |
82 | | - |
83 | | - |
84 | | -def load_collection(collection: Sequence[StacCollection], loader): |
85 | | - """ |
86 | | - Loads the collection to the PgSTAC database |
| 61 | + Bulk insert STAC records into pgSTAC. |
87 | 62 | """ |
88 | | - return loader.load_collections( |
89 | | - file=collection, |
90 | | - # use insert_ignore to avoid overwritting existing items or upsert to replace |
91 | | - insert_mode=Methods.upsert, |
92 | | - ) |
| 63 | + with PgstacDB(dsn=creds.dsn_string, debug=True) as db: |
| 64 | + loader = VEDALoader(db=db) |
| 65 | + |
| 66 | + items = [ |
| 67 | + # NOTE: Important to deserialize values to convert decimals to floats |
| 68 | + convert_decimals_to_float(i.item) |
| 69 | + for i in ingestions |
| 70 | + ] |
| 71 | + |
| 72 | + print(f"Ingesting {len(items)} items") |
| 73 | + loading_result = loader.load_items( |
| 74 | + file=items, |
| 75 | + # use insert_ignore to avoid overwritting existing items or upsert to replace |
| 76 | + insert_mode=Methods.upsert, |
| 77 | + ) |
93 | 78 |
|
| 79 | + # Trigger update on summaries and extents |
| 80 | + collections = set([item["collection"] for item in items]) |
| 81 | + for collection in collections: |
| 82 | + loader.update_collection_summaries(collection) |
94 | 83 |
|
95 | | -def load_into_pgstac( |
96 | | - db: "PgstacDB", |
97 | | - ingestions: Union[Sequence[AccessibleItem], Sequence[StacCollection]], |
98 | | - table: IngestionType, |
99 | | -): |
100 | | - """ |
101 | | - Bulk insert STAC records into pgSTAC. |
102 | | - The ingestion can be items or collection, determined by the `table` arg. |
103 | | - """ |
104 | | - loader = VEDALoader(db=db) |
105 | | - loading_function = load_items |
106 | | - if table == IngestionType.collections: |
107 | | - loading_function = load_collection |
108 | | - return loading_function(ingestions, loader) |
| 84 | + return loading_result |
0 commit comments