Merge ~ack/maas:fix-prometheus-stats into maas:master

Proposed by Alberto Donato
Status: Merged
Approved by: Alberto Donato
Approved revision: efe31469c7b192bbe614461b90d5e4a6344cb493
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~ack/maas:fix-prometheus-stats
Merge into: maas:master
Diff against target: 152 lines (+75/-15)
2 files modified
src/maasserver/stats.py (+18/-15)
src/maasserver/tests/test_stats.py (+57/-0)
Reviewer Review Type Date Requested Status
Björn Tillenius Approve
MAAS Lander Needs Fixing
Review via email: mp+362243@code.launchpad.net

Commit message

LP: #1813281 - coalesce counts in stats to prevent null values

To post a comment you must log in.
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b fix-prometheus-stats lp:~ack/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: FAILED
LOG: http://maas-ci-jenkins.internal:8080/job/maas/job/branch-tester/4955/console
COMMIT: 8a474263807ff87966f76a75e29e3415b2bc49cc

review: Needs Fixing
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b fix-prometheus-stats lp:~ack/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: FAILED
LOG: http://maas-ci-jenkins.internal:8080/job/maas/job/branch-tester/4960/console
COMMIT: efe31469c7b192bbe614461b90d5e4a6344cb493

review: Needs Fixing
Revision history for this message
Björn Tillenius (bjornt) wrote :

+1

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

There was an error fetching revisions from git servers. Please try again in a few minutes. If the problem persists, contact Launchpad support.

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 d0d7f1c..8b55ae9 100644
3--- a/src/maasserver/stats.py
4+++ b/src/maasserver/stats.py
5@@ -10,7 +10,11 @@ __all__ = [
6
7 from datetime import timedelta
8
9-from django.db.models import Sum
10+from django.db.models import (
11+ Sum,
12+ Value,
13+)
14+from django.db.models.functions import Coalesce
15 from maasserver.models import Config
16 from maasserver.utils.orm import transactional
17 from maasserver.utils.threads import deferToDatabase
18@@ -21,10 +25,7 @@ from twisted.application.internet import TimerService
19 log = LegacyLogger()
20
21 import base64
22-from collections import (
23- Counter,
24- defaultdict,
25-)
26+from collections import Counter
27 import json
28
29 from maasserver.enum import (
30@@ -45,13 +46,19 @@ from maasserver.utils import get_maas_user_agent
31 import requests
32
33
34+def NotNullSum(column):
35+ """Like Sum, but returns 0 if the aggregate is None."""
36+ return Coalesce(Sum(column), Value(0))
37+
38+
39 def get_machine_stats():
40 nodes = Node.objects.all()
41 machines = nodes.filter(node_type=NODE_TYPE.MACHINE)
42 # Rather overall amount of stats for machines.
43 return machines.aggregate(
44- total_cpu=Sum('cpu_count'), total_mem=Sum('memory'),
45- total_storage=Sum('blockdevice__size'))
46+ total_cpu=NotNullSum('cpu_count'),
47+ total_mem=NotNullSum('memory'),
48+ total_storage=NotNullSum('blockdevice__size'))
49
50
51 def get_machine_state_stats():
52@@ -85,12 +92,7 @@ def get_machines_by_architecture():
53 dict(
54 short_arch="SUBSTRING(architecture FROM '(.*)/')")
55 ).values_list('short_arch', flat=True)
56-
57- count_by_arch = defaultdict(int)
58- for arch in node_arches:
59- count_by_arch[arch] += 1
60-
61- return count_by_arch
62+ return Counter(node_arches)
63
64
65 def get_kvm_pods_stats():
66@@ -99,8 +101,9 @@ def get_kvm_pods_stats():
67 # total_mem is in MB
68 # local_storage is in bytes
69 available_resources = pods.aggregate(
70- cores=Sum('cores'), memory=Sum('memory'),
71- storage=Sum('local_storage'))
72+ cores=NotNullSum('cores'),
73+ memory=NotNullSum('memory'),
74+ storage=NotNullSum('local_storage'))
75
76 # available resources with overcommit
77 over_cores = over_memory = 0
78diff --git a/src/maasserver/tests/test_stats.py b/src/maasserver/tests/test_stats.py
79index 65d9ea9..de36c50 100644
80--- a/src/maasserver/tests/test_stats.py
81+++ b/src/maasserver/tests/test_stats.py
82@@ -113,6 +113,24 @@ class TestMAASStats(MAASServerTestCase):
83 }
84 self.assertEquals(compare, stats)
85
86+ def test_get_kvm_pods_stats_no_pod(self):
87+ self.assertEqual(
88+ get_kvm_pods_stats(),
89+ {'kvm_pods': 0,
90+ 'kvm_machines': 0,
91+ 'kvm_available_resources': {
92+ 'cores': 0,
93+ 'memory': 0,
94+ 'storage': 0,
95+ 'over_cores': 0,
96+ 'over_memory': 0
97+ },
98+ 'kvm_utilized_resources': {
99+ 'cores': 0,
100+ 'memory': 0,
101+ 'storage': 0
102+ }})
103+
104 def test_get_maas_stats(self):
105 # Make one component of everything
106 factory.make_RegionRackController()
107@@ -181,6 +199,45 @@ class TestMAASStats(MAASServerTestCase):
108 }
109 self.assertEquals(stats, json.dumps(compare))
110
111+ def test_get_maas_stats_no_machines(self):
112+ expected = {
113+ "controllers": {
114+ "regionracks": 0,
115+ "regions": 0,
116+ "racks": 0,
117+ },
118+ "nodes": {
119+ "machines": 0,
120+ "devices": 0,
121+ },
122+ "machine_stats": {
123+ "total_cpu": 0,
124+ "total_mem": 0,
125+ "total_storage": 0,
126+ },
127+ "machine_status": {
128+ "new": 0,
129+ "ready": 0,
130+ "allocated": 0,
131+ "deployed": 0,
132+ "commissioning": 0,
133+ "testing": 0,
134+ "deploying": 0,
135+ "failed_deployment": 0,
136+ "failed_commissioning": 0,
137+ "failed_testing": 0,
138+ "broken": 0,
139+ },
140+ "network_stats": {
141+ "spaces": 0,
142+ "fabrics": 1,
143+ "vlans": 1,
144+ "subnets_v4": 0,
145+ "subnets_v6": 0,
146+ },
147+ }
148+ self.assertEqual(json.loads(get_maas_stats()), expected)
149+
150 def test_get_request_params_returns_params(self):
151 factory.make_RegionRackController()
152 params = {

Subscribers

People subscribed via source and target branches