prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  "Top Rated Datasets" needs to be removed from stats
  **CKAN version**
  2.9.1
  
  **Describe the bug**
  "Top Rated Datasets" is still in /stats even though ratings are being deprecated #5558
  
  **Steps to reproduce**
  With stats enabled, go to /stats page.
  
  **Expected behavior**
  "Top Rated Datasets" is removed from stats.
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `ckanext/stats/blueprint.py`
  Content:
  ```
  1 # encoding: utf-8
  2 
  3 from flask import Blueprint
  4 
  5 from ckan.plugins.toolkit import c, render
  6 import ckanext.stats.stats as stats_lib
  7 import ckan.lib.helpers as h
  8 
  9 stats = Blueprint(u'stats', __name__)
  10 
  11 
  12 @stats.route(u'/stats')
  13 def index():
  14     stats = stats_lib.Stats()
  15     extra_vars = {
  16         u'top_rated_packages': stats.top_rated_packages(),
  17         u'largest_groups': stats.largest_groups(),
  18         u'top_tags': stats.top_tags(),
  19         u'top_package_creators': stats.top_package_creators(),
  20     }
  21     return render(u'ckanext/stats/index.html', extra_vars)
  22
  ```
  
  Path: `ckanext/stats/stats.py`
  Content:
  ```
  1 # encoding: utf-8
  2 
  3 import datetime
  4 import logging
  5 from ckan.common import config
  6 from six import text_type
  7 from sqlalchemy import Table, select, join, func, and_
  8 
  9 import ckan.plugins as p
  10 import ckan.model as model
  11 
  12 log = logging.getLogger(__name__)
  13 cache_enabled = p.toolkit.asbool(
  14     config.get('ckanext.stats.cache_enabled', False)
  15 )
  16 
  17 if cache_enabled:
  18     log.warn(
  19         'ckanext.stats does not support caching in current implementations'
  20     )
  21 
  22 DATE_FORMAT = '%Y-%m-%d'
  23 
  24 
  25 def table(name):
  26     return Table(name, model.meta.metadata, autoload=True)
  27 
  28 
  29 def datetime2date(datetime_):
  30     return datetime.date(datetime_.year, datetime_.month, datetime_.day)
  31 
  32 
  33 class Stats(object):
  34 
  35     @classmethod
  36     def top_rated_packages(cls, limit=10):
  37         # NB Not using sqlalchemy as sqla 0.4 doesn't work using both group_by
  38         # and apply_avg
  39         package = table('package')
  40         rating = table('rating')
  41         sql = select(
  42             [
  43                 package.c.id,
  44                 func.avg(rating.c.rating),
  45                 func.count(rating.c.rating)
  46             ],
  47             from_obj=[package.join(rating)]
  48         ).where(and_(package.c.private == False, package.c.state == 'active')
  49                 ).group_by(package.c.id).order_by(
  50                     func.avg(rating.c.rating).desc(),
  51                     func.count(rating.c.rating).desc()
  52                 ).limit(limit)
  53         res_ids = model.Session.execute(sql).fetchall()
  54         res_pkgs = [(
  55             model.Session.query(model.Package).get(text_type(pkg_id)), avg, num
  56         ) for pkg_id, avg, num in res_ids]
  57         return res_pkgs
  58 
  59     @classmethod
  60     def largest_groups(cls, limit=10):
  61         member = table('member')
  62         package = table('package')
  63 
  64         j = join(member, package, member.c.table_id == package.c.id)
  65 
  66         s = select(
  67             [member.c.group_id,
  68              func.count(member.c.table_id)]
  69         ).select_from(j).group_by(member.c.group_id).where(
  70             and_(
  71                 member.c.group_id != None, member.c.table_name == 'package',
  72                 package.c.private == False, package.c.state == 'active'
  73             )
  74         ).order_by(func.count(member.c.table_id).desc()).limit(limit)
  75 
  76         res_ids = model.Session.execute(s).fetchall()
  77         res_groups = [
  78             (model.Session.query(model.Group).get(text_type(group_id)), val)
  79             for group_id, val in res_ids
  80         ]
  81         return res_groups
  82 
  83     @classmethod
  84     def top_tags(cls, limit=10, returned_tag_info='object'):  # by package
  85         assert returned_tag_info in ('name', 'id', 'object')
  86         tag = table('tag')
  87         package_tag = table('package_tag')
  88         package = table('package')
  89         if returned_tag_info == 'name':
  90             from_obj = [package_tag.join(tag)]
  91             tag_column = tag.c.name
  92         else:
  93             from_obj = None
  94             tag_column = package_tag.c.tag_id
  95         j = join(
  96             package_tag, package, package_tag.c.package_id == package.c.id
  97         )
  98         s = select([tag_column,
  99                     func.count(package_tag.c.package_id)],
  100                    from_obj=from_obj).select_from(j).where(
  101                        and_(
  102                            package_tag.c.state == 'active',
  103                            package.c.private == False,
  104                            package.c.state == 'active'
  105                        )
  106                    )
  107         s = s.group_by(tag_column).order_by(
  108             func.count(package_tag.c.package_id).desc()
  109         ).limit(limit)
  110         res_col = model.Session.execute(s).fetchall()
  111         if returned_tag_info in ('id', 'name'):
  112             return res_col
  113         elif returned_tag_info == 'object':
  114             res_tags = [
  115                 (model.Session.query(model.Tag).get(text_type(tag_id)), val)
  116                 for tag_id, val in res_col
  117             ]
  118             return res_tags
  119 
  120     @classmethod
  121     def top_package_creators(cls, limit=10):
  122         userid_count = model.Session.query(
  123             model.Package.creator_user_id,
  124             func.count(model.Package.creator_user_id)
  125         ).filter(model.Package.state == 'active'
  126                  ).filter(model.Package.private == False).group_by(
  127                      model.Package.creator_user_id
  128                  ).order_by(func.count(model.Package.creator_user_id).desc()
  129                             ).limit(limit).all()
  130         user_count = [
  131             (model.Session.query(model.User).get(text_type(user_id)), count)
  132             for user_id, count in userid_count
  133             if user_id
  134         ]
  135         return user_count
  136
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/ckanext/stats/blueprint.py b/ckanext/stats/blueprint.py
  --- a/ckanext/stats/blueprint.py
  +++ b/ckanext/stats/blueprint.py
  @@ -13,7 +13,6 @@
   def index():
       stats = stats_lib.Stats()
       extra_vars = {
  -        u'top_rated_packages': stats.top_rated_packages(),
           u'largest_groups': stats.largest_groups(),
           u'top_tags': stats.top_tags(),
           u'top_package_creators': stats.top_package_creators(),
  diff --git a/ckanext/stats/stats.py b/ckanext/stats/stats.py
  --- a/ckanext/stats/stats.py
  +++ b/ckanext/stats/stats.py
  @@ -32,30 +32,6 @@
   
   class Stats(object):
   
  -    @classmethod
  -    def top_rated_packages(cls, limit=10):
  -        # NB Not using sqlalchemy as sqla 0.4 doesn't work using both group_by
  -        # and apply_avg
  -        package = table('package')
  -        rating = table('rating')
  -        sql = select(
  -            [
  -                package.c.id,
  -                func.avg(rating.c.rating),
  -                func.count(rating.c.rating)
  -            ],
  -            from_obj=[package.join(rating)]
  -        ).where(and_(package.c.private == False, package.c.state == 'active')
  -                ).group_by(package.c.id).order_by(
  -                    func.avg(rating.c.rating).desc(),
  -                    func.count(rating.c.rating).desc()
  -                ).limit(limit)
  -        res_ids = model.Session.execute(sql).fetchall()
  -        res_pkgs = [(
  -            model.Session.query(model.Package).get(text_type(pkg_id)), avg, num
  -        ) for pkg_id, avg, num in res_ids]
  -        return res_pkgs
  -
       @classmethod
       def largest_groups(cls, limit=10):
           member = table('member')
  
verification_info: {"golden_diff": "diff --git a/ckanext/stats/blueprint.py b/ckanext/stats/blueprint.py\n--- a/ckanext/stats/blueprint.py\n+++ b/ckanext/stats/blueprint.py\n@@ -13,7 +13,6 @@\n def index():\n     stats = stats_lib.Stats()\n     extra_vars = {\n-        u'top_rated_packages': stats.top_rated_packages(),\n         u'largest_groups': stats.largest_groups(),\n         u'top_tags': stats.top_tags(),\n         u'top_package_creators': stats.top_package_creators(),\ndiff --git a/ckanext/stats/stats.py b/ckanext/stats/stats.py\n--- a/ckanext/stats/stats.py\n+++ b/ckanext/stats/stats.py\n@@ -32,30 +32,6 @@\n \n class Stats(object):\n \n-    @classmethod\n-    def top_rated_packages(cls, limit=10):\n-        # NB Not using sqlalchemy as sqla 0.4 doesn't work using both group_by\n-        # and apply_avg\n-        package = table('package')\n-        rating = table('rating')\n-        sql = select(\n-            [\n-                package.c.id,\n-                func.avg(rating.c.rating),\n-                func.count(rating.c.rating)\n-            ],\n-            from_obj=[package.join(rating)]\n-        ).where(and_(package.c.private == False, package.c.state == 'active')\n-                ).group_by(package.c.id).order_by(\n-                    func.avg(rating.c.rating).desc(),\n-                    func.count(rating.c.rating).desc()\n-                ).limit(limit)\n-        res_ids = model.Session.execute(sql).fetchall()\n-        res_pkgs = [(\n-            model.Session.query(model.Package).get(text_type(pkg_id)), avg, num\n-        ) for pkg_id, avg, num in res_ids]\n-        return res_pkgs\n-\n     @classmethod\n     def largest_groups(cls, limit=10):\n         member = table('member')\n", "issue": "\"Top Rated Datasets\" needs to be removed from stats\n**CKAN version**\r\n2.9.1\r\n\r\n**Describe the bug**\r\n\"Top Rated Datasets\" is still in /stats even though ratings are being deprecated #5558\r\n\r\n**Steps to reproduce**\r\nWith stats enabled, go to /stats page.\r\n\r\n**Expected behavior**\r\n\"Top Rated Datasets\" is removed from stats.\r\n\n", "before_files": [{"content": "# encoding: utf-8\n\nfrom flask import Blueprint\n\nfrom ckan.plugins.toolkit import c, render\nimport ckanext.stats.stats as stats_lib\nimport ckan.lib.helpers as h\n\nstats = Blueprint(u'stats', __name__)\n\n\n@stats.route(u'/stats')\ndef index():\n    stats = stats_lib.Stats()\n    extra_vars = {\n        u'top_rated_packages': stats.top_rated_packages(),\n        u'largest_groups': stats.largest_groups(),\n        u'top_tags': stats.top_tags(),\n        u'top_package_creators': stats.top_package_creators(),\n    }\n    return render(u'ckanext/stats/index.html', extra_vars)\n", "path": "ckanext/stats/blueprint.py"}, {"content": "# encoding: utf-8\n\nimport datetime\nimport logging\nfrom ckan.common import config\nfrom six import text_type\nfrom sqlalchemy import Table, select, join, func, and_\n\nimport ckan.plugins as p\nimport ckan.model as model\n\nlog = logging.getLogger(__name__)\ncache_enabled = p.toolkit.asbool(\n    config.get('ckanext.stats.cache_enabled', False)\n)\n\nif cache_enabled:\n    log.warn(\n        'ckanext.stats does not support caching in current implementations'\n    )\n\nDATE_FORMAT = '%Y-%m-%d'\n\n\ndef table(name):\n    return Table(name, model.meta.metadata, autoload=True)\n\n\ndef datetime2date(datetime_):\n    return datetime.date(datetime_.year, datetime_.month, datetime_.day)\n\n\nclass Stats(object):\n\n    @classmethod\n    def top_rated_packages(cls, limit=10):\n        # NB Not using sqlalchemy as sqla 0.4 doesn't work using both group_by\n        # and apply_avg\n        package = table('package')\n        rating = table('rating')\n        sql = select(\n            [\n                package.c.id,\n                func.avg(rating.c.rating),\n                func.count(rating.c.rating)\n            ],\n            from_obj=[package.join(rating)]\n        ).where(and_(package.c.private == False, package.c.state == 'active')\n                ).group_by(package.c.id).order_by(\n                    func.avg(rating.c.rating).desc(),\n                    func.count(rating.c.rating).desc()\n                ).limit(limit)\n        res_ids = model.Session.execute(sql).fetchall()\n        res_pkgs = [(\n            model.Session.query(model.Package).get(text_type(pkg_id)), avg, num\n        ) for pkg_id, avg, num in res_ids]\n        return res_pkgs\n\n    @classmethod\n    def largest_groups(cls, limit=10):\n        member = table('member')\n        package = table('package')\n\n        j = join(member, package, member.c.table_id == package.c.id)\n\n        s = select(\n            [member.c.group_id,\n             func.count(member.c.table_id)]\n        ).select_from(j).group_by(member.c.group_id).where(\n            and_(\n                member.c.group_id != None, member.c.table_name == 'package',\n                package.c.private == False, package.c.state == 'active'\n            )\n        ).order_by(func.count(member.c.table_id).desc()).limit(limit)\n\n        res_ids = model.Session.execute(s).fetchall()\n        res_groups = [\n            (model.Session.query(model.Group).get(text_type(group_id)), val)\n            for group_id, val in res_ids\n        ]\n        return res_groups\n\n    @classmethod\n    def top_tags(cls, limit=10, returned_tag_info='object'):  # by package\n        assert returned_tag_info in ('name', 'id', 'object')\n        tag = table('tag')\n        package_tag = table('package_tag')\n        package = table('package')\n        if returned_tag_info == 'name':\n            from_obj = [package_tag.join(tag)]\n            tag_column = tag.c.name\n        else:\n            from_obj = None\n            tag_column = package_tag.c.tag_id\n        j = join(\n            package_tag, package, package_tag.c.package_id == package.c.id\n        )\n        s = select([tag_column,\n                    func.count(package_tag.c.package_id)],\n                   from_obj=from_obj).select_from(j).where(\n                       and_(\n                           package_tag.c.state == 'active',\n                           package.c.private == False,\n                           package.c.state == 'active'\n                       )\n                   )\n        s = s.group_by(tag_column).order_by(\n            func.count(package_tag.c.package_id).desc()\n        ).limit(limit)\n        res_col = model.Session.execute(s).fetchall()\n        if returned_tag_info in ('id', 'name'):\n            return res_col\n        elif returned_tag_info == 'object':\n            res_tags = [\n                (model.Session.query(model.Tag).get(text_type(tag_id)), val)\n                for tag_id, val in res_col\n            ]\n            return res_tags\n\n    @classmethod\n    def top_package_creators(cls, limit=10):\n        userid_count = model.Session.query(\n            model.Package.creator_user_id,\n            func.count(model.Package.creator_user_id)\n        ).filter(model.Package.state == 'active'\n                 ).filter(model.Package.private == False).group_by(\n                     model.Package.creator_user_id\n                 ).order_by(func.count(model.Package.creator_user_id).desc()\n                            ).limit(limit).all()\n        user_count = [\n            (model.Session.query(model.User).get(text_type(user_id)), count)\n            for user_id, count in userid_count\n            if user_id\n        ]\n        return user_count\n", "path": "ckanext/stats/stats.py"}], "after_files": [{"content": "# encoding: utf-8\n\nfrom flask import Blueprint\n\nfrom ckan.plugins.toolkit import c, render\nimport ckanext.stats.stats as stats_lib\nimport ckan.lib.helpers as h\n\nstats = Blueprint(u'stats', __name__)\n\n\n@stats.route(u'/stats')\ndef index():\n    stats = stats_lib.Stats()\n    extra_vars = {\n        u'largest_groups': stats.largest_groups(),\n        u'top_tags': stats.top_tags(),\n        u'top_package_creators': stats.top_package_creators(),\n    }\n    return render(u'ckanext/stats/index.html', extra_vars)\n", "path": "ckanext/stats/blueprint.py"}, {"content": "# encoding: utf-8\n\nimport datetime\nimport logging\nfrom ckan.common import config\nfrom six import text_type\nfrom sqlalchemy import Table, select, join, func, and_\n\nimport ckan.plugins as p\nimport ckan.model as model\n\nlog = logging.getLogger(__name__)\ncache_enabled = p.toolkit.asbool(\n    config.get('ckanext.stats.cache_enabled', False)\n)\n\nif cache_enabled:\n    log.warn(\n        'ckanext.stats does not support caching in current implementations'\n    )\n\nDATE_FORMAT = '%Y-%m-%d'\n\n\ndef table(name):\n    return Table(name, model.meta.metadata, autoload=True)\n\n\ndef datetime2date(datetime_):\n    return datetime.date(datetime_.year, datetime_.month, datetime_.day)\n\n\nclass Stats(object):\n\n    @classmethod\n    def largest_groups(cls, limit=10):\n        member = table('member')\n        package = table('package')\n\n        j = join(member, package, member.c.table_id == package.c.id)\n\n        s = select(\n            [member.c.group_id,\n             func.count(member.c.table_id)]\n        ).select_from(j).group_by(member.c.group_id).where(\n            and_(\n                member.c.group_id != None, member.c.table_name == 'package',\n                package.c.private == False, package.c.state == 'active'\n            )\n        ).order_by(func.count(member.c.table_id).desc()).limit(limit)\n\n        res_ids = model.Session.execute(s).fetchall()\n        res_groups = [\n            (model.Session.query(model.Group).get(text_type(group_id)), val)\n            for group_id, val in res_ids\n        ]\n        return res_groups\n\n    @classmethod\n    def top_tags(cls, limit=10, returned_tag_info='object'):  # by package\n        assert returned_tag_info in ('name', 'id', 'object')\n        tag = table('tag')\n        package_tag = table('package_tag')\n        package = table('package')\n        if returned_tag_info == 'name':\n            from_obj = [package_tag.join(tag)]\n            tag_column = tag.c.name\n        else:\n            from_obj = None\n            tag_column = package_tag.c.tag_id\n        j = join(\n            package_tag, package, package_tag.c.package_id == package.c.id\n        )\n        s = select([tag_column,\n                    func.count(package_tag.c.package_id)],\n                   from_obj=from_obj).select_from(j).where(\n                       and_(\n                           package_tag.c.state == 'active',\n                           package.c.private == False,\n                           package.c.state == 'active'\n                       )\n                   )\n        s = s.group_by(tag_column).order_by(\n            func.count(package_tag.c.package_id).desc()\n        ).limit(limit)\n        res_col = model.Session.execute(s).fetchall()\n        if returned_tag_info in ('id', 'name'):\n            return res_col\n        elif returned_tag_info == 'object':\n            res_tags = [\n                (model.Session.query(model.Tag).get(text_type(tag_id)), val)\n                for tag_id, val in res_col\n            ]\n            return res_tags\n\n    @classmethod\n    def top_package_creators(cls, limit=10):\n        userid_count = model.Session.query(\n            model.Package.creator_user_id,\n            func.count(model.Package.creator_user_id)\n        ).filter(model.Package.state == 'active'\n                 ).filter(model.Package.private == False).group_by(\n                     model.Package.creator_user_id\n                 ).order_by(func.count(model.Package.creator_user_id).desc()\n                            ).limit(limit).all()\n        user_count = [\n            (model.Session.query(model.User).get(text_type(user_id)), count)\n            for user_id, count in userid_count\n            if user_id\n        ]\n        return user_count\n", "path": "ckanext/stats/stats.py"}]}