-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from NavAbility/80/get_status_and_test_cleanup
[WIP] Statuses, `waitForCompletion`, and code+test cleanup
- Loading branch information
Showing
14 changed files
with
435 additions
and
324 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.