Merge "Fix getting host name from instances"

This commit is contained in:
Jenkins 2017-06-16 00:12:38 +00:00 committed by Gerrit Code Review
commit 1223668db8
4 changed files with 50 additions and 9 deletions

View File

@ -236,6 +236,33 @@ class TestUpTimeTransformer(base.DistilTestCase):
self.assertEqual({}, result)
def test_wash_data(self):
entries = [
{'timestamp': FAKE_DATA.tpre.isoformat(),
'metadata': {'instance_type': FAKE_DATA.flavor,
'status': 'active'}},
{'timestamp': FAKE_DATA.t0_10.isoformat(),
'metadata': {'instance_type': FAKE_DATA.flavor,
'status': 'active'}},
{'timestamp': FAKE_DATA.t0_30.isoformat(),
'metadata': {'instance_type': FAKE_DATA.flavor,
'status': 'shelving'}},
{'timestamp': FAKE_DATA.t1.isoformat(),
'metadata': {'instance_type': FAKE_DATA.flavor,
'status': 'active'}}
]
xform = conversion.UpTimeTransformer()
result = xform.transform_usage(
'instance',
entries,
FAKE_DATA.t0,
FAKE_DATA.t1
)
self.assertEqual({FAKE_DATA.flavor: 1800}, result)
self.assertEqual(3, len(entries))
@mock.patch.object(general, 'get_transformer_config',
mock.Mock(return_value=FAKE_CONFIG))

View File

@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import copy
from datetime import datetime
from distil.transformer import BaseTransformer
@ -25,6 +26,18 @@ class UpTimeTransformer(BaseTransformer):
which is broken apart into flavor at point in time.
"""
def _wash_data(self, entries, tracked):
"""Get rid of invalid entries."""
copy_entries = copy.deepcopy(entries)
for entry in copy_entries:
status = entry['metadata'].get(
'status', entry['metadata'].get('state', "")
)
if status not in tracked:
entries.remove(entry)
def _transform_usage(self, name, data, start, end):
# get tracked states from config
tracked = self.config['uptime']['tracked_states']
@ -38,8 +51,10 @@ class UpTimeTransformer(BaseTransformer):
state = sort_and_clip_end(data)
if not len(state):
# there was no data for this period.
self._wash_data(data, tracked)
if not len(state) or not len(data):
# there was no valid data for this period.
return usage_dict
last_state = state[0]
@ -56,11 +71,12 @@ class UpTimeTransformer(BaseTransformer):
for val in state[1:]:
if last_state["status"] in tracked:
diff = val["timestamp"] - last_timestamp
if val['timestamp'] > last_timestamp:
# if diff < 0 then we were looking back before the start
# of the window.
diff = val["timestamp"] - last_timestamp
_add_usage(diff)
last_timestamp = val['timestamp']
seen_sample_in_window = True
@ -84,9 +100,7 @@ class UpTimeTransformer(BaseTransformer):
result = {
'status': entry['metadata'].get(
'status', entry['metadata'].get(
'state', ""
)
'status', entry['metadata'].get('state', "")
),
'flavor': entry['metadata'].get('instance_type'),
'timestamp': timestamp

View File

@ -19,6 +19,7 @@
- OS-EXT-AZ:availability_zone
host:
sources:
- node
- host
-
meter: ip.floating

View File

@ -9,9 +9,8 @@ uptime:
- resized
- verify_resize
- suspended
# uncomment these when we want to bill shutdown:
# - shutoff
# - stopped
- shutoff
- stopped
from_image:
service: b1.standard
# What metadata values to check