Exemplo n.º 1
0
def id_agents():
    """Get last contact data from the DB.

    Args:
        None

    Returns:
        data: JSON data for the selected agent

    """
    # Initialize key variables
    data = []
    outcomes = defaultdict(lambda: defaultdict(dict))
    agent_data = defaultdict(lambda: defaultdict(dict))

    # Get starting timestamp
    secondsago = general.integerize(request.args.get('secondsago'))
    timestamp = general.integerize(request.args.get('ts_start'))
    ts_start = _ts_start(secondsago, timestamp)

    # Get the agent ids assigned to each datapoint
    mapping = db_multitable.datapoint_summary()

    # Get the contacts
    contacts = db_data.last_contacts(ts_start)

    # Store the contacts according to id_agent and agent_label
    for contact in contacts:
        # Track last contacts for each agent_label of each id_agent
        data_dict = {
            'timestamp': contact['timestamp'],
            'value': contact['value']}
        idx_datapoint = contact['idx_datapoint']
        id_agent = mapping[idx_datapoint]['id_agent']
        agent_label = mapping[idx_datapoint]['agent_label']
        outcomes[id_agent][agent_label] = data_dict

        # Track summary data for each id_agent
        agent_data[
            id_agent]['agent'] = mapping[idx_datapoint]['agent']
        agent_data[
            id_agent]['devicename'] = mapping[idx_datapoint]['devicename']

    # Create a list of dicts of contacts keyed by id_agent
    for id_agent, label_dict in outcomes.items():
        # Initalize dict for id_agent data
        new_dict = defaultdict(lambda: defaultdict(dict))
        for agent_label, value_dict in label_dict.items():
            new_dict['timeseries'][agent_label] = value_dict

        # Assign more new_dict values
        new_dict['agent'] = agent_data[id_agent]['agent']
        new_dict['devicename'] = agent_data[id_agent]['devicename']
        new_dict['id_agent'] = id_agent

        # Append dict to data
        data.append(new_dict)

    # Return
    return jsonify(data)
Exemplo n.º 2
0
def datapoints_query():
    """Get datapoint data filtered by query string values.

    Args:
        None

    Returns:
        data: JSON data for the selected agent

    """
    # Initialize key variables
    id_datapoint = request.args.get('id_datapoint')
    idx_deviceagent = request.args.get('idx_deviceagent')
    base_type = request.args.get('base_type')

    if bool(id_datapoint) is True:
        query = db_datapoint.GetIDDatapoint(id_datapoint)
        intermediate = query.everything()
        data = []
        data.append(intermediate)

    elif bool(idx_deviceagent) is True:
        data = db_datapoint.listing(
            general.integerize(idx_deviceagent), base_type=base_type)

    else:
        abort(404)

    # Return
    return jsonify(data)
Exemplo n.º 3
0
def getdata(value):
    """Get Agent data from the DB by idx value.

    Args:
        value: idx_datapoint value

    Returns:
        data: JSON data for the selected agent

    """
    # Initialize key variables
    idx_datapoint = int(value)
    secondsago = general.integerize(request.args.get('secondsago'))
    ts_stop = general.integerize(request.args.get('ts_start'))
    ts_start = general.integerize(request.args.get('ts_start'))

    # Process start and stop times
    if bool(secondsago) is True:
        ts_stop = int(datetime.utcnow().timestamp())
        ts_start = ts_stop - abs(secondsago)
    else:
        if bool(ts_start) is True and bool(ts_stop) is True:
            ts_start = abs(general.normalized_timestamp(
                general.integerize(request.args.get('ts_start'))
                ))
            ts_stop = abs(general.normalized_timestamp(
                general.integerize(request.args.get('ts_stop'))
                ))
        else:
            abort(404)

    # Fix start and stop times
    if ts_start > ts_stop:
        ts_start = ts_stop

    # Fail if more than a year of data is being requested
    if ts_stop - ts_start >= 31536000:
        abort(404)

    # Get data
    query = db_data.GetIDXData(CONFIG, idx_datapoint, ts_start, ts_stop)
    data = query.everything()

    # Return
    return jsonify(data)
Exemplo n.º 4
0
def lastcontacts():
    """Get last contact data from the DB.

    Args:
        None

    Returns:
        data: JSON data for the selected agent

    """
    # Get starting timestamp
    secondsago = general.integerize(request.args.get('secondsago'))
    timestamp = general.integerize(request.args.get('ts_start'))
    ts_start = _ts_start(secondsago, timestamp)

    # Get data
    data = db_data.last_contacts(ts_start)

    # Return
    return jsonify(data)
Exemplo n.º 5
0
def devicename_agents(devicename, id_agent):
    """Get last contact data from the DB.

    Args:
        devicename: Device table devicename
        id_agent: Agent table id_agent

    Returns:
        data: JSON data for the selected agent

    """
    # Initialize key variables
    data = []

    # Get starting timestamp
    secondsago = general.integerize(request.args.get('secondsago'))
    timestamp = general.integerize(request.args.get('ts_start'))
    ts_start = _ts_start(secondsago, timestamp)

    # Get idx_device and idx_agent
    device = db_device.GetDevice(devicename)
    if device.exists() is True:
        # Device Found
        idx_device = device.idx_device()

        # Now find idx_agent
        agent = db_agent.GetIDAgent(id_agent)
        if agent.exists() is True:
            idx_agent = agent.idx_agent()

        # Now get the idx_deviceagent
        deviceagent = db_deviceagent.GetDeviceAgent(idx_device, idx_agent)
        if deviceagent.exists() is True:
            idx_deviceagent = deviceagent.idx_deviceagent()

            # Now get the data
            data = db_data.last_contacts_by_device(
                int(idx_deviceagent), int(ts_start))

    # Return
    return jsonify(data)
Exemplo n.º 6
0
def deviceagents(value):
    """Get last contact data from the DB.

    Args:
        value: Index from the DeviceAgent table
        ts_start: Timestamp to start from

    Returns:
        data: JSON data for the selected agent

    """
    # Initialize key variables
    idx_deviceagent = int(value)

    # Get starting timestamp
    secondsago = general.integerize(request.args.get('secondsago'))
    timestamp = general.integerize(request.args.get('ts_start'))
    ts_start = _ts_start(secondsago, timestamp)

    # Get data
    data = db_data.last_contacts_by_device(idx_deviceagent, ts_start)

    # Return
    return jsonify(data)
Exemplo n.º 7
0
def datapoints(idx_datapoint):
    """Get datapoint data filtered by datapoint index value.

    Args:
        idx_datapoint: Datapoint index value

    Returns:
        data: JSON data for the selected agent

    """
    # Get data
    query = db_datapoint.GetIDXDatapoint(general.integerize(idx_datapoint))
    data = query.everything()

    # Return
    return jsonify(data)
Exemplo n.º 8
0
def agents(idx_agent):
    """Get Agent data from the DB by idx value.

    Args:
        idx_agent: Agent table idx_agent

    Returns:
        data: JSON data for the selected agent

    """
    # Get data
    query = db_agent.GetIDXAgent(general.integerize(idx_agent))
    data = query.everything()

    # Return
    return jsonify(data)
Exemplo n.º 9
0
def agents(idx_agent):
    """Get Agent data from the DB by idx value.

    Args:
        idx_agent: Agent table idx_agent

    Returns:
        data: JSON data for the selected agent

    """
    # Get data from cache
    key = ('DB/Agent/idx_agent/{}'.format(idx_agent))
    cache_value = CACHE.get(key)

    # Process cache miss
    if cache_value is None:
        query = db_agent.GetIDXAgent(general.integerize(idx_agent))
        data = query.everything()
        CACHE.set(key, data)
    else:
        data = cache_value

    # Return
    return jsonify(data)