Skip to content

Commit

Permalink
chore: propagate cli args and flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Vixtir committed Sep 12, 2023
1 parent a2f26fd commit 9a3616b
Show file tree
Hide file tree
Showing 21 changed files with 378 additions and 525 deletions.
52 changes: 19 additions & 33 deletions odd_dbt/app.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import subprocess
import traceback
from pathlib import Path
from typing import Optional

import typer
from dbt.cli.params import default_profiles_dir, default_project_dir
from odd_models.api_client.v2.odd_api_client import Client
from oddrn_generator import DbtGenerator

from odd_dbt import errors
from odd_dbt import get_version
from odd_dbt.context import DbtContext
from odd_dbt.logger import logger
from odd_dbt.mapper.dbt_test import DbtTestMapper
from odd_dbt.mapper.test_results import DbtTestMapper
from odd_dbt.service.dbt import run_tests, CliArgs, get_context

app = typer.Typer(
short_help="Run dbt tests and inject results to ODD platform",
Expand All @@ -20,8 +21,8 @@

@app.command()
def test(
project_dir: Path = typer.Option(default=Path.cwd()),
profiles_dir: Path = typer.Option(default=None),
project_dir: Path = typer.Option(default=default_project_dir()),
profiles_dir: Path = typer.Option(default=default_profiles_dir()),
target: Optional[str] = typer.Option(default=None),
profile: Optional[str] = typer.Option(default=None),
platform_host: str = typer.Option(..., "--host", "-h", envvar="ODD_PLATFORM_HOST"),
Expand All @@ -34,56 +35,41 @@ def test(
dbt_host: str = typer.Option(default="localhost"),
):
logger.info(f"Used OpenDataDiscovery dbt version: {get_version()}")
logger.info("Start dbt test process")
cli_args = CliArgs(
project_dir=project_dir,
profiles_dir=profiles_dir,
profile=profile,
target=target,
threads=1,
vars={},
)
try:
logger.info(
f"Run dbt process with {project_dir=}, {profiles_dir=}, {target=}, {profile=}"
)

args = [
"dbt",
"test",
]

if project_dir:
args.extend(["--project-dir", project_dir])

if profiles_dir:
args.extend(["--profiles-dir", profiles_dir])

if profile:
args.extend(["--profile", profile])

if target:
args.extend(["--target", target])

process = subprocess.run(args)

if process.returncode >= 2:
logger.error("Dbt test command failed")
raise typer.Exit(2)
run_tests(cli_args=cli_args)
context = get_context(cli_args=cli_args)

client = Client(host=platform_host, token=platform_token)

generator = DbtGenerator(host_settings=dbt_host)

dbt_data_source_oddrn = dbt_data_source_oddrn
if not dbt_data_source_oddrn:
dbt_data_source_oddrn = generator.get_data_source_oddrn()
logger.info(f"Creating data source for dbt: ODDRN={dbt_data_source_oddrn}")
client.create_data_source(
data_source_name="dbt",
data_source_oddrn=dbt_data_source_oddrn,
)
context = DbtContext(project_dir=project_dir, profile=profile, target=target)

logger.info("Got DBT test context. Start mapping...")
data_entities = DbtTestMapper(context=context, generator=generator).map()

logger.info("Mapping finished. Start injecting...")
client.ingest_data_entity_list(data_entities=data_entities)

logger.info("Injecting finished.")
except errors.DbtTestCommandError as e:
logger.error(e)
typer.Exit(2)
except Exception as e:
logger.debug(traceback.format_exc())
logger.error(e)
Expand Down
75 changes: 0 additions & 75 deletions odd_dbt/context.py

This file was deleted.

2 changes: 1 addition & 1 deletion odd_dbt/models/__init__.py → odd_dbt/domain/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .manifest import Manifest
from .profile import Profile
from .credentials import Credentials
from .project import Project
from .result import Result
from .run_results import RunResults
23 changes: 23 additions & 0 deletions odd_dbt/domain/cli_args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from dataclasses import dataclass
from pathlib import Path
from typing import Optional


@dataclass
class CliArgs:
project_dir: Path
profiles_dir: Path
profile: Optional[str]
target: Optional[str]
threads: Optional[int]
vars: Optional[dict[str, str]]


@dataclass
class FlagsArgs:
project_dir: Path
profiles_dir: Path
profile: Optional[str] = None
target: Optional[str] = None
vars: Optional[dict[str, str]] = None
use_colors: Optional[bool] = True
46 changes: 46 additions & 0 deletions odd_dbt/domain/context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from dbt.config.runtime import RuntimeConfig

from odd_dbt.domain import Manifest, Credentials, RunResults
from odd_dbt.domain.cli_args import CliArgs
from odd_dbt.errors import DbtInternalError
from odd_dbt.utils import load_json


class DbtContext:
def __init__(self, cli_args: CliArgs):
try:
self._config = RuntimeConfig.from_args(cli_args)
self.target_path = cli_args.project_dir / self._config.target_path
except Exception as e:
raise DbtInternalError(f"Failed getting dbt context: {e}") from e

@property
def adapter_type(self) -> str:
return self._config.get_metadata().adapter_type

@property
def catalog(self):
if (catalog := self.target_path / "catalog.json").is_file():
return load_json(catalog)

return None

@property
def credentials(self) -> Credentials:
return Credentials(**self._config.credentials.to_dict())

@property
def manifest(self) -> Manifest:
return Manifest(self.target_path / "manifest.json")

@property
def run_results(self) -> RunResults:
return RunResults(self.target_path / "run_results.json")

@property
def results(self) -> list[dict]:
return self.run_results.results

@property
def invocation_id(self) -> str:
return self.run_results.invocation_id
7 changes: 7 additions & 0 deletions odd_dbt/domain/credentials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Credentials:
def __init__(self, **config) -> None:
del config["password"]
self._config: dict[str, str] = config

def __getitem__(self, item):
return self._config.get(item)
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 6 additions & 7 deletions odd_dbt/models/run_results.py → odd_dbt/domain/run_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@
from funcy import lmap

from odd_dbt.utils import load_json

from .result import Result


class RunResults:
def __init__(self, file: Path) -> None:
self._run_results = load_json(file)
self._results = load_json(file)

@cached_property
def results(self):
return lmap(Result, self._run_results.get("results", []))
def results(self) -> list[Result]:
return lmap(Result, self._results.get("results", []))

@property
def invocation_id(self):
return self._run_results["metadata"]["invocation_id"]
def invocation_id(self) -> str:
return self._results["metadata"]["invocation_id"]

@property
def profiles_dir(self) -> Path:
return Path(self._run_results["args"]["profiles_dir"])
return Path(self._results["args"]["profiles_dir"])
11 changes: 7 additions & 4 deletions odd_dbt/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ def __init__(self, original_message: str, *args: object) -> None:
super().__init__(original_message, *args)


class CantParseReason(Exception):
class ParsingReasonError(Exception):
...


class ProfilerRenderError(Exception):
def __str__(self):
return "Couldn't parse profile config"
class ProfileError(Exception):
...


class DbtTestCommandError(Exception):
...
4 changes: 3 additions & 1 deletion odd_dbt/logger.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from odd_models.logger import logger
from odd_models.logger import logger

logger = logger
65 changes: 0 additions & 65 deletions odd_dbt/mapper/data_source.py

This file was deleted.

Loading

0 comments on commit 9a3616b

Please sign in to comment.