Skip to content

Commit d27a94e

Browse files
author
Fernando Ojeda
committed
Refactor usage vs information.
1 parent a911fac commit d27a94e

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

SoftLayer/CLI/virt/usage.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import SoftLayer
77
from SoftLayer.CLI import environment
8+
from SoftLayer.CLI import exceptions
89
from SoftLayer.CLI import formatting
910
from SoftLayer.CLI import helpers
1011
from SoftLayer.utils import clean_time
@@ -31,23 +32,28 @@ def cli(env, identifier, start_date, end_date, valid_type, summary_period):
3132
result = vsi.get_summary_data_usage(vs_id, start_date=start_date, end_date=end_date,
3233
valid_type=valid_type, summary_period=summary_period)
3334

35+
if len(result) == 0:
36+
raise exceptions.CLIAbort('No metric data for this range of dates provided')
37+
3438
count = 0
35-
counter = 0.0
39+
counter = 0.00
3640
for data in result:
41+
if valid_type == "MEMORY_USAGE":
42+
usage_counter = data['counter'] / 2 ** 30
43+
else:
44+
usage_counter = data['counter']
45+
3746
table.add_row([
38-
data['counter'],
47+
round(usage_counter, 2),
3948
clean_time(data['dateTime']),
4049
data['type'],
4150
])
42-
counter = counter + float(data['counter'])
51+
counter = counter + usage_counter
4352
count = count + 1
4453

45-
if valid_type == "MEMORY_USAGE":
46-
average = (counter / count) / 2 ** 30
47-
else:
48-
average = counter / count
54+
average = counter / count
4955

50-
env.fout(table_average.add_row([average]))
56+
env.fout(table_average.add_row([round(average, 2)]))
5157

5258
env.fout(table_average)
5359
env.fout(table)

tests/CLI/modules/vs/vs_tests.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,9 +681,18 @@ def test_usage_vs_cpu(self):
681681
self.assert_no_fail(result)
682682

683683
def test_usage_vs_memory(self):
684-
685684
result = self.run_command(
686685
['vs', 'usage', '100', '--start_date=2019-3-4', '--end_date=2019-4-2', '--valid_type=MEMORY_USAGE',
687686
'--summary_period=300'])
688687

689688
self.assert_no_fail(result)
689+
690+
def test_usage_metric_data_empty(self):
691+
usage_vs = self.set_mock('SoftLayer_Metric_Tracking_Object', 'getSummaryData')
692+
test_usage = []
693+
usage_vs.return_value = test_usage
694+
result = self.run_command(
695+
['vs', 'usage', '100', '--start_date=2019-3-4', '--end_date=2019-4-2', '--valid_type=CPU0',
696+
'--summary_period=300'])
697+
self.assertEqual(result.exit_code, 2)
698+
self.assertIsInstance(result.exception, exceptions.CLIAbort)

0 commit comments

Comments
 (0)