Skip to content

Commit

Permalink
Merge pull request #82 from NavAbility/80/get_status_and_test_cleanup
Browse files Browse the repository at this point in the history
[WIP] Statuses, `waitForCompletion`, and code+test cleanup
  • Loading branch information
GearsAD authored Mar 28, 2022
2 parents 6f2678e + ecde4cb commit 5e4d8e3
Show file tree
Hide file tree
Showing 14 changed files with 435 additions and 324 deletions.
8 changes: 6 additions & 2 deletions src/NavAbilitySDK.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ module NavAbilitySDK
# Global imports
using DocStringExtensions
using LinearAlgebra
using JSON

# Graphql
include("./navability/graphql/Factor.jl")
include("./navability/graphql/Status.jl")
include("./navability/graphql/Variable.jl")
export GQL_FRAGMENT_FACTORS, GQL_GETFACTOR, GQL_GETFACTORS, GQL_GETFACTORSFILTERED
export GQL_FRAGMENT_VARIABLES, GQL_GETVARIABLE, GQL_GETVARIABLES, GQL_GETVARIABLESFILTERED

include("./navability/graphql/QueriesDeprecated.jl")
export QUERY_VARIABLE_LABELS
Expand Down Expand Up @@ -40,10 +40,14 @@ export FactorType, Factor
include("./navability/services/Variable.jl")
include("./navability/services/Factor.jl")
include("./navability/services/Solve.jl")
include("./navability/services/Status.jl")
include("./navability/services/Utils.jl")
export getVariable, getVariables, listVariables, ls
export addVariable, addPackedVariable
export getFactor, getFactors, listFactors, lsf
export addFactor, addPackedFactor
export solveSession, solveFederated
export getStatusMessages, getStatusLatest, getStatusesLatest
export waitForCompletion

end
17 changes: 17 additions & 0 deletions src/navability/entities/Client.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
import Base: show

"""
$(TYPEDEF)
The context for a session, made from a user, robot, and session.
Users can have multiple robots and robots can have multiple sessions.
So this indicates a unique session.
"""
struct Client
userId::String
robotId::String
sessionId::String
end

function show(io::IO, c::Client)
print(io, "Client: User=$(c.userId), Robot=$(c.robotId), Session=$(c.sessionId)")
end

"""
$(TYPEDEF)
Some calls interact across multiple users, robots, and sessions.
A scope allows you to specify these more complex contexts.
"""
struct Scope
environmentIds::Vector{String}
userIds::Vector{String}
Expand Down
25 changes: 25 additions & 0 deletions src/navability/entities/StatusMessage.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Base: show

"""
$(TYPEDEF)
NOTE: Not used yet.
A status message from the server indicating the progress of
a request, e.g. addVariable or solveSession.
"""
struct StatusMessage
requestId::String
action::String
state::String
timestamp::String
client::Client

function show(io::IO, s::StatusMessage)
print(io, """StatusMessage:
client: $(self.client)
requestId: $(s.requestId)
action: $(self.action)
state: $(self.state)
timestamp: $(self.timestamp)
"""
)
end
31 changes: 31 additions & 0 deletions src/navability/graphql/Status.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
GQL_GETSTATUSMESSAGES = """
query sdk_ls_statusmessages(\$id: ID!) {
statusMessages(id: \$id) {
requestId,
action,
state,
timestamp,
client {
userId,
robotId,
sessionId
}
}
}
"""

GQL_GETSTATUSLATEST = """
query sdk_get_statuslatest(\$id: ID!) {
statusLatest(id: \$id) {
requestId,
action,
state,
timestamp,
client {
userId,
robotId,
sessionId
}
}
}
"""
2 changes: 0 additions & 2 deletions src/navability/services/Factor.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using ..NavAbilitySDK
using JSON

function addPackedFactor(navAbilityClient::NavAbilityClient, client::Client, factor)::String
response = navAbilityClient.mutate(MutationOptions(
Expand Down
1 change: 0 additions & 1 deletion src/navability/services/Solve.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using ..NavAbilitySDK

function solveSession(navAbilityClient::NavAbilityClient, client::Client)::String
response = navAbilityClient.mutate(MutationOptions(
Expand Down
67 changes: 67 additions & 0 deletions src/navability/services/Status.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

"""
$(SIGNATURES)
Get all the statuses for a request.
Args:
navAbilityClient (NavAbilityClient): The NavAbility client.
id (String): The ID of the request that you want the statuses on.
"""
function getStatusMessages(navAbilityClient::NavAbilityClient, id::String)
response = navAbilityClient.mutate(MutationOptions(
"sdk_ls_statusmessages",
GQL_GETSTATUSMESSAGES,
Dict(
"id" => id
)
))
rootData = JSON.parse(response.Data)
if haskey(rootData, "errors")
throw("Error: $(data["errors"])")
end
data = get(rootData,"data",nothing)
if data === nothing return "Error" end
statusMessages = get(data,"statusMessages","Error")
# TODO: What about marshalling?
return statusMessages
end

"""
$(SIGNATURES)
Get the latest status message for a request.
Args:
navAbilityClient (NavAbilityClient): The NavAbility client.
id (String): The ID of the request that you want the latest status on.
"""
function getStatusLatest(navAbilityClient::NavAbilityClient, id::String)
response = navAbilityClient.mutate(MutationOptions(
"sdk_get_statuslatest",
GQL_GETSTATUSLATEST,
Dict(
# "client" => client,
"id" => id
)
))
rootData = JSON.parse(response.Data)
if haskey(rootData, "errors")
throw("Error: $(data["errors"])")
end
data = get(rootData,"data",nothing)
if data === nothing return "Error" end
statusMessage = get(data,"statusLatest","Error")
# TODO: What about marshalling?
return statusMessage
end

"""
$(SIGNATURES)
Helper function to get a dictionary of all latest statues for a list of results.
Args:
navAbilityClient (NavAbilityClient): The NavAbility client.
ids (Vector{String}): A list of the IDS that you want statuses on.
"""
function getStatusesLatest(navAbilityClient::NavAbilityClient, ids::Vector{String})
return Dict(r=>getStatusLatest(navAbilityClient, r) for r in ids)
end
34 changes: 34 additions & 0 deletions src/navability/services/Utils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
$(SIGNATURES)
Wait for the requests to complete, poll until done.
Args:
requestIds (List[str]): The request IDs that should be polled.
maxSeconds (int, optional): Maximum wait time. Defaults to 60.
expectedStatus (str, optional): Expected status message per request.
Defaults to "Complete".
"""
function waitForCompletion(
navAbilityClient::NavAbilityClient,
requestIds::Vector{String};
maxSeconds::Int = 60,
expectedStatuses::Vector{String} = Nothing,
exceptionMessage::String = "Requests did not complete in time")
#
if expectedStatuses == Nothing
expectedStatuses = ["Complete", "Failed"]
end
wait_time = maxSeconds
tasksComplete = false
while !tasksComplete
statuses = values(getStatusesLatest(navAbilityClient, requestIds))
tasksComplete = all(s["state"] in expectedStatuses for s in statuses)
if tasksComplete
return
else
sleep(2)
wait_time -= 2
wait_time <= 0 && throw(error(exceptionMessage))
end
end
end
2 changes: 0 additions & 2 deletions src/navability/services/Variable.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using ..NavAbilitySDK
using JSON

function addPackedVariable(navAbilityClient::NavAbilityClient, client::Client, variable)::String
response = navAbilityClient.mutate(MutationOptions(
Expand Down
Loading

0 comments on commit 5e4d8e3

Please sign in to comment.