summaryrefslogtreecommitdiff
path: root/opendc/api/v1/simulations/simulationId/experiments
diff options
context:
space:
mode:
authorleonoverweel <l.overweel@gmail.com>2017-01-24 12:05:15 +0100
committerleonoverweel <l.overweel@gmail.com>2017-01-24 12:05:15 +0100
commit86a50a4f6df9ece982743a3b7ca510846d248909 (patch)
tree79edc0478908b7fee9e5dca2088e562c7a62038b /opendc/api/v1/simulations/simulationId/experiments
Initial commit
Diffstat (limited to 'opendc/api/v1/simulations/simulationId/experiments')
-rw-r--r--opendc/api/v1/simulations/simulationId/experiments/__init__.py0
-rw-r--r--opendc/api/v1/simulations/simulationId/experiments/endpoint.py114
-rw-r--r--opendc/api/v1/simulations/simulationId/experiments/experimentId/__init__.py0
-rw-r--r--opendc/api/v1/simulations/simulationId/experiments/experimentId/endpoint.py138
-rw-r--r--opendc/api/v1/simulations/simulationId/experiments/experimentId/last-simulated-tick/__init__.py0
-rw-r--r--opendc/api/v1/simulations/simulationId/experiments/experimentId/last-simulated-tick/endpoint.py43
-rw-r--r--opendc/api/v1/simulations/simulationId/experiments/experimentId/machine-states/__init__.py0
-rw-r--r--opendc/api/v1/simulations/simulationId/experiments/experimentId/machine-states/endpoint.py51
-rw-r--r--opendc/api/v1/simulations/simulationId/experiments/experimentId/rack-states/__init__.py0
-rw-r--r--opendc/api/v1/simulations/simulationId/experiments/experimentId/rack-states/endpoint.py51
-rw-r--r--opendc/api/v1/simulations/simulationId/experiments/experimentId/room-states/__init__.py0
-rw-r--r--opendc/api/v1/simulations/simulationId/experiments/experimentId/room-states/endpoint.py51
-rw-r--r--opendc/api/v1/simulations/simulationId/experiments/experimentId/statistics/__init__.py0
-rw-r--r--opendc/api/v1/simulations/simulationId/experiments/experimentId/statistics/task-durations/__init__.py0
-rw-r--r--opendc/api/v1/simulations/simulationId/experiments/experimentId/statistics/task-durations/endpoint.py44
-rw-r--r--opendc/api/v1/simulations/simulationId/experiments/experimentId/task-states/__init__.py0
-rw-r--r--opendc/api/v1/simulations/simulationId/experiments/experimentId/task-states/endpoint.py51
17 files changed, 543 insertions, 0 deletions
diff --git a/opendc/api/v1/simulations/simulationId/experiments/__init__.py b/opendc/api/v1/simulations/simulationId/experiments/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/opendc/api/v1/simulations/simulationId/experiments/__init__.py
diff --git a/opendc/api/v1/simulations/simulationId/experiments/endpoint.py b/opendc/api/v1/simulations/simulationId/experiments/endpoint.py
new file mode 100644
index 00000000..8cc4165f
--- /dev/null
+++ b/opendc/api/v1/simulations/simulationId/experiments/endpoint.py
@@ -0,0 +1,114 @@
+from opendc.models.experiment import Experiment
+from opendc.models.queued_experiment import QueuedExperiment
+from opendc.models.simulation import Simulation
+from opendc.util import database, exceptions
+from opendc.util.rest import Response
+
+def GET(request):
+ """Get this Simulation's Experiments."""
+
+ # Make sure required parameters are there
+
+ try:
+ request.check_required_parameters(
+ path = {
+ 'simulationId': 'int'
+ }
+ )
+
+ except exceptions.ParameterError as e:
+ return Response(400, e.message)
+
+ # Instantiate a Simulation from the database
+
+ simulation = Simulation.from_primary_key((request.params_path['simulationId'],))
+
+ # Make sure this Simulation exists
+
+ if not simulation.exists():
+ return Response(404, '{} not found.'.format(simulation))
+
+ # Make sure this user is authorized to view this Simulation's Experiments
+
+ if not simulation.google_id_has_at_least(request.google_id, 'VIEW'):
+ return Reponse(403, 'Forbidden from viewing Experiments for {}.'.format(simulation))
+
+ # Get and return the Experiments
+
+ experiments = Experiment.query('simulation_id', request.params_path['simulationId'])
+
+ return Response(
+ 200,
+ 'Successfully retrieved Experiments for {}.'.format(simulation),
+ [x.to_JSON() for x in experiments]
+ )
+
+def POST(request):
+ """Add a new Experiment for this Simulation."""
+
+ # Make sure required parameters are there
+
+ try:
+ request.check_required_parameters(
+ path = {
+ 'simulationId': 'int'
+ },
+ body = {
+ 'experiment': {
+ 'simulationId': 'int',
+ 'pathId': 'int',
+ 'traceId': 'int',
+ 'schedulerName': 'string',
+ 'name': 'string'
+ }
+ }
+ )
+
+ except exceptions.ParameterError as e:
+ return Response(400, e.message)
+
+ # Make sure the passed object's simulation id matches the path simulation id
+
+ if request.params_path['simulationId'] != request.params_body['experiment']['simulationId']:
+ return Response(403, 'ID mismatch.')
+
+ # Instantiate a Simulation from the database
+
+ simulation = Simulation.from_primary_key((request.params_path['simulationId'],))
+
+ # Make sure this Simulation exists
+
+ if not simulation.exists():
+ return Response(404, '{} not found.'.format(simulation))
+
+ # Make sure this user is authorized to edit this Simulation's Experiments
+
+ if not simulation.google_id_has_at_least(request.google_id, 'EDIT'):
+ return Response(403, 'Forbidden from adding an experiment to {}.'.format(simulation))
+
+ # Instantiate an Experiment
+
+ experiment = Experiment.from_JSON(request.params_body['experiment'])
+
+ # Try to insert this Experiment
+
+ try:
+ experiment.insert()
+
+ except exceptions.ForeignKeyError as e:
+ return Response(400, 'Foreign key constraint not met.')
+
+ # Queue this Experiment for simulation
+
+ queued_experiment = QueuedExperiment(experiment_id = experiment.id)
+ queued_experiment.insert()
+
+ # Return this Experiment
+
+ experiment.read()
+
+ return Response(
+ 200,
+ 'Successfully added {}.'.format(experiment),
+ experiment.to_JSON()
+ )
diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/__init__.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/opendc/api/v1/simulations/simulationId/experiments/experimentId/__init__.py
diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/endpoint.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/endpoint.py
new file mode 100644
index 00000000..7af62b92
--- /dev/null
+++ b/opendc/api/v1/simulations/simulationId/experiments/experimentId/endpoint.py
@@ -0,0 +1,138 @@
+from opendc.models.experiment import Experiment
+from opendc.util import database, exceptions
+from opendc.util.rest import Response
+
+def GET(request):
+ """Get this Experiment."""
+
+ try:
+ request.check_required_parameters(
+ path = {
+ 'simulationId': 'int',
+ 'experimentId': 'int'
+ }
+ )
+
+ except exceptions.ParameterError as e:
+ return Response(400, e.message)
+
+ # Instantiate an Experiment from the database
+
+ experiment = Experiment.from_primary_key((request.params_path['experimentId'],))
+
+ # Make sure this Experiment exists
+
+ if not experiment.exists():
+ return Response(404, '{} not found.'.format(experiment))
+
+ # Make sure this user is authorized to view this Experiment
+
+ if not experiment.google_id_has_at_least(request.google_id, 'VIEW'):
+ return Response(403, 'Forbidden from retrieving {}.'.format(experiment))
+
+ # Return this Experiment
+
+ experiment.read()
+
+ return Response(
+ 200,
+ 'Successfully retrieved {}.'.format(experiment),
+ experiment.to_JSON()
+ )
+
+def PUT(request):
+ """Update this Experiment's Path, Trace, Scheduler, and/or name."""
+
+ # Make sure required parameters are there
+
+ try:
+ request.check_required_parameters(
+ path = {
+ 'simulationId': 'int',
+ 'experimentId': 'int'
+ },
+ body = {
+ 'experiment': {
+ 'pathId': 'int',
+ 'traceId': 'int',
+ 'schedulerName': 'string',
+ 'name': 'string'
+ }
+ }
+ )
+
+ except exceptions.ParameterError as e:
+ return Response(400, e.message)
+
+ # Instantiate an Experiment from the database
+
+ experiment = Experiment.from_primary_key((request.params_path['experimentId'],))
+
+ # Make sure this Experiment exists
+
+ if not experiment.exists():
+ return Response(404, '{} not found.'.format(experiment))
+
+ # Make sure this user is authorized to edit this Experiment
+
+ if not experiment.google_id_has_at_least(request.google_id, 'EDIT'):
+ return Response(403, 'Forbidden from updating {}.'.format(experiment))
+
+ # Update this Experiment
+
+ experiment.path_id = request.params_body['experiment']['pathId']
+ experiment.trace_id = request.params_body['experiment']['traceId']
+ experiment.scheduler_name = request.params_body['experiment']['schedulerName']
+ experiment.name = request.params_body['experiment']['name']
+
+ try:
+ experiment.update()
+
+ except exceptions.ForeignKeyError:
+ return Response(400, 'Foreign key error.')
+
+ # Return this Experiment
+
+ return Response(
+ 200,
+ 'Successfully updated {}.'.format(experiment),
+ experiment.to_JSON()
+ )
+
+def DELETE(request):
+ """Delete this Experiment."""
+
+ # Make sure required parameters are there
+
+ try:
+ request.check_required_parameters(
+ path = {
+ 'simulationId': 'int',
+ 'experimentId': 'int'
+ }
+ )
+
+ except exceptions.ParameterError as e:
+ return Response(400, e.message)
+
+ # Instantiate an Experiment and make sure it exists
+
+ experiment = Experiment.from_primary_key((request.params_path['experimentId'],))
+
+ if not experiment.exists():
+ return Response(404, '{} not found.'.format(experiment))
+
+ # Make sure this user is authorized to delete this Experiment
+
+ if not experiment.google_id_has_at_least(request.google_id, 'EDIT'):
+ return Response(403, 'Forbidden from deleting {}.'.format(experiment))
+
+ # Delete and return this Experiment
+
+ experiment.delete()
+
+ return Response(
+ 200,
+ 'Successfully deleted {}.'.format(experiment),
+ experiment.to_JSON()
+ )
diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/last-simulated-tick/__init__.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/last-simulated-tick/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/opendc/api/v1/simulations/simulationId/experiments/experimentId/last-simulated-tick/__init__.py
diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/last-simulated-tick/endpoint.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/last-simulated-tick/endpoint.py
new file mode 100644
index 00000000..ed33be85
--- /dev/null
+++ b/opendc/api/v1/simulations/simulationId/experiments/experimentId/last-simulated-tick/endpoint.py
@@ -0,0 +1,43 @@
+from opendc.models.experiment import Experiment
+from opendc.util import database, exceptions
+from opendc.util.rest import Response
+
+def GET(request):
+ """Get this Experiment's last simulated tick."""
+
+ # Make sure required parameters are there
+
+ try:
+ request.check_required_parameters(
+ path = {
+ 'simulationId': 'int',
+ 'experimentId': 'int'
+ }
+ )
+
+ except exceptions.ParameterError as e:
+ return Response(400, e.message)
+
+ # Instantiate an Experiment from the database
+
+ experiment = Experiment.from_primary_key((request.params_path['experimentId'],))
+
+ # Make sure this Experiment exisits
+
+ if not experiment.exists():
+ return Response(404, '{} not found.'.format(experiment))
+
+ # Make sure this user is authorized to view this Experiment's last simulated tick
+
+ if not experiment.google_id_has_at_least(request.google_id, 'VIEW'):
+ return Response(403, 'Forbidden from viewing Room States for {}.'.format(experiment))
+
+ # Get and return the last simulated tick
+
+ last_simulated_tick = experiment.get_last_simulated_tick()
+
+ return Response(
+ 200,
+ 'Successfully retrieved Room States for {}.'.format(experiment),
+ {'lastSimulatedTick': last_simulated_tick}
+ )
diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/machine-states/__init__.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/machine-states/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/opendc/api/v1/simulations/simulationId/experiments/experimentId/machine-states/__init__.py
diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/machine-states/endpoint.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/machine-states/endpoint.py
new file mode 100644
index 00000000..c5ba0abe
--- /dev/null
+++ b/opendc/api/v1/simulations/simulationId/experiments/experimentId/machine-states/endpoint.py
@@ -0,0 +1,51 @@
+from opendc.models.experiment import Experiment
+from opendc.models.machine_state import MachineState
+from opendc.util import database, exceptions
+from opendc.util.rest import Response
+
+def GET(request):
+ """Get this Experiment's Machine States."""
+
+ # Make sure required parameters are there
+
+ try:
+ request.check_required_parameters(
+ path = {
+ 'simulationId': 'int',
+ 'experimentId': 'int'
+ }
+ )
+
+ except exceptions.ParameterError as e:
+ return Response(400, e.message)
+
+ # Instantiate an Experiment from the database
+
+ experiment = Experiment.from_primary_key((request.params_path['experimentId'],))
+
+ # Make sure this Experiment exisits
+
+ if not experiment.exists():
+ return Response(404, '{} not found.'.format(experiment))
+
+ # Make sure this user is authorized to view this Experiment's Machine States
+
+ if not experiment.google_id_has_at_least(request.google_id, 'VIEW'):
+ return Response(403, 'Forbidden from viewing Machine States for {}.'.format(experiment))
+
+ # Get and return the Machine States
+
+ if 'tick' in request.params_query:
+ machine_states = MachineState.from_experiment_id_and_tick(
+ request.params_path['experimentId'],
+ request.params_query['tick']
+ )
+
+ else:
+ machine_states = MachineState.from_experiment_id(request.params_path['experimentId'])
+
+ return Response(
+ 200,
+ 'Successfully retrieved Machine States for {}.'.format(experiment),
+ [x.to_JSON() for x in machine_states]
+ )
diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/rack-states/__init__.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/rack-states/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/opendc/api/v1/simulations/simulationId/experiments/experimentId/rack-states/__init__.py
diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/rack-states/endpoint.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/rack-states/endpoint.py
new file mode 100644
index 00000000..6e71c496
--- /dev/null
+++ b/opendc/api/v1/simulations/simulationId/experiments/experimentId/rack-states/endpoint.py
@@ -0,0 +1,51 @@
+from opendc.models.experiment import Experiment
+from opendc.models.rack_state import RackState
+from opendc.util import database, exceptions
+from opendc.util.rest import Response
+
+def GET(request):
+ """Get this Experiment's Tack States."""
+
+ # Make sure required parameters are there
+
+ try:
+ request.check_required_parameters(
+ path = {
+ 'simulationId': 'int',
+ 'experimentId': 'int'
+ }
+ )
+
+ except exceptions.ParameterError as e:
+ return Response(400, e.message)
+
+ # Instantiate an Experiment from the database
+
+ experiment = Experiment.from_primary_key((request.params_path['experimentId'],))
+
+ # Make sure this Experiment exisits
+
+ if not experiment.exists():
+ return Response(404, '{} not found.'.format(experiment))
+
+ # Make sure this user is authorized to view this Experiment's Rack States
+
+ if not experiment.google_id_has_at_least(request.google_id, 'VIEW'):
+ return Response(403, 'Forbidden from viewing Rack States for {}.'.format(experiment))
+
+ # Get and return the Rack States
+
+ if 'tick' in request.params_query:
+ rack_states = RackState.from_experiment_id_and_tick(
+ request.params_path['experimentId'],
+ request.params_query['tick']
+ )
+
+ else:
+ rack_states = RackState.from_experiment_id(request.params_path['experimentId'])
+
+ return Response(
+ 200,
+ 'Successfully retrieved Rack States for {}.'.format(experiment),
+ [x.to_JSON() for x in rack_states]
+ )
diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/room-states/__init__.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/room-states/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/opendc/api/v1/simulations/simulationId/experiments/experimentId/room-states/__init__.py
diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/room-states/endpoint.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/room-states/endpoint.py
new file mode 100644
index 00000000..d8c9fb45
--- /dev/null
+++ b/opendc/api/v1/simulations/simulationId/experiments/experimentId/room-states/endpoint.py
@@ -0,0 +1,51 @@
+from opendc.models.experiment import Experiment
+from opendc.models.room_state import RoomState
+from opendc.util import database, exceptions
+from opendc.util.rest import Response
+
+def GET(request):
+ """Get this Experiment's Room States."""
+
+ # Make sure required parameters are there
+
+ try:
+ request.check_required_parameters(
+ path = {
+ 'simulationId': 'int',
+ 'experimentId': 'int'
+ }
+ )
+
+ except exceptions.ParameterError as e:
+ return Response(400, e.message)
+
+ # Instantiate an Experiment from the database
+
+ experiment = Experiment.from_primary_key((request.params_path['experimentId'],))
+
+ # Make sure this Experiment exisits
+
+ if not experiment.exists():
+ return Response(404, '{} not found.'.format(experiment))
+
+ # Make sure this user is authorized to view this Experiment's Room States
+
+ if not experiment.google_id_has_at_least(request.google_id, 'VIEW'):
+ return Response(403, 'Forbidden from viewing Room States for {}.'.format(experiment))
+
+ # Get and return the Room States
+
+ if 'tick' in request.params_query:
+ room_states = RoomState.from_experiment_id_and_tick(
+ request.params_path['experimentId'],
+ request.params_query['tick']
+ )
+
+ else:
+ room_states = RoomState.from_experiment_id(request.params_path['experimentId'])
+
+ return Response(
+ 200,
+ 'Successfully retrieved Room States for {}.'.format(experiment),
+ [x.to_JSON() for x in room_states]
+ )
diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/statistics/__init__.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/statistics/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/opendc/api/v1/simulations/simulationId/experiments/experimentId/statistics/__init__.py
diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/statistics/task-durations/__init__.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/statistics/task-durations/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/opendc/api/v1/simulations/simulationId/experiments/experimentId/statistics/task-durations/__init__.py
diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/statistics/task-durations/endpoint.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/statistics/task-durations/endpoint.py
new file mode 100644
index 00000000..1bb60ca2
--- /dev/null
+++ b/opendc/api/v1/simulations/simulationId/experiments/experimentId/statistics/task-durations/endpoint.py
@@ -0,0 +1,44 @@
+from opendc.models.experiment import Experiment
+from opendc.models.task_duration import TaskDuration
+from opendc.util import database, exceptions
+from opendc.util.rest import Response
+
+def GET(request):
+ """Get this Experiment's Task Durations."""
+
+ # Make sure required parameters are there
+
+ try:
+ request.check_required_parameters(
+ path = {
+ 'simulationId': 'int',
+ 'experimentId': 'int'
+ }
+ )
+
+ except exceptions.ParameterError as e:
+ return Response(400, e.message)
+
+ # Instantiate an Experiment from the database
+
+ experiment = Experiment.from_primary_key((request.params_path['experimentId'],))
+
+ # Make sure this Experiment exisits
+
+ if not experiment.exists():
+ return Response(404, '{} not found.'.format(experiment))
+
+ # Make sure this user is authorized to view this Experiment's Task Durations
+
+ if not experiment.google_id_has_at_least(request.google_id, 'VIEW'):
+ return Response(403, 'Forbidden from viewing Task Durations for {}.'.format(experiment))
+
+ # Get and return the Task Durations
+
+ task_durations = TaskDuration.from_experiment_id(request.params_path['experimentId'])
+
+ return Response(
+ 200,
+ 'Successfully retrieved Task Durations for {}.'.format(experiment),
+ [x.to_JSON() for x in task_durations]
+ )
diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/task-states/__init__.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/task-states/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/opendc/api/v1/simulations/simulationId/experiments/experimentId/task-states/__init__.py
diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/task-states/endpoint.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/task-states/endpoint.py
new file mode 100644
index 00000000..b11512c0
--- /dev/null
+++ b/opendc/api/v1/simulations/simulationId/experiments/experimentId/task-states/endpoint.py
@@ -0,0 +1,51 @@
+from opendc.models.experiment import Experiment
+from opendc.models.task_state import TaskState
+from opendc.util import database, exceptions
+from opendc.util.rest import Response
+
+def GET(request):
+ """Get this Experiment's Task States."""
+
+ # Make sure required parameters are there
+
+ try:
+ request.check_required_parameters(
+ path = {
+ 'simulationId': 'int',
+ 'experimentId': 'int'
+ }
+ )
+
+ except exceptions.ParameterError as e:
+ return Response(400, e.message)
+
+ # Instantiate an Experiment from the database
+
+ experiment = Experiment.from_primary_key((request.params_path['experimentId'],))
+
+ # Make sure this Experiment exists
+
+ if not experiment.exists():
+ return Response(404, '{} not found.'.format(experiment))
+
+ # Make sure this user is authorized to view Task States for this Experiment
+
+ if not experiment.google_id_has_at_least(request.google_id, 'VIEW'):
+ return Response(403, 'Forbidden from viewing Task States for {}.'.format(experiment))
+
+ # Get and return the Task States
+
+ if 'tick' in request.params_query:
+ task_states = TaskState.from_experiment_id_and_tick(
+ request.params_path['experimentId'],
+ request.params_query['tick']
+ )
+
+ else:
+ task_states = TaskState.query('experiment_id', request.params_path['experimentId'])
+
+ return Response(
+ 200,
+ 'Successfully retrieved Task States for {}.'.format(experiment),
+ [x.to_JSON() for x in task_states]
+ )