Skip to content

Commit 776695b

Browse files
authored
Merge pull request #37 from pebeto/improving_code_base
Improving code base
2 parents de98ac2 + 60a2535 commit 776695b

File tree

13 files changed

+139
-101
lines changed

13 files changed

+139
-101
lines changed

.github/codecov.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ignore:
2+
- "src/deprecated.jl"

docs/make.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ makedocs(;
1717
"Home" => "index.md",
1818
"Tutorial" => "tutorial.md",
1919
"Reference" => "reference.md"
20-
]
20+
],
21+
checkdocs=:exports
2122
)
2223

2324
deploydocs(;

docs/src/reference.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ MLFlowExperiment
1414
MLFlowRun
1515
MLFlowRunInfo
1616
MLFlowRunData
17+
MLFlowRunDataParam
1718
MLFlowRunDataMetric
1819
MLFlowRunStatus
1920
MLFlowArtifactFileInfo
@@ -28,7 +29,7 @@ getexperiment
2829
getorcreateexperiment
2930
deleteexperiment
3031
searchexperiments
31-
listexperiments
32+
restoreexperiment
3233
```
3334

3435
# Runs
@@ -55,5 +56,5 @@ uri
5556
generatefilterfromentity_type
5657
generatefilterfromparams
5758
generatefilterfromattributes
58-
59+
healthcheck
5960
```

src/MLFlowClient.jl

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ using JSON
2020
using ShowCases
2121
using FilePathsBase: AbstractPath
2222

23-
include("types/core.jl")
23+
include("types/mlflow.jl")
24+
export
25+
MLFlow
26+
27+
include("types/experiment.jl")
2428
export
25-
MLFlow,
2629
MLFlowExperiment
2730

28-
include("types/runs.jl")
31+
include("types/run.jl")
2932
export
3033
MLFlowRunStatus,
3134
MLFlowRunInfo,
@@ -38,13 +41,15 @@ export
3841
get_run_id,
3942
get_params
4043

41-
include("types/artifacts.jl")
44+
include("types/artifact.jl")
4245
export
4346
MLFlowArtifactFileInfo,
4447
MLFlowArtifactDirInfo,
4548
get_path,
4649
get_size
4750

51+
include("api.jl")
52+
4853
include("utils.jl")
4954
export
5055
generatefilterfromparams
@@ -75,4 +80,7 @@ export
7580
logmetric,
7681
logartifact,
7782
listartifacts
83+
84+
include("deprecated.jl")
85+
7886
end

src/api.jl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
mlfget(mlf, endpoint; kwargs...)
3+
4+
Performs a HTTP GET to a specified endpoint. kwargs are turned into GET params.
5+
"""
6+
function mlfget(mlf, endpoint; kwargs...)
7+
apiuri = uri(mlf, endpoint, kwargs)
8+
apiheaders = headers(mlf, Dict("Content-Type" => "application/json"))
9+
10+
try
11+
response = HTTP.get(apiuri, apiheaders)
12+
return JSON.parse(String(response.body))
13+
catch e
14+
throw(e)
15+
end
16+
end
17+
18+
"""
19+
mlfpost(mlf, endpoint; kwargs...)
20+
21+
Performs a HTTP POST to the specified endpoint. kwargs are converted to JSON and become the POST body.
22+
"""
23+
function mlfpost(mlf, endpoint; kwargs...)
24+
apiuri = uri(mlf, endpoint)
25+
apiheaders = headers(mlf, Dict("Content-Type" => "application/json"))
26+
body = JSON.json(kwargs)
27+
28+
try
29+
response = HTTP.post(apiuri, apiheaders, body)
30+
return JSON.parse(String(response.body))
31+
catch e
32+
throw(e)
33+
end
34+
end

src/deprecated.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ Deprecated (last MLFlow version: 1.30.1) in favor of [`searchexperiments`](@ref)
77
"""
88

99
function listexperiments(mlf::MLFlow)
10-
endpoint = "experiments/list"
10+
endpoint = "experiments/list"
1111
mlfget(mlf, endpoint)
1212
end

src/experiments.jl

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,24 @@ An object of type [`MLFlowExperiment`](@ref).
1515
"""
1616
function createexperiment(mlf::MLFlow; name=missing, artifact_location=missing, tags=missing)
1717
endpoint = "experiments/create"
18+
1819
if ismissing(name)
1920
name = string(UUIDs.uuid4())
2021
end
21-
result = mlfpost(mlf, endpoint; name=name, artifact_location=artifact_location, tags=tags)
22-
experiment_id = parse(Int, result["experiment_id"])
23-
getexperiment(mlf, experiment_id)
22+
23+
try
24+
result = mlfpost(mlf, endpoint; name=name, artifact_location=artifact_location, tags=tags)
25+
experiment_id = parse(Int, result["experiment_id"])
26+
return getexperiment(mlf, experiment_id)
27+
catch e
28+
if isa(e, HTTP.ExceptionRequest.StatusError) && e.status == 400
29+
error_code = JSON.parse(String(e.response.body))["error_code"]
30+
if error_code == MLFLOW_ERROR_CODES.RESOURCE_ALREADY_EXISTS
31+
error("Experiment with name \"$name\" already exists")
32+
end
33+
end
34+
throw(e)
35+
end
2436
end
2537

2638
"""
@@ -38,7 +50,9 @@ An instance of type [`MLFlowExperiment`](@ref)
3850
"""
3951
function getexperiment(mlf::MLFlow, experiment_id::Integer)
4052
try
41-
result = _getexperimentbyid(mlf, experiment_id)
53+
endpoint = "experiments/get"
54+
arguments = (:experiment_id => experiment_id,)
55+
result = mlfget(mlf, endpoint; arguments...)["experiment"]
4256
return MLFlowExperiment(result)
4357
catch e
4458
if isa(e, HTTP.ExceptionRequest.StatusError) && e.status == 404
@@ -62,7 +76,9 @@ An instance of type [`MLFlowExperiment`](@ref)
6276
"""
6377
function getexperiment(mlf::MLFlow, experiment_name::String)
6478
try
65-
result = _getexperimentbyname(mlf, experiment_name)
79+
endpoint = "experiments/get-by-name"
80+
arguments = (:experiment_name => experiment_name,)
81+
result = mlfget(mlf, endpoint; arguments...)["experiment"]
6682
return MLFlowExperiment(result)
6783
catch e
6884
if isa(e, HTTP.ExceptionRequest.StatusError) && e.status == 404
@@ -71,16 +87,6 @@ function getexperiment(mlf::MLFlow, experiment_name::String)
7187
throw(e)
7288
end
7389
end
74-
function _getexperimentbyid(mlf::MLFlow, experiment_id::Integer)
75-
endpoint = "experiments/get"
76-
arguments = (:experiment_id => experiment_id,)
77-
mlfget(mlf, endpoint; arguments...)["experiment"]
78-
end
79-
function _getexperimentbyname(mlf::MLFlow, experiment_name::String)
80-
endpoint = "experiments/get-by-name"
81-
arguments = (:experiment_name => experiment_name,)
82-
mlfget(mlf, endpoint; arguments...)["experiment"]
83-
end
8490

8591
"""
8692
getorcreateexperiment(mlf::MLFlow, experiment_name::String; artifact_location=missing, tags=missing)
@@ -98,11 +104,12 @@ An instance of type [`MLFlowExperiment`](@ref)
98104
99105
"""
100106
function getorcreateexperiment(mlf::MLFlow, experiment_name::String; artifact_location=missing, tags=missing)
101-
exp = getexperiment(mlf, experiment_name)
102-
if ismissing(exp)
103-
exp = createexperiment(mlf, name=experiment_name, artifact_location=artifact_location, tags=tags)
107+
experiment = getexperiment(mlf, experiment_name)
108+
109+
if ismissing(experiment)
110+
return createexperiment(mlf, name=experiment_name, artifact_location=artifact_location, tags=tags)
104111
end
105-
exp
112+
return experiment
106113
end
107114

108115
"""
@@ -122,14 +129,14 @@ function deleteexperiment(mlf::MLFlow, experiment_id::Integer)
122129
endpoint = "experiments/delete"
123130
try
124131
mlfpost(mlf, endpoint; experiment_id=experiment_id)
132+
return true
125133
catch e
126134
if isa(e, HTTP.ExceptionRequest.StatusError) && e.status == 404
127135
# experiment already deleted
128136
return true
129137
end
130138
throw(e)
131139
end
132-
true
133140
end
134141

135142
"""
@@ -164,14 +171,16 @@ function restoreexperiment(mlf::MLFlow, experiment_id::Integer)
164171
endpoint = "experiments/restore"
165172
try
166173
mlfpost(mlf, endpoint; experiment_id=experiment_id)
174+
return true
167175
catch e
168176
if isa(e, HTTP.ExceptionRequest.StatusError) && e.status == 404
169-
# experiment already restored
170-
return true
177+
error_code = JSON.parse(String(e.response.body))["error_code"]
178+
if error_code == MLFLOW_ERROR_CODES.RESOURCE_DOES_NOT_EXIST
179+
error("Experiment with id \"$experiment_id\" does not exist")
180+
end
171181
end
172182
throw(e)
173183
end
174-
true
175184
end
176185

177186
restoreexperiment(mlf::MLFlow, experiment::MLFlowExperiment) =
File renamed without changes.

src/types/core.jl renamed to src/types/experiment.jl

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,3 @@
1-
"""
2-
MLFlow
3-
4-
Base type which defines location and version for MLFlow API service.
5-
6-
# Fields
7-
- `baseuri::String`: base MLFlow tracking URI, e.g. `http://localhost:5000`
8-
- `apiversion`: used API version, e.g. `2.0`
9-
- `headers`: HTTP headers to be provided with the REST API requests (useful for authetication tokens)
10-
11-
# Constructors
12-
13-
- `MLFlow(baseuri; apiversion=2.0,headers=Dict())`
14-
- `MLFlow()` - defaults to `MLFlow("http://localhost:5000")`
15-
16-
# Examples
17-
18-
```@example
19-
mlf = MLFlow()
20-
```
21-
22-
```@example
23-
remote_url="https://<your-server>.cloud.databricks.com"; # address of your remote server
24-
mlf = MLFlow(remote_url, headers=Dict("Authorization" => "Bearer <your-secret-token>"))
25-
```
26-
27-
"""
28-
struct MLFlow
29-
baseuri::String
30-
apiversion
31-
headers::Dict
32-
end
33-
MLFlow(baseuri; apiversion=2.0,headers=Dict()) = MLFlow(baseuri, apiversion,headers)
34-
MLFlow() = MLFlow("http://localhost:5000", 2.0, Dict())
35-
Base.show(io::IO, t::MLFlow) = show(io, ShowCase(t, [:baseuri,:apiversion], new_lines=true))
36-
371
"""
382
MLFlowExperiment
393

src/types/mlflow.jl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
MLFlow
3+
4+
Base type which defines location and version for MLFlow API service.
5+
6+
# Fields
7+
- `baseuri::String`: base MLFlow tracking URI, e.g. `http://localhost:5000`
8+
- `apiversion`: used API version, e.g. `2.0`
9+
- `headers`: HTTP headers to be provided with the REST API requests (useful for authetication tokens)
10+
11+
# Constructors
12+
13+
- `MLFlow(baseuri; apiversion=2.0,headers=Dict())`
14+
- `MLFlow()` - defaults to `MLFlow("http://localhost:5000")`
15+
16+
# Examples
17+
18+
```@example
19+
mlf = MLFlow()
20+
```
21+
22+
```@example
23+
remote_url="https://<your-server>.cloud.databricks.com"; # address of your remote server
24+
mlf = MLFlow(remote_url, headers=Dict("Authorization" => "Bearer <your-secret-token>"))
25+
```
26+
27+
"""
28+
struct MLFlow
29+
baseuri::String
30+
apiversion::Union{Integer, AbstractFloat}
31+
headers::Dict
32+
end
33+
MLFlow(baseuri; apiversion=2.0,headers=Dict()) = MLFlow(baseuri, apiversion,headers)
34+
MLFlow() = MLFlow("http://localhost:5000", 2.0, Dict())
35+
Base.show(io::IO, t::MLFlow) = show(io, ShowCase(t, [:baseuri,:apiversion], new_lines=true))

0 commit comments

Comments
 (0)