Skip to content

Commit

Permalink
fix: graphql mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
hopeyen committed Mar 20, 2023
1 parent 7b8342d commit d306ba7
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 81 deletions.
2 changes: 1 addition & 1 deletion Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[[deps.TheGraphData]]
deps = ["CSV", "GraphQLClient", "Mocking", "TensorCast", "TypedTables"]
git-tree-sha1 = "a8e55dcab58a88fdd27c315e3f09ba48891a908e"
git-tree-sha1 = "65b0fe4819e206fcabc133a8eb3a039d7eefb144"
repo-rev = "main"
repo-url = "https://github.com/semiotic-ai/TheGraphData.jl.git"
uuid = "871720c8-5dfb-4fa2-998e-3fe6ebd08819"
Expand Down
3 changes: 3 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ gas = 100
min_signal = 1000
verbose = true
num_reported_options = 2
execution_mode = "none"
indexer_url = "http://localhost:18000"
network_subgraph_endpoint = "https://api.thegraph.com/subgraphs/name/graphprotocol/graph-network-goerli"
2 changes: 1 addition & 1 deletion src/AllocationOpt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function main(config::Dict)

# Use config for using actionqueue or rules with the top profit batch
ix = first(popts)[:index]
execute(a, ix, fs, xs, ps, config)
execute(a, ix, fs, xs, profitmatrix, config)

return nothing
end
Expand Down
101 changes: 51 additions & 50 deletions src/reporting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ function writejson(results::AbstractString, config::AbstractDict)
end

"""
unallocate(::Val{:none}, a, t, config)
unallocate_action(::Val{:none}, a, t, config)
Do nothing.
Expand All @@ -184,14 +184,14 @@ julia> t = flextable([
Dict("amount" => "1", "profit" => "0", "ipfshash" => "Qma"),
Dict("amount" => "2", "profit" => "0", "ipfshash" => "Qmb"),
])
julia> AllocationOpt.unallocate(Val{:none}, a, t, Dict())
julia> AllocationOpt.unallocate_action(Val{:none}, a, t, Dict())
```
"""
unallocate(::Val{:none}, a, t, config) = nothing
unallocate_action(::Val{:none}, a, t, config) = nothing


"""
reallocate(::Val{:none}, a, t, config)
reallocate_action(::Val{:none}, a, t, config)
Do nothing.
Expand All @@ -205,12 +205,12 @@ julia> t = flextable([
Dict("amount" => "1", "profit" => "0", "ipfshash" => "Qma"),
Dict("amount" => "2", "profit" => "0", "ipfshash" => "Qmb"),
])
julia> AllocationOpt.reallocate(Val{:none}, a, t, Dict())
julia> AllocationOpt.reallocate_action(Val{:none}, a, t, Dict())
"""
reallocate(::Val{:none}, a, t, config) = nothing
reallocate_action(::Val{:none}, a, t, config) = nothing

"""
allocate(::Val{:none}, a, t, config)
allocate_action(::Val{:none}, a, t, config)
Do nothing.
Expand All @@ -224,13 +224,13 @@ julia> t = flextable([
Dict("amount" => "1", "profit" => "0", "ipfshash" => "Qma"),
Dict("amount" => "2", "profit" => "0", "ipfshash" => "Qmb"),
])
julia> AllocationOpt.allocate(Val{:none}, a, t, Dict())
julia> AllocationOpt.allocate_action(Val{:none}, a, t, Dict())
```
"""
allocate(::Val{:none}, a, t, config) = nothing
allocate_action(::Val{:none}, a, t, config) = nothing

"""
unallocate(::Val{:rules}, a::FlexTable, t::FlexTable, config::AbstractDict)
unallocate_action(::Val{:rules}, a::FlexTable, t::FlexTable, config::AbstractDict)
Print a rule that stops old allocations that the optimiser has not chosen and that aren't
frozen.
Expand All @@ -243,10 +243,10 @@ julia > a = flextable([
julia> t = flextable([
Dict("amount" => "2", "profit" => "0", "ipfshash" => "Qmb"),
])
julia> AllocationOpt.unallocate(Val{:rules}, a, t, Dict("frozenlist" => []))
julia> AllocationOpt.unallocate_action(Val{:rules}, a, t, Dict("frozenlist" => []))
```
"""
function unallocate(::Val{:rules}, a::FlexTable, t::FlexTable, config::AbstractDict)
function unallocate_action(::Val{:rules}, a::FlexTable, t::FlexTable, config::AbstractDict)
frozenlist = config["frozenlist"]
existingipfs = ipfshash(Val(:allocation), a)
proposedipfs = t.ipfshash
Expand All @@ -257,7 +257,7 @@ function unallocate(::Val{:rules}, a::FlexTable, t::FlexTable, config::AbstractD
end

"""
reallocate(::Val{:rules}, a::FlexTable, t::FlexTable, config::AbstractDict)
reallocate_action(::Val{:rules}, a::FlexTable, t::FlexTable, config::AbstractDict)
Print a rule that reallocates the old allocation with a new allocation amount
Expand All @@ -271,10 +271,10 @@ julia> t = flextable([
Dict("amount" => "1", "profit" => "0", "ipfshash" => "Qma"),
Dict("amount" => "2", "profit" => "0", "ipfshash" => "Qmb"),
])
julia> AllocationOpt.reallocate(Val(:rules), a, t, Dict())
julia> AllocationOpt.reallocate_action(Val(:rules), a, t, Dict())
```
"""
function reallocate(
function reallocate_action(
::Val{:rules},
a::FlexTable,
t::FlexTable,
Expand All @@ -292,12 +292,12 @@ function reallocate(
ipfses,
amounts
)
println(actions)
println.(actions)
return actions
end

"""
allocate(::Val{:rules}, a::FlexTable, t::FlexTable, config::AbstractDict)
allocate_action(::Val{:rules}, a::FlexTable, t::FlexTable, config::AbstractDict)
Print the rules that allocates to new subgraphs.
Expand All @@ -311,9 +311,9 @@ julia> t = flextable([
Dict("amount" => "1", "profit" => "0", "ipfshash" => "Qma"),
Dict("amount" => "2", "profit" => "0", "ipfshash" => "Qmb"),
])
julia> AllocationOpt.allocate(Val{:rules}, a, t, Dict())
julia> AllocationOpt.allocate_action(Val{:rules}, a, t, Dict())
"""
function allocate(
function allocate_action(
::Val{:rules},
a::FlexTable,
t::FlexTable,
Expand All @@ -331,7 +331,7 @@ function allocate(
ipfses,
amounts
)
println(actions)
println.(actions)
return actions
end

Expand Down Expand Up @@ -360,14 +360,14 @@ end
end

@enum ActionType begin
allocateaction
unallocateaction
reallocateaction
collectaction
allocate
unallocate
reallocate
collect
end

"""
unallocate(::Val{:actionqueue}, a::FlexTable, t::FlexTable, config::AbstractDict)
unallocate_action(::Val{:actionqueue}, a::FlexTable, t::FlexTable, config::AbstractDict)
Create and push the unallocate actions to the action queue.
Expand All @@ -379,9 +379,9 @@ julia > a = flextable([
julia> t = flextable([
Dict("amount" => "2", "profit" => "0", "ipfshash" => "Qmb"),
])
julia> AllocationOpt.unallocate(Val{:rules}, a, t, Dict("frozenlist" => []))
julia> AllocationOpt.unallocate_action(Val{:rules}, a, t, Dict("frozenlist" => []))
"""
function unallocate(
function unallocate_action(
::Val{:actionqueue},
a::FlexTable,
t::FlexTable,
Expand All @@ -393,23 +393,24 @@ function unallocate(
actions::Vector{Dict{String, Any}} = map(
r -> Dict(
"status" => queued,
"type" => unallocateaction,
"allocation" => id(Val(:allocation), r),
"ipfshash" => ipfshash(Val(:allocation), r),
"user" => "AllocationOpt",
"type" => unallocate,
"allocationID" => id(Val(:allocation), r),
"deploymentID" => ipfshash(Val(:allocation), r),
"source" => "AllocationOpt",
"reason" => "AllocationOpt",
"priority" => 0,
),
ft
)

# Send graphql mutation to action queue
_ = @mock(mutate("queueActions", Dict("actions" => actions)))
@mock(mutate("queueActions", Dict("actions" => actions); direct_write=true))

return actions
end

"""
reallocate(::Val{:actionqueue}, a::FlexTable, t::FlexTable, config::AbstractDict)
reallocate_action(::Val{:actionqueue}, a::FlexTable, t::FlexTable, config::AbstractDict)
Create and push reallocate actions to the action queue.
Expand All @@ -423,10 +424,10 @@ julia> t = flextable([
Dict("amount" => "1", "profit" => "0", "ipfshash" => "Qma"),
Dict("amount" => "2", "profit" => "0", "ipfshash" => "Qmb"),
])
julia> AllocationOpt.reallocate(Val(:actionqueue), a, t, Dict())
julia> AllocationOpt.reallocate_action(Val(:actionqueue), a, t, Dict())
```
"""
function reallocate(
function reallocate_action(
::Val{:actionqueue},
a::FlexTable,
t::FlexTable,
Expand All @@ -439,25 +440,25 @@ function reallocate(
actions::Vector{Dict{String, Any}} = map(
r -> Dict(
"status" => queued,
"type" => reallocateaction,
"allocation" => id(Val(:allocation), r),
"ipfshash" => ipfshash(Val(:allocation), r),
"type" => reallocate,
"allocationID" => id(Val(:allocation), r),
"deploymentID" => ipfshash(Val(:allocation), r),
"amount" => format(r.amount),
"user" => "AllocationOpt",
"source" => "AllocationOpt",
"reason" => "Expected profit: $(format(r.profit))",
"priority" => 0,
),
ti,
)

# Send graphql mutation to action queue
_ = @mock(mutate("queueActions", Dict("actions" => actions)))
@mock(mutate("queueActions", Dict("actions" => actions); direct_write=true))

return actions
end

"""
allocate(::Val{:actionqueue}, a::FlexTable, t::FlexTable, config::AbstractDict)
allocate_action(::Val{:actionqueue}, a::FlexTable, t::FlexTable, config::AbstractDict)
Create and push allocate actions to the action queue.
Expand All @@ -471,10 +472,10 @@ julia> t = flextable([
Dict("amount" => "1", "profit" => "0", "ipfshash" => "Qma"),
Dict("amount" => "2", "profit" => "0", "ipfshash" => "Qmb"),
])
julia> AllocationOpt.allocate(Val(:actionqueue), a, t, Dict())
julia> AllocationOpt.allocate_action(Val(:actionqueue), a, t, Dict())
```
"""
function allocate(
function allocate_action(
::Val{:actionqueue},
a::FlexTable,
t::FlexTable,
Expand All @@ -487,18 +488,18 @@ function allocate(
actions::Vector{Dict{String, Any}} = map(
r -> Dict(
"status" => queued,
"type" => allocateaction,
"ipfshash" => r.ipfshash,
"type" => allocate,
"deploymentID" => r.ipfshash,
"amount" => format(r.amount),
"user" => "AllocationOpt",
"source" => "AllocationOpt",
"reason" => "Expected profit: $(format(r.profit))",
"priority" => 0,
),
ts,
)

# Send graphql mutation to action queue
_ = @mock(mutate("queueActions", Dict("actions" => actions)))
@mock(mutate("queueActions", Dict("actions" => actions); direct_write=true))

return actions
end
Expand Down Expand Up @@ -546,9 +547,9 @@ function execute(
mode = Val(Symbol(config["execution_mode"]))

indexerurlclient(mode, config)
_ = unallocate(mode, a, t, config)
_ = reallocate(mode, a, t, config)
_ = allocate(mode, a, t, config)
_ = unallocate_action(mode, a, t, config)
_ = reallocate_action(mode, a, t, config)
_ = allocate_action(mode, a, t, config)

return nothing
end
Expand Down
2 changes: 1 addition & 1 deletion test/patch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ writejson_success_patch = @patch function JSON.print(io, s)
return s
end

mutate_success_patch = @patch function mutate(v, a)
mutate_success_patch = @patch function mutate(v, a; kwargs...)
println("mutate stub ==> simulating queueActions")
return [Dict(v => a["actions"])]
end
Loading

0 comments on commit d306ba7

Please sign in to comment.