Repository: mozilla/bugbug

Issue:
Add Blank metric
See https://github.com/mozilla/rust-code-analysis/commit/86ac7c967cf70be99ef8d5a138da45b1c4266dca


Discussion:
@Luni-4 So we have to implement the blank metric just like we dig in that repo of rust code analysis. can you guide me more on this ?? I would like to work on this issue
@rajpratyush this would be similar to 51195344ff2c889ba58db4621f9c13e3091ab140.
so can i work on both this one and #1448 
@rajpratyush I told you, you can work on anything you want. Issues are not assigned until there is a PR linked to them :)
Hi @rajpratyush! Thanks for your help! :)

Yes, you can implement the two issues you have chosen in this way:
- One pull request for the `Blank` metric
- One pull request for the `Mi` metric

To get the data associated to those metrics from `rust-code-analysis`, use as reference the commit posted above by @marco-c 
@marco-c  @Luni-4  @rajpratyush  If this is still available I would love to work on this.
Hey, I had started a PR on this issue but a KeyError on a line is making the [bugbug tests](https://community-tc.services.mozilla.com/tasks/d2w90CyRRQug4835ZEPdKQ) fail.

Copy-pasting my message on the PR:

> 
> 
> The bugbug test logs [here](https://community-tc.services.mozilla.com/tasks/P6uOPVR_TK6cmNc7k_s9bg/runs/0/logs/public/logs/live.log) always show a KeyError when using the `metrics` dictonary.
> 
> [`obj["blank_total"] += metrics["blank"]["sum"]` on line 508](https://github.com/1Gokul/bugbug/blob/e10c6a654d91ef5cc02c9cef05e4f6e792193e8c/bugbug/repository.py#L508) seems to be causing the error.
> 
> Any ideas on how to fix?

I actually wasn't sure if I should've included the line. A KeyError means that the "blank_total_ key doesn't exist, right? So should I remove the line?

@1Gokul for issues about the PR, please comment in the PR itself.
Hi, am I able to work on this issue or has it already been assigned to @1Gokul? 
> 
> 
> Hi, am I able to work on this issue or has it already been assigned to @1Gokul?

It's actually assigned to @Patil2099 because he made the first pull request. [My PR will be checked only if his is abandoned.](https://github.com/mozilla/bugbug/pull/2203#issuecomment-787523883)

Resolving patch:
diff --git a/bugbug/commit_features.py b/bugbug/commit_features.py
--- a/bugbug/commit_features.py
+++ b/bugbug/commit_features.py
@@ -151,6 +151,7 @@ def __call__(self, commit, **kwargs):
             "Average file number of instruction loc": commit["metrics"]["ploc_avg"],
             "Average file number of logical loc": commit["metrics"]["lloc_avg"],
             "Average file number of comment loc": commit["metrics"]["cloc_avg"],
+            "Average file blank": commit["metrics"]["blank_avg"],
             "Average file number of function arguments": commit["metrics"]["nargs_avg"],
             "Average file number of function exit points": commit["metrics"][
                 "nexits_avg"
@@ -172,6 +173,7 @@ def __call__(self, commit, **kwargs):
             "Maximum file number of instruction loc": commit["metrics"]["ploc_max"],
             "Maximum file number of logical loc": commit["metrics"]["lloc_max"],
             "Maximum file number of comment loc": commit["metrics"]["cloc_max"],
+            "Maximum file blank": commit["metrics"]["blank_max"],
             "Maximum file number of function arguments": commit["metrics"]["nargs_max"],
             "Maximum file number of function exit points": commit["metrics"][
                 "nexits_max"
@@ -193,6 +195,7 @@ def __call__(self, commit, **kwargs):
             "Minimum file number of instruction loc": commit["metrics"]["ploc_min"],
             "Minimum file number of logical loc": commit["metrics"]["lloc_min"],
             "Minimum file number of comment loc": commit["metrics"]["cloc_min"],
+            "Minimum file blank": commit["metrics"]["blank_min"],
             "Minimum file number of function arguments": commit["metrics"]["nargs_min"],
             "Minimum file number of function exit points": commit["metrics"][
                 "nexits_min"
@@ -214,6 +217,7 @@ def __call__(self, commit, **kwargs):
             "Total file number of instruction loc": commit["metrics"]["ploc_total"],
             "Total file number of logical loc": commit["metrics"]["lloc_total"],
             "Total file number of comment loc": commit["metrics"]["cloc_total"],
+            "Total file blank": commit["metrics"]["blank_total"],
             "Total file number of function arguments": commit["metrics"]["nargs_total"],
             "Total file number of function exit points": commit["metrics"][
                 "nexits_total"
@@ -274,6 +278,7 @@ def __call__(self, commit, **kwargs):
             "Average function number of instruction loc": merged_metrics["ploc_avg"],
             "Average function number of logical loc": merged_metrics["lloc_avg"],
             "Average function number of comment loc": merged_metrics["cloc_avg"],
+            "Average function blank": merged_metrics["blank_avg"],
             "Average function number of function arguments": merged_metrics[
                 "nargs_avg"
             ],
@@ -297,6 +302,7 @@ def __call__(self, commit, **kwargs):
             "Maximum function number of instruction loc": merged_metrics["ploc_max"],
             "Maximum function number of logical loc": merged_metrics["lloc_max"],
             "Maximum function number of comment loc": merged_metrics["cloc_max"],
+            "Maximum function blank": merged_metrics["blank_max"],
             "Maximum function number of function arguments": merged_metrics[
                 "nargs_max"
             ],
@@ -320,6 +326,7 @@ def __call__(self, commit, **kwargs):
             "Minimum function number of instruction loc": merged_metrics["ploc_min"],
             "Minimum function number of logical loc": merged_metrics["lloc_min"],
             "Minimum function number of comment loc": merged_metrics["cloc_min"],
+            "Minimum function blank": merged_metrics["blank_min"],
             "Minimum function number of function arguments": merged_metrics[
                 "nargs_min"
             ],
@@ -343,6 +350,7 @@ def __call__(self, commit, **kwargs):
             "Total function number of instruction loc": merged_metrics["ploc_total"],
             "Total function number of logical loc": merged_metrics["lloc_total"],
             "Total function number of comment loc": merged_metrics["cloc_total"],
+            "Total function blank": merged_metrics["blank_total"],
             "Total function number of function arguments": merged_metrics[
                 "nargs_total"
             ],
@@ -374,6 +382,7 @@ def __call__(self, commit, **kwargs):
             "Diff in number of instruction loc": commit["metrics_diff"]["ploc_total"],
             "Diff in number of logical loc": commit["metrics_diff"]["lloc_total"],
             "Diff in number of comment loc": commit["metrics_diff"]["cloc_total"],
+            "Diff in blank": commit["metrics_diff"]["blank_total"],
             "Diff in number of function arguments": commit["metrics_diff"][
                 "nargs_total"
             ],
diff --git a/bugbug/repository.py b/bugbug/repository.py
--- a/bugbug/repository.py
+++ b/bugbug/repository.py
@@ -137,6 +137,7 @@ def get_type(path: str) -> str:
     "ploc",
     "lloc",
     "cloc",
+    "blank",
     "nargs",
     "nexits",
     "cognitive",
@@ -463,6 +464,7 @@ def get_summary_metrics(obj, metrics_space):
         obj["ploc_max"] = max(obj["ploc_max"], metrics["loc"]["ploc"])
         obj["lloc_max"] = max(obj["lloc_max"], metrics["loc"]["lloc"])
         obj["cloc_max"] = max(obj["cloc_max"], metrics["loc"]["cloc"])
+        obj["blank_max"] = max(obj["blank_max"], metrics["loc"]["blank"])
         obj["nargs_max"] = max(obj["nargs_max"], metrics["nargs"]["sum"])
         obj["nexits_max"] = max(obj["nexits_max"], metrics["nexits"]["sum"])
         obj["cognitive_max"] = max(obj["cognitive_max"], metrics["cognitive"]["sum"])
@@ -487,6 +489,7 @@ def get_summary_metrics(obj, metrics_space):
         obj["ploc_min"] = min(obj["ploc_min"], metrics["loc"]["ploc"])
         obj["lloc_min"] = min(obj["lloc_min"], metrics["loc"]["lloc"])
         obj["cloc_min"] = min(obj["cloc_min"], metrics["loc"]["cloc"])
+        obj["blank_min"] = min(obj["blank_min"], metrics["loc"]["blank"])
         obj["nargs_min"] = min(obj["nargs_min"], metrics["nargs"]["sum"])
         obj["nexits_min"] = min(obj["nexits_min"], metrics["nexits"]["sum"])
         obj["cognitive_min"] = min(obj["cognitive_min"], metrics["cognitive"]["sum"])
@@ -524,6 +527,7 @@ def get_space_metrics(
     obj["ploc_total"] += metrics["loc"]["ploc"]
     obj["lloc_total"] += metrics["loc"]["lloc"]
     obj["cloc_total"] += metrics["loc"]["cloc"]
+    obj["blank_total"] += metrics["loc"]["blank"]
     obj["nargs_total"] += metrics["nargs"]["sum"]
     obj["nexits_total"] += metrics["nexits"]["sum"]
     obj["cognitive_total"] += metrics["cognitive"]["sum"]


Test patch:
diff --git a/tests/test_repository.py b/tests/test_repository.py
--- a/tests/test_repository.py
+++ b/tests/test_repository.py
@@ -2475,6 +2475,10 @@ def test_set_commit_metrics():
         "mi_visual_studio_max": 78.94722643886807,
         "mi_visual_studio_min": 78.94722643886807,
         "mi_visual_studio_total": 73.97332286868733,
+        "blank_avg": 0.0,
+        "blank_max": 0.0,
+        "blank_min": 0.0,
+        "blank_total": 1.0,
     }
     assert commit.metrics_diff == {
         "cloc_total": 0.0,
@@ -2492,6 +2496,7 @@ def test_set_commit_metrics():
         "mi_original_total": 0.0,
         "mi_sei_total": 0.0,
         "mi_visual_studio_total": 0.0,
+        "blank_total": 0.0,
     }
     assert commit.functions == {
         "prova.cpp": [
@@ -2513,6 +2518,7 @@ def test_set_commit_metrics():
                     "mi_original_total": 134.9997572104644,
                     "mi_sei_total": 119.16444811614275,
                     "mi_visual_studio_total": 78.94722643886807,
+                    "blank_total": 0.0,
                 },
                 "name": "main",
                 "start": 3,
@@ -2648,6 +2654,10 @@ def test_set_commit_metrics_meaningful_diff():
         "mi_visual_studio_max": 74.65013689584615,
         "mi_visual_studio_min": 74.65013689584615,
         "mi_visual_studio_total": 70.67438557926586,
+        "blank_avg": 0.0,
+        "blank_max": 0.0,
+        "blank_min": 0.0,
+        "blank_total": 1.0,
     }
     assert commit.metrics_diff == {
         "cloc_total": 0.0,
@@ -2665,6 +2675,7 @@ def test_set_commit_metrics_meaningful_diff():
         "mi_original_total": -5.641182764910724,
         "mi_sei_total": -8.138506399684957,
         "mi_visual_studio_total": -3.298937289421474,
+        "blank_total": 0.0,
     }
     assert commit.functions == {
         "prova.cpp": [
@@ -2686,6 +2697,7 @@ def test_set_commit_metrics_meaningful_diff():
                     "mi_original_total": 127.65173409189691,
                     "mi_sei_total": 108.56349160264799,
                     "mi_visual_studio_total": 74.65013689584615,
+                    "blank_total": 0.0,
                 },
                 "name": "main",
                 "start": 3,