exp push: push experiments revs to studio (#9160)

* exp push: push experiments revs to studio

* log json data that is sent
This commit is contained in:
skshetry
2023-03-16 11:24:50 +05:45
committed by GitHub
parent 990e834f1d
commit 58029e9719
3 changed files with 76 additions and 3 deletions
+1
View File
@@ -300,6 +300,7 @@ SCHEMA = {
"feature": FeatureSchema(
{
Optional("machine", default=False): Bool,
Optional("push_exp_to_studio", default=False): Bool,
},
),
"plots": {
+48 -3
View File
@@ -15,10 +15,12 @@ from .utils import exp_commits, exp_refs, exp_refs_by_baseline, resolve_name
logger = logging.getLogger(__name__)
STUDIO_ENDPOINT = ""
@locked
@scm_context
def push( # noqa: C901
def push( # noqa: C901, PLR0912
repo,
git_remote: str,
exp_names: Union[Iterable[str], str],
@@ -29,6 +31,8 @@ def push( # noqa: C901
push_cache: bool = False,
**kwargs,
) -> Iterable[str]:
from dvc.utils import env2bool
exp_ref_set: Set["ExpRefInfo"] = set()
if all_commits:
exp_ref_set.update(exp_refs(repo.scm))
@@ -69,7 +73,48 @@ def push( # noqa: C901
push_result[SyncStatus.UP_TO_DATE] + push_result[SyncStatus.SUCCESS]
)
_push_cache(repo, push_cache_ref, **kwargs)
return [ref.name for ref in push_result[SyncStatus.SUCCESS]]
refs = push_result[SyncStatus.SUCCESS]
config = repo.config
if refs and config["feature"]["push_exp_to_studio"] and not env2bool("DVC_TEST"):
from dvc_studio_client.post_live_metrics import get_studio_token_and_repo_url
token, repo_url = get_studio_token_and_repo_url()
if token and repo_url:
logger.debug(
"pushing experiments %s to Studio",
", ".join(ref.name for ref in refs),
)
_notify_studio([str(ref) for ref in refs], repo_url, token)
return [ref.name for ref in refs]
def _notify_studio(
refs: List[str],
repo_url: str,
token: str,
endpoint: Optional[str] = None,
):
if not refs:
return
import os
import requests
from requests.adapters import HTTPAdapter
endpoint = endpoint or os.getenv("STUDIO_ENDPOINT", STUDIO_ENDPOINT)
assert endpoint is not None
session = requests.Session()
session.mount(endpoint, HTTPAdapter(max_retries=3))
json = {"repo_url": repo_url, "client": "dvc", "refs": refs}
logger.trace("Sending %s to %s", json, endpoint) # type: ignore[attr-defined]
headers = {"Authorization": f"token {token}"}
resp = session.post(endpoint, json=json, headers=headers, timeout=5)
resp.raise_for_status()
def _push(
@@ -83,7 +128,7 @@ def _push(
from dvc.scm import GitAuthError
refspec_list = [f"{exp_ref}:{exp_ref}" for exp_ref in refs]
logger.debug("git push experiment '%s' -> '%s'", refspec_list, git_remote)
logger.debug("git push experiment %s -> '%s'", refspec_list, git_remote)
with TqdmGit(desc="Pushing git refs") as pbar:
try:
+27
View File
@@ -0,0 +1,27 @@
from requests import Response
from dvc.repo.experiments.push import STUDIO_ENDPOINT, _notify_studio
def test_notify_studio_for_exp_push(mocker):
valid_response = Response()
valid_response.status_code = 200
mock_post = mocker.patch("requests.Session.post", return_value=valid_response)
_notify_studio(
["ref1", "ref2", "ref3"],
"[email protected]:iterative/dvc.git",
"TOKEN",
)
assert mock_post.called
assert mock_post.call_args == mocker.call(
STUDIO_ENDPOINT,
json={
"repo_url": "[email protected]:iterative/dvc.git",
"client": "dvc",
"refs": ["ref1", "ref2", "ref3"],
},
headers={"Authorization": "token TOKEN"},
timeout=5,
)