Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create org task #449

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions ddpui/api/data_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""All the master data api cane found here"""

from ninja import NinjaAPI
from ninja.errors import HttpError
from django.forms.models import model_to_dict

from ninja.errors import ValidationError
from ninja.responses import Response
from pydantic.error_wrappers import ValidationError as PydanticValidationError


from ddpui import auth
from ddpui.models.tasks import Task
from ddpui.utils.custom_logger import CustomLogger
from ddpui.utils.constants import (
TASK_DBTRUN,
TASK_GITPULL,
)
from ddpui.ddpdbt import dbt_service

dataapi = NinjaAPI(urls_namespace="master_data")
# http://127.0.0.1:8000/api/docs


logger = CustomLogger("ddpui")


@dataapi.exception_handler(ValidationError)
def ninja_validation_error_handler(request, exc): # pylint: disable=unused-argument
"""
Handle any ninja validation errors raised in the apis
These are raised during request payload validation
exc.errors is correct
"""
return Response({"detail": exc.errors}, status=422)


@dataapi.exception_handler(PydanticValidationError)
def pydantic_validation_error_handler(
request, exc: PydanticValidationError
): # pylint: disable=unused-argument
"""
Handle any pydantic errors raised in the apis
These are raised during response payload validation
exc.errors() is correct
"""
print(exc)
return Response({"detail": exc.errors()}, status=500)


@dataapi.exception_handler(Exception)
def ninja_default_error_handler(
request, exc: Exception # skipcq PYL-W0613
): # pylint: disable=unused-argument
"""Handle any other exception raised in the apis"""
logger.info(exc)
return Response({"detail": "something went wrong"}, status=500)


@dataapi.get("/tasks/", auth=auth.CanManagePipelines())
def get_tasks(request):
"""Fetch master list of tasks related to transformation"""
tasks = [
model_to_dict(task, exclude=["id"])
for task in Task.objects.filter(type__in=["dbt", "git"]).all()
]
return tasks


@dataapi.get("/tasks/{slug}/config/", auth=auth.CanManagePipelines())
def get_task_config(request, slug):
"""Get task config which details about the parameters that can be added/used while running it"""
task = Task.objects.filter(slug=slug).first()

if not task:
raise HttpError(404, "Task not found")

return dbt_service.task_config_params(task)
15 changes: 15 additions & 0 deletions ddpui/ddpdbt/dbt_service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import shutil
from ddpui.models.org_user import Org
from ddpui.models.tasks import Task
from ddpui.models.org import OrgPrefectBlock
from ddpui.ddpprefect import prefect_service
from ddpui.ddpprefect import DBTCORE, SHELLOPERATION
Expand Down Expand Up @@ -35,3 +36,17 @@
shellblock.delete()

secretsmanager.delete_github_token(org)


def task_config_params(task: Task):
"""Return the config dictionary to setup parameters on this task"""

# dbt task config parameters
TASK_CONIF_PARAM = {

Check warning on line 45 in ddpui/ddpdbt/dbt_service.py

View check run for this annotation

Codecov / codecov/patch

ddpui/ddpdbt/dbt_service.py#L45

Added line #L45 was not covered by tests
"dbt-deps": {"flags": ["upgrade"], "options": ["add-package"]},
"dbt-run": {"flags": ["full-refresh"], "options": ["select", "exclude"]},
"dbt-test": {"flags": [], "options": ["select", "exclude"]},
"dbt-seed": {"flags": [], "options": ["select"]},
}

return TASK_CONIF_PARAM[task.slug] if task.slug in TASK_CONIF_PARAM else None

Check warning on line 52 in ddpui/ddpdbt/dbt_service.py

View check run for this annotation

Codecov / codecov/patch

ddpui/ddpdbt/dbt_service.py#L52

Added line #L52 was not covered by tests
2 changes: 2 additions & 0 deletions ddpui/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
from ddpui.api.webhook_api import webhookapi
from ddpui.api.superset_api import supersetapi
from ddpui.api.pipeline_api import pipelineapi
from ddpui.api.data_api import dataapi
from ddpui.healthcheck import healthcheck

urlpatterns = [
# path("admin/", admin.site.urls), # Uncomment if you want to use django-admin app
path("api/dashboard/", dashboardapi.urls),
path("api/airbyte/", airbyteapi.urls),
path("api/data/", dataapi.urls),
path("api/dbt/", dbtapi.urls),
path("api/prefect/tasks/", orgtaskapi.urls),
path("api/prefect/", pipelineapi.urls),
Expand Down
Loading