def get_love(sender_username=None, recipient_username=None, limit=None): """Get all love from a particular sender or to a particular recipient. :param sender_username: If present, only return love sent from a particular user. :param recipient_username: If present, only return love sent to a particular user. :param limit: If present, only return this many items. """ sender_username = logic.alias.name_for_alias(sender_username) recipient_username = logic.alias.name_for_alias(recipient_username) if not (sender_username or recipient_username): raise TaintedLove( 'Not gonna give you all the love in the world. Sorry.') if sender_username == recipient_username: raise TaintedLove('Who sends love to themselves? Honestly?') love_query = ( Love.query().filter(Love.secret == False) # noqa .order(-Love.timestamp)) if sender_username: sender_key = Employee.get_key_for_username(sender_username) love_query = love_query.filter(Love.sender_key == sender_key) if recipient_username: recipient_key = Employee.get_key_for_username(recipient_username) love_query = love_query.filter(Love.recipient_key == recipient_key) if limit: return love_query.fetch_async(limit) else: return love_query.fetch_async()
def rebuild_love_count(): utc_dt = datetime.datetime.utcnow() - datetime.timedelta( days=7) # rebuild last week and this week week_start, _ = utc_week_limits(utc_dt) set_toggle_state(LOVE_SENDING_ENABLED, False) logging.info('Deleting LoveCount table... {}MB'.format( memory_usage().current())) ndb.delete_multi( LoveCount.query(LoveCount.week_start >= week_start).fetch( keys_only=True)) employee_dict = {employee.key: employee for employee in Employee.query()} logging.info('Rebuilding LoveCount table... {}MB'.format( memory_usage().current())) cursor = None count = 0 while True: loves, cursor, has_more = Love.query( Love.timestamp >= week_start).fetch_page(500, start_cursor=cursor) for l in loves: LoveCount.update(l, employee_dict=employee_dict) count += len(loves) logging.info('Processed {} loves, {}MB'.format( count, memory_usage().current())) if not has_more: break logging.info('Done. {}MB'.format(memory_usage().current())) set_toggle_state(LOVE_SENDING_ENABLED, True)
def _love_query(start_dt, end_dt, include_secret): query = Love.query().order(-Love.timestamp) if type(start_dt) is datetime: query = query.filter(Love.timestamp >= start_dt) if type(end_dt) is datetime: query = query.filter(Love.timestamp <= end_dt) if type(include_secret) is bool and include_secret is False: query = query.filter(Love.secret == False) # noqa return query
def rebuild_love_count(): set_toggle_state(LOVE_SENDING_ENABLED, False) logging.info('Rebuilding LoveCount table...') ndb.delete_multi(LoveCount.query().fetch(keys_only=True)) for l in Love.query().iter(batch_size=1000): LoveCount.update(l) logging.info('Done.') set_toggle_state(LOVE_SENDING_ENABLED, True)
def combine_employees(old_username, new_username): set_toggle_state(LOVE_SENDING_ENABLED, False) old_employee_key = Employee.query(Employee.username == old_username).get( keys_only=True) new_employee_key = Employee.query(Employee.username == new_username).get( keys_only=True) if not old_employee_key: raise NoSuchEmployee(old_username) elif not new_employee_key: raise NoSuchEmployee(new_username) # First, we need to update the actual instances of Love sent to/from the old employee logging.info('Reassigning {}\'s love to {}...'.format( old_username, new_username)) love_to_save = [] # Reassign all love sent FROM old_username for sent_love in Love.query(Love.sender_key == old_employee_key).iter(): sent_love.sender_key = new_employee_key love_to_save.append(sent_love) # Reassign all love sent TO old_username for received_love in Love.query( Love.recipient_key == old_employee_key).iter(): received_love.recipient_key = new_employee_key love_to_save.append(received_love) ndb.put_multi(love_to_save) logging.info('Done.') # Second, we need to update the LoveCount table logging.info('Updating LoveCount table...') love_counts_to_delete, love_counts_to_save = [], [] for old_love_count in LoveCount.query(ancestor=old_employee_key).iter(): # Try to find a corresponding row for the new employee new_love_count = LoveCount.query( ancestor=new_employee_key, filters=(LoveCount.week_start == old_love_count.week_start)).get() if new_love_count is None: # If there's no corresponding row for the new user, create one new_love_count = LoveCount( parent=new_employee_key, received_count=old_love_count.received_count, sent_count=old_love_count.sent_count, week_start=old_love_count.week_start) else: # Otherwise, combine the two rows new_love_count.received_count += old_love_count.received_count new_love_count.sent_count += old_love_count.sent_count # You `delete` keys but you `put` entities... Google's APIs are weird love_counts_to_delete.append(old_love_count.key) love_counts_to_save.append(new_love_count) ndb.delete_multi(love_counts_to_delete) ndb.put_multi(love_counts_to_save) logging.info('Done.') # Now we can delete the old employee logging.info('Deleting employee {}...'.format(old_username)) old_employee_key.delete() logging.info('Done.') # ... Which means we need to rebuild the index rebuild_index() set_toggle_state(LOVE_SENDING_ENABLED, True)