fix(core): restore adapter command behavior
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
"""Direct PostgreSQL implementation of the DWH port."""
|
||||
|
||||
from tht.config import DatabaseConfig
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from sqlalchemy.exc import OperationalError, SQLAlchemyError
|
||||
|
||||
from tht.db import execute, sampling
|
||||
from tht.db.connection import can_create_in_schema, make_engine, ping, writable_tables
|
||||
@@ -22,8 +22,10 @@ class PostgresDwhAdapter:
|
||||
def health(self) -> DwhHealth:
|
||||
try:
|
||||
ping(self._engine)
|
||||
except OperationalError as exc:
|
||||
return DwhHealth(ok=False, detail=str(exc.orig), error_kind="connection")
|
||||
except SQLAlchemyError as exc:
|
||||
return DwhHealth(ok=False, detail=str(exc))
|
||||
return DwhHealth(ok=False, detail=str(exc), error_kind="connection")
|
||||
writable = tuple(writable_tables(self._engine, self._config.db_schema))
|
||||
can_create = can_create_in_schema(self._engine, self._config.db_schema)
|
||||
return DwhHealth(ok=True, database=self._config.database, schema=self._config.db_schema,
|
||||
|
||||
@@ -21,11 +21,12 @@ class ThothRestDwhAdapter:
|
||||
try:
|
||||
result = self._client.ping()
|
||||
except RestError as exc:
|
||||
return DwhHealth(ok=False, detail=str(exc))
|
||||
return DwhHealth(ok=False, detail=str(exc), error_kind="connection")
|
||||
ok = bool(result.get("db_connected") and result.get("schema_accessible"))
|
||||
return DwhHealth(ok=ok, detail=None if ok else str(result),
|
||||
database=self._database.database, schema=self._database.db_schema,
|
||||
endpoint=self._client.cfg.base_url, read_only=True)
|
||||
endpoint=self._client.cfg.base_url, read_only=True,
|
||||
error_kind=None if ok else "inaccessible")
|
||||
|
||||
def introspect(self) -> PhysicalSchema:
|
||||
return introspect_rest(
|
||||
|
||||
@@ -19,7 +19,12 @@ def ping_cmd(config: Path = CONFIG_OPT) -> None:
|
||||
raise typer.Exit(code=1)
|
||||
health = build_dwh(cfg).health()
|
||||
if not health.ok:
|
||||
typer.secho(f"ERRORE di connessione: {health.detail}", fg=typer.colors.RED, err=True)
|
||||
message = (
|
||||
f"ERRORE: DWH non accessibile via REST (risposta: {health.detail})."
|
||||
if health.error_kind == "inaccessible"
|
||||
else f"ERRORE di connessione: {health.detail}"
|
||||
)
|
||||
typer.secho(message, fg=typer.colors.RED, err=True)
|
||||
raise typer.Exit(code=1)
|
||||
if health.endpoint:
|
||||
typer.secho(f"OK: connesso via REST a {health.endpoint} (schema {health.schema})",
|
||||
|
||||
+27
-24
@@ -12,6 +12,30 @@ def _lsh_dir(cfg) -> Path:
|
||||
return cfg.paths.indexes / "lsh"
|
||||
|
||||
|
||||
def _extract_lsh_values(dwh, physical, annotations, limit):
|
||||
from tht.db.sampling import SkippedColumn, TruncatedColumn, is_text_type
|
||||
from tht.mschema.eligibility import effective_eligibility
|
||||
|
||||
values, skipped, truncated = {}, [], []
|
||||
for table_name, table in physical.tables.items():
|
||||
table_ann = annotations.tables.get(table_name)
|
||||
for column_name, column in table.columns.items():
|
||||
ann_col = table_ann.columns.get(column_name) if table_ann else None
|
||||
if not is_text_type(column.type) or not effective_eligibility(column, ann_col)[0]:
|
||||
continue
|
||||
try:
|
||||
distinct = dwh.distinct_values(table_name, column_name, limit=limit)
|
||||
except Exception as exc:
|
||||
skipped.append(SkippedColumn(table_name, column_name, f"errore: {exc}"))
|
||||
continue
|
||||
vals = [str(value) for value in distinct.values if value not in (None, "")]
|
||||
if vals:
|
||||
values.setdefault(table_name, {})[column_name] = vals
|
||||
if distinct.truncated:
|
||||
truncated.append(TruncatedColumn(table_name, column_name, len(vals)))
|
||||
return values, skipped, truncated
|
||||
|
||||
|
||||
@lsh_app.command("build")
|
||||
def build_cmd(config: Path = CONFIG_OPT) -> None:
|
||||
"""Costruisce l'indice LSH dai valori del database e lo salva su pickle."""
|
||||
@@ -33,31 +57,10 @@ def build_cmd(config: Path = CONFIG_OPT) -> None:
|
||||
|
||||
typer.echo("Estrazione valori (i più frequenti) dalle colonne testuali eligible...")
|
||||
from tht.adapters.factory import build_dwh
|
||||
from tht.db.sampling import SkippedColumn, TruncatedColumn, is_text_type
|
||||
from tht.mschema.eligibility import effective_eligibility
|
||||
|
||||
dwh = build_dwh(cfg)
|
||||
values: dict[str, dict[str, list[str]]] = {}
|
||||
skipped: list[SkippedColumn] = []
|
||||
truncated: list[TruncatedColumn] = []
|
||||
for table_name, table in physical.tables.items():
|
||||
table_ann = annotations.tables.get(table_name)
|
||||
for column_name, column in table.columns.items():
|
||||
ann_col = table_ann.columns.get(column_name) if table_ann else None
|
||||
if not is_text_type(column.type) or not effective_eligibility(column, ann_col)[0]:
|
||||
continue
|
||||
try:
|
||||
distinct = dwh.distinct_values(
|
||||
table_name, column_name, limit=cfg.lsh.max_values_per_column
|
||||
)
|
||||
except Exception as e:
|
||||
skipped.append(SkippedColumn(table_name, column_name, f"errore: {e}"))
|
||||
continue
|
||||
vals = [str(value) for value in distinct.values if value not in (None, "")]
|
||||
if vals:
|
||||
values.setdefault(table_name, {})[column_name] = vals
|
||||
if distinct.truncated or len(vals) >= cfg.lsh.max_values_per_column:
|
||||
truncated.append(TruncatedColumn(table_name, column_name, len(vals)))
|
||||
values, skipped, truncated = _extract_lsh_values(
|
||||
dwh, physical, annotations, cfg.lsh.max_values_per_column
|
||||
)
|
||||
n_values = sum(len(v) for t in values.values() for v in t.values())
|
||||
typer.echo(f" {n_values} valori da {sum(len(t) for t in values.values())} colonne")
|
||||
for s in skipped:
|
||||
|
||||
@@ -25,6 +25,7 @@ class DwhHealth:
|
||||
read_only: bool | None = None
|
||||
writable_tables: tuple[str, ...] = ()
|
||||
can_create: bool = False
|
||||
error_kind: str | None = None
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
|
||||
Reference in New Issue
Block a user