Repository: github/issue-metrics
Pull request #164 (2023-11-01 15:58:15)

feature: support median and 90 percentile in json format.
<!-- Please ensure your PR title is brief and descriptive for a good changelog entry -->
<!-- Link to issue if there is one -->

<!-- Describe what the changes are -->

## Proposed Changes
In addition to the #163 , support for median and 90 percentile in json format.
If you prefer this Pull Request, please close #163.

## Readiness Checklist

### Author/Contributor
- [x] If documentation is needed for this change, has that been included in this pull request
- [x] run `make lint` and fix any issues that you have introduced
- [x] run `make test` and ensure you have test coverage for the lines you are introducing

### Reviewer
- [x] Label as either `bug`, `documentation`, `enhancement`, `infrastructure`, or `breaking`



Patch:
diff --git a/json_writer.py b/json_writer.py
index f31e556..cc4d12d 100644
--- a/json_writer.py
+++ b/json_writer.py
@@ -76,23 +76,42 @@ def write_to_json(
     if not issues_with_metrics:
         return ""
 
+    # time to first response
     average_time_to_first_response = None
+    med_time_to_first_response = None
+    p90_time_to_first_response = None
     if stats_time_to_first_response is not None:
         average_time_to_first_response = stats_time_to_first_response['avg']
+        med_time_to_first_response = stats_time_to_first_response['med']
+        p90_time_to_first_response = stats_time_to_first_response['90p']
 
+    # time to close
     average_time_to_close = None
+    med_time_to_close = None
+    p90_time_to_close = None
     if stats_time_to_close is not None:
         average_time_to_close = stats_time_to_close['avg']
+        med_time_to_close = stats_time_to_close['med']
+        p90_time_to_close = stats_time_to_close['90p']
 
+    # time to answer
     average_time_to_answer = None
+    med_time_to_answer = None
+    p90_time_to_answer = None
     if stats_time_to_answer is not None:
         average_time_to_answer = stats_time_to_answer['avg']
+        med_time_to_answer = stats_time_to_answer['med']
+        p90_time_to_answer = stats_time_to_answer['90p']
 
     average_time_in_labels = {}
-    for stats_type, labels in stats_time_in_labels.items():
-        if stats_type == 'avg':
-            for label, time in labels.items():
-                average_time_in_labels[label] = str(time)
+    med_time_in_labels = {}
+    p90_time_in_labels = {}
+    for label, time in stats_time_in_labels['avg'].items():
+        average_time_in_labels[label] = str(time)
+    for label, time in stats_time_in_labels['med'].items():
+        med_time_in_labels[label] = str(time)
+    for label, time in stats_time_in_labels['90p'].items():
+        p90_time_in_labels[label] = str(time)
 
     # Create a dictionary with the metrics
     metrics = {
@@ -100,6 +119,14 @@ def write_to_json(
         "average_time_to_close": str(average_time_to_close),
         "average_time_to_answer": str(average_time_to_answer),
         "average_time_in_labels": average_time_in_labels,
+        "median_time_to_first_response": str(med_time_to_first_response),
+        "median_time_to_close": str(med_time_to_close),
+        "median_time_to_answer": str(med_time_to_answer),
+        "median_time_in_labels": med_time_in_labels,
+        "90_percentile_time_to_first_response": str(p90_time_to_first_response),
+        "90_percentile_time_to_close": str(p90_time_to_close),
+        "90_percentile_time_to_answer": str(p90_time_to_answer),
+        "90_percentile_time_in_labels": p90_time_in_labels,
         "num_items_opened": num_issues_opened,
         "num_items_closed": num_issues_closed,
         "total_item_count": len(issues_with_metrics),


Test patch:
diff --git a/test_json_writer.py b/test_json_writer.py
index 99e2b6a..d807066 100644
--- a/test_json_writer.py
+++ b/test_json_writer.py
@@ -66,6 +66,14 @@ class TestWriteToJson(unittest.TestCase):
             "average_time_to_close": "5 days, 0:00:00",
             "average_time_to_answer": "1 day, 0:00:00",
             "average_time_in_labels": {"bug": "1 day, 16:24:12"},
+            "median_time_to_first_response": "2 days, 12:00:00",
+            "median_time_to_close": "4 days, 0:00:00",
+            "median_time_to_answer": "2 days, 0:00:00",
+            "median_time_in_labels": {"bug": "1 day, 16:24:12"},
+            "90_percentile_time_to_first_response": "1 day, 12:00:00",
+            "90_percentile_time_to_close": "3 days, 0:00:00",
+            "90_percentile_time_to_answer": "3 days, 0:00:00",
+            "90_percentile_time_in_labels": {"bug": "1 day, 16:24:12"},
             "num_items_opened": 2,
             "num_items_closed": 1,
             "total_item_count": 2,
@@ -146,6 +154,14 @@ class TestWriteToJson(unittest.TestCase):
             "average_time_to_close": "None",
             "average_time_to_answer": "None",
             "average_time_in_labels": {},
+            "median_time_to_first_response": "None",
+            "median_time_to_close": "None",
+            "median_time_to_answer": "None",
+            "median_time_in_labels": {},
+            "90_percentile_time_to_first_response": "None",
+            "90_percentile_time_to_close": "None",
+            "90_percentile_time_to_answer": "None",
+            "90_percentile_time_in_labels": {},
             "num_items_opened": 2,
             "num_items_closed": 0,
             "total_item_count": 2,