~ubuntu-branches/ubuntu/trusty/ceilometer/trusty-proposed

« back to all changes in this revision

Viewing changes to ceilometer/transformer/conversions.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, James Page, Chuck Short
  • Date: 2014-01-23 15:08:11 UTC
  • mfrom: (1.1.11)
  • Revision ID: package-import@ubuntu.com-20140123150811-1zaismsuyh1hcl8y
Tags: 2014.1~b2-0ubuntu1
[ James Page ]
* d/control: Add python-jsonpath-rw to BD's.
* d/p/fix-setup-requirements.patch: Bump WebOb to support < 1.4.
 (LP: #1261101)

[ Chuck Short ]
* New upstream version.
* debian/control, debian/ceilometer-common.install: Split out
  ceilometer-alarm-evaluator and ceilometer-alarm-notifier into their
  own packages. (LP: #1250002)
* debian/ceilometer-agent-central.logrotate,
  debian/ceilometer-agent-compute.logrotate,
  debian/ceilometer-api.logrotate,
  debian/ceilometer-collector.logrotate: Add logrotate files, 
  thanks to Ahmed Rahal. (LP: #1224223)
* Fix typos in upstart files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
# under the License.
18
18
 
19
19
import collections
 
20
import re
20
21
 
21
22
from ceilometer.openstack.common.gettextutils import _  # noqa
22
23
from ceilometer.openstack.common import log
64
65
        """
65
66
        self.source = source
66
67
        self.target = target
 
68
        self.scale = target.get('scale')
67
69
        LOG.debug(_('scaling conversion transformer with source:'
68
70
                    ' %(source)s target: %(target)s:')
69
71
                  % {'source': source,
70
72
                     'target': target})
71
73
        super(ScalingTransformer, self).__init__(**kwargs)
72
74
 
73
 
    @staticmethod
74
 
    def _scale(s, scale):
 
75
    def _scale(self, s):
75
76
        """Apply the scaling factor (either a straight multiplicative
76
77
           factor or else a string to be eval'd).
77
78
        """
78
79
        ns = Namespace(s.as_dict())
79
80
 
 
81
        scale = self.scale
80
82
        return ((eval(scale, {}, ns) if isinstance(scale, basestring)
81
83
                 else s.volume * scale) if scale else s.volume)
82
84
 
 
85
    def _map(self, s, attr):
 
86
        """Apply the name or unit mapping if configured.
 
87
        """
 
88
        mapped = None
 
89
        from_ = self.source.get('map_from')
 
90
        to_ = self.target.get('map_to')
 
91
        if from_ and to_:
 
92
            if from_.get(attr) and to_.get(attr):
 
93
                try:
 
94
                    mapped = re.sub(from_[attr], to_[attr], getattr(s, attr))
 
95
                except Exception:
 
96
                    pass
 
97
        return mapped or self.target.get(attr, getattr(s, attr))
 
98
 
83
99
    def _convert(self, s, growth=1):
84
100
        """Transform the appropriate sample fields.
85
101
        """
86
 
        scale = self.target.get('scale')
87
102
        return sample.Sample(
88
 
            name=self.target.get('name', s.name),
89
 
            unit=self.target.get('unit', s.unit),
 
103
            name=self._map(s, 'name'),
 
104
            unit=self._map(s, 'unit'),
90
105
            type=self.target.get('type', s.type),
91
 
            volume=self._scale(s, scale) * growth,
 
106
            volume=self._scale(s) * growth,
92
107
            user_id=s.user_id,
93
108
            project_id=s.project_id,
94
109
            resource_id=s.resource_id,
115
130
    def __init__(self, **kwargs):
116
131
        """Initialize transformer with configured parameters.
117
132
        """
 
133
        super(RateOfChangeTransformer, self).__init__(**kwargs)
118
134
        self.cache = {}
119
 
        super(RateOfChangeTransformer, self).__init__(**kwargs)
 
135
        self.scale = self.scale or '1'
120
136
 
121
137
    def handle_sample(self, context, s):
122
138
        """Handle a sample, converting if necessary."""