← Back to team overview

sts-sponsors team mailing list archive

[Merge] ~troyanov/maas:dhcp-snippets-metrics into maas:master

 

Anton Troyanov has proposed merging ~troyanov/maas:dhcp-snippets-metrics into maas:master.

Commit message:
feat(metrics): collect usage of DHCP snippets

In order to identify how often DHCP snippets feature is used, we should
collect usage statistics.

Requested reviews:
  MAAS Maintainers (maas-maintainers)

For more details, see:
https://code.launchpad.net/~troyanov/maas/+git/maas/+merge/443490
-- 
Your team MAAS Maintainers is requested to review the proposed merge of ~troyanov/maas:dhcp-snippets-metrics into maas:master.
diff --git a/src/maasserver/stats.py b/src/maasserver/stats.py
index e13ba55..7c9b9ca 100644
--- a/src/maasserver/stats.py
+++ b/src/maasserver/stats.py
@@ -14,7 +14,7 @@ from collections import Counter, defaultdict
 from datetime import timedelta
 import json
 
-from django.db.models import Case, Count, F, Max, When
+from django.db.models import Case, Count, F, Max, Q, When
 import requests
 from twisted.application.internet import TimerService
 
@@ -29,6 +29,7 @@ from maasserver.models import (
     BMC,
     BootResourceFile,
     Config,
+    DHCPSnippet,
     Fabric,
     Machine,
     Node,
@@ -435,6 +436,15 @@ def get_vault_stats():
     return {"enabled": Config.objects.get_config("vault_enabled", False)}
 
 
+def get_dhcp_snippets_stats():
+    dhcp_snippets = DHCPSnippet.objects.aggregate(
+        node_count=Count("pk", filter=(~Q(node=None) & Q(subnet=None))),
+        subnet_count=Count("pk", filter=(~Q(subnet=None) & Q(node=None))),
+        global_count=Count("pk", filter=(Q(subnet=None) & Q(node=None))),
+    )
+
+    return dhcp_snippets
+
 def get_maas_stats():
     # TODO
     # - architectures
@@ -461,6 +471,7 @@ def get_maas_stats():
             "regions": node_types.get(NODE_TYPE.REGION_CONTROLLER, 0),
             "racks": node_types.get(NODE_TYPE.RACK_CONTROLLER, 0),
         },
+        "dhcp_snippets": get_dhcp_snippets_stats(),
         "nodes": {
             "machines": node_types.get(NODE_TYPE.MACHINE, 0),
             "devices": node_types.get(NODE_TYPE.DEVICE, 0),
diff --git a/src/maasserver/tests/test_stats.py b/src/maasserver/tests/test_stats.py
index 204938f..d2c2ab8 100644
--- a/src/maasserver/tests/test_stats.py
+++ b/src/maasserver/tests/test_stats.py
@@ -39,6 +39,7 @@ from maasserver.stats import (
     get_brownfield_stats,
     get_custom_images_deployed_stats,
     get_custom_images_uploaded_stats,
+    get_dhcp_snippets_stats,
     get_lxd_initial_auth_stats,
     get_maas_stats,
     get_machine_stats,
@@ -470,6 +471,11 @@ class TestMAASStats(MAASServerTestCase):
             "vault": {
                 "enabled": False,
             },
+            "dhcp_snippets": {
+                "node_count": 0,
+                "subnet_count": 0,
+                "global_count": 0,
+            },
         }
         self.assertEqual(stats, expected)
 
@@ -650,6 +656,11 @@ class TestMAASStats(MAASServerTestCase):
             "vault": {
                 "enabled": False,
             },
+            "dhcp_snippets": {
+                "node_count": 0,
+                "subnet_count": 0,
+                "global_count": 0,
+            },
         }
         self.assertEqual(get_maas_stats(), expected)
 
@@ -808,6 +819,23 @@ class TestMAASStats(MAASServerTestCase):
         Config.objects.set_config("vault_enabled", False)
         self.assertEqual({"enabled": False}, get_vault_stats())
 
+    def test_get_dhcp_snippet_stats(self):
+        for _ in range(3):
+            node = factory.make_Node()
+            factory.make_DHCPSnippet(node=node)
+
+        for _ in range(4):
+            subnet = factory.make_Subnet()
+            factory.make_DHCPSnippet(subnet=subnet)
+
+        for _ in range(5):
+            factory.make_DHCPSnippet()
+
+        self.assertEqual(
+            {"node_count": 3, "subnet_count": 4, "global_count": 5},
+            get_dhcp_snippets_stats(),
+        )
+
 
 class FakeRequest:
     def __init__(self, user):

Follow ups