Skip to content

Commit

Permalink
revert back the getVersion in graphql
Browse files Browse the repository at this point in the history
Signed-off-by: Huong Nguyen <[email protected]>
  • Loading branch information
Huong Nguyen committed Dec 5, 2024
1 parent 2c92a9e commit b4364d0
Show file tree
Hide file tree
Showing 8 changed files with 6 additions and 1,303 deletions.
3 changes: 0 additions & 3 deletions package/kedro_viz/api/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from kedro_viz.api.rest.responses.utils import EnhancedORJSONResponse
from kedro_viz.integrations.kedro import telemetry as kedro_telemetry

from .graphql.router import router as graphql_router
from .rest.router import router as rest_router

_HTML_DIR = Path(__file__).parent.parent.absolute() / "html"
Expand Down Expand Up @@ -63,7 +62,6 @@ def create_api_app_from_project(
"""
app = _create_base_api_app()
app.include_router(rest_router)
app.include_router(graphql_router)

# Check for html directory existence.
if Path(_HTML_DIR).is_dir():
Expand All @@ -81,7 +79,6 @@ async def favicon():
return FileResponse(_HTML_DIR / "favicon.ico")

@app.get("/")
@app.get("/experiment-tracking")
async def index():
heap_app_id = kedro_telemetry.get_heap_app_id(project_path)
heap_user_identity = kedro_telemetry.get_heap_identity()
Expand Down
152 changes: 5 additions & 147 deletions package/kedro_viz/api/graphql/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,160 +2,20 @@

from __future__ import annotations

import json
import logging
from typing import List, Optional
from packaging.version import parse

import strawberry
from graphql.validation import NoSchemaIntrospectionCustomRule
from packaging.version import parse
from strawberry import ID
from strawberry.extensions import AddValidationRules
from strawberry.tools import merge_types
from graphql.validation import NoSchemaIntrospectionCustomRule

from kedro_viz import __version__
from kedro_viz.data_access import data_access_manager
from kedro_viz.integrations.pypi import get_latest_version, is_running_outdated_version

from .serializers import (
format_run,
format_run_metric_data,
format_run_tracking_data,
format_runs,
)
from .types import (
MetricPlotDataset,
Run,
RunInput,
TrackingDataset,
TrackingDatasetGroup,
UpdateRunDetailsFailure,
UpdateRunDetailsResponse,
UpdateRunDetailsSuccess,
Version,
)
from .types import Version

logger = logging.getLogger(__name__)


@strawberry.type
class RunsQuery:
@strawberry.field(
description="Get metadata for specified run_ids from the session store"
)
def run_metadata(self, run_ids: List[ID]) -> List[Run]:
# TODO: this is hacky and should be improved together with reworking the format
# functions.
# Note we keep the order here the same as the queried run_ids.
runs = {
run.id: run
for run in format_runs(
data_access_manager.runs.get_runs_by_ids(run_ids),
data_access_manager.runs.get_user_run_details_by_run_ids(run_ids),
)
}
return [runs[run_id] for run_id in run_ids if run_id in runs]

@strawberry.field(description="Get metadata for all runs from the session store")
def runs_list(self) -> List[Run]:
all_runs = data_access_manager.runs.get_all_runs()
if not all_runs:
return []
all_run_ids = [run.id for run in all_runs]
return format_runs(
all_runs,
data_access_manager.runs.get_user_run_details_by_run_ids(all_run_ids),
)

@strawberry.field(
description="Get tracking datasets for specified group and run_ids"
)
def run_tracking_data(
self,
run_ids: List[ID],
group: TrackingDatasetGroup,
show_diff: Optional[bool] = True,
) -> List[TrackingDataset]:
tracking_dataset_models = data_access_manager.tracking_datasets.get_tracking_datasets_by_group_by_run_ids(
run_ids, group
)
# TODO: this handling of dataset.runs is hacky and should be done by e.g. a
# proper query parameter instead of filtering to right run_ids here.
# Note we keep the order here the same as the queried run_ids.

all_tracking_datasets = []

for dataset in tracking_dataset_models:
runs = {run_id: dataset.runs[run_id] for run_id in run_ids}
formatted_tracking_data = format_run_tracking_data(runs, show_diff)
if formatted_tracking_data:
tracking_data = TrackingDataset(
dataset_name=dataset.dataset_name,
dataset_type=dataset.dataset_type,
data=formatted_tracking_data,
run_ids=run_ids,
)
all_tracking_datasets.append(tracking_data)

return all_tracking_datasets

@strawberry.field(
description="Get metrics data for a limited number of recent runs"
)
def run_metrics_data(self, limit: Optional[int] = 25) -> MetricPlotDataset:
run_ids = [
run.id for run in data_access_manager.runs.get_all_runs(limit_amount=limit)
]
group = TrackingDatasetGroup.METRIC

metric_dataset_models = data_access_manager.tracking_datasets.get_tracking_datasets_by_group_by_run_ids(
run_ids, group
)

metric_data = {}
for dataset in metric_dataset_models:
metric_data[dataset.dataset_name] = dataset.runs

formatted_metric_data = format_run_metric_data(metric_data, run_ids)
return MetricPlotDataset(data=formatted_metric_data)


@strawberry.type
class Mutation:
@strawberry.mutation(description="Update run metadata")
def update_run_details(
self, run_id: ID, run_input: RunInput
) -> UpdateRunDetailsResponse:
run = data_access_manager.runs.get_run_by_id(run_id)
if not run:
return UpdateRunDetailsFailure(
id=run_id, error_message=f"Given run_id: {run_id} doesn't exist"
)
updated_run = format_run(
run.id,
json.loads(run.blob),
data_access_manager.runs.get_user_run_details(run.id),
)

# only update user run title if the input is not empty
if run_input.title is not None and bool(run_input.title.strip()):
updated_run.title = run_input.title

if run_input.bookmark is not None:
updated_run.bookmark = run_input.bookmark

if run_input.notes is not None and bool(run_input.notes.strip()):
updated_run.notes = run_input.notes

data_access_manager.runs.create_or_update_user_run_details(
run_id,
updated_run.title,
updated_run.bookmark,
updated_run.notes,
)
return UpdateRunDetailsSuccess(run=updated_run)


@strawberry.type
class VersionQuery:
@strawberry.field(description="Get the installed and latest Kedro-Viz versions")
Expand All @@ -168,11 +28,9 @@ def version(self) -> Version:
latest=str(latest_version) or "",
)


schema = strawberry.Schema(
query=(merge_types("Query", (RunsQuery, VersionQuery))),
mutation=Mutation,
query=merge_types("Query", (VersionQuery,)),
extensions=[
AddValidationRules([NoSchemaIntrospectionCustomRule]),
],
)
)
Loading

0 comments on commit b4364d0

Please sign in to comment.