def start_date(self): if self.horizon == "monthly": return TimeSplitter.month(TimeSplitter.now()) elif self.horizon == "daily": return TimeSplitter.day(TimeSplitter.now()) else: return None
def archive(self): # debugging now_start = TimeSplitter.now() message_delta = 0 logger.debug('archive started at %s with %s messages total', now_start, len(self.messages)) # run at 12:31:20 # bp at 12:30:00 breakpoint = self._live_message_breakpoint() breakpoint = breakpoint.replace(second=0, microsecond=0) ''' map/reduce self.messages into history = { '{minute}': { '{channel} { 'cancer: cancer points, 'messages': messages count } } ''' history = collections.defaultdict(lambda: collections.defaultdict(lambda: {'cancer': 0, 'messages': 0})) # run until there's no old message left with self.messages_lock: while True: try: message = self.messages.popleft() except IndexError: logger.info('archive ate all the messages, meaning we got no message in the last minute') break # group messages by minute message['date'] = message['date'].replace(second=0, microsecond=0) # stop if the message is too new if message['date'] >= breakpoint: self.messages.appendleft(message) logger.debug('archiving loop stopped at message %s', message['date']) break # defaultdict builds everything as needed history[message['date']][message['channel']]['cancer'] += message['cancer'] history[message['date']][message['channel']]['messages'] += 1 # debugging message_delta += 1 # debugging now_end = TimeSplitter.now() logger.info('archived %s messages in %s ms, %s messages left', message_delta, (now_end - now_start).total_seconds() * 1000, len(self.messages)) return history
def update_leaderboard(self, summary): new = self._history_to_leaderboard(summary) # update the all-time leaderboard self._update_leaderboard('all', new) # update the monthly leaderboard new['date'] = TimeSplitter.month(summary['date']) self._update_leaderboard('monthly', new) # update the daily leaderboard new['date'] = TimeSplitter.day(summary['date']) self._update_leaderboard('daily', new)
def store(self, channel, cancer): message = { 'date': TimeSplitter.now(), 'channel': channel, 'cancer': int(cancer) } with self.messages_lock: self.messages.append(message)
def _live_message_breakpoint(self): # messages are old and ready to be archived after 1 minute return TimeSplitter.last_minute()
def archive(self): # debugging now_start = TimeSplitter.now() message_delta = 0 logger.debug('archive started at %s with %s messages total', now_start, len(self.messages)) # run at 12:31:20 # time_breakpoint at 12:30:00 time_breakpoint = self._live_message_breakpoint() time_breakpoint = time_breakpoint.replace(second=0, microsecond=0) ''' map/reduce self.messages into history = { '{minute}': { '{channel} { 'cancer: cancer points, 'messages': messages count } } ''' history = collections.defaultdict( lambda: collections.defaultdict(lambda: { 'cancer': 0, 'messages': 0 })) # run until there's no old message left with self.messages_lock: while True: try: message = self.messages.popleft() except IndexError: logger.info( 'archive ate all the messages, meaning we got no message in the last minute' ) break # group messages by minute message['date'] = message['date'].replace(second=0, microsecond=0) # stop if the message is too new if message['date'] >= time_breakpoint: self.messages.appendleft(message) logger.debug('archiving loop stopped at message %s', message['date']) break # defaultdict builds everything as needed history[message['date']][ message['channel']]['cancer'] += message['cancer'] history[message['date']][message['channel']]['messages'] += 1 # debugging message_delta += 1 # debugging now_end = TimeSplitter.now() logger.info('archived %s messages in %s ms, %s messages left', message_delta, (now_end - now_start).total_seconds() * 1000, len(self.messages)) return history
def _live_message_breakpoint(): # messages are old and ready to be archived after 1 minute return TimeSplitter.last_minute()
def test_default(self): result = TimeSplitter.month(datetime.datetime(2015, 2, 20, 12, 20, 30)) expected = datetime.datetime(2015, 2, 1, 0, 0, 0) self.assertEqual(result, expected)
def test_default(self): self.assertSecondsAgo(TimeSplitter.last_month(), 30*60*60*24)
def test_default(self): self.assertSecondsAgo(TimeSplitter.last_day(), 60*60*24)
def test_default(self): self.assertSecondsAgo(TimeSplitter.last_minute(), 60)
def test_default(self): self.assertSecondsAgo(TimeSplitter.now(), 0)