def compute(self, storage_obj, metric, start, stop, window=None, center=False): """Returns list of (timestamp, window, aggregated value) tuples. :param storage_obj: a call is placed to the storage object to retrieve the stored data. :param metric: the metric :param start: start timestamp :param stop: stop timestamp :param window: format string specifying the size over which to aggregate the retrieved data :param center: how to index the aggregated data (central timestamp or leftmost timestamp) """ if window is None: raise aggregates.CustomAggFailure( 'Moving aggregate must have window specified.') try: window = utils.to_timespan(window) except ValueError: raise aggregates.CustomAggFailure('Invalid value for window') min_grain, data = self.retrieve_data(storage_obj, metric, start, stop, window) return self.aggregate_data(data, numpy.mean, window, min_grain, center, min_size=1)
def _handle_binary_op(engine, table, op, nodes): try: field_name, value = list(nodes.items())[0] except Exception: raise indexer.QueryError() if field_name == "lifespan": attr = getattr(table, "ended_at") - getattr(table, "started_at") value = utils.to_timespan(value) if engine == "mysql": # NOTE(jd) So subtracting 2 timestamps in MySQL result in some # weird results based on string comparison. It's useless and it # does not work at all with seconds or anything. Just skip it. raise exceptions.NotImplementedError else: try: attr = getattr(table, field_name) except AttributeError: raise indexer.QueryAttributeError(table, field_name) if not hasattr(attr, "type"): # This is not a column raise indexer.QueryAttributeError(table, field_name) # Convert value to the right type if value is not None and isinstance(attr.type, base.PreciseTimestamp): value = utils.to_timestamp(value) return op(attr, value)
def check_window_valid(window): """Takes in the window parameter string, reformats as a float.""" if window is None: msg = 'Moving aggregate must have window specified.' raise aggregates.CustomAggFailure(msg) try: return utils.to_timespan(six.text_type(window)).total_seconds() except Exception: raise aggregates.CustomAggFailure('Invalid value for window')
def _handle_binary_op(engine, table, op, nodes): try: field_name, value = list(nodes.items())[0] except Exception: raise indexer.QueryError() if field_name == "lifespan": attr = getattr(table, "ended_at") - getattr(table, "started_at") value = utils.to_timespan(value) if engine == "mysql": # NOTE(jd) So subtracting 2 timestamps in MySQL result in some # weird results based on string comparison. It's useless and it # does not work at all with seconds or anything. Just skip it. raise exceptions.NotImplementedError else: try: attr = getattr(table, field_name) except AttributeError: raise indexer.QueryAttributeError(table, field_name) if not hasattr(attr, "type"): # This is not a column raise indexer.QueryAttributeError(table, field_name) # Convert value to the right type if value is not None: converter = None if isinstance(attr.type, base.PreciseTimestamp): converter = utils.to_timestamp elif (isinstance(attr.type, sqlalchemy_utils.UUIDType) and not isinstance(value, uuid.UUID)): converter = utils.ResourceUUID elif isinstance(attr.type, types.String): converter = six.text_type elif isinstance(attr.type, types.Integer): converter = int elif isinstance(attr.type, types.Numeric): converter = float if converter: try: value = converter(value) except Exception: raise indexer.QueryValueError(value, field_name) return op(attr, value)
def Timespan(value): return utils.to_timespan(value).total_seconds()
first = True for arg in args: if not first: args_parser += pp.Suppress(",").setName(",") args_parser += arg first = False args_parser += pp.Suppress(")").setName(")") if not args: args_parser = pp.Optional(args_parser) parser = parser + pp.Group(args_parser) return parser.setParseAction( lambda t: carbonara.Transformation(t[0], tuple(t[1]))) # NOTE(sileht): not sure pp.nums + "." is enough to support all # pandas.to_timedelta() formats timespan = pp.Word(pp.nums + ".").setName("timespan") timespan = timespan.setParseAction(lambda t: utils.to_timespan(t[0])) absolute = transform("absolute") negative = transform("negative") resample = transform("resample", timespan) rolling = transform("rolling", pp.Word(pp.alphas), pp.Word(pp.nums)) transform = pp.delimitedList( absolute | negative | resample | rolling, delim=":") parse = functools.partial(transform.parseString, parseAll=True) TransformationParserError = pp.ParseException
from gnocchi import utils WORST_CASE_BYTES_PER_POINT = 8.04 if (len(sys.argv) - 1) % 2 != 0: print("Usage: %s <granularity> <timespan> ... <granularity> <timespan>" % sys.argv[0]) sys.exit(1) def sizeof_fmt(num, suffix='B'): for unit in ('', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi'): if abs(num) < 1024.0: return "%3.1f%s%s" % (num, unit, suffix) num /= 1024.0 return "%.1f%s%s" % (num, 'Yi', suffix) size = 0 for g, t in utils.grouper(sys.argv[1:], 2): granularity = utils.to_timespan(g) timespan = utils.to_timespan(t) points = timespan.total_seconds() / granularity.total_seconds() cursize = points * WORST_CASE_BYTES_PER_POINT size += cursize print("%s over %s = %d points = %s" % (g, t, points, sizeof_fmt(cursize))) print("Total: " + sizeof_fmt(size))
WORST_CASE_BYTES_PER_POINT = 8.04 if (len(sys.argv) - 2) % 2 != 0: print("Usage: %s <number of agg methods> <granularity> <timespan> ..." % sys.argv[0]) sys.exit(1) def sizeof_fmt(num, suffix='B'): for unit in ('', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi'): if abs(num) < 1024.0: return "%3.1f%s%s" % (num, unit, suffix) num /= 1024.0 return "%.1f%s%s" % (num, 'Yi', suffix) size = 0 agg_methods = int(sys.argv[1]) for g, t in utils.grouper(sys.argv[2:], 2): granularity = utils.to_timespan(g) timespan = utils.to_timespan(t) points = timespan / granularity cursize = points * WORST_CASE_BYTES_PER_POINT size += cursize print("%s over %s = %d points = %s" % (g, t, points, sizeof_fmt(cursize))) size *= agg_methods print("Total: " + sizeof_fmt(size))