← Back to team overview

sts-sponsors team mailing list archive

[Merge] ~troyanov/maas:tags-with-kernel-opts-metrics into maas:master

 

Anton Troyanov has proposed merging ~troyanov/maas:tags-with-kernel-opts-metrics into maas:master.

Commit message:
feat(metrics): collect usage of tags

In order to identify how tags are being used, we want to collect
statistics about:
- total tags
- automatic tags
- tags with kernel options

Requested reviews:
  MAAS Lander (maas-lander): unittests
  MAAS Maintainers (maas-maintainers)

For more details, see:
https://code.launchpad.net/~troyanov/maas/+git/maas/+merge/443507
-- 
Your team MAAS Maintainers is requested to review the proposed merge of ~troyanov/maas:tags-with-kernel-opts-metrics into maas:master.
diff --git a/src/maasserver/stats.py b/src/maasserver/stats.py
index 3763c23..06dc61d 100644
--- a/src/maasserver/stats.py
+++ b/src/maasserver/stats.py
@@ -38,6 +38,7 @@ from maasserver.models import (
     Space,
     StaticIPAddress,
     Subnet,
+    Tag,
     VLAN,
     VMCluster,
 )
@@ -446,6 +447,14 @@ def get_dhcp_snippets_stats():
     return dhcp_snippets
 
 
+def get_tags_stats():
+    return Tag.objects.aggregate(
+        total_count=Count("pk"),
+        automatic_tag_count=Count("pk", filter=~Q(definition="")),
+        with_kernel_opts_count=Count("pk", filter=~Q(kernel_opts="")),
+    )
+
+
 def get_maas_stats():
     # TODO
     # - architectures
@@ -498,6 +507,7 @@ def get_maas_stats():
         "tls_configuration": get_tls_configuration_stats(),
         "bmcs": get_bmc_stats(),
         "vault": get_vault_stats(),
+        "tags": get_tags_stats(),
     }
 
 
diff --git a/src/maasserver/tests/test_stats.py b/src/maasserver/tests/test_stats.py
index d2c2ab8..18053e9 100644
--- a/src/maasserver/tests/test_stats.py
+++ b/src/maasserver/tests/test_stats.py
@@ -46,6 +46,7 @@ from maasserver.stats import (
     get_machines_by_architecture,
     get_request_params,
     get_storage_layouts_stats,
+    get_tags_stats,
     get_tls_configuration_stats,
     get_vault_stats,
     get_vm_hosts_stats,
@@ -476,6 +477,11 @@ class TestMAASStats(MAASServerTestCase):
                 "subnet_count": 0,
                 "global_count": 0,
             },
+            "tags": {
+                "total_count": 0,
+                "automatic_tag_count": 0,
+                "with_kernel_opts_count": 0,
+            },
         }
         self.assertEqual(stats, expected)
 
@@ -661,6 +667,11 @@ class TestMAASStats(MAASServerTestCase):
                 "subnet_count": 0,
                 "global_count": 0,
             },
+            "tags": {
+                "total_count": 0,
+                "automatic_tag_count": 0,
+                "with_kernel_opts_count": 0,
+            },
         }
         self.assertEqual(get_maas_stats(), expected)
 
@@ -836,6 +847,30 @@ class TestMAASStats(MAASServerTestCase):
             get_dhcp_snippets_stats(),
         )
 
+    def test_get_tags_stats(self):
+        for _ in range(2):
+            factory.make_Tag(definition="", kernel_opts="")
+
+        for _ in range(2):
+            factory.make_Tag(definition=factory.make_name(), kernel_opts="")
+
+        for _ in range(3):
+            factory.make_Tag(definition="", kernel_opts=factory.make_name())
+
+        for _ in range(3):
+            factory.make_Tag(
+                definition=factory.make_name(), kernel_opts=factory.make_name()
+            )
+
+        self.assertEqual(
+            {
+                "total_count": 10,
+                "automatic_tag_count": 5,
+                "with_kernel_opts_count": 6,
+            },
+            get_tags_stats(),
+        )
+
 
 class FakeRequest:
     def __init__(self, user):

References