@command_history.command(name='cog')
@commands.is_owner()
async def command_history_cog(self, ctx, days: typing.Optional[int]=7, *, cog: str=None):
    'Command history for a cog or grouped by a cog.'
    interval = datetime.timedelta(days=days)
    if (cog is not None):
        cog = self.bot.get_cog(cog)
        if (cog is None):
            return (await ctx.send(f'Unknown cog: {cog}'))
        query = 'SELECT *, t.success + t.failed AS "total"\n                       FROM (\n                           SELECT command,\n                                  SUM(CASE WHEN failed THEN 0 ELSE 1 END) AS "success",\n                                  SUM(CASE WHEN failed THEN 1 ELSE 0 END) AS "failed"\n                           FROM commands\n                           WHERE command = any($1::text[])\n                           AND used > (CURRENT_TIMESTAMP - $2::interval)\n                           GROUP BY command\n                       ) AS t\n                       ORDER BY "total" DESC\n                       LIMIT 30;\n                    '
        return (await self.tabulate_query(ctx, query, [c.qualified_name for c in cog.walk_commands()], interval))
    query = 'SELECT *, t.success + t.failed AS "total"\n                   FROM (\n                       SELECT command,\n                              SUM(CASE WHEN failed THEN 0 ELSE 1 END) AS "success",\n                              SUM(CASE WHEN failed THEN 1 ELSE 0 END) AS "failed"\n                       FROM commands\n                       WHERE used > (CURRENT_TIMESTAMP - $1::interval)\n                       GROUP BY command\n                   ) AS t;\n                '

    class Count():
        __slots__ = ('success', 'failed', 'total')

        def __init__(self):
            self.success = 0
            self.failed = 0
            self.total = 0

        def add(self, record):
            self.success += record['success']
            self.failed += record['failed']
            self.total += record['total']
    data = defaultdict(Count)
    records = (await ctx.db.fetch(query, interval))
    for record in records:
        command = self.bot.get_command(record['command'])
        if ((command is None) or (command.cog is None)):
            data['No Cog'].add(record)
        else:
            data[command.cog.qualified_name].add(record)
    table = formats.TabularData()
    table.set_columns(['Cog', 'Success', 'Failed', 'Total'])
    data = sorted([(cog, e.success, e.failed, e.total) for (cog, e) in data.items()], key=(lambda t: t[(- 1)]), reverse=True)
    table.add_rows(data)
    render = table.render()
    (await ctx.safe_send(f'''```
{render}
```'''))
Command history for a cog or grouped by a cog.
@command_history.command(name='cog')
@commands.is_owner()
async def command_history_cog(self, ctx, days: typing.Optional[int]=7, *, cog: str=None):
    
    interval = datetime.timedelta(days=days)
    if (cog is not None):
        cog = self.bot.get_cog(cog)
        if (cog is None):
            return (await ctx.send(f'Unknown cog: {cog}'))
        query = 'SELECT *, t.success + t.failed AS "total"\n                       FROM (\n                           SELECT command,\n                                  SUM(CASE WHEN failed THEN 0 ELSE 1 END) AS "success",\n                                  SUM(CASE WHEN failed THEN 1 ELSE 0 END) AS "failed"\n                           FROM commands\n                           WHERE command = any($1::text[])\n                           AND used > (CURRENT_TIMESTAMP - $2::interval)\n                           GROUP BY command\n                       ) AS t\n                       ORDER BY "total" DESC\n                       LIMIT 30;\n                    '
        return (await self.tabulate_query(ctx, query, [c.qualified_name for c in cog.walk_commands()], interval))
    query = 'SELECT *, t.success + t.failed AS "total"\n                   FROM (\n                       SELECT command,\n                              SUM(CASE WHEN failed THEN 0 ELSE 1 END) AS "success",\n                              SUM(CASE WHEN failed THEN 1 ELSE 0 END) AS "failed"\n                       FROM commands\n                       WHERE used > (CURRENT_TIMESTAMP - $1::interval)\n                       GROUP BY command\n                   ) AS t;\n                '

    class Count():
        __slots__ = ('success', 'failed', 'total')

        def __init__(self):
            self.success = 0
            self.failed = 0
            self.total = 0

        def add(self, record):
            self.success += record['success']
            self.failed += record['failed']
            self.total += record['total']
    data = defaultdict(Count)
    records = (await ctx.db.fetch(query, interval))
    for record in records:
        command = self.bot.get_command(record['command'])
        if ((command is None) or (command.cog is None)):
            data['No Cog'].add(record)
        else:
            data[command.cog.qualified_name].add(record)
    table = formats.TabularData()
    table.set_columns(['Cog', 'Success', 'Failed', 'Total'])
    data = sorted([(cog, e.success, e.failed, e.total) for (cog, e) in data.items()], key=(lambda t: t[(- 1)]), reverse=True)
    table.add_rows(data)
    render = table.render()
    (await ctx.safe_send(f'```
{render}
```'))