Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add main XY (TSP) endpoints support to client with tests #28

Merged
merged 4 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ usage: tsp-cli-client [-h] [--ip IP] [--port PORT] [--open-trace TRACE_PATH]
[--list-experiment UUID] [--list-experiments]
[--delete-experiment UUID] [--list-outputs UUID]
[--list-output OUTPUT_ID] [--get-tree OUTPUT_ID]
[--get-xy-tree OUTPUT_ID] [--get-xy OUTPUT_ID]
[--items [ITEMS ...]] [--times [TIMES ...]]
[--uuid UUID] [--uuids [UUIDS ...]] [--do-delete-traces]
[--paths [PATHS ...]] [--list-extensions]
[--load-extension EXTENSION_PATH]
Expand Down Expand Up @@ -83,7 +85,12 @@ optional arguments:
--list-outputs UUID Get details on the given trace
--list-output OUTPUT_ID
Get details on the given output of a trace
--get-tree OUTPUT_ID Get the tree of an output
--get-tree OUTPUT_ID Get the timegraph tree of an output
--get-xy-tree OUTPUT_ID
Get the XY tree of an output
--get-xy OUTPUT_ID Get the XY data of an output
--items [ITEMS ...] The list of XY items requested
--times [TIMES ...] The list of XY times requested
--uuid UUID The UUID of a trace
--uuids [UUIDS ...] The list of UUIDs
--do-delete-traces Also delete traces when deleting experiment
Expand Down
64 changes: 64 additions & 0 deletions test_tsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,17 @@

import os
import pytest
import time

from tsp.response import ResponseStatus
from tsp.tsp_client import TspClient

REQUESTED_TIME_XY_START = 1332170682440133097
REQUESTED_TIME_XY_END = 1332170692664579801
REQUESTED_TIME_XY_LENGTH = 10
REQUESTED_TIME_XY_STEP = (REQUESTED_TIME_XY_END -
REQUESTED_TIME_XY_START) / REQUESTED_TIME_XY_LENGTH


class TestTspClient:
"""TspClient test methods.
Expand Down Expand Up @@ -228,6 +236,62 @@ def test_fetch_timegraph_tree(self, kernel):
self._delete_experiments()
self._delete_traces()

def test_fetch_xy_tree(self, kernel):
traces = []
response = self.tsp_client.open_trace(os.path.basename(kernel), kernel)
traces.append(response.model.UUID)
response = self.tsp_client.open_experiment(
os.path.basename(kernel), traces)
assert response.status_code == 200
experiment_uuid = response.model.UUID

response = self.tsp_client.fetch_experiment_outputs(experiment_uuid)
output_id = response.model.descriptors[0].id
response = self.tsp_client.fetch_xy_tree(experiment_uuid, output_id)
assert response.status_code == 200
assert response.model.model_type == response.model.model_type.XY_TREE
self._delete_experiments()
self._delete_traces()

def test_fetch_xy(self, kernel):
traces = []
response = self.tsp_client.open_trace(os.path.basename(kernel), kernel)
traces.append(response.model.UUID)
response = self.tsp_client.open_experiment(
os.path.basename(kernel), traces)
assert response.status_code == 200
experiment_uuid = response.model.UUID

response = self.tsp_client.fetch_experiment_outputs(experiment_uuid)
output_id = response.model.descriptors[0].id
status = ResponseStatus.RUNNING.name
while status == ResponseStatus.RUNNING.name:
time.sleep(1)
response = self.tsp_client.fetch_xy_tree(
experiment_uuid, output_id)
assert response.model is not None
status = response.model.status.upper()

parameters = {}
requested_items = []
for entry in response.model.model.entries:
requested_items.append(entry.id)
parameters[TspClient.REQUESTED_ITEM_KEY] = requested_items

requested_times = []
requested_time = REQUESTED_TIME_XY_START
while len(requested_times) < REQUESTED_TIME_XY_LENGTH:
requested_time += REQUESTED_TIME_XY_STEP
requested_times.append(int(requested_time))
parameters[TspClient.REQUESTED_TIME_KEY] = requested_times

params = {TspClient.PARAMETERS_KEY: parameters}
response = self.tsp_client.fetch_xy(experiment_uuid, output_id, params)
assert response.status_code == 200
assert response.model.model_type == response.model.model_type.XY
self._delete_experiments()
self._delete_traces()

def test_fetch_extensions_none(self):
response = self.tsp_client.fetch_extensions()
assert response.status_code == 200
Expand Down
64 changes: 63 additions & 1 deletion tsp-cli-client
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ from os.path import os

from tsp.tree_model import TreeModel
from tsp.tsp_client import TspClient
from tsp.xy_model import XYModel

#api_token = 'your_token_goes_here'
NS_PER_SEC = 1000000000
Expand Down Expand Up @@ -134,7 +135,15 @@ if __name__ == "__main__":
parser.add_argument("--list-output", dest="list_output",
help="Get details on the given output of a trace", metavar="OUTPUT_ID")
parser.add_argument("--get-tree", dest="get_tree",
help="Get the tree of an output", metavar="OUTPUT_ID")
help="Get the timegraph tree of an output", metavar="OUTPUT_ID")
parser.add_argument("--get-xy-tree", dest="get_xy_tree",
help="Get the XY tree of an output", metavar="OUTPUT_ID")
parser.add_argument("--get-xy", dest="get_xy",
help="Get the XY data of an output", metavar="OUTPUT_ID")
parser.add_argument("--items", dest="items",
help="The list of XY items requested", nargs="*")
parser.add_argument("--times", dest="times",
help="The list of XY times requested", nargs="*")
parser.add_argument("--uuid", dest="uuid", help="The UUID of a trace")
parser.add_argument("--uuids", dest="uuids",
help="The list of UUIDs", nargs="*")
Expand Down Expand Up @@ -298,6 +307,9 @@ if __name__ == "__main__":
options.uuid, options.get_tree)
if response.status_code == 200:
tree = response.model.model
if tree is None:
print("Tree had no model; retry?")
sys.exit(1)

if (output_descriptor.type == "DATA_TREE"):
treeModel = TreeModel(tree.entries, tree.descriptors)
Expand All @@ -311,6 +323,56 @@ if __name__ == "__main__":
print("Trace UUID is missing")
sys.exit(1)

if (options.get_xy_tree):
if options.uuid is not None:

output_descriptor = get_descriptor(
options.uuid, options.get_xy_tree)
if (output_descriptor is None):
sys.exit(1)

response = tsp_client.fetch_xy_tree(
options.uuid, options.get_xy_tree)
if response.status_code == 200:
tree = response.model.model
if tree is None:
print("Tree had no model; retry?")
sys.exit(1)

if (output_descriptor.type == "TREE_TIME_XY"):
treeModel = TreeModel(tree.entries, tree.descriptors)
else:
treeModel = TreeModel(tree.entries)
treeModel.print()
sys.exit(0)
else:
sys.exit(1)
else:
print("Trace UUID is missing")
sys.exit(1)

if (options.get_xy):
if not options.items or not options.times:
marco-miller marked this conversation as resolved.
Show resolved Hide resolved
print("Provide requested --items and --times for the XY data")
sys.exit(1)

if options.uuid is not None:
parameters = {TspClient.REQUESTED_TIME_KEY: list(map(int, options.times)),
marco-miller marked this conversation as resolved.
Show resolved Hide resolved
TspClient.REQUESTED_ITEM_KEY: list(map(int, options.items))}
params = {TspClient.PARAMETERS_KEY: parameters}

response = tsp_client.fetch_xy(
options.uuid, options.get_xy, params)
if response.status_code == 200:
xyModel = response.model.model
xyModel.print()
sys.exit(0)
else:
sys.exit(1)
else:
print("Trace UUID is missing")
sys.exit(1)

if (options.extensions):
response = tsp_client.fetch_extensions()
if response.status_code == 200:
Expand Down
7 changes: 3 additions & 4 deletions tsp/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from tsp.output_descriptor import OutputDescriptor
from tsp.entry_model import EntryModel
from tsp.time_graph_model import TimeGraphModel
from tsp.xy_model import XYModel

MODEL_KEY = "model"
OUTPUT_DESCRIPTOR_KEY = "output"
Expand Down Expand Up @@ -79,14 +80,12 @@ def __init__(self, params, model_type):
if self.model_type == ModelType.TIME_GRAPH_TREE:
self.model = EntryModel(params.get(MODEL_KEY), self.model_type)
elif self.model_type == ModelType.XY_TREE:
# TODO
print("not implemented")
self.model = EntryModel(params.get(MODEL_KEY))
elif self.model_type == ModelType.STATES:
# TODO
print("not implemented")
elif self.model_type == ModelType.XY:
# TODO
print("not implemented")
self.model = XYModel(params.get(MODEL_KEY))

'''
Output descriptor
Expand Down
55 changes: 49 additions & 6 deletions tsp/tsp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@
headers_form = {'content-type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'}

PARAMETERS_KEY = 'parameters'
REQUESTED_TIME_KEY = 'requested_times'


class TspClient(object):
'''
Trace Server Protocol tsp_cli_client
'''

PARAMETERS_KEY = 'parameters'
REQUESTED_TIME_KEY = 'requested_times'
REQUESTED_ITEM_KEY = 'requested_items'

def __init__(self, base_url):
'''
Constructor
Expand Down Expand Up @@ -240,9 +241,7 @@ def fetch_timegraph_tree(self, exp_uuid, output_id, parameters=None):

params = parameters
if (parameters is None):
requested_times = [0, 1]
my_parameters = {REQUESTED_TIME_KEY: requested_times}
params = {PARAMETERS_KEY: my_parameters}
params = {}

response = requests.post(api_url, json=params, headers=headers)

Expand All @@ -252,6 +251,50 @@ def fetch_timegraph_tree(self, exp_uuid, output_id, parameters=None):
print("failed to get tree: {0}".format(response.status_code))
return TspClientResponse(None, response.status_code, response.text)

def fetch_xy_tree(self, exp_uuid, output_id, parameters=None):
'''
Fetch XY tree, Model extends Entry
:param exp_uuid: Experiment UUID
:param output_id: Output ID
:param parameters: Query object
:returns: :class: `TspClientResponse <GenericResponse>` object XY entry response with entries and headers
:rtype: TspClientResponse
'''
api_url = '{0}experiments/{1}/outputs/XY/{2}/tree'.format(
self.base_url, exp_uuid, output_id)

params = parameters
if (parameters is None):
params = {}

response = requests.post(api_url, json=params, headers=headers)

if response.status_code == 200:
return TspClientResponse(GenericResponse(json.loads(response.content.decode('utf-8')), ModelType.XY_TREE), response.status_code, response.text)
else:
print("failed to get tree: {0}".format(response.status_code))
return TspClientResponse(None, response.status_code, response.text)

def fetch_xy(self, exp_uuid, output_id, parameters):
'''
Fetch XY xy, XYModel
:param exp_uuid: Experiment UUID
:param output_id: Output ID
:param parameters: Query object (mandatory here; no defaults possible)
:returns: :class: `TspClientResponse <GenericResponse>` object XY series response
:rtype: TspClientResponse
'''
api_url = '{0}experiments/{1}/outputs/XY/{2}/xy'.format(
self.base_url, exp_uuid, output_id)

response = requests.post(api_url, json=parameters, headers=headers)

if response.status_code == 200:
return TspClientResponse(GenericResponse(json.loads(response.content.decode('utf-8')), ModelType.XY), response.status_code, response.text)
else:
print("failed to get xy: {0}".format(response.status_code))
return TspClientResponse(None, response.status_code, response.text)

def fetch_extensions(self):
'''
Fetch Extensions (loaded files)
Expand Down
Loading