Merge ~andreserl/maas:2.3_machine_stats into maas:2.3

Proposed by Andres Rodriguez
Status: Merged
Approved by: Andres Rodriguez
Approved revision: 56018f9b966223eb4e7c4ad3d541e3adc39b7477
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~andreserl/maas:2.3_machine_stats
Merge into: maas:2.3
Diff against target: 73 lines (+20/-4)
2 files modified
src/maasserver/stats.py (+10/-1)
src/maasserver/tests/test_stats.py (+10/-3)
Reviewer Review Type Date Requested Status
Andres Rodriguez (community) Approve
Mike Pontillo (community) Needs Fixing
MAAS Lander unittests Pending
Review via email: mp+342593@code.launchpad.net

Commit message

Backport d2f6dd9cff9072c27549fd9aff6e82994a5f0ebe from master

Calculate the total amount of machine resources.-

To post a comment you must log in.
Revision history for this message
Andres Rodriguez (andreserl) wrote :

selfie!

review: Approve
Revision history for this message
MAAS Lander (maas-lander) wrote :
Revision history for this message
Mike Pontillo (mpontillo) wrote :

So this failed to land in a new and interesting way compared to the off-by-one in this code that I just did the drive-by fix for earlier today (which should also be backported).

https://paste.ubuntu.com/p/XkWZSGYKm3/

It seems this is happening because the test is comparing the JSON *string*, and for whatever reason the version of Python in MAAS 2.3 isn't outputting the JSON fields in the same order!

I would suggest fixing this per the inline comment below (and also backporting the off-by-one fix).

review: Needs Fixing
~andreserl/maas:2.3_machine_stats updated
56018f9... by Andres Rodriguez

Fix tests

Revision history for this message
Andres Rodriguez (andreserl) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/src/maasserver/stats.py b/src/maasserver/stats.py
index c603223..f6da73e 100644
--- a/src/maasserver/stats.py
+++ b/src/maasserver/stats.py
@@ -10,6 +10,7 @@ __all__ = [
1010
11from datetime import timedelta11from datetime import timedelta
1212
13from django.db.models import Sum
13from maasserver.models import Config14from maasserver.models import Config
14from maasserver.utils.orm import transactional15from maasserver.utils.orm import transactional
15from maasserver.utils.threads import deferToDatabase16from maasserver.utils.threads import deferToDatabase
@@ -30,6 +31,13 @@ import requests
3031
3132
32def get_maas_stats():33def get_maas_stats():
34 nodes = Node.objects.all()
35 machines = nodes.filter(node_type=NODE_TYPE.MACHINE)
36 # Rather overall amount of stats for machines.
37 stats = machines.aggregate(
38 total_cpu=Sum('cpu_count'), total_mem=Sum('memory'),
39 total_storage=Sum('blockdevice__size'))
40 # Get all node types to get count values
33 node_types = Node.objects.values_list('node_type', flat=True)41 node_types = Node.objects.values_list('node_type', flat=True)
34 node_types = Counter(node_types)42 node_types = Counter(node_types)
3543
@@ -43,7 +51,8 @@ def get_maas_stats():
43 "nodes": {51 "nodes": {
44 "machines": node_types.get(NODE_TYPE.MACHINE, 0),52 "machines": node_types.get(NODE_TYPE.MACHINE, 0),
45 "devices": node_types.get(NODE_TYPE.DEVICE, 0),53 "devices": node_types.get(NODE_TYPE.DEVICE, 0),
46 }54 },
55 "machine_stats": stats,
47 })56 })
4857
4958
diff --git a/src/maasserver/tests/test_stats.py b/src/maasserver/tests/test_stats.py
index 2c745b1..2bb0e1a 100644
--- a/src/maasserver/tests/test_stats.py
+++ b/src/maasserver/tests/test_stats.py
@@ -40,10 +40,12 @@ class TestMAASStats(MAASServerTestCase):
40 factory.make_RegionRackController()40 factory.make_RegionRackController()
41 factory.make_RegionController()41 factory.make_RegionController()
42 factory.make_RackController()42 factory.make_RackController()
43 factory.make_Machine()43 m1 = factory.make_Machine(cpu_count=2, memory=200)
44 m2 = factory.make_Machine(cpu_count=3, memory=100)
44 factory.make_Device()45 factory.make_Device()
4546
46 stats = get_maas_stats()47 stats = get_maas_stats()
48 total_storage = int((m1.storage + m2.storage) * 1000000)
47 compare = {49 compare = {
48 "controllers": {50 "controllers": {
49 "regionracks": 1,51 "regionracks": 1,
@@ -51,11 +53,16 @@ class TestMAASStats(MAASServerTestCase):
51 "racks": 1,53 "racks": 1,
52 },54 },
53 "nodes": {55 "nodes": {
54 "machines": 1,56 "machines": 2,
55 "devices": 1,57 "devices": 1,
56 },58 },
59 "machine_stats": {
60 "total_cpu": 5,
61 "total_mem": 300,
62 "total_storage": total_storage,
63 },
57 }64 }
58 self.assertEquals(stats, json.dumps(compare))65 self.assertEquals(json.loads(stats), compare)
5966
60 def test_get_request_params_returns_params(self):67 def test_get_request_params_returns_params(self):
61 factory.make_RegionRackController()68 factory.make_RegionRackController()

Subscribers

People subscribed via source and target branches