def _SerializeAggregateMetrics(obj):
  """Utility method to serialize AggregateMetric and AggregatedCounter objects to JSON.
  This method is designed to be passed to json.dumps().
  """
  if isinstance(obj, metric.AggregatedMetric):
    return {'start_time': obj.start_time,
            'end_time': obj.end_time,
            'group_key': obj.group_key,
            'machines': list(obj.machines),
            'data': obj.counter_data
            }
  elif isinstance(obj, metric.AggregatedMetric.AggregatedCounter):
    logging.info('description: %r' % obj.description)
    return {'description': obj.description,
            'is_average': obj.is_average,
            'machine_data': obj.machine_data,
            'cluster_total': obj.cluster_total,
            'cluster_avg': obj.cluster_avg
            }
  else:
    raise web.HTTPError(400, "Expected instance of AggregatedMetric or AggregatedCounter, got %r" % obj)
dataset/ETHPy150Open viewfinderco/viewfinder/backend/www/admin/counters.py/_SerializeAggregateMetrics/original