Esempio n. 1
0
def compute_resource_volume_sum(resource, meter):
    """Return the total volume for a meter.

    :param resource: The ID of the resource.
    :param meter: The name of the meter.
    :param start_timestamp: ISO-formatted string of the
        earliest time to include in the calculation.
    :param end_timestamp: ISO-formatted string of the
        latest time to include in the calculation.
    :param search_offset: Number of minutes before and
        after start and end timestamps to query.
    """
    q_ts = _get_query_timestamps(flask.request.args)

    # Query the database for the max volume
    f = storage.EventFilter(
        meter=meter,
        project=acl.get_limited_to_project(flask.request.headers),
        resource=resource,
        start=q_ts['query_start'],
        end=q_ts['query_end'],
    )
    # TODO(sberler): do we want to return an error if the resource
    # does not exist?
    results = list(flask.request.storage_conn.get_volume_sum(f))
    value = None
    if results:
        value = results[0].get('value')  # there should only be one!

    return flask.jsonify(volume=value)
Esempio n. 2
0
def compute_duration_by_resource(resource, meter):
    """Return the earliest timestamp, last timestamp,
    and duration for the resource and meter.

    :param resource: The ID of the resource.
    :param meter: The name of the meter.
    :param start_timestamp: ISO-formatted string of the
        earliest timestamp to return.
    :param end_timestamp: ISO-formatted string of the
        latest timestamp to return.
    :param search_offset: Number of minutes before
        and after start and end timestamps to query.
    """
    q_ts = _get_query_timestamps(flask.request.args)
    start_timestamp = q_ts['start_timestamp']
    end_timestamp = q_ts['end_timestamp']

    # Query the database for the interval of timestamps
    # within the desired range.
    f = storage.EventFilter(
        meter=meter,
        project=acl.get_limited_to_project(flask.request.headers),
        resource=resource,
        start=q_ts['query_start'],
        end=q_ts['query_end'],
    )
    stats = flask.request.storage_conn.get_meter_statistics(f)
    min_ts, max_ts = stats.duration_start, stats.duration_end

    # "Clamp" the timestamps we return to the original time
    # range, excluding the offset.
    LOG.debug('start_timestamp %s, end_timestamp %s, min_ts %s, max_ts %s',
              start_timestamp, end_timestamp, min_ts, max_ts)
    if start_timestamp and min_ts and min_ts < start_timestamp:
        min_ts = start_timestamp
        LOG.debug('clamping min timestamp to range')
    if end_timestamp and max_ts and max_ts > end_timestamp:
        max_ts = end_timestamp
        LOG.debug('clamping max timestamp to range')

    # If we got valid timestamps back, compute a duration in minutes.
    #
    # If the min > max after clamping then we know the
    # timestamps on the samples fell outside of the time
    # range we care about for the query, so treat them as
    # "invalid."
    #
    # If the timestamps are invalid, return None as a
    # sentinal indicating that there is something "funny"
    # about the range.
    if min_ts and max_ts and (min_ts <= max_ts):
        duration = timeutils.delta_seconds(min_ts, max_ts)
    else:
        min_ts = max_ts = duration = None

    return flask.jsonify(start_timestamp=min_ts,
                         end_timestamp=max_ts,
                         duration=duration,
                         )
Esempio n. 3
0
def list_meters_all():
    """Return a list of meters.
    :param metadata.<key> match on the metadata within the resource. (optional)
    """
    rq = flask.request
    meters = rq.storage_conn.get_meters(
        project=acl.get_limited_to_project(rq.headers),
        metaquery=_get_metaquery(rq.args))
    return flask.jsonify(meters=list(meters))
Esempio n. 4
0
def list_meters_all():
    """Return a list of meters.
    :param metadata.<key>: match on the metadata within the resource.
                           (optional)
    """
    rq = flask.request
    meters = rq.storage_conn.get_meters(
        project=acl.get_limited_to_project(rq.headers),
        metaquery=_get_metaquery(rq.args))
    return flask.jsonify(meters=[m.as_dict() for m in meters])
Esempio n. 5
0
def _list_users(source=None):
    """Return a list of user names.
    """
    # TODO(jd) it might be better to return the real list of users that are
    # belonging to the project, but that's not provided by the storage
    # drivers for now
    if acl.get_limited_to_project(flask.request.headers):
        users = [flask.request.headers.get('X-User-id')]
    else:
        users = flask.request.storage_conn.get_users(source=source)
    return flask.jsonify(users=list(users))
Esempio n. 6
0
def _list_users(source=None):
    """Return a list of user names.
    """
    # TODO(jd) it might be better to return the real list of users that are
    # belonging to the project, but that's not provided by the storage
    # drivers for now
    if acl.get_limited_to_project(flask.request.headers):
        users = [flask.request.headers.get('X-User-id')]
    else:
        users = flask.request.storage_conn.get_users(source=source)
    return flask.jsonify(users=list(users))
Esempio n. 7
0
def list_meters_by_source(source):
    """Return a list of meters by source.

    :param source: The ID of the owning source.
    :param metadata.<key> match on the metadata within the resource. (optional)
    """
    rq = flask.request
    meters = rq.storage_conn.get_meters(
        source=source,
        project=acl.get_limited_to_project(rq.headers),
        metaquery=_get_metaquery(rq.args))
    return flask.jsonify(meters=list(meters))
Esempio n. 8
0
def list_all_resources():
    """Return a list of all known resources.

    :param start_timestamp: Limits resources by last update time >= this value.
        (optional)
    :type start_timestamp: ISO date in UTC
    :param end_timestamp: Limits resources by last update time < this value.
        (optional)
    :type end_timestamp: ISO date in UTC
    :param metadata.<key> match on the metadata within the resource. (optional)
    """
    return _list_resources(
        project=acl.get_limited_to_project(flask.request.headers))
Esempio n. 9
0
def list_meters_by_user(user):
    """Return a list of meters by user.

    :param user: The ID of the owning user.
    :param metadata.<key>: match on the metadata within the resource.
                           (optional)
    """
    rq = flask.request
    meters = rq.storage_conn.get_meters(
        user=user,
        project=acl.get_limited_to_project(rq.headers),
        metaquery=_get_metaquery(rq.args))
    return flask.jsonify(meters=[m.as_dict() for m in meters])
Esempio n. 10
0
def list_all_resources():
    """Return a list of all known resources.

    :param start_timestamp: Limits resources by last update time >= this value.
        (optional)
    :type start_timestamp: ISO date in UTC
    :param end_timestamp: Limits resources by last update time < this value.
        (optional)
    :type end_timestamp: ISO date in UTC
    :param metadata.<key>: match on the metadata within the resource.
                           (optional)
    """
    return _list_resources(
        project=acl.get_limited_to_project(flask.request.headers))
Esempio n. 11
0
def list_resources_by_user(user):
    """Return a list of resources owned by the user.

    :param user: The ID of the owning user.
    :param start_timestamp: Limits resources by last update time >= this value.
        (optional)
    :type start_timestamp: ISO date in UTC
    :param end_timestamp: Limits resources by last update time < this value.
        (optional)
    :type end_timestamp: ISO date in UTC
    :param metadata.<key> match on the metadata within the resource. (optional)
    """
    return _list_resources(
        user=user,
        project=acl.get_limited_to_project(flask.request.headers),
    )
Esempio n. 12
0
def _list_projects(source=None):
    """Return a list of project names.
    """
    project = acl.get_limited_to_project(flask.request.headers)
    if project:
        if source:
            if project in flask.request.storage_conn.get_projects(
                    source=source):
                projects = [project]
            else:
                projects = []
        else:
            projects = [project]
    else:
        projects = flask.request.storage_conn.get_projects(source=source)
    return flask.jsonify(projects=list(projects))
Esempio n. 13
0
def _list_projects(source=None):
    """Return a list of project names.
    """
    project = acl.get_limited_to_project(flask.request.headers)
    if project:
        if source:
            if project in flask.request.storage_conn.get_projects(
                    source=source):
                projects = [project]
            else:
                projects = []
        else:
            projects = [project]
    else:
        projects = flask.request.storage_conn.get_projects(source=source)
    return flask.jsonify(projects=list(projects))
Esempio n. 14
0
def list_samples_by_user(user, meter):
    """Return a list of raw samples for the user.

    :param user: The ID of the user.
    :param meter: The name of the meter.
    :param start_timestamp: Limits samples by timestamp >= this value.
        (optional)
    :type start_timestamp: ISO date in UTC
    :param end_timestamp: Limits samples by timestamp < this value.
        (optional)
    :type end_timestamp: ISO date in UTC
    """
    return _list_samples(
        user=user,
        meter=meter,
        project=acl.get_limited_to_project(flask.request.headers),
    )
Esempio n. 15
0
def list_resources_by_source(source):
    """Return a list of resources for which a source is reporting
    data.

    :param source: The ID of the reporting source.
    :param start_timestamp: Limits resources by last update time >= this value.
        (optional)
    :type start_timestamp: ISO date in UTC
    :param end_timestamp: Limits resources by last update time < this value.
        (optional)
    :type end_timestamp: ISO date in UTC
    :param metadata.<key> match on the metadata within the resource. (optional)
    """
    return _list_resources(
        source=source,
        project=acl.get_limited_to_project(flask.request.headers),
    )
Esempio n. 16
0
def list_events_by_user(user, meter):
    """Return a list of raw metering events for the user.

    :param user: The ID of the user.
    :param meter: The name of the meter.
    :param start_timestamp: Limits events by timestamp >= this value.
        (optional)
    :type start_timestamp: ISO date in UTC
    :param end_timestamp: Limits events by timestamp < this value.
        (optional)
    :type end_timestamp: ISO date in UTC
    """
    return _list_events(
        user=user,
        meter=meter,
        project=acl.get_limited_to_project(flask.request.headers),
    )
Esempio n. 17
0
def list_samples_by_source(source, meter):
    """Return a list of raw samples for the source.

    :param source: The ID of the reporting source.
    :param meter: The name of the meter.
    :param start_timestamp: Limits samples by timestamp >= this value.
        (optional)
    :type start_timestamp: ISO date in UTC
    :param end_timestamp: Limits samples by timestamp < this value.
        (optional)
    :type end_timestamp: ISO date in UTC
    """
    return _list_samples(
        source=source,
        meter=meter,
        project=acl.get_limited_to_project(flask.request.headers),
    )
Esempio n. 18
0
def list_resources_by_user(user):
    """Return a list of resources owned by the user.

    :param user: The ID of the owning user.
    :param start_timestamp: Limits resources by last update time >= this value.
        (optional)
    :type start_timestamp: ISO date in UTC
    :param end_timestamp: Limits resources by last update time < this value.
        (optional)
    :type end_timestamp: ISO date in UTC
    :param metadata.<key>: match on the metadata within the resource.
                           (optional)
    """
    return _list_resources(
        user=user,
        project=acl.get_limited_to_project(flask.request.headers),
    )
Esempio n. 19
0
def list_resources_by_source(source):
    """Return a list of resources for which a source is reporting
    data.

    :param source: The ID of the reporting source.
    :param start_timestamp: Limits resources by last update time >= this value.
        (optional)
    :type start_timestamp: ISO date in UTC
    :param end_timestamp: Limits resources by last update time < this value.
        (optional)
    :type end_timestamp: ISO date in UTC
    :param metadata.<key>: match on the metadata within the resource.
                           (optional)
    """
    return _list_resources(
        source=source,
        project=acl.get_limited_to_project(flask.request.headers),
    )
Esempio n. 20
0
def compute_resource_volume_sum(resource, meter):
    """Return the sum of samples for a meter.

    :param resource: The ID of the resource.
    :param meter: The name of the meter.
    :param start_timestamp: ISO-formatted string of the
        earliest time to include in the calculation.
    :param end_timestamp: ISO-formatted string of the
        latest time to include in the calculation.
    :param search_offset: Number of minutes before and
        after start and end timestamps to query.
    """
    return _get_statistics(
        'sum',
        meter=meter,
        resource=resource,
        project=acl.get_limited_to_project(flask.request.headers),
    )
Esempio n. 21
0
def check_authorized_project(project):
    authorized_project = acl.get_limited_to_project(flask.request.headers)
    if authorized_project and authorized_project != project:
        flask.abort(404)
Esempio n. 22
0
def check_authorized_project(project):
    authorized_project = acl.get_limited_to_project(flask.request.headers)
    if authorized_project and authorized_project != project:
        flask.abort(404)