Skip to content

Commit f3e651c

Browse files
committed
Implement setruntag
1 parent 5452ee2 commit f3e651c

File tree

5 files changed

+133
-38
lines changed

5 files changed

+133
-38
lines changed

src/MLFlowClient.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,23 @@ export
5757
getexperiment,
5858
createexperiment,
5959
deleteexperiment,
60-
updateexperiment,
6160
setexperimenttag,
61+
updateexperiment,
6262
restoreexperiment,
6363
searchexperiments,
6464
getexperimentbyname
6565

6666
include("services/run.jl")
6767
export
68+
getrun,
6869
createrun,
6970
deleterun,
70-
restorerun,
71-
getrun
71+
setruntag,
72+
restorerun
7273
include("services/loggers.jl")
7374
export
74-
logmetric,
7575
logbatch,
76-
loginputs
76+
loginputs,
77+
logmetric
7778

7879
end

src/services/experiment.jl

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ experiment are also deleted.
8585
# Returns
8686
`true` if successful. Otherwise, raises exception.
8787
"""
88-
function deleteexperiment(instance::MLFlow, experiment_id::String)
88+
function deleteexperiment(instance::MLFlow, experiment_id::String)::Bool
8989
mlfpost(instance, "experiments/delete"; experiment_id=experiment_id)
9090
return true
9191
end
92-
deleteexperiment(instance::MLFlow, experiment_id::Integer) =
92+
deleteexperiment(instance::MLFlow, experiment_id::Integer)::Bool =
9393
deleteexperiment(instance, string(experiment_id))
94-
deleteexperiment(instance::MLFlow, experiment::Experiment) =
94+
deleteexperiment(instance::MLFlow, experiment::Experiment)::Bool =
9595
deleteexperiment(instance, experiment.experiment_id)
9696

9797
"""
@@ -110,13 +110,13 @@ underlying artifacts associated with experiment are also restored.
110110
# Returns
111111
`true` if successful. Otherwise, raises exception.
112112
"""
113-
function restoreexperiment(instance::MLFlow, experiment_id::String)
113+
function restoreexperiment(instance::MLFlow, experiment_id::String)::Bool
114114
mlfpost(instance, "experiments/restore"; experiment_id=experiment_id)
115115
return true
116116
end
117-
restoreexperiment(instance::MLFlow, experiment_id::Integer) =
117+
restoreexperiment(instance::MLFlow, experiment_id::Integer)::Bool =
118118
restoreexperiment(instance, string(experiment_id))
119-
restoreexperiment(instance::MLFlow, experiment::Experiment) =
119+
restoreexperiment(instance::MLFlow, experiment::Experiment)::Bool =
120120
restoreexperiment(instance, experiment.experiment_id)
121121

122122
"""
@@ -138,15 +138,17 @@ The new name must be unique.
138138
`true` if successful. Otherwise, raises exception.
139139
"""
140140
function updateexperiment(instance::MLFlow, experiment_id::String,
141-
new_name::String)
141+
new_name::String)::Bool
142142
mlfpost(instance, "experiments/update"; experiment_id=experiment_id,
143143
new_name=new_name)
144144
return true
145145
end
146-
updateexperiment(instance::MLFlow, experiment_id::Integer, new_name::String) =
146+
updateexperiment(instance::MLFlow, experiment_id::Integer,
147+
new_name::String)::Bool =
147148
updateexperiment(instance, string(experiment_id), new_name)
148-
updateexperiment(instance::MLFlow, experiment::Experiment, new_name::String) =
149-
updateexperiment(instance, experiment.experiment_id, new_name::String)
149+
updateexperiment(instance::MLFlow, experiment::Experiment,
150+
new_name::String)::Bool =
151+
updateexperiment(instance, experiment.experiment_id, new_name)
150152

151153
"""
152154
searchexperiments(instance::MLFlow; max_results::Int64=20000,
@@ -201,14 +203,19 @@ Set a tag on an experiment. Experiment tags are metadata that can be updated.
201203
- `experiment_id`: ID of the experiment under which to log the tag.
202204
- `key`: Name of the tag.
203205
- `value`: String value of the tag being logged.
206+
207+
# Returns
208+
`true` if successful. Otherwise, raises exception.
204209
"""
205-
setexperimenttag(instance::MLFlow, experiment_id::String, key::String,
206-
value::String) =
210+
function setexperimenttag(instance::MLFlow, experiment_id::String, key::String,
211+
value::String)::Bool
207212
mlfpost(instance, "experiments/set-experiment-tag";
208213
experiment_id=experiment_id, key=key, value=value)
214+
return true
215+
end
209216
setexperimenttag(instance::MLFlow, experiment_id::Integer, key::String,
210-
value::String) =
217+
value::String)::Bool =
211218
setexperimenttag(instance, string(experiment_id), key, value)
212219
setexperimenttag(instance::MLFlow, experiment::Experiment, key::String,
213-
value::String) =
220+
value::String)::Bool =
214221
setexperimenttag(instance, experiment.experiment_id, key, value)

src/services/loggers.jl

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,22 @@ represent ML model accuracy. A metric can be logged multiple times.
1717
- `value`: Double value of the metric being logged.
1818
- `timestamp`: Unix timestamp in milliseconds at the time metric was logged.
1919
- `step`: Step at which to log the metric.
20+
21+
# Returns
22+
`true` if successful. Otherwise, raises exception.
2023
"""
21-
logmetric(instance::MLFlow, run_id::String, key::String, value::Float64;
22-
timestamp::Int64=round(Int, now() |> datetime2unix),
23-
step::Union{Int64, Missing}=missing) =
24-
mlfpost(instance, "runs/log-metric"; run_id=run_id, key=key, value=value, timestamp=timestamp, step=step)
24+
function logmetric(instance::MLFlow, run_id::String, key::String,
25+
value::Float64; timestamp::Int64=round(Int, now() |> datetime2unix),
26+
step::Union{Int64, Missing}=missing)::Bool
27+
mlfpost(instance, "runs/log-metric"; run_id=run_id, key=key, value=value,
28+
timestamp=timestamp, step=step)
29+
return true
30+
end
2531
logmetric(instance::MLFlow, run::Run, key::String, value::Float64;
2632
timestamp::Int64=round(Int, now() |> datetime2unix),
27-
step::Union{Int64, Missing}=missing) =
28-
logmetric(instance, run.info.run_id, key, value; timestamp=timestamp, step=step)
33+
step::Union{Int64, Missing}=missing)::Bool =
34+
logmetric(instance, run.info.run_id, key, value; timestamp=timestamp,
35+
step=step)
2936

3037
"""
3138
logbatch(instance::MLFlow, run_id::String;
@@ -38,18 +45,33 @@ Log a batch of metrics, params, and tags for a run. In case of error, partial
3845
data may be written.
3946
4047
For more information about this function, check [MLFlow official documentation](https://mlflow.org/docs/latest/rest-api.html#log-batch).
48+
49+
# Arguments
50+
- `instance`: [`MLFlow`](@ref) configuration.
51+
- `run_id`: ID of the run to log under.
52+
- `metrics`: Metrics to log.
53+
- `params`: Params to log.
54+
- `tags`: Tags to log.
55+
56+
**Note**: A single request can contain up to 1000 metrics, and up to 1000
57+
metrics, params, and tags in total.
58+
59+
# Returns
60+
`true` if successful. Otherwise, raises exception.
4161
"""
42-
logbatch(instance::MLFlow, run_id::String;
62+
function logbatch(instance::MLFlow, run_id::String;
4363
metrics::MLFlowUpsertData{Metric}=Metric[],
4464
params::MLFlowUpsertData{Param}=Param[],
45-
tags::MLFlowUpsertData{Tag}=Tag[]) =
65+
tags::MLFlowUpsertData{Tag}=Tag[])::Bool
4666
mlfpost(instance, "runs/log-batch"; run_id=run_id,
4767
metrics=parse(Metric, metrics), params=parse(Param, params),
4868
tags=parse(Tag, tags))
69+
return true
70+
end
4971
logbatch(instance::MLFlow, run::Run;
5072
metrics::MLFlowUpsertData{Metric}=Metric[],
5173
params::MLFlowUpsertData{Param}=Param[],
52-
tags::MLFlowUpsertData{Tag}=Tag[]) =
74+
tags::MLFlowUpsertData{Tag}=Tag[])::Bool =
5375
logbatch(instance, run.info.run_id; metrics=metrics, params=params,
5476
tags=tags)
5577

@@ -61,8 +83,14 @@ logbatch(instance::MLFlow, run::Run;
6183
- `instance`: [`MLFlow`](@ref) configuration.
6284
- `run_id`: ID of the run to log under This field is required.
6385
- `datasets`: Dataset inputs.
86+
87+
# Returns
88+
`true` if successful. Otherwise, raises exception.
6489
"""
65-
loginputs(instance::MLFlow, run_id::String, datasets::Array{DatasetInput}) =
90+
function loginputs(instance::MLFlow, run_id::String,
91+
datasets::Array{DatasetInput})::Bool
6692
mlfpost(instance, "runs/log-inputs"; run_id=run_id, datasets=datasets)
67-
loginputs(instance::MLFlow, run::Run, datasets::Array{DatasetInput}) =
93+
return true
94+
end
95+
loginputs(instance::MLFlow, run::Run, datasets::Array{DatasetInput})::Bool =
6896
loginputs(instance, run.info.run_id, datasets)

src/services/run.jl

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,21 @@ An instance of type [`Run`](@ref).
2020
function createrun(instance::MLFlow, experiment_id::String;
2121
run_name::Union{String, Missing}=missing,
2222
start_time::Union{Int64, Missing}=missing,
23-
tags::MLFlowUpsertData{Tag}=Tag[])
23+
tags::MLFlowUpsertData{Tag}=Tag[])::Run
2424
result = mlfpost(instance, "runs/create"; experiment_id=experiment_id,
2525
run_name=run_name, start_time=start_time, tags=parse(Tag, tags))
2626
return result["run"] |> Run
2727
end
2828
createrun(instance::MLFlow, experiment_id::Integer;
2929
run_name::Union{String, Missing}=missing,
3030
start_time::Union{Integer, Missing}=missing,
31-
tags::MLFlowUpsertData{Tag}=Tag[]) =
31+
tags::MLFlowUpsertData{Tag}=Tag[])::Run =
3232
createrun(instance, string(experiment_id); run_name=run_name,
3333
start_time=start_time, tags=tags)
3434
createrun(instance::MLFlow, experiment::Experiment;
3535
run_name::Union{String, Missing}=missing,
3636
start_time::Union{Integer, Missing}=missing,
37-
tags::MLFlowUpsertData{Tag}=Tag[]) =
37+
tags::MLFlowUpsertData{Tag}=Tag[])::Run =
3838
createrun(instance, string(experiment.experiment_id); run_name=run_name,
3939
start_time=start_time, tags=tags)
4040

@@ -51,11 +51,12 @@ Mark a run for deletion.
5151
# Returns
5252
`true` if successful. Otherwise, raises exception.
5353
"""
54-
function deleterun(instance::MLFlow, run_id::String)
54+
function deleterun(instance::MLFlow, run_id::String)::Bool
5555
mlfpost(instance, "runs/delete"; run_id=run_id)
5656
return true
5757
end
58-
deleterun(instance::MLFlow, run::Run) = deleterun(instance, run.info.run_id)
58+
deleterun(instance::MLFlow, run::Run)::Bool =
59+
deleterun(instance, run.info.run_id)
5960

6061
"""
6162
restorerun(instance::MLFlow, run_id::String)
@@ -70,11 +71,12 @@ Restore a deleted run.
7071
# Returns
7172
`true` if successful. Otherwise, raises exception.
7273
"""
73-
function restorerun(instance::MLFlow, run_id::String)
74+
function restorerun(instance::MLFlow, run_id::String)::Bool
7475
mlfpost(instance, "runs/restore"; run_id=run_id)
7576
return true
7677
end
77-
restorerun(instance::MLFlow, run::Run) = restorerun(instance, run.info.run_id)
78+
restorerun(instance::MLFlow, run::Run)::Bool =
79+
restorerun(instance, run.info.run_id)
7880

7981
"""
8082
getrun(instance::MLFlow, run_id::String)
@@ -91,7 +93,31 @@ return the maximum of these values.
9193
# Returns
9294
An instance of type [`Run`](@ref).
9395
"""
94-
function getrun(instance::MLFlow, run_id::String)
96+
function getrun(instance::MLFlow, run_id::String)::Run
9597
result = mlfget(instance, "runs/get"; run_id=run_id)
9698
return result["run"] |> Run
9799
end
100+
101+
"""
102+
setruntag(instance::MLFlow, run_id::String, key::String, value::String)
103+
setruntag(instance::MLFlow, run::Run, key::String, value::String)
104+
105+
Set a tag on a run. Tags are run metadata that can be updated during a run and
106+
after a run completes.
107+
108+
# Arguments
109+
- `instance`: [`MLFlow`](@ref) configuration.
110+
- `run_id`: ID of the run under which to log the tag.
111+
- `key`: Name of the tag.
112+
- `value`: String value of the tag being logged.
113+
114+
# Returns
115+
`true` if successful. Otherwise, raises exception.
116+
"""
117+
function setruntag(instanceL::MLFlow, run_id::String, key::String,
118+
value::String):Bool
119+
mlfpost(instanceL, "runs/set-tag"; run_id=run_id, key=key, value=value)
120+
return true
121+
end
122+
setruntag(instance::MLFlow, run::Run, key::String, value::String)::Bool =
123+
setruntag(instance, run.info.run_id, key, value)

test/services/run.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,36 @@ end
9090

9191
deleteexperiment(mlf, experiment_id)
9292
end
93+
94+
@testset verbose = true "set run tag" begin
95+
@ensuremlf
96+
experiment_id = createexperiment(mlf, UUIDs.uuid4() |> string)
97+
98+
@testset "set tag with run string id" begin
99+
run = createrun(mlf, experiment_id)
100+
setruntag(mlf, run.info.run_id, "tag", "value")
101+
102+
run = refresh(mlf, run)
103+
104+
@test run.data.tags |> !isempty
105+
@test (run.data.tags |> first).key == "tag"
106+
@test (run.data.tags |> first).value == "value"
107+
108+
deleterun(mlf, run)
109+
end
110+
111+
@testset "set tag with run" begin
112+
run = createrun(mlf, experiment_id)
113+
setruntag(mlf, run, "tag", "value")
114+
115+
run = refresh(mlf, run)
116+
117+
@test run.data.tags |> !isempty
118+
@test (run.data.tags |> first).key == "tag"
119+
@test (run.data.tags |> first).value == "value"
120+
121+
deleterun(mlf, run)
122+
end
123+
124+
deleteexperiment(mlf, experiment_id)
125+
end

0 commit comments

Comments
 (0)