Where are CATS in the world?

We have a growing online community.

It would be cool to see the distribution of timezones though.

! pip install slackclient
import altair
import pandas
import logging
logger = logging.getLogger()

import os
import slack
import json
from collections import Counter
import pprint
class TimeZoneCounter:
    client = None
    def __init__(self):
        # connect to the slack API
        self.client = slack.WebClient(token=os.environ['SLACK_API_TOKEN'])
    def summary(self):
        # make a counter thingo in python
        cntr = Counter()
        # get a list of our users
        # TODO: we need to paginate through this
#       https://api.slack.com/methods/users.list
        res = self.client.users_list()
        data = res.data
        metadata = data.get("response_metadata")
        if metadata:
          cursor = metadata.get("next_cursor")
        members = [*data["members"]]
        while cursor:
          print(f"Fetching next page for cursor {cursor}")
          res = self.client.users_list(cursor=cursor)
          metadata = res.data.get("response_metadata")
          if metadata:
            cursor = metadata.get("next_cursor")
          members = [*members, *res.data["members"]]
        # pull out the timezones
        timezones = [mem.get('tz') for mem in members]
        # make running totals of timezones
        for tz in timezones:
            cntr[tz] += 1
        return cntr

OK, let's use this object to make our queries

tzc = TimeZoneCounter()
res = tzc.summary()

And now present it in a more readable fashion

We should be able to make a cool chart, but for now, we just have this list.

data = list(res.items())
df = pandas.DataFrame.from_records(data, columns=['timezone', 'count'])
df.sort_values('count', ascending=False)
altair.Chart(data=df, mark="bar").encode(x='timezone', y='count')
Runtimes (1)