Skip to content

Commit e8db113

Browse files
authored
Merge pull request #163 from eichisanden/main
fix: #161 Incorrect output values in issue_metrics.json
2 parents 9291773 + 8af3ba3 commit e8db113

File tree

3 files changed

+133
-20
lines changed

3 files changed

+133
-20
lines changed

json_writer.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525

2626
def write_to_json(
2727
issues_with_metrics: Union[List[IssueWithMetrics], None],
28-
average_time_to_first_response: Union[timedelta, None],
29-
average_time_to_close: Union[timedelta, None],
30-
average_time_to_answer: Union[timedelta, None],
31-
average_time_in_labels: Union[dict, None],
28+
stats_time_to_first_response: Union[dict[str, timedelta], None],
29+
stats_time_to_close: Union[dict[str, timedelta], None],
30+
stats_time_to_answer: Union[dict[str, timedelta], None],
31+
stats_time_in_labels: Union[dict[str, dict[str, timedelta]], None],
3232
num_issues_opened: Union[int, None],
3333
num_issues_closed: Union[int, None],
3434
search_query: str,
@@ -76,16 +76,30 @@ def write_to_json(
7676
if not issues_with_metrics:
7777
return ""
7878

79+
average_time_to_first_response = None
80+
if stats_time_to_first_response is not None:
81+
average_time_to_first_response = stats_time_to_first_response['avg']
82+
83+
average_time_to_close = None
84+
if stats_time_to_close is not None:
85+
average_time_to_close = stats_time_to_close['avg']
86+
87+
average_time_to_answer = None
88+
if stats_time_to_answer is not None:
89+
average_time_to_answer = stats_time_to_answer['avg']
90+
91+
average_time_in_labels = {}
92+
for stats_type, labels in stats_time_in_labels.items():
93+
if stats_type == 'avg':
94+
for label, time in labels.items():
95+
average_time_in_labels[label] = str(time)
96+
7997
# Create a dictionary with the metrics
80-
labels_metrics = {}
81-
if average_time_in_labels:
82-
for label, time in average_time_in_labels.items():
83-
labels_metrics[label] = str(time)
8498
metrics = {
8599
"average_time_to_first_response": str(average_time_to_first_response),
86100
"average_time_to_close": str(average_time_to_close),
87101
"average_time_to_answer": str(average_time_to_answer),
88-
"average_time_in_labels": labels_metrics,
102+
"average_time_in_labels": average_time_in_labels,
89103
"num_items_opened": num_issues_opened,
90104
"num_items_closed": num_issues_closed,
91105
"total_item_count": len(issues_with_metrics),

markdown_writer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ def get_non_hidden_columns(labels) -> List[str]:
7070

7171
def write_to_markdown(
7272
issues_with_metrics: Union[List[IssueWithMetrics], None],
73-
average_time_to_first_response: Union[timedelta, None],
74-
average_time_to_close: Union[timedelta, None],
75-
average_time_to_answer: Union[timedelta, None],
73+
average_time_to_first_response: Union[dict[str, timedelta], None],
74+
average_time_to_close: Union[dict[str, timedelta], None],
75+
average_time_to_answer: Union[dict[str, timedelta], None],
7676
average_time_in_labels: Union[dict, None],
7777
num_issues_opened: Union[int, None],
7878
num_issues_closed: Union[int, None],

test_json_writer.py

Lines changed: 107 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
class TestWriteToJson(unittest.TestCase):
1111
"""Tests for the write_to_json function."""
1212

13+
# Show differences without omission in assertion
14+
maxDiff = None
15+
1316
def test_write_to_json(self):
1417
"""Test that write_to_json writes the correct JSON file."""
1518
issues_with_metrics = [
@@ -34,9 +37,27 @@ def test_write_to_json(self):
3437
labels_metrics={},
3538
),
3639
]
37-
average_time_to_first_response = timedelta(days=2.5)
38-
average_time_to_close = timedelta(days=5)
39-
average_time_to_answer = timedelta(days=1)
40+
41+
stats_time_to_first_response = {
42+
"avg": timedelta(days=2.5),
43+
"med": timedelta(days=2.5),
44+
"90p": timedelta(days=1.5),
45+
}
46+
stats_time_to_close = {
47+
"avg": timedelta(days=5),
48+
"med": timedelta(days=4),
49+
"90p": timedelta(days=3),
50+
}
51+
stats_time_to_answer = {
52+
"avg": timedelta(days=1),
53+
"med": timedelta(days=2),
54+
"90p": timedelta(days=3),
55+
}
56+
stats_time_in_labels = {
57+
"avg": {"bug": timedelta(days=1, hours=16, minutes=24, seconds=12)},
58+
"med": {"bug": timedelta(days=1, hours=16, minutes=24, seconds=12)},
59+
"90p": {"bug": timedelta(days=1, hours=16, minutes=24, seconds=12)},
60+
}
4061
num_issues_opened = 2
4162
num_issues_closed = 1
4263

@@ -75,12 +96,90 @@ def test_write_to_json(self):
7596
self.assertEqual(
7697
write_to_json(
7798
issues_with_metrics=issues_with_metrics,
78-
average_time_to_first_response=average_time_to_first_response,
79-
average_time_to_close=average_time_to_close,
80-
average_time_to_answer=average_time_to_answer,
81-
average_time_in_labels={
82-
"bug": timedelta(days=1, hours=16, minutes=24, seconds=12)
99+
stats_time_to_first_response=stats_time_to_first_response,
100+
stats_time_to_close=stats_time_to_close,
101+
stats_time_to_answer=stats_time_to_answer,
102+
stats_time_in_labels=stats_time_in_labels,
103+
num_issues_opened=num_issues_opened,
104+
num_issues_closed=num_issues_closed,
105+
search_query="is:issue repo:owner/repo",
106+
),
107+
json.dumps(expected_output),
108+
)
109+
110+
def test_write_to_json_with_no_response(self):
111+
"""Test where there is no answer to a issue."""
112+
issues_with_metrics = [
113+
IssueWithMetrics(
114+
title="Issue 1",
115+
html_url="https://github.com/owner/repo/issues/1",
116+
author="alice",
117+
time_to_first_response=None,
118+
time_to_close=None,
119+
time_to_answer=None,
120+
labels_metrics={},
121+
),
122+
IssueWithMetrics(
123+
title="Issue 2",
124+
html_url="https://github.com/owner/repo/issues/2",
125+
author="bob",
126+
time_to_first_response=None,
127+
time_to_close=None,
128+
time_to_answer=None,
129+
labels_metrics={},
130+
),
131+
]
132+
133+
stats_time_to_first_response = None
134+
stats_time_to_close = None
135+
stats_time_to_answer = None
136+
stats_time_in_labels = {
137+
"avg": {},
138+
"med": {},
139+
"90p": {},
140+
}
141+
num_issues_opened = 2
142+
num_issues_closed = 0
143+
144+
expected_output = {
145+
"average_time_to_first_response": "None",
146+
"average_time_to_close": "None",
147+
"average_time_to_answer": "None",
148+
"average_time_in_labels": {},
149+
"num_items_opened": 2,
150+
"num_items_closed": 0,
151+
"total_item_count": 2,
152+
"issues": [
153+
{
154+
"title": "Issue 1",
155+
"html_url": "https://github.com/owner/repo/issues/1",
156+
"author": "alice",
157+
"time_to_first_response": "None",
158+
"time_to_close": "None",
159+
"time_to_answer": "None",
160+
"label_metrics": {},
83161
},
162+
{
163+
"title": "Issue 2",
164+
"html_url": "https://github.com/owner/repo/issues/2",
165+
"author": "bob",
166+
"time_to_first_response": "None",
167+
"time_to_close": "None",
168+
"time_to_answer": "None",
169+
"label_metrics": {},
170+
},
171+
],
172+
"search_query": "is:issue repo:owner/repo",
173+
}
174+
175+
# Call the function and check the output
176+
self.assertEqual(
177+
write_to_json(
178+
issues_with_metrics=issues_with_metrics,
179+
stats_time_to_first_response=stats_time_to_first_response,
180+
stats_time_to_close=stats_time_to_close,
181+
stats_time_to_answer=stats_time_to_answer,
182+
stats_time_in_labels=stats_time_in_labels,
84183
num_issues_opened=num_issues_opened,
85184
num_issues_closed=num_issues_closed,
86185
search_query="is:issue repo:owner/repo",

0 commit comments

Comments
 (0)