class WriteOnlyStorage(StorageInterface): def __init__(self): super().__init__() self._store = PersistentStore() # subscribe to summaries from the publisher socket self.context = zmq.Context() self.socket = self.context.socket(zmq.SUB) self.socket.setsockopt(zmq.SUBSCRIBE, b'summary') read_socket = Config.get( 'monitor.socket.read.cancer_summary') or Config.get( 'monitor.socket.cancer_summary') self.socket.connect(read_socket) logger.info("connected summary socket to %s", read_socket) # start listening for summaries to persist # @socket.recv() # @db.write() def record(self): # update leaderboards with summaries we receive (one per channel per minute) while True: [topic, msg] = self.socket.recv_multipart() summary = pickle.loads(msg) self._store.update_leaderboard(summary)
def __init__(self): super().__init__() self._store = PersistentStore() # request cancer levels self.context = zmq.Context() self.poller = zmq.Poller() self._connect()
def __init__(self): super().__init__() self._store = PersistentStore() # subscribe to summaries from the publisher socket self.context = zmq.Context() self.socket = self.context.socket(zmq.SUB) self.socket.setsockopt(zmq.SUBSCRIBE, b'summary') read_socket = Config.get( 'monitor.socket.read.cancer_summary') or Config.get( 'monitor.socket.cancer_summary') self.socket.connect(read_socket) logger.info("connected summary socket to %s", read_socket)
def __init__(self): super().__init__() self._store = PersistentStore() # subscribe to summaries from the publisher socket self.context = zmq.Context() self.socket = self.context.socket(zmq.SUB) self.socket.setsockopt(zmq.SUBSCRIBE, b"summary") self.socket.connect(Config.get("monitor.socket.cancer_summary")) logger.info("connected summary socket to %s", Config.get("monitor.socket.cancer_summary"))
class WriteOnlyStorage(StorageInterface): def __init__(self): super().__init__() self._store = PersistentStore() # subscribe to summaries from the publisher socket self.context = zmq.Context() self.socket = self.context.socket(zmq.SUB) self.socket.setsockopt(zmq.SUBSCRIBE, b"summary") self.socket.connect(Config.get("monitor.socket.cancer_summary")) logger.info("connected summary socket to %s", Config.get("monitor.socket.cancer_summary")) # start listening for summaries to persist # @socket.recv() # @db.write() def record(self): # update leaderboards with summaries we receive (one per channel per minute) while True: [topic, msg] = self.socket.recv_multipart() summary = pickle.loads(msg) self._store.update_leaderboard(summary)
""" Add Twitch channel id to leaderboards """ import sys from twitchcancer.storage.persistentstore import PersistentStore from twitchcancer.utils.twitchapi import TwitchApi if __name__ == '__main__': store = PersistentStore() leaderboards = store.db.leaderboard.find({'channel': { '$regex': '#.*' }}, limit=100) names = [ leaderboard['channel'].replace('#', '') for leaderboard in leaderboards ] if not names: print('Nothing to migrate') sys.exit() users = TwitchApi.request('https://api.twitch.tv/kraken/users?login='******','.join(names))["users"] for user in users: channel_name = user['name'] channel_id = user['_id'] query = {'channel': '#' + channel_name} update = {'$set': {'channel': channel_name, 'channel_id': channel_id}} print('Migrating {} to {}'.format(channel_name, channel_id)) store.db.daily_leaderboard.update_one(query, update)
def setUp(self, init): self.store = PersistentStore() self.store.db = MagicMock()
class ReadOnlyStorage(StorageInterface): def __init__(self): super().__init__() self._store = PersistentStore() # request cancer levels self.context = zmq.Context() self.poller = zmq.Poller() self._connect() # request cancer level from a live message store # @socket.read() def cancer(self): self.socket.send(b'') if self.poller.poll(2 * 1000): # 2s timeout in milliseconds return self.socket.recv_pyobj() else: logger.warning("no reply to a live cancer request, will reconnect") self._disconnect() self._connect() return [] # read leaderboards from the database # @db.read() def leaderboards(self, horizon): return self._store.leaderboards(horizon) # returns a full leaderboard # @db.read() def leaderboard(self, leaderboard): return self._store.leaderboard(leaderboard) # read channel data from the database # @db.read() def channel(self, channel): return self._store.channel(channel) # returns an overview of the current status of both the db and the monitoring process # @db.read() # @socket.read() def status(self): # get totals from the database status = { 'total': self._store.status(), 'live': { 'channels': 0, 'messages': 0, 'cancer': 0, } } # get live cancer levels live = self.cancer() # add up current cancer levels to have stats for channel in live: status['live']['messages'] += channel['messages'] status['live']['cancer'] += channel['cancer'] status['live']['channels'] = len(live) return status # search for a channel by its name # @db.read() def search(self, channel): return self._store.search(channel) # create a socket and connect to the cancer server # @socket.connect() def _connect(self): self.socket = self.context.socket(zmq.REQ) read_socket = Config.get('monitor.socket.read.cancer_request') or Config.get('monitor.socket.cancer_request') self.socket.connect(read_socket) self.poller.register(self.socket, zmq.POLLIN) logger.info("connected cancer request socket to %s", read_socket) # disconnect from the cancer server # @socket.close() def _disconnect(self): self.socket.setsockopt(zmq.LINGER, 0) self.socket.close() self.poller.unregister(self.socket)