Repository: chaoss/augur

Issue:
`pull_request_closed_no_merge` metric is broken
**Describe the bug:**
The `pull_request_closed_no_merge` metric returns a 500 internal server error.

**To Reproduce:**
Steps to reproduce the behavior:
1. Checkout `dev`
2. Run `augur run --disable-housekeeper`
3. Hit `http://localhost:5000/api/unstable/repo/21000/pull-request-closed-no-merge`
4. Observe error

**Expected behavior:**
I expected the metric to return a valid response object, even if it was empty.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Desktop (please complete the following information):**
 - OS: macOS v 10.14.6

**Additional context**
I noticed that there is a missing `GROUP_BY` clause in the SQL query, based on the usage of `COUNT`. This appears to be the main culprit of the issue.

Broken Travis build: https://travis-ci.org/chaoss/augur/builds/657396384



Discussion:
@ccarterlandis Has this been occurring only for the empty objects?
I think It's a 404 error  :sweat_smile:
```
>       assert response.status_code == 200
E       assert 404 == 200
E        +  where 404 = <Response [404]>.status_code
```
Check out this line of the metrics test: https://travis-ci.org/chaoss/augur/jobs/657396385#L581. You'll see the SQL error I'm referring to
I didn't even notice this earlier, but the metrics API test is also failing because the route is incorrect. It should be `...unstable/repos/...` instead of `.../unstable/repo/` - this is why it's returning a 404. I'd also suggest using the same repo ID in both your metric and API test.
> Check out this line of the metrics test: https://travis-ci.org/chaoss/augur/jobs/657396385#L581. You'll see the SQL error I'm referring to

Gotcha, I see where the error is now. I'll send in a fix ASAP
I'll fix both of them in a PR.

Resolving patch:
diff --git a/augur/metrics/pull_request/pull_request.py b/augur/metrics/pull_request/pull_request.py
--- a/augur/metrics/pull_request/pull_request.py
+++ b/augur/metrics/pull_request/pull_request.py
@@ -78,9 +78,13 @@ def pull_requests_closed_no_merge(self, repo_group_id, repo_id=None, period='day
     if repo_id:
         closedNoMerge = s.sql.text("""
             SELECT DATE_TRUNC(:period, pull_requests.pr_closed_at) AS closed_date,
-            COUNT(pull_request_id)
-            FROM pull_requests WHERE repo_id = :repo_id and pull_requests.pr_closed_at is NOT NULL and
+            COUNT(pull_request_id) as pr_count
+            FROM pull_requests JOIN repo ON pull_requests.repo_id = repo.repo_id
+            WHERE pull_requests.repo_id = :repo_id
+            AND pull_requests.pr_closed_at is NOT NULL AND
             pull_requests.pr_merged_at is NULL
+            GROUP BY closed_date, pull_request_id
+            ORDER BY closed_date
         """)
         results = pd.read_sql(closedNoMerge, self.database, params={'repo_id': repo_id, 'period': period,
                                                                      'begin_date': begin_date,
@@ -89,9 +93,11 @@ def pull_requests_closed_no_merge(self, repo_group_id, repo_id=None, period='day
     else:
         closedNoMerge = s.sql.text("""
             SELECT DATE_TRUNC(:period, pull_requests.pr_closed_at) AS closed_date,
-            COUNT(pull_request_id)
-            FROM pull_requests WHERE repo_id in (SELECT repo_id FROM repo WHERE repo_group_id = :repo_group_id)
+            COUNT(pull_request_id) as pr_count
+            FROM pull_requests JOIN repo ON pull_requests.repo_id = repo.repo_id WHERE pull_requests.repo_id in (SELECT repo_id FROM repo WHERE repo_group_id = :repo_group_id)
             and pull_requests.pr_closed_at is NOT NULL and pull_requests.pr_merged_at is NULL
+            GROUP BY closed_date, pull_request_id
+            ORDER BY closed_date
         """)
 
         results = pd.read_sql(closedNoMerge, self.database,
diff --git a/augur/metrics/pull_request/routes.py b/augur/metrics/pull_request/routes.py
--- a/augur/metrics/pull_request/routes.py
+++ b/augur/metrics/pull_request/routes.py
@@ -384,7 +384,6 @@ def create_pull_request_routes(server):
     @apiName pull-request-closed-no-merge
     @apiGroup Experimental
     @apiDescription Timeseries of pull request which were closed but not merged
-    @apiParam {string} repo_group_id Repository Group ID.
     @apiParam {string} repo_id Repository ID.
     @apiParam {string} [begin_date="1970-1-1 0:0:0"] Beginning date specification. E.g. values: `2018`, `2018-05`, `2019-05-01`
     @apiParam {string} [end_date="current date"] Ending date specification. E.g. values: `2018`, `2018-05`, `2019-05-01`
@@ -392,8 +391,26 @@ def create_pull_request_routes(server):
                     [
                         {
                             "date": "2019-01-01T00:00:00.000Z",
-                            "rate": 5.3333333333
+                            "pr_count": 3
                         }
                     ]
     """
     server.addRepoMetric(metrics.pull_requests_closed_no_merge, 'pull-requests-closed-no-merge')
+
+    """
+    @api {get} /repo-groups/:repo_group_id/pull-request-closed-no-merge Pull Request Closed but not merged(Repo)
+    @apiName pull-request-closed-no-merge
+    @apiGroup Experimental
+    @apiDescription Timeseries of pull request which were closed but not merged
+    @apiParam {string} repo_group_id Repository Group ID.
+    @apiParam {string} [begin_date="1970-1-1 0:0:0"] Beginning date specification. E.g. values: `2018`, `2018-05`, `2019-05-01`
+    @apiParam {string} [end_date="current date"] Ending date specification. E.g. values: `2018`, `2018-05`, `2019-05-01`
+    @apiSuccessExample {json} Success-Response:
+                    [
+                        {
+                            "date": "2019-01-01T00:00:00.000Z",
+                            "pr_count": 3
+                        }
+                    ]
+    """
+    server.addRepoGroupMetric(metrics.pull_requests_closed_no_merge, 'pull-requests-closed-no-merge')


Test patch:
diff --git a/test/api/test_pull_request_routes.py b/test/api/test_pull_request_routes.py
--- a/test/api/test_pull_request_routes.py
+++ b/test/api/test_pull_request_routes.py
@@ -20,8 +20,9 @@ def test_pull_requests_merge_contributor_new_by_repo(metrics):
     assert data[0]["count"] > 0
 
 def test_pull_requests_closed_no_merge(metrics):
-    response = requests.get('http://localhost:5000/api/unstable/repo/20/pull-request-closed-no-merge')
+    response = requests.get('http://localhost:5000/api/unstable/repos/21000/pull-requests-closed-no-merge')
     data = response.json()
     assert response.status_code == 200
     assert len(data) >= 1
-    assert data[0]["count"] > 0
+    assert data[0]["pr_count"] > 0
+
diff --git a/test/metrics/test_pull_request_metrics.py b/test/metrics/test_pull_request_metrics.py
--- a/test/metrics/test_pull_request_metrics.py
+++ b/test/metrics/test_pull_request_metrics.py
@@ -28,7 +28,7 @@ def test_pull_request_acceptance_rate(metrics):
                                      end_date='2019-12-31 23:59:59',group_by='year').iloc[0]['rate'] > 0
 
 def test_pull_request_closed_no_merge(metrics):
-    assert metrics.pull_requests_closed_no_merge(24).iloc[0]['count'] > 0
+    assert metrics.pull_requests_closed_no_merge(24).iloc[0]['pr_count'] > 0
     assert metrics.pull_requests_closed_no_merge(24, 21000, begin_date='2018-1-1 00:00:00',
-                                     end_date='2019-12-31 23:59:59', group_by='year').iloc[0]['count'] > 0
+                                     end_date='2019-12-31 23:59:59').iloc[0]['pr_count'] > 0