Skip to content

Instantly share code, notes, and snippets.

@asalkeld
Created December 4, 2012 12:06
Show Gist options
  • Save asalkeld/4203131 to your computer and use it in GitHub Desktop.
Save asalkeld/4203131 to your computer and use it in GitHub Desktop.
cw using wsme
import logging
import datetime
from pecan import request
#from pecan.rest import RestController
from wsme.types import Base, wsattr
import wsme
import wsme.pecan
from ceilometer import storage
LOG = logging.getLogger(__name__)
class Dimension(Base):
Name = unicode
Value = unicode
def __init__(self, name, value):
self.Name = name
self.Value = value
class Metric(Base):
MetricName = unicode
Namespace = unicode
Dimensions = wsattr(['Dimension'])
def __init__(self, name=None, namespace=None, dimensions={}):
self.MetricName = name
self.Namespace = namespace
self.Dimensions = [Dimension(n, v) for n, v in dimensions.iteritems()]
class ListMetricsResult(Base):
Metrics = wsattr([Metric])
Namespace = unicode
NextToken = unicode
def __init__(self):
self.NextToken = request.params.get('NextToken')
self.Namespace = request.params.get('Namespace')
self.Metrics = []
name = request.params.get('MetricName')
resources = list(request.storage_conn.get_resources())
for r in resources:
for met in r['meter']:
if name is None or met['counter_name'] == name:
m = Metric(met['counter_name'],
'OS',
{'resource_id': r['resource_id']})
self.Metrics.append(m)
class Datapoint(Base):
Average = float
Maximum = float
Minimum = float
SampleCount = float
Sum = float
Timestamp = datetime.datetime
Unit = unicode
def __init__(self, avg=None, _max=None, _min=None,
count=None, _sum=None, ts=None, unit=None):
if avg: self.Average = float(avg)
if _max: self.Maximum = float(_max)
if _min: self.Minimum = float(_min)
if count: self.SampleCount = float(count)
if _sum: self.Sum = float(_sum)
if ts: self.Timestamp = ts
if unit: self.Unit = unit
class GetMetricStatisticsRestult(Base):
label = str
datapoints = wsattr(['Datapoint'])
def __init__(self):
Period = request.params.get('Period')
f = storage.EventFilter(
start=request.params.get('StartTime'),
end=request.params.get('EndTime'),
meter=request.params.get('MetricName'),
)
self.label = request.params.get('MeasureName', 'measurement')
self.datapoints = []
events = list(request.storage_conn.get_raw_events(f))
for e in events:
d = Datapoint(unit=request.params.get('Unit'),
avg=e['counter_volume'],
_min=e['counter_volume'],
_max=e['counter_volume'],
ts=e['timestamp'])
self.datapoints.append(d)
class CloudWatchController(object):
"""TODO(asalkeld) this routing is not correct.
we need one entry point and the Action parameter should
route the call, like this:
&Action=GetMetricStatistics
&Version=2009-05-15
&Period=60
&Statistics.member.1=Average
&Namespace=AWS/EC2
&StartTime=2009-01-16T00:00:00
&EndTime=2009-01-16T00:02:00
&MeasureName=CPUUtilization
&Timestamp=2009-01-08-18
&AWSAccessKeyId=XXX YOUR ACCESS KEY XXX
&Signature=%XXX YOUR SIGNATURE XXX%3D
"""
@wsme.pecan.wsexpose(ListMetricsResult)
def ListMetrics(self):
return ListMetricsResult()
@wsme.pecan.wsexpose(GetMetricStatisticsRestult)
def GetMetricStatistics(self):
return GetMetricStatisticsRestult()
@dhellmann
Copy link

wsme.Base.init copies all of its incoming arguments to local attributes, so you don't need Dimension.init.

@dhellmann
Copy link

You're mixing the responsibility for looking up data and for serializing and deserializing by putting that logic in the classes derived from wsme.Base. That will cause problems, for example, when those classes are used to generate examples in the documentation using the sphinx autodoc extensions, because they will need some sort of database to pull data from.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment