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 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 top_lovers_and_lovees(utc_week_start, dept=None, limit=20): """Synchronously return a list of (employee key, sent love count) and a list of (employee key, received love count), each sorted in descending order of love sent or received. """ sent_query = LoveCount.query(LoveCount.week_start == utc_week_start) if dept: sent_query = sent_query.filter( ndb.OR(LoveCount.meta_department == dept, LoveCount.department == dept)) sent = sent_query.order(-LoveCount.sent_count).fetch() lovers = [] for c in sent: if len(lovers) == limit: break if c.sent_count == 0: continue employee_key = c.key.parent() lovers.append((employee_key, c.sent_count)) received = sorted(sent, key=lambda c: c.received_count, reverse=True) lovees = [] for c in received: if len(lovees) == limit: break if c.received_count == 0: continue employee_key = c.key.parent() lovees.append((employee_key, c.received_count)) return (lovers, lovees)
def _send_love(recipient_key, message, sender_key, secret): """Send love and do associated bookkeeping.""" new_love = Love( sender_key=sender_key, recipient_key=recipient_key, message=message, secret=(secret is True), ) new_love.put() LoveCount.update(new_love) # Send email asynchronously taskqueue.add(url='/tasks/love/email', params={'id': new_love.key.id()}) if not secret: logic.event.add_event( logic.event.LOVESENT, {'love_id': new_love.key.id()}, )
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)