Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
Fixes the bucket naming when retrieving metrics
Browse files Browse the repository at this point in the history
This patch also adds code coverage unit tests, and reformats the
time in the response body when getting metrics.

Change-Id: Iae083518cfe4bdcd0a12a1d72db32df10e6e9596
  • Loading branch information
amitgandhinz committed May 9, 2016
1 parent 46d006f commit f67cb9f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 29 deletions.
9 changes: 6 additions & 3 deletions poppy/metrics/blueflood/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ def _result_formatter(self, response):

serialized_response = response.json()
try:
time_series = serialized_response['values']
for timerange in time_series:
resp_dict[timerange['timestamp']] = timerange['sum']
values = serialized_response['values']
for val in values:
key = helper.datetime_from_epoch(int(val['timestamp']))
resp_dict[key] = val['sum']
except KeyError:
msg = 'content from {0} not conforming ' \
'to API contracts'.format(response.url)
Expand Down Expand Up @@ -86,6 +87,8 @@ def read(self, metric_names, from_timestamp, to_timestamp, resolution):
for metric_name in metric_names:
tenanted_blueflood_url_with_metric = helper.join_url(
tenanted_blueflood_url, metric_name.strip().replace(" ", ""))
LOG.info("Querying BlueFlood Metric: {0}".format(
tenanted_blueflood_url_with_metric))
urls.append(helper.set_qs_on_url(
tenanted_blueflood_url_with_metric,
**params))
Expand Down
7 changes: 6 additions & 1 deletion poppy/metrics/blueflood/utils/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import calendar
import time

try: # pragma: no cover
Expand Down Expand Up @@ -46,7 +47,11 @@ def join_url(base_url, url):


def datetime_to_epoch(datetime_obj):
return time.mktime(datetime_obj.timetuple()) * 1000
return calendar.timegm(datetime_obj.timetuple()) * 1000


def datetime_from_epoch(ms):
return time.strftime('%Y-%m-%dT%H:%M:%S', time.gmtime(ms / 1000))


def resolution_converter_seconds_to_enum(resolution_seconds):
Expand Down
7 changes: 4 additions & 3 deletions poppy/provider/akamai/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -1243,9 +1243,10 @@ def get_metrics_by_domain(self, project_id, domain_name, regions,
if 'httpResponseCode' in metricType:
http_series = metricType.split('_')[1]
for region in regions:
metric_buckets.append('_'.join(['requestCount', domain_name,
region,
http_series]))
metric_buckets.append('_'.join(['httpResponseCode',
http_series,
domain_name,
region]))
else:
for region in regions:
metric_buckets.append('_'.join([metricType, domain_name,
Expand Down
22 changes: 16 additions & 6 deletions tests/unit/metrics/blueflood/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,29 @@ def test_read(self, metric_name):
with mock.patch.object(client.BlueFloodMetricsClient,
'async_requests',
auto_spec=True) as mock_async:
timestamp1 = str(int(time.time()))
timestamp2 = str(int(time.time()) + 100)
timestamp3 = str(int(time.time()) + 200)
timestamp1 = str((int(time.time()) + 0) * 1000)
timestamp2 = str((int(time.time()) + 100) * 1000)
timestamp3 = str((int(time.time()) + 200) * 1000)
json_dict = {
'values': [
{
'numPoints': 2,
'timestamp': timestamp1,
'sum': 45
},
{
'numPoints': 1,
'timestamp': timestamp2,
'sum': 34
},
{
'numPoints': 3,
'timestamp': timestamp3,
'sum': 11
},
]
}

metric_names = []
regions = ['Mock_region{0}'.format(i) for i in range(6)]
for region in regions:
Expand Down Expand Up @@ -120,9 +124,15 @@ def test_read(self, metric_name):
metric_name, response = result
self.assertIn(metric_name, metric_names)
metric_names.remove(metric_name)
self.assertEqual(response[timestamp1], 45)
self.assertEqual(response[timestamp2], 34)
self.assertEqual(response[timestamp3], 11)
self.assertEqual(response[time.strftime(
'%Y-%m-%dT%H:%M:%S', time.gmtime(int(timestamp1) / 1000))],
45)
self.assertEqual(response[time.strftime(
'%Y-%m-%dT%H:%M:%S', time.gmtime(int(timestamp2) / 1000))],
34)
self.assertEqual(response[time.strftime(
'%Y-%m-%dT%H:%M:%S', time.gmtime(int(timestamp3) / 1000))],
11)

def test_format_results_exception(self):
json_dict = {
Expand Down
13 changes: 1 addition & 12 deletions tests/unit/metrics/blueflood/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@

"""Unittests for BlueFlood utils"""

import datetime
import time


from poppy.metrics.blueflood.utils import helper

from tests.unit import base
Expand All @@ -31,7 +27,7 @@ def setUp(self):
self.url = 'https://www.metrics.com'

def _almostequal(self, entity1, entity2, delta=1):
if abs(entity1-entity2) <= delta:
if abs(entity1 - entity2) <= delta:
return True
else:
return False
Expand Down Expand Up @@ -60,13 +56,6 @@ def test_retrieve_last_relative_url(self):
self.assertEqual(helper.retrieve_last_relative_url(non_relative_url),
relative_url)

def test_datetime_to_epoch(self):
datetime_obj = datetime.datetime.today()
expected = helper.datetime_to_epoch(datetime_obj=datetime_obj)
observed = int(time.time()) * 1000
equality = self._almostequal(expected, observed)
self.assertEqual(equality, True)

def test_resolution_converter_seconds_to_enum_happy(self):
seconds_series = ['0', '300', '1200', '3600', '14400', '86400']
for seconds in seconds_series:
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/provider/akamai/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -995,10 +995,10 @@ def test_get_metrics_by_domain_metrics_controller(self, metrictype):
self.assertEqual(metric_split[1], domain_name)
self.assertIn(metric_split[2], regions)
else:
self.assertEqual(metric_split[0], 'requestCount')
self.assertEqual(metric_split[1], domain_name)
self.assertIn(metric_split[2], regions)
self.assertIn(metric_split[3], metrictype.split('_')[1])
self.assertEqual(metric_split[0], 'httpResponseCode')
self.assertIn(metric_split[1], metrictype.split('_')[1])
self.assertEqual(metric_split[2], domain_name)
self.assertIn(metric_split[3], regions)

@ddt.data('requestCount', 'bandwidthOut', 'httpResponseCode_1XX',
'httpResponseCode_2XX', 'httpResponseCode_3XX',
Expand Down

0 comments on commit f67cb9f

Please sign in to comment.