|
1 |
| -const IntOrString = Union{Int, String} |
2 |
| -const MLFlowUpsertData{T} = Union{Array{T}, Dict{String, String}, |
3 |
| - Array{Pair{String, String}}, Array{Dict{String, String}}} |
| 1 | +const NumberOrString = Union{Number, String} |
| 2 | +const MLFlowUpsertData{T} = Union{ |
| 3 | + Array{T}, |
| 4 | + Array{<:Dict{String, <:Any}}, |
| 5 | + Dict{String, <:NumberOrString}, |
| 6 | + Array{<:Pair{String, <:NumberOrString}}, |
| 7 | + Array{<:Tuple{String, <:NumberOrString}} |
| 8 | +} |
4 | 9 |
|
5 | 10 | const MLFLOW_ERROR_CODES = (;
|
6 | 11 | RESOURCE_ALREADY_EXISTS = "RESOURCE_ALREADY_EXISTS",
|
7 | 12 | RESOURCE_DOES_NOT_EXIST = "RESOURCE_DOES_NOT_EXIST",
|
8 | 13 | )
|
9 | 14 |
|
10 |
| -function dict_to_array(dict::Dict{String, String})::MLFlowUpsertData |
11 |
| - tags = Tag[] |
| 15 | +function dict_to_T_array(::Type{T}, dict::Dict{String, <:NumberOrString}) where T<:LoggingData |
| 16 | + entities = T[] |
12 | 17 | for (key, value) in dict
|
13 |
| - push!(tags, Tag(key, value)) |
| 18 | + if T<:Metric |
| 19 | + push!(entities, Metric(key, Float64(value), |
| 20 | + round(Int, now() |> datetime2unix), missing)) |
| 21 | + else |
| 22 | + push!(entities, T(key, value |> string)) |
| 23 | + end |
14 | 24 | end
|
15 | 25 |
|
16 |
| - return tags |
| 26 | + return entities |
17 | 27 | end
|
18 | 28 |
|
19 |
| -function pairsarray_to_array(pair_array::Array{<:Pair})::MLFlowUpsertData |
20 |
| - entity_array = Tag[] |
| 29 | +function pairarray_to_T_array(::Type{T}, pair_array::Array{<:Pair}) where T<:LoggingData |
| 30 | + entities = T[] |
21 | 31 | for pair in pair_array
|
22 |
| - println(pair) |
23 | 32 | key = pair.first |> string
|
24 |
| - value = pair.second |> string |
25 |
| - push!(entity_array, Tag(key, value)) |
| 33 | + if T<:Metric |
| 34 | + value = pair.second |
| 35 | + push!(entities, Metric(key, Float64(value), |
| 36 | + round(Int, now() |> datetime2unix), missing)) |
| 37 | + else |
| 38 | + value = pair.second |> string |
| 39 | + push!(entities, T(key, value)) |
| 40 | + end |
26 | 41 | end
|
27 | 42 |
|
28 |
| - return entity_array |
| 43 | + return entities |
29 | 44 | end
|
30 | 45 |
|
31 |
| -function dictarray_to_array(dict_array::Array{Dict{String, String}})::MLFlowUpsertData |
32 |
| - tags = Tag[] |
| 46 | +function tuplearray_to_T_array(::Type{T}, |
| 47 | + tuple_array::Array{<:Tuple{String, <:NumberOrString}}) where T<:LoggingData |
| 48 | + entities = T[] |
| 49 | + for tuple in tuple_array |
| 50 | + if length(tuple) != 2 |
| 51 | + error("Tuple must have exactly two elements (format: (key, value))") |
| 52 | + end |
| 53 | + |
| 54 | + key = tuple |> first |> string |
| 55 | + if T<: Metric |
| 56 | + value = tuple |> last |
| 57 | + push!(entities, Metric(key, Float64(value), |
| 58 | + round(Int, now() |> datetime2unix), missing)) |
| 59 | + else |
| 60 | + value = tuple |> last |> string |
| 61 | + push!(entities, T(key, value)) |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + return entities |
| 66 | +end |
| 67 | + |
| 68 | +function dictarray_to_T_array(::Type{T}, |
| 69 | + dict_array::Array{<:Dict{String, <:Any}}) where T<:LoggingData |
| 70 | + entities = T[] |
33 | 71 | for dict in dict_array
|
34 |
| - push!(tags, Tag(dict["key"], dict["value"])) |
| 72 | + key = dict["key"] |> string |
| 73 | + if T<:Metric |
| 74 | + value = Float64(dict["value"]) |
| 75 | + if haskey(dict, "timestamp") |
| 76 | + timestamp = dict["timestamp"] |
| 77 | + else |
| 78 | + timestamp = round(Int, now() |> datetime2unix) |
| 79 | + end |
| 80 | + push!(entities, Metric(key, value, timestamp, missing)) |
| 81 | + else |
| 82 | + value = dict["value"] |> string |
| 83 | + push!(entities, T(key, value)) |
| 84 | + end |
35 | 85 | end
|
36 | 86 |
|
37 |
| - return tags |
| 87 | + return entities |
38 | 88 | end
|
39 | 89 |
|
40 |
| -function parse(entities::MLFlowUpsertData{T}) where T<:LoggingData |
41 |
| - println(typeof(entities)) |
42 |
| - if entities isa Dict{String, String} |
43 |
| - return entities |> dict_to_array |
44 |
| - elseif entities isa Array{Pair{String, String}} |
45 |
| - return entities |> pairsarray_to_array |
46 |
| - elseif entities isa Array{Dict{String, String}} |
47 |
| - return entities |> dictarray_to_array |
| 90 | +function parse(::Type{T}, entities::MLFlowUpsertData{T}) where T<:LoggingData |
| 91 | + if entities isa Dict{String, <:NumberOrString} |
| 92 | + return dict_to_T_array(T, entities) |
| 93 | + elseif entities isa Array{<:Dict{String, <:Any}} |
| 94 | + return dictarray_to_T_array(T, entities) |
| 95 | + elseif entities isa Array{<:Pair{String, <:NumberOrString}} |
| 96 | + return pairarray_to_T_array(T, entities) |
| 97 | + elseif entities isa Array{<:Tuple{String, <:NumberOrString}} |
| 98 | + return tuplearray_to_T_array(T, entities) |
48 | 99 | end
|
49 | 100 | return entities
|
50 | 101 | end
|
|
0 commit comments