def aggregate_detail(slug_list, with_data_table=False): """Template Tag to display multiple metrics. * ``slug_list`` -- A list of slugs to display * ``with_data_table`` -- if True, prints the raw data in a table. """ r = get_r() metrics_data = [] granularities = r._granularities() # XXX converting granularties into their key-name for metrics. keys = ['seconds', 'minutes', 'hours', 'day', 'week', 'month', 'year'] key_mapping = {gran: key for gran, key in zip(GRANULARITIES, keys)} keys = [key_mapping[gran] for gran in granularities] # Our metrics data is of the form: # # (slug, {time_period: value, ... }). # # Let's convert this to (slug, list_of_values) so that the list of # values is in the same order as the granularties for slug, data in r.get_metrics(slug_list): values = [data[t] for t in keys] metrics_data.append((slug, values)) return { 'chart_id': "metric-aggregate-{0}".format("-".join(slug_list)), 'slugs': slug_list, 'metrics': metrics_data, 'with_data_table': with_data_table, 'granularities': [g.title() for g in keys], }
def gauge(slug, maximum=9000, size=200, coerce='float'): """Include a Donut Chart for the specified Gauge. * ``slug`` -- the unique slug for the Gauge. * ``maximum`` -- The maximum value for the gauge (default is 9000) * ``size`` -- The size (in pixels) of the gauge (default is 200) * ``coerce`` -- type to which gauge values should be coerced. The default is float. Use ``{% gauge some_slug coerce='int' %}`` to coerce to integer """ coerce_options = {'float': float, 'int': int, 'str': str} coerce = coerce_options.get(coerce, float) redis = get_r() value = coerce(redis.get_gauge(slug)) if value < maximum and coerce == float: diff = round(maximum - value, 2) elif value < maximum: diff = maximum - value else: diff = 0 return { 'slug': slug, 'current_value': value, 'max_value': maximum, 'size': size, 'diff': diff, }
def handle(self, *args, **options): if len(args) == 0: raise CommandError("You must provide a metric name") metric_slug = args[0] r = get_r() r.delete_metric(metric_slug)
def get_slug_list(filter): slug_list = get_r().metric_slugs_by_category()['Uncategorized'] slugs = [] for slug in slug_list: if filter in slug: slugs.append(slug) return slugs
def metric_history(slug, granularity="daily", since=None, with_data_table=False): """Template Tag to display a metric's history. * ``slug`` -- the metric's unique slug * ``granularity`` -- the granularity: daily, hourly, weekly, monthly, yearly * ``since`` -- a datetime object or a string string matching one of the following patterns: "YYYY-mm-dd" for a date or "YYYY-mm-dd HH:MM:SS" for a date & time. * ``with_data_table`` -- if True, prints the raw data in a table. """ r = get_r() try: if since and len(since) == 10: # yyyy-mm-dd since = datetime.strptime(since, "%Y-%m-%d") elif since and len(since) == 19: # yyyy-mm-dd HH:MM:ss since = datetime.strptime(since, "%Y-%m-%d %H:%M:%S") except (TypeError, ValueError): # assume we got a datetime object or leave since = None pass metric_history = r.get_metric_history( slugs=slug, since=since, granularity=granularity ) return { 'since': since, 'slug': slug, 'granularity': granularity, 'metric_history': metric_history, 'with_data_table': with_data_table, }
def aggregate_history(slugs, granularity="daily", since=None, with_data_table=False): """Template Tag to display history for multiple metrics. * ``slug_list`` -- A list of slugs to display * ``granularity`` -- the granularity: seconds, minutes, hourly, daily, weekly, monthly, yearly * ``since`` -- a datetime object or a string string matching one of the following patterns: "YYYY-mm-dd" for a date or "YYYY-mm-dd HH:MM:SS" for a date & time. * ``with_data_table`` -- if True, prints the raw data in a table. NOTE: If you specify with_data_table=True, this code will make an additional call out to retreive metrics and format them properly, which could be a little slow. """ r = get_r() slugs = list(slugs) try: if since and len(since) == 10: # yyyy-mm-dd since = datetime.strptime(since, "%Y-%m-%d") elif since and len(since) == 19: # yyyy-mm-dd HH:MM:ss since = datetime.strptime(since, "%Y-%m-%d %H:%M:%S") except (TypeError, ValueError): # assume we got a datetime object or leave since = None pass history = r.get_metric_history_chart_data( slugs=slugs, since=since, granularity=granularity ) # If we want to display the raw data, fetch it in a columnar format tabular_data = None if with_data_table: tabular_data = r.get_metric_history_as_columns( slugs=slugs, since=since, granularity=granularity ) return { 'chart_id': "metric-aggregate-history-{0}".format("-".join(slugs)), 'slugs': slugs, 'since': since, 'granularity': granularity, 'metric_history': history, 'with_data_table': with_data_table, 'tabular_data': tabular_data, }
def handle_noargs(self, *args, **options): r = get_r() # Store *only* slugs in the Set of metric slugs. Prior to this, we # stored metric keys, and split the slug from the key upon retrieval try: keys = r.r.smembers(r._metric_slugs_key) slugs = set(s.split(":")[1] for s in keys) r.r.srem(r._metric_slugs_key, *keys) # Remove the old keys r.r.sadd(r._metric_slugs_key, *slugs) # Keep the new slugs p = "\nMetrics: Converted {0} Keys to {1} Slugs\n" self.stdout.write(p.format(len(keys), len(slugs))) except (IndexError, ResponseError): # If none of our old keys contain a ':', we don't need to replace them. pass # Similar to metric keys above... # Store *only* slugs in the Set of Gauge slugs. Prior to this, we # stored gauge keys, and split the slug from the key upon retrieval try: keys = r.r.smembers(r._gauge_slugs_key) slugs = set(s.split(":")[1] for s in keys) r.r.srem(r._gauge_slugs_key, *keys) # Remove the old keys r.r.sadd(r._gauge_slugs_key, *slugs) # Keep the new slugs p = "Gauges: Converted {0} Keys to {1} Slugs\n" self.stdout.write(p.format(len(keys), len(slugs))) except (IndexError, ResponseError): # If none of our old keys contain a ':', we don't need to replace them. pass # Convert all Categories from JSON to Sets. i = 0 categories = r.categories() for category in categories: try: k = r._category_key(category) data = r.r.get( k) # Get the JSON-ecoded list of metrics in this Category if data: # redis gives us a None-value for a non-existing key data = json.loads(data) # convert to python r.r.delete(k) # Delete the existing info in redis r.r.sadd(k, *set(data)) # Re-create as a Set i += 1 except ResponseError: pass if i > 0: p = "Converted {0} Categories from JSON -> Redis Sets\n" self.stdout.write(p.format(i))
def metric_detail(slug, with_data_table=False): """Template Tag to display a metric's *current* detail. * ``slug`` -- the metric's unique slug * ``with_data_table`` -- if True, prints the raw data in a table. """ r = get_r() return { 'granularities': list(r._granularities()), 'slug': slug, 'metrics': r.get_metric(slug), 'with_data_table': with_data_table, }
def handle_noargs(self, *args, **options): r = get_r() # Store *only* slugs in the Set of metric slugs. Prior to this, we # stored metric keys, and split the slug from the key upon retrieval try: keys = r.r.smembers(r._metric_slugs_key) slugs = set(s.split(":")[1] for s in keys) r.r.srem(r._metric_slugs_key, *keys) # Remove the old keys r.r.sadd(r._metric_slugs_key, *slugs) # Keep the new slugs p = "\nMetrics: Converted {0} Keys to {1} Slugs\n" self.stdout.write(p.format(len(keys), len(slugs))) except (IndexError, ResponseError): # If none of our old keys contain a ':', we don't need to replace them. pass # Similar to metric keys above... # Store *only* slugs in the Set of Gauge slugs. Prior to this, we # stored gauge keys, and split the slug from the key upon retrieval try: keys = r.r.smembers(r._gauge_slugs_key) slugs = set(s.split(":")[1] for s in keys) r.r.srem(r._gauge_slugs_key, *keys) # Remove the old keys r.r.sadd(r._gauge_slugs_key, *slugs) # Keep the new slugs p = "Gauges: Converted {0} Keys to {1} Slugs\n" self.stdout.write(p.format(len(keys), len(slugs))) except (IndexError, ResponseError): # If none of our old keys contain a ':', we don't need to replace them. pass # Convert all Categories from JSON to Sets. i = 0 categories = r.categories() for category in categories: try: k = r._category_key(category) data = r.r.get(k) # Get the JSON-ecoded list of metrics in this Category if data: # redis gives us a None-value for a non-existing key data = json.loads(data) # convert to python r.r.delete(k) # Delete the existing info in redis r.r.sadd(k, *set(data)) # Re-create as a Set i += 1 except ResponseError: pass if i > 0: p = "Converted {0} Categories from JSON -> Redis Sets\n" self.stdout.write(p.format(i))
def aggregate_detail(slug_list, with_data_table=False): """Template Tag to display multiple metrics. * ``slug_list`` -- A list of slugs to display * ``with_data_table`` -- if True, prints the raw data in a table. """ r = get_r() return { 'chart_id': "metric-aggregate-{0}".format("-".join(slug_list)), 'slugs': slug_list, 'metrics': r.get_metrics(slug_list), 'with_data_table': with_data_table, }
def _current_average_response_time(self): # Assume `_start_time` and `_end_time` are set, retrieve the current # gauge value (if any), average the current response time, and return # the value (as a string). r = get_r() request_time = self._end_time - self._start_time current = r.get_gauge(self._key) if current is not None: current = (float(current) + request_time) / 2 else: current = request_time return current
def metric_detail(slug, with_data_table=False): """Template Tag to display a metric's *current* detail. * ``slug`` -- the metric's unique slug * ``with_data_table`` -- if True, prints the raw data in a table. """ r = get_r() granularities = list(r._granularities()) metrics = r.get_metric(slug) metrics_data = [] for g in granularities: metrics_data.append((g, metrics[g])) return { 'granularities': [g.title() for g in granularities], 'slug': slug, 'metrics': metrics_data, 'with_data_table': with_data_table, }
def gauge(slug, maximum=9000, size=200): """Include a Donut Chart for the specified Gauge. * ``slug`` -- the unique slug for the Gauge. * ``maximum`` -- The maximum value for the gauge (default is 9000) * ``size`` -- The size (in pixels) of the gauge (default is 200) """ r = get_r() value = int(r.get_gauge(slug)) if value < maximum: diff = maximum - value else: diff = 0 return { 'slug': slug, 'current_value': value, 'max_value': maximum, 'size': size, 'diff': diff, }
def aggregate_history(slugs, granularity="daily", since=None, with_data_table=False): """Template Tag to display history for multiple metrics. * ``slug_list`` -- A list of slugs to display * ``granularity`` -- the granularity: seconds, minutes, hourly, daily, weekly, monthly, yearly * ``since`` -- a datetime object or a string string matching one of the following patterns: "YYYY-mm-dd" for a date or "YYYY-mm-dd HH:MM:SS" for a date & time. * ``with_data_table`` -- if True, prints the raw data in a table. """ r = get_r() slugs = list(slugs) try: if since and len(since) == 10: # yyyy-mm-dd since = datetime.strptime(since, "%Y-%m-%d") elif since and len(since) == 19: # yyyy-mm-dd HH:MM:ss since = datetime.strptime(since, "%Y-%m-%d %H:%M:%S") except (TypeError, ValueError): # assume we got a datetime object or leave since = None pass history = r.get_metric_history_chart_data(slugs=slugs, since=since, granularity=granularity) return { 'chart_id': "metric-aggregate-history-{0}".format("-".join(slugs)), 'slugs': slugs, 'since': since, 'granularity': granularity, 'metric_history': history, 'with_data_table': with_data_table, }
def handle_noargs(self, **options): """Send Report E-mails.""" r = get_r() since = datetime.utcnow() - timedelta(days=1) metrics = {} categories = r.metric_slugs_by_category() for category_name, slug_list in categories.items(): metrics[category_name] = [] for slug in slug_list: metric_values = r.get_metric_history(slug, since=since) metrics[category_name].append( (slug, metric_values) ) # metrics is now: # -------------- # { Category : [ # ('foo', [('m:foo:2012-07-18', 1), ('m:foo:2012-07-19, 2), ...]) # ], # ... # } template = "redis_metrics/email/report.{fmt}" data = { 'today': since, 'metrics': metrics, } message = render_to_string(template.format(fmt='txt'), data) message_html = render_to_string(template.format(fmt='html'), data) msg = EmailMultiAlternatives( subject="Redis Metrics Report", body=message, from_email=settings.DEFAULT_FROM_EMAIL, to=[email for name, email in settings.ADMINS] ) msg.attach_alternative(message_html, "text/html") msg.send()
def aggregate_history(slugs, granularity="daily", since=None, with_data_table=False): """Template Tag to display history for multiple metrics. * ``slug_list`` -- A list of slugs to display * ``granularity`` -- the granularity: seconds, minutes, hourly, daily, weekly, monthly, yearly * ``since`` -- a datetime object or a string string matching one of the following patterns: "YYYY-mm-dd" for a date or "YYYY-mm-dd HH:MM:SS" for a date & time. * ``with_data_table`` -- if True, prints the raw data in a table. """ r = get_r() slugs = list(slugs) try: if since and len(since) == 10: # yyyy-mm-dd since = datetime.strptime(since, "%Y-%m-%d") elif since and len(since) == 19: # yyyy-mm-dd HH:MM:ss since = datetime.strptime(since, "%Y-%m-%d %H:%M:%S") except (TypeError, ValueError): # assume we got a datetime object or leave since = None pass history = r.get_metric_history_chart_data( slugs=slugs, since=since, granularity=granularity ) return { 'chart_id': "metric-aggregate-history-{0}".format("-".join(slugs)), 'slugs': slugs, 'since': since, 'granularity': granularity, 'metric_history': history, 'with_data_table': with_data_table, }
def metric_list(): r = get_r() return { 'metrics': r.metric_slugs_by_category(), }
def handle(self, *args, **options): if not len(args) == 1: raise CommandError("You must provide a gauge name") r = get_r() r.delete_gauge(args[0])