Neater implementation of metric render methods by pulling out 'render' as a base method that calls self.render_item

This commit is contained in:
Paul "LeoNerd" Evans 2015-03-04 19:28:17 +00:00
parent 72625f2f4d
commit a99d6edc05

View file

@ -36,8 +36,15 @@ class BaseMetric(object):
return not len(self.keys) return not len(self.keys)
def _render_key(self, values): def _render_key(self, values):
if self.is_scalar():
return ""
# TODO: some kind of value escape # TODO: some kind of value escape
return ",".join(["%s=%s" % kv for kv in zip(self.keys, values)]) return "{%s}" % (
",".join(["%s=%s" % kv for kv in zip(self.keys, values)])
)
def render(self):
return map_concat(self.render_item, sorted(self.counts.keys()))
class CounterMetric(BaseMetric): class CounterMetric(BaseMetric):
@ -69,12 +76,8 @@ class CounterMetric(BaseMetric):
def fetch(self): def fetch(self):
return dict(self.counts) return dict(self.counts)
def render(self): def render_item(self, k):
if self.is_scalar(): return ["%s%s %d" % (self.name, self._render_key(k), self.counts[k])]
return ["%s %d" % (self.name, self.counts[()])]
return ["%s{%s} %d" % (self.name, self._render_key(k), self.counts[k])
for k in sorted(self.counts.keys())]
class CallbackMetric(BaseMetric): class CallbackMetric(BaseMetric):
@ -93,7 +96,7 @@ class CallbackMetric(BaseMetric):
if self.is_scalar(): if self.is_scalar():
return ["%s %d" % (self.name, value)] return ["%s %d" % (self.name, value)]
return ["%s{%s} %d" % (self.name, self._render_key(k), value[k]) return ["%s%s %d" % (self.name, self._render_key(k), value[k])
for k in sorted(value.keys())] for k in sorted(value.keys())]
@ -121,18 +124,12 @@ class TimerMetric(CounterMetric):
else: else:
self.times[values] += msec self.times[values] += msec
def render(self): def render_item(self, k):
if self.is_scalar(): keystr = self._render_key(k)
return ["%s:count %d" % (self.name, self.counts[()]),
"%s:msec %d" % (self.name, self.times[()])]
def render_item(k): return ["%s%s:count %d" % (self.name, keystr, self.counts[k]),
keystr = self._render_key(k) "%s%s:msec %d" % (self.name, keystr, self.times[k])]
return ["%s{%s}:count %d" % (self.name, keystr, self.counts[k]),
"%s{%s}:msec %d" % (self.name, keystr, self.times[k])]
return map_concat(render_item, sorted(self.counts.keys()))
class CacheMetric(object): class CacheMetric(object):