def get_realm_day_counts() -> Dict[(str, Dict[(str, str)])]:
    query = SQL("\n        select\n            r.string_id,\n            (now()::date - date_sent::date) age,\n            count(*) cnt\n        from zerver_message m\n        join zerver_userprofile up on up.id = m.sender_id\n        join zerver_realm r on r.id = up.realm_id\n        join zerver_client c on c.id = m.sending_client_id\n        where\n            (not up.is_bot)\n        and\n            date_sent > now()::date - interval '8 day'\n        and\n            c.name not in ('zephyr_mirror', 'ZulipMonitoring')\n        group by\n            r.string_id,\n            age\n        order by\n            r.string_id,\n            age\n    ")
    cursor = connection.cursor()
    cursor.execute(query)
    rows = dictfetchall(cursor)
    cursor.close()
    counts: Dict[(str, Dict[(int, int)])] = defaultdict(dict)
    for row in rows:
        counts[row['string_id']][row['age']] = row['cnt']
    result = {}
    for string_id in counts:
        raw_cnts = [counts[string_id].get(age, 0) for age in range(8)]
        min_cnt = min(raw_cnts[1:])
        max_cnt = max(raw_cnts[1:])

        def format_count(cnt: int, style: Optional[str]=None) -> str:
            if (style is not None):
                good_bad = style
            elif (cnt == min_cnt):
                good_bad = 'bad'
            elif (cnt == max_cnt):
                good_bad = 'good'
            else:
                good_bad = 'neutral'
            return f'<td class="number {good_bad}">{cnt}</td>'
        cnts = (format_count(raw_cnts[0], 'neutral') + ''.join(map(format_count, raw_cnts[1:])))
        result[string_id] = dict(cnts=cnts)
    return result
def get_realm_day_counts() -> Dict[(str, Dict[(str, str)])]:
    query = SQL("\n        select\n            r.string_id,\n            (now()::date - date_sent::date) age,\n            count(*) cnt\n        from zerver_message m\n        join zerver_userprofile up on up.id = m.sender_id\n        join zerver_realm r on r.id = up.realm_id\n        join zerver_client c on c.id = m.sending_client_id\n        where\n            (not up.is_bot)\n        and\n            date_sent > now()::date - interval '8 day'\n        and\n            c.name not in ('zephyr_mirror', 'ZulipMonitoring')\n        group by\n            r.string_id,\n            age\n        order by\n            r.string_id,\n            age\n    ")
    cursor = connection.cursor()
    cursor.execute(query)
    rows = dictfetchall(cursor)
    cursor.close()
    counts: Dict[(str, Dict[(int, int)])] = defaultdict(dict)
    for row in rows:
        counts[row['string_id']][row['age']] = row['cnt']
    result = {}
    for string_id in counts:
        raw_cnts = [counts[string_id].get(age, 0) for age in range(8)]
        min_cnt = min(raw_cnts[1:])
        max_cnt = max(raw_cnts[1:])

        def format_count(cnt: int, style: Optional[str]=None) -> str:
            if (style is not None):
                good_bad = style
            elif (cnt == min_cnt):
                good_bad = 'bad'
            elif (cnt == max_cnt):
                good_bad = 'good'
            else:
                good_bad = 'neutral'
            return f'<td class="number {good_bad}">{cnt}</td>'
        cnts = (format_count(raw_cnts[0], 'neutral') + .join(map(format_count, raw_cnts[1:])))
        result[string_id] = dict(cnts=cnts)
    return result