Skip to content

Commit 6fc6507

Browse files
committed
Add computed is_finalized column
1 parent a42a601 commit 6fc6507

File tree

4 files changed

+67
-7
lines changed

4 files changed

+67
-7
lines changed

map-integration/macrostrat/map_integration/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,15 @@ def change_slug(map: MapInfo, new_slug: str, dry_run: bool = False):
202202
print(f"Changed slug from {map.slug} to {new_slug}")
203203

204204

205+
@cli.command(name="update-status")
206+
def update_status():
207+
"""Update the status of all maps."""
208+
from .status import update_status_for_all_maps
209+
from .database import db
210+
211+
update_status_for_all_maps(db)
212+
213+
205214
# TODO: integrate this migration command with the main database migrations
206215
def _run_migrations(database: str = None):
207216
"""Run migrations to convert a Macrostrat v1 sources table to v2 format."""

map-integration/macrostrat/map_integration/process/__init__.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515
1616
"""
1717

18-
from ..database import db, sql_file
19-
from ..match import match_liths, match_strat_names, match_units
20-
from ..utils import IngestionCLI
21-
from ..utils.map_info import MapInfo
2218
from .extract_strat_name_candidates import extract_strat_name_candidates
2319
from .geometry import create_rgeom, create_webgeom
2420
from .insert import copy_to_maps
2521
from .legend_lookup import legend_lookup
2622
from .lookup import make_lookup
2723
from .status import processing_status
24+
from ..database import db, sql_file
25+
from ..match import match_liths, match_strat_names, match_units
26+
from ..utils import IngestionCLI
27+
from ..utils.map_info import MapInfo
2828

2929
cli = IngestionCLI(
3030
no_args_is_help=True, name="process", help="Process map data once ingested"
@@ -81,3 +81,15 @@ def legend(map: MapInfo):
8181

8282
cli.add_command(make_lookup, name="lookup", rich_help_panel="Lookup")
8383
cli.add_command(legend_lookup, name="legend-lookup", rich_help_panel="Lookup")
84+
85+
86+
def finalize_map(source: MapInfo):
87+
"""
88+
Finalize a map source by setting is_finalized to True in the sources table.
89+
90+
This is a computed parameter, so we can change its design in the future.
91+
"""
92+
db.run_query(
93+
"UPDATE maps.sources SET is_finalized = TRUE WHERE source_id = :source_id",
94+
{"source_id": source.id},
95+
)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from rich import print
2+
3+
from .utils.map_info import has_map_schema_data, MapInfo
4+
5+
6+
def update_status_for_all_maps(db):
7+
"""
8+
Check the status of a map.
9+
"""
10+
# Get all map IDs
11+
maps = db.run_query(
12+
"SELECT source_id, slug FROM maps.sources ORDER BY source_id"
13+
).all()
14+
15+
print("Checking whether all maps have data in the [cyan]maps[/] schema...")
16+
17+
for _map in maps:
18+
map_info = MapInfo(id=_map.source_id, slug=_map.slug)
19+
is_finalized = has_map_schema_data(db, map_info)
20+
21+
db.run_query(
22+
"UPDATE maps.sources SET is_finalized = :is_finalized WHERE source_id = :map_id",
23+
dict(is_finalized=is_finalized, map_id=map_info.id),
24+
)
25+
26+
_prt_val = "[green]finalized[/]" if is_finalized else "[yellow]not finalized[/]"
27+
28+
print(f"#{map_info.id} {map_info.slug} is {_prt_val}")
29+
30+
db.session.commit()

map-integration/macrostrat/map_integration/utils/map_info.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
from typing import Optional
22

3+
from macrostrat.database import Database
34
from psycopg2.sql import Identifier
45
from pydantic import BaseModel
56
from typer import Argument
67
from typing_extensions import Annotated
78

89
from macrostrat.core import app
910
from macrostrat.core.exc import MacrostratError
10-
from macrostrat.database import Database
11-
12-
from ..database import db
1311
from ._database import table_exists
12+
from ..database import db
1413

1514

1615
class _MapInfo(BaseModel):
@@ -21,6 +20,10 @@ class _MapInfo(BaseModel):
2120
url: Optional[str] = None
2221
name: Optional[str] = None
2322

23+
@property
24+
def source_id(self):
25+
return self.id
26+
2427

2528
def complete_map_slugs(incomplete: str):
2629
return (
@@ -117,3 +120,9 @@ def feature_counts(db, info: MapInfo):
117120
),
118121
).one()
119122
return res
123+
124+
125+
def has_map_schema_data(db: Database, map: MapInfo):
126+
counts = feature_counts(db, map)
127+
total = counts.n_polygons + counts.n_lines + counts.n_points
128+
return total > 0

0 commit comments

Comments
 (0)