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

[KED-3060] Create viz version graphql endpoint #727

Merged
merged 13 commits into from
Feb 3, 2022
1 change: 1 addition & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Please follow the established format:
## Bug fixes and other changes
- Migrate Kedro-UI buttons to Kedro-viz as Kedro-UI is now deprecated. (#716)
- Add a Husky pre-push hook. (#723)
- Create a `version` GraphQL query to get versions of Kedro-Viz. (#727)

# Release 4.3.1

Expand Down
52 changes: 40 additions & 12 deletions package/kedro_viz/api/graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
from strawberry import ID
from strawberry.asgi import GraphQL

from kedro_viz import __version__
from kedro_viz.data_access import data_access_manager
from kedro_viz.integrations.pypi import get_latest_version
from kedro_viz.models.experiments_tracking import RunModel, UserRunDetailsModel

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -69,15 +71,15 @@ def format_run(
user_run_details.notes if user_run_details and user_run_details.notes else ""
)
run = Run(
id=ID(run_id),
author="",
bookmark=bookmark,
gitBranch=git_data.get("branch") if git_data else None,
gitSha=git_data.get("commit_sha") if git_data else None,
bookmark=bookmark,
title=title,
id=ID(run_id),
notes=notes,
timestamp=run_blob["session_id"],
runCommand=run_blob["cli"]["command_path"],
timestamp=run_blob["session_id"],
title=title,
)
return run

Expand Down Expand Up @@ -119,6 +121,16 @@ def get_runs(run_ids: List[ID]) -> List[Run]:
)


def get_version() -> Version:
tynandebold marked this conversation as resolved.
Show resolved Hide resolved
"""Get the user's installed Viz version and the latest version on PyPI.
Returns:
the currently installed and most-recent released version of Viz.
"""
latest_version = get_latest_version()
version = Version(installed=__version__, latest=latest_version)
return version


def get_all_runs() -> List[Run]:
"""Get all runs from the session store.

Expand Down Expand Up @@ -247,24 +259,32 @@ def get_run_tracking_data(
class Run:
"""Run object format"""

id: ID
title: str
timestamp: str
author: Optional[str]
bookmark: Optional[bool]
gitBranch: Optional[str]
gitSha: Optional[str]
bookmark: Optional[bool]
id: ID
notes: Optional[str]
runCommand: Optional[str]
timestamp: str
title: str


@strawberry.type
class TrackingDataset:
"""TrackingDataset object to structure tracking data for a Run."""

data: Optional[JSONObject]
datasetName: Optional[str]
datasetType: Optional[str]
data: Optional[JSONObject]


@strawberry.type
class Version:
"""The installed and latest Kedro Viz versions."""

installed: str
latest: str


@strawberry.type
Expand Down Expand Up @@ -295,17 +315,25 @@ class Query:

@strawberry.field
def run_metadata(self, run_ids: List[ID]) -> List[Run]:
"""Query to get data for specific runs from the session store"""
"""Query to get data for specific run metadata from the session store"""
return get_runs(run_ids)

@strawberry.field
def runs_list(self) -> List[Run]:
"""Query to get data for all the runs from the session store"""
return get_all_runs()

@strawberry.field
def run_tracking_data(
self, run_ids: List[ID], show_diff: Optional[bool] = False
) -> List[TrackingDataset]:
"""Query to get data for specific runs from the session store"""
return get_run_tracking_data(run_ids, show_diff)

runs_list: List[Run] = strawberry.field(resolver=get_all_runs)
@strawberry.field
def version(self) -> Version:
tynandebold marked this conversation as resolved.
Show resolved Hide resolved
"""Query to get Kedro-Viz version"""
return get_version()


schema = strawberry.Schema(query=Query, subscription=Subscription)
Expand All @@ -316,8 +344,8 @@ class RunInput:
"""Run input to update bookmark, title and notes"""

bookmark: Optional[bool] = None
title: Optional[str] = None
notes: Optional[str] = None
title: Optional[str] = None


@strawberry.type
Expand Down
6 changes: 3 additions & 3 deletions package/kedro_viz/launchers/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ def commands():
)
def viz(host, port, browser, load_file, save_file, pipeline, env, autoreload):
"""Visualise a Kedro pipeline using Kedro viz."""
current_version = VersionInfo.parse(__version__)
installed_version = VersionInfo.parse(__version__)
latest_version = get_latest_version()

if latest_version is not None and current_version < latest_version:
if latest_version is not None and installed_version < latest_version:
click.echo(
click.style(
"WARNING: You are using an old version of Kedro Viz. "
f"You are using version {current_version}; "
f"You are using version {installed_version}; "
f"however, version {latest_version} is now available.\n"
"You should consider upgrading via the `pip install -U kedro-viz` command.\n"
"You can view the complete changelog at "
Expand Down
14 changes: 14 additions & 0 deletions package/tests/test_api/test_graphql/test_queries.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import pytest

from kedro_viz import __version__
from kedro_viz.integrations.pypi import get_latest_version


class TestQueryNoSessionStore:
def test_graphql_run_list_endpoint(self, client):
Expand Down Expand Up @@ -55,6 +58,17 @@ def test_graphql_runs_metadata_endpoint(self, example_run_ids, client):
"data": {"runMetadata": [{"id": example_run_ids[0], "bookmark": True}]}
}

def test_graphql_version_endpoint(self, client):
response = client.post(
"/graphql",
json={"query": "{version {installed latest}}"},
)
assert response.json() == {
"data": {
"version": {"installed": __version__, "latest": get_latest_version()}
}
}

def test_run_tracking_data_query(
self,
example_run_ids,
Expand Down
6 changes: 3 additions & 3 deletions package/tests/test_launchers/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def test_kedro_viz_command_run_server(command_options, run_server_args, mocker):


def test_kedro_viz_command_should_log_outdated_version(mocker, mock_http_response):
current_version = VersionInfo.parse(__version__)
mock_version = f"{current_version.major + 1}.0.0"
installed_version = VersionInfo.parse(__version__)
mock_version = f"{installed_version.major + 1}.0.0"
requests_get = mocker.patch("requests.get")
requests_get.return_value = mock_http_response(
data={"info": {"version": mock_version}}
Expand All @@ -77,7 +77,7 @@ def test_kedro_viz_command_should_log_outdated_version(mocker, mock_http_respons

mock_click_echo.assert_called_once_with(
"\x1b[33mWARNING: You are using an old version of Kedro Viz. "
f"You are using version {current_version}; "
f"You are using version {installed_version}; "
f"however, version {mock_version} is now available.\n"
"You should consider upgrading via the `pip install -U kedro-viz` command.\n"
"You can view the complete changelog at "
Expand Down
20 changes: 13 additions & 7 deletions src/apollo/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,38 @@ type Mutation {
}

type Query {
runsList: [Run!]!
runMetadata(runIds: [ID!]!): [Run!]!
runsList: [Run!]!
runTrackingData(runIds: [ID!]!, showDiff: Boolean = false): [TrackingDataset!]!
version: Version!
}

type Run {
id: ID!
title: String!
timestamp: String!
author: String
bookmark: Boolean
gitBranch: String
gitSha: String
bookmark: Boolean
id: ID!
notes: String
runCommand: String
timestamp: String!
title: String!
}

input RunInput {
bookmark: Boolean = null
title: String = null
notes: String = null
title: String = null
}

type Subscription {
runsAdded: [Run!]!
}

type TrackingDataset {
data: JSONObject
datasetName: String
datasetType: String
data: JSONObject
}

type UpdateRunDetailsFailure {
Expand All @@ -49,3 +50,8 @@ union UpdateRunDetailsResponse = UpdateRunDetailsSuccess | UpdateRunDetailsFailu
type UpdateRunDetailsSuccess {
run: Run!
}

type Version {
installed: String!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

naming this as Current might be more relevant as I understand there can be multiple versions of a package installed in a python environment ( @limdauto @AntonyMilneQB can correct me if I'm wrong)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there can't be multiple versions of a package installed in the same python environment. There can be many environments.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarifying @limdauto - on that note, I've heard that it is possible to install multiple versions of a package by specifying it to be installed in a different directory other than the default site-packages? ( not that it is a common use case at all) Again, I'm no python dev, so you know best 😄

On the subject, Current version to me sounds more intuitive as we normally refer to 'current' and 'latest' versions - curious to hear your thoughts.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I didn't like current is because I didn't know what it was describing, and it seemed like it could describe both. As in: is it the current version the user has installed? Or is it the current version that's released on PyPI?

With installed and latest I think it's more clear. It might even be clearer if it's installed and latestRelease.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also don't like current for the same reason as @tynandebold mentioned. installed and latest make most sense to me.

With that said, we are already using current as the variable name where we do the CLI nudge. So whatever we go for, let's be consistent so we don't have current, latest and installed in the codebase.

Copy link
Contributor

@antonymilne antonymilne Feb 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@studioswong you could indeed install different version of the same Python package to different locations. But when we do from kedro_viz import __version__, it will be well-defined and import from only one installation of the package (the precedence of different locations is given by sys.path in Python). So it does make sense to refer to the "installed Python package" as a unique entity.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sticking with installed and latest. I'll change in the cli files, too.

latest: String!
}