Esempio n. 1
0
 def __init__(self, start_time=None, end_time=None, event_type=None,
              message_id=None, traits_filter=[]):
     self.start_time = utils.sanitize_timestamp(start_time)
     self.end_time = utils.sanitize_timestamp(end_time)
     self.message_id = message_id
     self.event_type = event_type
     self.traits_filter = traits_filter
Esempio n. 2
0
def point_to_stat(point, tags, period, aggregate, unit='%'):
    """Convert the InfluxDB point to the Statistics object"""

    kwargs = {}
    if not point['last'] or not point['first']:
        return
    if not aggregate:
        for func in DEFAULT_AGGREGATES:
            kwargs[BACK_FUNC_TRANSITIONS.get(func, func)] = point.get(func)
    else:
        kwargs['aggregate'] = {}
        for description in aggregate:
            func = AGGREGATE_FUNC_TRANSITIONS.get(description.func,
                                                  description.func)
            kwargs['aggregate'][description.func] = point.get(func)
            if func in DEFAULT_AGGREGATES:
                kwargs[description.func] = point.get(func)

    tags = tags or {}
    tags = dict((key.replace("metadata", "resource_metadata"), value)
                for key, value in tags.items())
    kwargs["groupby"] = tags
    kwargs["duration_start"] = utils.sanitize_timestamp(point["first"])
    kwargs["duration_end"] = utils.sanitize_timestamp(point["last"])
    kwargs["duration"] = (kwargs["duration_end"] -
                          kwargs["duration_start"]).total_seconds()
    kwargs["period"] = period or 0
    kwargs["period_start"] = utils.sanitize_timestamp(point["time"])
    kwargs["period_end"] = (utils.sanitize_timestamp(point["time"]) +
                            datetime.timedelta(seconds=period)
                            if period else kwargs['duration_end'])
    kwargs["unit"] = unit
    return models.Statistics(**kwargs)
Esempio n. 3
0
 def __init__(
     self,
     user=None,
     project=None,
     start_timestamp=None,
     start_timestamp_op=None,
     end_timestamp=None,
     end_timestamp_op=None,
     resource=None,
     meter=None,
     source=None,
     message_id=None,
     metaquery=None,
 ):
     self.user = user
     self.project = project
     self.start_timestamp = utils.sanitize_timestamp(start_timestamp)
     self.start_timestamp_op = start_timestamp_op
     self.end_timestamp = utils.sanitize_timestamp(end_timestamp)
     self.end_timestamp_op = end_timestamp_op
     self.resource = resource
     self.meter = meter
     self.source = source
     self.metaquery = metaquery or {}
     self.message_id = message_id
Esempio n. 4
0
 def __init__(self, start_time=None, end_time=None, event_type=None,
              message_id=None, traits_filter=[]):
     self.start_time = utils.sanitize_timestamp(start_time)
     self.end_time = utils.sanitize_timestamp(end_time)
     self.message_id = message_id
     self.event_type = event_type
     self.traits_filter = traits_filter
Esempio n. 5
0
 def __init__(self, start_timestamp=None, end_timestamp=None,
              event_type=None, message_id=None, traits_filter=None,
              admin_proj=None):
     self.start_timestamp = utils.sanitize_timestamp(start_timestamp)
     self.end_timestamp = utils.sanitize_timestamp(end_timestamp)
     self.message_id = message_id
     self.event_type = event_type
     self.traits_filter = traits_filter or []
     self.admin_proj = admin_proj
Esempio n. 6
0
 def __init__(self, user=None, project=None, start=None, end=None,
              resource=None, meter=None, source=None, metaquery={}):
     self.user = user
     self.project = project
     self.start = utils.sanitize_timestamp(start)
     self.end = utils.sanitize_timestamp(end)
     self.resource = resource
     self.meter = meter
     self.source = source
     self.metaquery = metaquery
Esempio n. 7
0
    def get_time_boundary(self, sample_filter):
        """Find timestamp of the first matching sample in the database."""

        response = self._query(
            influx_utils.make_time_bounds_query(sample_filter))
        try:
            first_point = response.get_points(influx_utils.MEASUREMENT).next()
        except StopIteration:
            raise base.NoResultFound()

        start_timestamp = utils.sanitize_timestamp(first_point['first'])
        end_timestamp = utils.sanitize_timestamp(first_point['last'])
        return start_timestamp, end_timestamp
Esempio n. 8
0
def search_results_to_resources(results):
    """Transforms results of the search to the Resource instances."""

    for record in results['hits']['hits']:
        yield models.Resource(
            resource_id=record['_id'],
            first_sample_timestamp=utils.sanitize_timestamp(
                record['_source'].get('first_sample_timestamp')),
            last_sample_timestamp=utils.sanitize_timestamp(
                record['_source']['last_sample_timestamp']),
            source=record['_source'].get('source'),
            project_id=record['_source'].get('project_id'),
            user_id=record['_source'].get('user_id'),
            metadata=record['_source'].get('metadata', {}))
Esempio n. 9
0
def sample_to_point(sample):
    point = {
        "measurement": 'sample',
        "time": utils.sanitize_timestamp(sample["timestamp"]),
        "fields": {
            "value": float(sample.get("counter_volume")),
            "message_id": sample.get("message_id"),
            "timestamp": sample.get("timestamp").isoformat(),
            "message_signature": sample.get("message_signature"),
            "type": sample.get("counter_type"),
            "unit": sample.get("counter_unit"),
            "recorded_at": timeutils.utcnow(True).isoformat(),
        },
        "tags": {
            "meter": sample.get("counter_name"),
            "resource_id": sample.get("resource_id"),
            "project_id": sample.get("project_id"),
            "user_id": sample.get("user_id"),
            "source": sample.get("source")
        }
    }
    tags, fields = escape_metadata(sample.get("resource_metadata", {}))
    point['tags'].update(tags)
    point['fields'].update(fields)
    return point
Esempio n. 10
0
 def __init__(self, user=None, project=None,
              start_timestamp=None, start_timestamp_op=None,
              end_timestamp=None, end_timestamp_op=None,
              resource=None, meter=None,
              source=None, message_id=None,
              metaquery=None):
     self.user = user
     self.project = project
     self.start_timestamp = utils.sanitize_timestamp(start_timestamp)
     self.start_timestamp_op = start_timestamp_op
     self.end_timestamp = utils.sanitize_timestamp(end_timestamp)
     self.end_timestamp_op = end_timestamp_op
     self.resource = resource
     self.meter = meter
     self.source = source
     self.metaquery = metaquery or {}
     self.message_id = message_id
Esempio n. 11
0
def point_to_sample(point):
    """Transform  the point to a Sample object."""

    return models.Sample(
        point.get("source"),
        point["meter"],
        point.get("type"),
        point.get("unit", ""),
        float(point["value"]),
        point.get("user_id"),
        point.get("project_id"),
        point.get("resource_id"),
        utils.sanitize_timestamp(point["timestamp"]),
        transform_metadata(point),
        point.get("message_id"),
        point.get("message_signature"),
        recorded_at=(utils.sanitize_timestamp(point['recorded_at'])
                     if "recorded_at" in point else None))
def count_samples(storage_conn, start_timestamp=None, end_timestamp=None):
    if start_timestamp:
        start_timestamp = utils.sanitize_timestamp(start_timestamp)
    if start_timestamp:
        end_timestamp = utils.sanitize_timestamp(end_timestamp)
    if isinstance(storage_conn, impl_sqlalchemy.Connection):
        from ceilometer.storage.sqlalchemy import models
        session = storage_conn._engine_facade.get_session()
        query = session.query(models.Sample.id)
        if start_timestamp:
            query = query.filter(models.Sample.timestamp >= start_timestamp)
        if end_timestamp:
            query = query.filter(models.Sample.timestamp < end_timestamp)
        return query.count()
    elif isinstance(storage_conn, impl_mongodb.Connection):
        ts_range = pymongo_utils.make_timestamp_range(start_timestamp,
                                                      end_timestamp)
        return storage_conn.db.meter.count(ts_range)
    else:
        print("Unsupported type of storage connection: %s" % storage_conn)
        sys.exit(1)
Esempio n. 13
0
 def __init__(self, start, end, event_name=None, traits={}):
     self.start = utils.sanitize_timestamp(start)
     self.end = utils.sanitize_timestamp(end)
     self.event_name = event_name
     self.traits = traits
Esempio n. 14
0
 def __init__(self, start, end, event_name=None, traits={}):
     self.start = utils.sanitize_timestamp(start)
     self.end = utils.sanitize_timestamp(end)
     self.event_name = event_name
     self.traits = traits