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 store(self, channel, cancer): message = { 'date': TimeSplitter.now(), 'channel': channel, 'cancer': int(cancer) } with self.messages_lock: self.messages.append(message)
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 test_default(self): self.assertSecondsAgo(TimeSplitter.now(), 0)