Exemplo n.º 1
0
def edit_result(result_id=None):
    logging.info("Received API edit request for result_id: {%s}", result_id)
    logging.debug("POST params: %s", request.form)
    if not result_id:
        logging.warning("Receieved invalid result_id {%s}", result_id)
        return JSON_FAILURE(reason='Invalid Result')

    result = TestResult.get_result_by_id(result_id)
    if not result:
        logging.warning("Could not find result in db (result_id={%s}",
                        result_id)
        return JSON_FAILURE(reason='Invalid Result')

    logging.debug("Setting device IP to {%s}", str(request.remote_addr))
    result.device_ip = str(request.remote_addr)

    # Columns allowed to be updated and their types
    columns = TestResult.get_public_columns()

    for col in columns:
        if col in request.form:
            col_type = columns[col]
            logging.debug("Parsing column in request, {'%s' = '%s', type=%s}",
                          str(col), str(request.form[col]), str(col_type))
            datum = col_type(request.form[col])
            setattr(result, col, datum)

    # Request from Nic
    if "throughput_latency" in request.form:
        result.download_latencies = [float(request.form["throughput_latency"])]
        result.upload_latencies = [float(request.form["throughput_latency"])]

    result.save()

    return JSON_SUCCESS()
Exemplo n.º 2
0
def result_status(result_id=None):
    if not result_id:
        return JSON_FAILURE(reason='Invalid Result')

    result = TestResult.get_result_by_id(result_id)
    if not result:
        return JSON_FAILURE(reason='Invalid Result')

    return JSON_SUCCESS(state=result.state)
Exemplo n.º 3
0
def get_result(result_id=None):
    logging.info("Retrieving result for result_id %s", result_id)
    if not result_id:
        return JSON_FAILURE(reason='Invalid Result')

    result = TestResult.get_result_by_id(result_id)
    if not result:
        return JSON_FAILURE(reason='Invalid Result')

    return JSON_SUCCESS(**result.exportDict())
Exemplo n.º 4
0
def login():
    """ Called when an API user wishes to log in. """
    if 'email' not in request.form or 'password' not in request.form:
        return (JSON_FAILURE(), 401)

    email = request.form['email']
    submitted_password = request.form['password']
    this_user = User.log_user_in(email, submitted_password)

    if not this_user:
        return (JSON_FAILURE(), 401)

    return JSON_SUCCESS(user_token=this_user.new_token())
Exemplo n.º 5
0
def delete_result():
    if User.from_session():
        rec = TestResult.get_result_by_id(request.form['test_id'])
        rec.delete()

        return JSON_SUCCESS()
    else:
        return JSON_FAILURE()
Exemplo n.º 6
0
def delete_set(set_id=None):
    invalid_set = JSON_FAILURE(reason='Invalid Set')
    if not set_id:
        return invalid_set

    user_set = TestSet.get_set_by_id(set_id)
    if not user_set:
        return invalid_set

    user_set.delete()
    return JSON_SUCCESS()
Exemplo n.º 7
0
def edit():
    ip = request.remote_addr
    token = request.form['router_token'].strip()

    rec = TestResult.get_result_by_token_ip(token, ip)

    if not rec:
        return JSON_FAILURE(reason="Invalid token or IP.")
    if 'data' not in request.form:
        return JSON_FAILURE(reason="Missing 'data' parameter")
    # Columns allowed to be updated and their types
    columns = TestResult.get_public_columns()

    data = json.loads(request.form['data'])

    for col in columns:
        if col in data:
            col_type = columns[col]
            datum = col_type(data[col])
            setattr(rec, col, datum)

    ploss_cnt, ploss_len = 0, 0

    # Calculate packet loss from the down/up latencies. (-1 = packet lost)
    if rec.download_latencies:
        ploss_cnt += rec.download_latencies.count(-1)
        ploss_len += len(rec.download_latencies)

    if rec.upload_latencies:
        ploss_cnt += rec.upload_latencies.count(-1)
        ploss_len += len(rec.upload_latencies)

    if ploss_len > 0:
        rec.packet_loss_under_load = float(ploss_cnt) / float(ploss_len)

    rec.save()

    return JSON_SUCCESS()
Exemplo n.º 8
0
def get_config():
    ip = request.remote_addr
    token = request.form['router_token'].strip()

    rec = TestResult.get_result_by_token_ip(token, ip)

    if 'data' in request.form:
        data = json.loads(request.form['data'])
        rec.interface_stats = data

    if not rec:
        return JSON_FAILURE()

    rec.state = 'running'
    db.session.commit()
    rec.save()

    config = TestConfiguration()

    return JSON_SUCCESS(config=config.get_config())
Exemplo n.º 9
0
 def __init__(self, invalid_handler=None):
     if invalid_handler:
         self.invalid_handler = invalid_handler
     else:
         self.invalid_handler = lambda: (JSON_FAILURE(), 401)
Exemplo n.º 10
0
def start_test(test_type=None):
    logging.info("Starting test for device type {%s}", str(test_type))
    if 'set_id' not in request.form:
        return JSON_FAILURE(reason='Missing set_id')

    set_id = request.form['set_id'].strip()
    test_set = TestSet.get_set_by_id(set_id)
    logging.debug("Found test_set: {%s}", str(test_set))

    if not test_set:
        return JSON_FAILURE(reason='Invalid set_id', bad_id=str(set_id))
    if not test_type:
        return JSON_FAILURE(reason='Invalid test type',
                            test_type=str(test_type))

    config = TestConfiguration()
    conf_json = config.get_config()

    if test_type == 'mobile':
        # Create a new mobile TestResult
        result = test_set.new_result(device_type="mobile")

        # Commit to database
        test_set.save()
        result.save()

        # Return config + new result_id
        return JSON_SUCCESS(
            config=conf_json,
            result_id=result.test_id,
        )
    elif test_type == 'router':
        # Create a new Router TestResult
        result = test_set.new_result(device_type="router")

        if 'address' in request.form:
            # If the client specified a router address, use that
            ip = request.form['address']
        else:
            # Get IP of connected client. This should be the address for the router
            ip = request.remote_addr

        router = Router(ip)
        result.test_token = base64.b64encode(router.req_id)
        result.device_ip = ip

        result.save()
        test_set.save()

        try:
            # Send a magic packet to the router
            router.wakeup()
        except NameError:
            return JSON_FAILURE()

        # Return config + new result_id
        return JSON_SUCCESS(
            config=conf_json,
            result_id=result.test_id,
        )
    return JSON_FAILURE(reason="Invalid test type!")
Exemplo n.º 11
0
def test_finished(result_id):
    test_result = TestResult.get_result_by_id(result_id)
    if not test_result:
        return JSON_FAILURE(reason="Invalid test result id")

    return JSON_SUCCESS(state=test_result.state)