| 1 |
""" |
| 2 |
RD injections: This code fetches metadata on RDs (identified by their |
| 3 |
normalised source ID) from the database and injects it into parse contexts. |
| 4 |
|
| 5 |
In the Structure's completeElement method, the elements can then |
| 6 |
look for injected values using ctx.getInjected(key[, default]). |
| 7 |
""" |
| 8 |
|
| 9 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 10 |
#c |
| 11 |
#c This program is free software, covered by the GNU GPL. See the |
| 12 |
#c COPYING file in the source distribution. |
| 13 |
|
| 14 |
|
| 15 |
import contextlib |
| 16 |
|
| 17 |
from gavo import base |
| 18 |
from gavo.base import sqlsupport |
| 19 |
|
| 20 |
|
| 21 |
@contextlib.contextmanager |
| 22 |
def ignoringExpectedErrors(): |
| 23 |
"""a context manager that swallows exceptions the database throws |
| 24 |
when tables don't exist. |
| 25 |
|
| 26 |
We want that here as the tables we're querying here (for any RD that's |
| 27 |
being loaded) will not exist while bootstrapping. |
| 28 |
""" |
| 29 |
try: |
| 30 |
yield |
| 31 |
except sqlsupport.ProgrammingError as ex: |
| 32 |
if ex.pgcode=="42P01": # table not found for postgres |
| 33 |
return [] |
| 34 |
raise |
| 35 |
except base.QueryCanceledError: |
| 36 |
# Don't fail just because there's a deadlock... |
| 37 |
return [] |
| 38 |
|
| 39 |
|
| 40 |
def _getRecordsFor(rdId, fromTable, conn): |
| 41 |
"""returns (dict) records from fromTable coming from rdId. |
| 42 |
""" |
| 43 |
with ignoringExpectedErrors(): |
| 44 |
# the timeout is for when someone holds a lock (which should be rare |
| 45 |
# but happens; we don't want to fail for such a triviality). |
| 46 |
return list( |
| 47 |
conn.queryToDicts( |
| 48 |
"select * from %s where sourceRD=%%(rdId)s"%fromTable, |
| 49 |
locals(), timeout=0.25)) |
| 50 |
return [] |
| 51 |
|
| 52 |
|
| 53 |
def injectIntoContext(ctx, rdId): |
| 54 |
"""fills context's injected metadata from material from the various |
| 55 |
database tables for the RD rdId. |
| 56 |
""" |
| 57 |
with base.getTableConn() as conn: |
| 58 |
for row in _getRecordsFor(rdId, "dc.rdmeta", conn): |
| 59 |
if row["data_updated"]: |
| 60 |
ctx.inject("_dataUpdated", row["data_updated"]) |
| 61 |
if row.get("spatial"): |
| 62 |
ctx.inject("spatial_coverage", row["spatial"]) |
| 63 |
if row.get("temporal"): |
| 64 |
ctx.inject("temporal_coverage", row["temporal"]) |
| 65 |
if row.get("spectral"): |
| 66 |
ctx.inject("spectral_coverage", row["spectral"]) |
| 67 |
|
| 68 |
for row in _getRecordsFor(rdId, "dc.resources", conn): |
| 69 |
key = "resprop:%s#%s"%(row["sourcerd"], row["resid"]) |
| 70 |
ctx.inject(key, row) |
| 71 |
|
| 72 |
tablesInRD = [] |
| 73 |
for row in _getRecordsFor(rdId, "dc.tablemeta", conn): |
| 74 |
tablesInRD.append(row["tablename"]) |
| 75 |
tableId = row["tablename"].split(".")[-1] |
| 76 |
ctx.inject(f"table:{tableId}", row) |
| 77 |
|
| 78 |
for tableName in tablesInRD: |
| 79 |
tableId = tableName.split(".")[-1] |
| 80 |
with ignoringExpectedErrors(): |
| 81 |
for row in conn.queryToDicts("SELECT * FROM dc.simple_col_stats" |
| 82 |
" WHERE tableName=%(tableName)s", locals(), |
| 83 |
timeout=0.25): |
| 84 |
key = f"colstats:{tableId}:{row['column_name']}" |
| 85 |
ctx.inject(key, row) |
| 86 |
|
| 87 |
for row in conn.queryToDicts("SELECT * FROM dc.discrete_string_values" |
| 88 |
" WHERE tableName=%(tableName)s", locals(), |
| 89 |
timeout=0.25): |
| 90 |
key = f"discrete-strings:{tableId}:{row['column_name']}" |
| 91 |
ctx.inject(key, dict(zip(row["vals"], row["freqs"]))) |