def update(cls, love, employee_dict=None): utc_week_start, _ = utc_week_limits(love.timestamp) sender_count = cls.query(ancestor=love.sender_key).filter( cls.week_start == utc_week_start).get() if sender_count is not None: sender_count.sent_count += 1 else: employee = employee_dict[ love.sender_key] if employee_dict else love.sender_key.get() sender_count = cls( parent=love.sender_key, sent_count=1, week_start=utc_week_start, department=employee.department, meta_department=employee.meta_department, ) sender_count.put() recipient_count = cls.query(ancestor=love.recipient_key).filter( cls.week_start == utc_week_start).get() if recipient_count is not None: recipient_count.received_count += 1 else: employee = employee_dict[ love. recipient_key] if employee_dict else love.recipient_key.get() recipient_count = cls( parent=love.recipient_key, received_count=1, week_start=utc_week_start, department=employee.department, meta_department=employee.meta_department, ) recipient_count.put()
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 update(cls, love): utc_week_start, _ = utc_week_limits(love.timestamp) sender_count = cls.query( ancestor=love.sender_key, filters=(cls.week_start == utc_week_start) ).get() if sender_count is not None: sender_count.sent_count += 1 else: sender_count = cls( parent=love.sender_key, sent_count=1, week_start=utc_week_start ) sender_count.put() recipient_count = cls.query( ancestor=love.recipient_key, filters=(cls.week_start == utc_week_start) ).get() if recipient_count is not None: recipient_count.received_count += 1 else: recipient_count = cls( parent=love.recipient_key, received_count=1, week_start=utc_week_start ) recipient_count.put()
def get_leaderboard_data(timespan, department): # If last week, we need to subtract *before* getting the week limits to # avoid being off by one hour on weeks that include a DST transition utc_now = datetime.utcnow() if timespan == TIMESPAN_LAST_WEEK: utc_now -= timedelta(days=7) utc_week_start, _ = utc_week_limits(utc_now) top_lovers, top_lovees = logic.love_count.top_lovers_and_lovees( utc_week_start, dept=department) top_lover_dicts = [{ 'employee': employee_key.get_async(), 'num_sent': sent_count } for employee_key, sent_count in top_lovers] top_loved_dicts = [{ 'employee': employee_key.get_async(), 'num_received': received_count } for employee_key, received_count in top_lovees] # get results for the futures set up previously map(to_the_future, top_lover_dicts) map(to_the_future, top_loved_dicts) return (top_lover_dicts, top_loved_dicts)
def leaderboard(): timespan = request.args.get('timespan', TIMESPAN_THIS_WEEK) department = request.args.get('department', None) # If last week, we need to subtract *before* getting the week limits to # avoid being off by one hour on weeks that include a DST transition utc_now = datetime.utcnow() if timespan == TIMESPAN_LAST_WEEK: utc_now -= timedelta(days=7) utc_week_start, _ = utc_week_limits(utc_now) top_lovers, top_lovees = logic.love_count.top_lovers_and_lovees(utc_week_start, dept=department) top_lover_dicts = [ { 'employee': employee_key.get_async(), 'num_sent': sent_count } for employee_key, sent_count in top_lovers ] top_loved_dicts = [ { 'employee': employee_key.get_async(), 'num_received': received_count } for employee_key, received_count in top_lovees ] # get results for the futures set up previously map(to_the_future, top_lover_dicts) map(to_the_future, top_loved_dicts) return render_template( 'leaderboard.html', top_loved=top_loved_dicts, top_lovers=top_lover_dicts, departments=logic.department.META_DEPARTMENTS, sub_departments=logic.department.META_DEPARTMENT_MAP, selected_dept=department, selected_timespan=timespan, org_title=config.ORG_TITLE, )
def remove(cls, love): utc_week_start, _ = utc_week_limits(love.timestamp) sender_count = cls.query(ancestor=love.sender_key).filter( cls.week_start == utc_week_start).get() if sender_count is not None and sender_count.sent_count > 0: sender_count.sent_count -= 1 if sender_count.sent_count == 0 and sender_count.received_count == 0: sender_count.key.delete() else: sender_count.put() recipient_count = cls.query( ancestor=love.recipient_key, filters=(cls.week_start == utc_week_start)).get() if recipient_count is not None and recipient_count.received_count > 0: recipient_count.received_count -= 1 if recipient_count.sent_count == 0 and recipient_count.received_count == 0: recipient_count.key.delete() else: recipient_count.put()