Пример #1
0
def GET(request):
    """Get this Experiment."""

    try:
        request.check_required_parameters(path={'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())
Пример #2
0
def DELETE(request):
    """Delete this Experiment."""

    # Make sure required parameters are there

    try:
        request.check_required_parameters(path={'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())
Пример #3
0
def GET(request):
    """Get this Experiment's last simulated tick."""

    # Make sure required parameters are there

    try:
        request.check_required_parameters(path={'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's last simulated tick

    if not experiment.google_id_has_at_least(request.google_id, 'VIEW'):
        return Response(
            403, 'Forbidden from viewing last simulated tick for {}.'.format(
                experiment))

    return Response(
        200, 'Successfully retrieved last simulated tick for {}.'.format(
            experiment), {'lastSimulatedTick': experiment.last_simulated_tick})
Пример #4
0
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={'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())
Пример #5
0
def GET(request):
    """Get this Experiment's Task States."""

    # Make sure required parameters are there

    try:
        request.check_required_parameters(
            path={
                '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]
    )
Пример #6
0
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])
Пример #7
0
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'])
    experiment.state = 'QUEUED'
    experiment.last_simulated_tick = 0

    # Try to insert this Experiment

    try:
        experiment.insert()

    except exceptions.ForeignKeyError as e:
        return Response(400, 'Foreign key constraint not met.' + e.message)

    # Return this Experiment

    experiment.read()

    return Response(200, 'Successfully added {}.'.format(experiment),
                    experiment.to_JSON())