Merge ~andreserl/maas:2.5_more_stats into maas:master

Proposed by Andres Rodriguez
Status: Merged
Approved by: Andres Rodriguez
Approved revision: e29d4631f21f7aa3e82b78cb1b965569b48f2584
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~andreserl/maas:2.5_more_stats
Merge into: maas:master
Diff against target: 139 lines (+78/-5)
2 files modified
src/maasserver/stats.py (+50/-2)
src/maasserver/tests/test_stats.py (+28/-3)
Reviewer Review Type Date Requested Status
Blake Rouse (community) Approve
Review via email: mp+355808@code.launchpad.net

Commit message

Stats for networks and most important machine states.

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

Looks good to me. Please improve the commit message before landing, like what stats are you adding.

review: Approve
Revision history for this message
MAAS Lander (maas-lander) wrote :
~andreserl/maas:2.5_more_stats updated
78265e6... 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 978af87..d0544b9 100644
3--- a/src/maasserver/stats.py
4+++ b/src/maasserver/stats.py
5@@ -24,8 +24,17 @@ import base64
6 from collections import Counter
7 import json
8
9-from maasserver.enum import NODE_TYPE
10-from maasserver.models import Node
11+from maasserver.enum import (
12+ NODE_TYPE,
13+ NODE_STATUS,
14+)
15+from maasserver.models import (
16+ Node,
17+ Fabric,
18+ VLAN,
19+ Space,
20+ Subnet,
21+)
22 from maasserver.utils import get_maas_user_agent
23 import requests
24
25@@ -39,11 +48,48 @@ def get_machine_stats():
26 total_storage=Sum('blockdevice__size'))
27
28
29+def get_machine_state_stats():
30+ node_status = Node.objects.values_list('status', flat=True)
31+ node_status = Counter(node_status)
32+
33+ return {
34+ "ready": node_status.get(NODE_STATUS.READY, 0),
35+ "allocated": node_status.get(NODE_STATUS.ALLOCATED, 0),
36+ "deploying": node_status.get(NODE_STATUS.DEPLOYING, 0),
37+ "deployed": node_status.get(NODE_STATUS.DEPLOYED, 0),
38+ "failed_deployment": node_status.get(
39+ NODE_STATUS.FAILED_DEPLOYMENT, 0),
40+ "failed_commissioning": node_status.get(
41+ NODE_STATUS.FAILED_COMMISSIONING, 0),
42+ }
43+
44+
45+def get_subnets_stats():
46+ subnets = Subnet.objects.all()
47+ v4 = [net for net in subnets if net.get_ip_version() == 4]
48+ v6 = [net for net in subnets if net.get_ip_version() == 6]
49+ return {
50+ "spaces": Space.objects.count(),
51+ "fabrics": Fabric.objects.count(),
52+ "vlans": VLAN.objects.count(),
53+ "subnets_v4": len(v4),
54+ "subnets_v6": len(v6),
55+ }
56+
57+
58 def get_maas_stats():
59+ # TODO
60+ # - architectures
61+ # - resource pools
62+ # - pods
63 # Get all node types to get count values
64 node_types = Node.objects.values_list('node_type', flat=True)
65 node_types = Counter(node_types)
66+ # get summary of machine resources, and its statuses.
67 stats = get_machine_stats()
68+ machine_status = get_machine_state_stats()
69+ # get summary of network objects
70+ netstats = get_subnets_stats()
71
72 return json.dumps({
73 "controllers": {
74@@ -57,6 +103,8 @@ def get_maas_stats():
75 "devices": node_types.get(NODE_TYPE.DEVICE, 0),
76 },
77 "machine_stats": stats,
78+ "machine_status": machine_status,
79+ "network_stats": netstats,
80 })
81
82
83diff --git a/src/maasserver/tests/test_stats.py b/src/maasserver/tests/test_stats.py
84index d0200f7..ee7a657 100644
85--- a/src/maasserver/tests/test_stats.py
86+++ b/src/maasserver/tests/test_stats.py
87@@ -10,7 +10,13 @@ import json
88
89 from django.db import transaction
90 from maasserver import stats
91-from maasserver.models import Config
92+from maasserver.models import (
93+ Config,
94+ Fabric,
95+ Space,
96+ Subnet,
97+ VLAN,
98+)
99 from maasserver.stats import (
100 get_maas_stats,
101 get_machine_stats,
102@@ -41,10 +47,14 @@ class TestMAASStats(MAASServerTestCase):
103 factory.make_RegionRackController()
104 factory.make_RegionController()
105 factory.make_RackController()
106- factory.make_Machine(cpu_count=2, memory=200)
107- factory.make_Machine(cpu_count=3, memory=100)
108+ factory.make_Machine(cpu_count=2, memory=200, status=4)
109+ factory.make_Machine(cpu_count=3, memory=100, status=11)
110 factory.make_Device()
111
112+ subnets = Subnet.objects.all()
113+ v4 = [net for net in subnets if net.get_ip_version() == 4]
114+ v6 = [net for net in subnets if net.get_ip_version() == 6]
115+
116 stats = get_maas_stats()
117 machine_stats = get_machine_stats()
118
119@@ -68,6 +78,21 @@ class TestMAASStats(MAASServerTestCase):
120 "total_mem": 300,
121 "total_storage": total_storage,
122 },
123+ "machine_status": {
124+ "ready": 1,
125+ "allocated": 0,
126+ "deploying": 0,
127+ "deployed": 0,
128+ "failed_deployment": 1,
129+ "failed_commissioning": 0,
130+ },
131+ "network_stats": {
132+ "spaces": Space.objects.count(),
133+ "fabrics": Fabric.objects.count(),
134+ "vlans": VLAN.objects.count(),
135+ "subnets_v4": len(v4),
136+ "subnets_v6": len(v6),
137+ },
138 }
139 self.assertEquals(stats, json.dumps(compare))
140

Subscribers

People subscribed via source and target branches