Merge ~andreserl/maas:better_stats into maas:master

Proposed by Andres Rodriguez
Status: Merged
Approved by: Andres Rodriguez
Approved revision: 7f3268a7dfb613a95377cafdcec0752555237151
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~andreserl/maas:better_stats
Merge into: maas:master
Diff against target: 69 lines (+19/-3)
2 files modified
src/maasserver/stats.py (+10/-1)
src/maasserver/tests/test_stats.py (+9/-2)
Reviewer Review Type Date Requested Status
Blake Rouse (community) Approve
Review via email: mp+342520@code.launchpad.net

Commit message

Calculate the total amount of machine resources.

To post a comment you must log in.
Revision history for this message
Blake Rouse (blake-rouse) wrote :

Looks good!

review: Approve
Revision history for this message
MAAS Lander (maas-lander) wrote :
Revision history for this message
MAAS Lander (maas-lander) wrote :
~andreserl/maas:better_stats updated
487b858... by Andres Rodriguez

Fix lint

Preview Diff

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

Subscribers

People subscribed via source and target branches