Skip to content

Commit b9df0cd

Browse files
committed
Add optional monorepo information into schema and post_live_metrics
1 parent 3afc0c3 commit b9df0cd

File tree

3 files changed

+79
-33
lines changed

3 files changed

+79
-33
lines changed

src/dvc_studio_client/post_live_metrics.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ def post_live_metrics( # noqa: C901,PLR0912,PLR0913
123123
studio_token: Optional[str] = None,
124124
studio_repo_url: Optional[str] = None,
125125
studio_url: Optional[str] = None,
126+
dvc_experiment_parent_data: Optional[Dict[str, Any]] = None,
127+
subdir: Optional[str] = None,
126128
) -> Optional[bool]:
127129
"""Post `event_type` to Studio's `api/live`.
128130
@@ -239,6 +241,10 @@ def post_live_metrics( # noqa: C901,PLR0912,PLR0913
239241
if message:
240242
# Cutting the message to match the commit title length limit.
241243
body["message"] = message[:72]
244+
if subdir:
245+
body["subdir"] = subdir
246+
if dvc_experiment_parent_data:
247+
body["dvc_experiment_parent_data"] = dvc_experiment_parent_data
242248
elif event_type == "data":
243249
if step is None:
244250
logger.warning("Missing `step` in `data` event.")

src/dvc_studio_client/schema.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from voluptuous import All, Any, Exclusive, Lower, Match, Required, Schema
1+
from voluptuous import All, Any, Exclusive, Lower, Match, Optional, Required, Schema
22

33

44
def choices(*choices):
@@ -32,6 +32,8 @@ def choices(*choices):
3232
"start": BASE_SCHEMA.extend(
3333
{
3434
"message": str,
35+
Optional("dvc_experiment_parent_data"): dict,
36+
Optional("subdir"): str,
3537
},
3638
),
3739
"data": BASE_SCHEMA.extend(

tests/test_post_live_metrics.py

Lines changed: 70 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -38,59 +38,97 @@ def test_post_live_metrics_skip_on_schema_error(caplog, monkeypatch):
3838

3939

4040
def test_post_live_metrics_start_event(mocker, monkeypatch):
41-
monkeypatch.setenv(DVC_STUDIO_URL, "https://0.0.0.0")
42-
monkeypatch.setenv(DVC_STUDIO_TOKEN, "FOO_TOKEN")
43-
monkeypatch.setenv(STUDIO_REPO_URL, "FOO_REPO_URL")
41+
repo_url = "FOO_REPO_URL"
42+
studio_url = "https://0.0.0.0"
43+
studio_token = "FOO_TOKEN"
44+
baseline_sha = "f" * 40
45+
event_type = "start"
46+
name = "fooname"
47+
client = "fooclient"
48+
49+
post_url = f"{studio_url}/api/live"
50+
headers = {
51+
"Authorization": f"token {studio_token}",
52+
"Content-type": "application/json",
53+
}
54+
base_event = {
55+
"type": "start",
56+
"repo_url": "FOO_REPO_URL",
57+
"baseline_sha": baseline_sha,
58+
"name": name,
59+
"client": client,
60+
}
61+
62+
monkeypatch.setenv(DVC_STUDIO_URL, studio_url)
63+
monkeypatch.setenv(DVC_STUDIO_TOKEN, studio_token)
64+
monkeypatch.setenv(STUDIO_REPO_URL, repo_url)
4465

4566
mocked_response = mocker.MagicMock()
4667
mocked_response.status_code = 200
4768
mocked_post = mocker.patch("requests.post", return_value=mocked_response)
4869

4970
assert post_live_metrics(
50-
"start",
51-
"f" * 40,
52-
"fooname",
53-
"fooclient",
71+
event_type,
72+
baseline_sha,
73+
name,
74+
client,
5475
)
5576

5677
mocked_post.assert_called_with(
57-
"https://0.0.0.0/api/live",
58-
json={
59-
"type": "start",
60-
"repo_url": "FOO_REPO_URL",
61-
"baseline_sha": "f" * 40,
62-
"name": "fooname",
63-
"client": "fooclient",
64-
},
65-
headers={
66-
"Authorization": "token FOO_TOKEN",
67-
"Content-type": "application/json",
68-
},
78+
post_url,
79+
json=base_event,
80+
headers=headers,
6981
timeout=(30, 5),
7082
)
7183

7284
post_live_metrics(
73-
"start",
74-
"f" * 40,
75-
"fooname",
76-
"fooclient",
85+
event_type,
86+
baseline_sha,
87+
name,
88+
client,
7789
params={"params.yaml": {"foo": "bar"}},
7890
)
7991

8092
mocked_post.assert_called_with(
81-
"https://0.0.0.0/api/live",
93+
post_url,
8294
json={
83-
"type": "start",
84-
"repo_url": "FOO_REPO_URL",
85-
"baseline_sha": "f" * 40,
86-
"name": "fooname",
87-
"client": "fooclient",
95+
**base_event,
8896
"params": {"params.yaml": {"foo": "bar"}},
8997
},
90-
headers={
91-
"Authorization": "token FOO_TOKEN",
92-
"Content-type": "application/json",
98+
headers=headers,
99+
timeout=(30, 5),
100+
)
101+
102+
dvc_experiment_parent_data = {
103+
"sha": baseline_sha,
104+
"message": "test message",
105+
"title": "test message",
106+
"author": {
107+
"name": "Monrepo user",
108+
"email": "overcomplicated@iLoveMonorepos.com",
109+
},
110+
"date": "2021-09-01T12:00:00+00:00",
111+
"branch": "main",
112+
}
113+
subdir = "subdir"
114+
115+
assert post_live_metrics(
116+
"start",
117+
baseline_sha,
118+
name,
119+
client,
120+
dvc_experiment_parent_data=dvc_experiment_parent_data,
121+
subdir=subdir,
122+
)
123+
124+
mocked_post.assert_called_with(
125+
post_url,
126+
json={
127+
**base_event,
128+
"dvc_experiment_parent_data": dvc_experiment_parent_data,
129+
"subdir": subdir,
93130
},
131+
headers=headers,
94132
timeout=(30, 5),
95133
)
96134

0 commit comments

Comments
 (0)