def test_get_previous_multi_meetings(minimal_database): pref_1 = SubscriptionDateTime(datetime=datetime.now() - timedelta(weeks=MEETING_COOLDOWN_WEEKS - 1)).put() subscription = MeetingSubscription(title='all engineering weekly', datetime=[pref_1]).put() user_pref = UserSubscriptionPreferences(preference=pref_1, subscription=subscription).put() user1 = User(email='*****@*****.**', metadata={'department': 'dept'}, subscription_preferences=[user_pref]).put() user2 = User(email='*****@*****.**', metadata={'department': 'dept2'}, subscription_preferences=[user_pref]).put() meeting_spec = MeetingSpec(meeting_subscription=subscription, datetime=pref_1.get().datetime).put() meeting1 = Meeting(meeting_spec=meeting_spec, cancelled=False).put() meeting2 = Meeting(meeting_spec=meeting_spec, cancelled=False).put() MeetingParticipant(meeting=meeting1, user=user2).put() MeetingParticipant(meeting=meeting1, user=user1).put() MeetingParticipant(meeting=meeting2, user=user2).put() MeetingParticipant(meeting=meeting2, user=user1).put() assert get_previous_meetings(subscription) == set([(user1.id(), user2.id()), (user1.id(), user2.id())])
def test_get_previous_meetings(session): pref_1 = SubscriptionDateTime(datetime=datetime.now() - timedelta(weeks=MEETING_COOLDOWN_WEEKS - 1)) subscription = MeetingSubscription(title='all engineering weekly', datetime=[pref_1]) user_pref = UserSubscriptionPreferences(preference=pref_1, subscription=subscription) user1 = User(email='*****@*****.**', meta_data={'department': 'dept'}, subscription_preferences=[user_pref]) user2 = User(email='*****@*****.**', meta_data={'department': 'dept2'}, subscription_preferences=[user_pref]) meeting_spec = MeetingSpec(meeting_subscription=subscription, datetime=pref_1.datetime) meeting = Meeting(meeting_spec=meeting_spec, cancelled=False) mp1 = MeetingParticipant(meeting=meeting, user=user2) mp2 = MeetingParticipant(meeting=meeting, user=user1) session.add(pref_1) session.add(subscription) session.add(user_pref) session.add(user1) session.add(user2) session.add(meeting_spec) session.add(meeting) session.add(mp1) session.add(mp2) session.commit() assert get_previous_meetings(subscription) == set([(user1.id, user2.id)])
def test_get_user_weights(minimal_database): pref_1 = SubscriptionDateTime( datetime=datetime.now() - timedelta(weeks=MEETING_COOLDOWN_WEEKS - 1)).put() subscription = MeetingSubscription(title='all engineering weekly', datetime=[pref_1]).put() user_pref = UserSubscriptionPreferences(preference=pref_1, subscription=subscription).put() user1 = User(email='*****@*****.**', metadata={ 'department': 'dept' }, subscription_preferences=[user_pref]).put() user2 = User(email='*****@*****.**', metadata={ 'department': 'dept2' }, subscription_preferences=[user_pref]).put() meeting_spec = MeetingSpec(meeting_subscription=subscription, datetime=pref_1.get().datetime).put() meeting = Meeting(meeting_spec=meeting_spec, cancelled=False).put() MeetingParticipant(meeting=meeting, user=user2).put() MeetingParticipant(meeting=meeting, user=user1).put() previous_meetings_count = get_previous_meetings_counts( [user1.get(), user2.get()], subscription) assert (get_user_weights([user1.get(), user2.get()], previous_meetings_count, 10, 5) == [[0, 5], [5, 0]])
def test_get_meeting_participants(app, database): pref = UserSubscriptionPreferences(subscription=database.sub.key, preference=database.prefs[0].key) pref.put() user1 = User(email='*****@*****.**', metadata={'department': 'dept'}, subscription_preferences=[pref.key]) user1.put() user2 = User(email='*****@*****.**', metadata={'department': 'dept'}, subscription_preferences=[pref.key]) user2.put() meeting1 = Meeting(meeting_spec=database.specs[0].key, cancelled=False).put() MeetingParticipant(meeting=meeting1, user=user1.key).put() MeetingParticipant(meeting=meeting1, user=user2.key).put() with app.test_request_context('/v1/metrics/meeting_participants'): participants = json.loads(meeting_participants()) assert participants == [{ 'date': '2017-10-27T23:00:00', 'meeting': 'agx0ZXN0YmVkLXRlc3RyDQsSB01lZXRpbmcYCgw', 'meeting_title': 'Yelp Weekly', 'participant': '*****@*****.**', 'time': '04:00PM' }, { 'date': '2017-10-27T23:00:00', 'meeting': 'agx0ZXN0YmVkLXRlc3RyDQsSB01lZXRpbmcYCgw', 'meeting_title': 'Yelp Weekly', 'participant': '*****@*****.**', 'time': '04:00PM' }]
def test_generate_group_meetings(): pref_1 = SubscriptionDateTime( datetime=datetime.now() - timedelta(weeks=MEETING_COOLDOWN_WEEKS - 1)).put() subscription = MeetingSubscription(title='all engineering weekly', datetime=[pref_1]).put() user_pref = UserSubscriptionPreferences(preference=pref_1, subscription=subscription).put() user1 = User(email='*****@*****.**', metadata={ 'department': 'dept' }, subscription_preferences=[user_pref]).put() user2 = User(email='*****@*****.**', metadata={ 'department': 'dept2' }, subscription_preferences=[user_pref]).put() user3 = User(email='*****@*****.**', metadata={ 'department': 'dept3' }, subscription_preferences=[user_pref]).put() meeting_spec = MeetingSpec(meeting_subscription=subscription, datetime=pref_1.get().datetime).put() meeting = Meeting(meeting_spec=meeting_spec, cancelled=False).put() MeetingParticipant(meeting=meeting, user=user3).put() MeetingParticipant(meeting=meeting, user=user2).put() MeetingParticipant(meeting=meeting, user=user1).put() matched, unmatched = generate_group_meetings( [user1.get(), user2.get(), user3.get()], meeting_spec.get(), 3, 10, 5) assert {user.key for user in matched[0]} == {user1, user2, user3} assert unmatched == []
def test_generate_group_meetings(session): pref_1 = SubscriptionDateTime(datetime=datetime.now() - timedelta(weeks=MEETING_COOLDOWN_WEEKS - 1)) subscription = MeetingSubscription(title='all engineering weekly', datetime=[pref_1]) user_pref = UserSubscriptionPreferences(preference=pref_1, subscription=subscription) user1 = User(email='*****@*****.**', meta_data={'department': 'dept'}, subscription_preferences=[user_pref]) user2 = User(email='*****@*****.**', meta_data={'department': 'dept2'}, subscription_preferences=[user_pref]) user3 = User(email='*****@*****.**', meta_data={'department': 'dept3'}, subscription_preferences=[user_pref]) meeting_spec = MeetingSpec(meeting_subscription=subscription, datetime=pref_1.datetime) meeting = Meeting(meeting_spec=meeting_spec, cancelled=False) mp1 = MeetingParticipant(meeting=meeting, user=user3) mp2 = MeetingParticipant(meeting=meeting, user=user2) mp3 = MeetingParticipant(meeting=meeting, user=user1) session.add(pref_1) session.add(subscription) session.add(user_pref) session.add(user1) session.add(user2) session.add(user3) session.add(meeting_spec) session.add(meeting) session.add(mp1) session.add(mp2) session.add(mp3) session.commit() matched, unmatched = generate_group_meetings([user1, user2, user3], meeting_spec, 3, 10, 5) assert {user for user in matched[0]} == {user1, user2, user3} assert unmatched == []
def get_previous_meetings(cooldown=MEETING_COOLDOWN_WEEKS): meetings = defaultdict(list) # get all meeting specs from x weeks ago til now time_threshold_for_meetings = datetime.now() - timedelta(weeks=cooldown) meeting_spec_keys = [ spec.key for spec in MeetingSpec.query( MeetingSpec.datetime > time_threshold_for_meetings).fetch() ] logging.info('Previous Meeting History: ') logging.info([ meeting.get().datetime.strftime("%Y-%m-%d %H:%M") for meeting in meeting_spec_keys ]) if meeting_spec_keys == []: return set([]) # get all meetings from meeting specs meeting_keys = [ meeting.key for meeting in Meeting.query().filter( Meeting.meeting_spec.IN(meeting_spec_keys)).fetch() ] if meeting_keys == []: return set([]) # get all participants from meetings participants = MeetingParticipant.query().filter( MeetingParticipant.meeting.IN(meeting_keys)).fetch() if participants == []: return set([]) # group by meeting Id for participant in participants: meetings[participant.meeting.id()].append(participant.user) # ids are sorted, all matches should be in increasing order by id for the matching algorithm to work disallowed_meetings = set([ tuple(sorted(meeting, key=lambda Key: Key.id())) for meeting in meetings.values() ]) logging.info('Past Meetings') logging.info([ tuple([meeting.get().get_username() for meeting in meeting]) for meeting in disallowed_meetings ]) disallowed_meetings = { tuple([meeting.id() for meeting in meeting]) for meeting in disallowed_meetings } return disallowed_meetings
def save_meetings(matches, spec): for match in matches: meeting_key = Meeting(meeting_spec=spec.key).put() MeetingParticipant(meeting=meeting_key, user=match[0].key).put() MeetingParticipant(meeting=meeting_key, user=match[1].key).put() logging.info(meeting_key) logging.info('{}, {}'.format( match[0].get_username(), match[1].get_username(), ))
def save_meetings(matches, spec): for match in matches: # Last element in match is the key for meeting time matched_users = match[:-1] meeting_key = Meeting(meeting_spec=spec.key).put() for user in matched_users: MeetingParticipant(meeting=meeting_key, user=user.key).put() username_list = [user.get_username() for user in matched_users] logging.info(meeting_key) logging.info(', '.join(username_list))
def test_previous_meeting_penalty(minimal_database): pref_1 = SubscriptionDateTime(datetime=datetime.now() - timedelta(weeks=MEETING_COOLDOWN_WEEKS - 1)).put() pref_2 = SubscriptionDateTime(datetime=datetime.now() - timedelta(weeks=MEETING_COOLDOWN_WEEKS - 2)).put() pref_3 = SubscriptionDateTime(datetime=datetime.now() - timedelta(weeks=MEETING_COOLDOWN_WEEKS - 3)).put() subscription = MeetingSubscription(title='all engineering weekly', datetime=[pref_1, pref_2, pref_3]).put() user_pref1 = UserSubscriptionPreferences(preference=pref_1, subscription=subscription).put() user_pref2 = UserSubscriptionPreferences(preference=pref_2, subscription=subscription).put() user_pref3 = UserSubscriptionPreferences(preference=pref_3, subscription=subscription).put() meeting_spec1 = MeetingSpec(meeting_subscription=subscription, datetime=pref_1.get().datetime) meeting_spec1.put() meeting_spec2 = MeetingSpec(meeting_subscription=subscription, datetime=pref_2.get().datetime) meeting_spec2.put() meeting_spec3 = MeetingSpec(meeting_subscription=subscription, datetime=pref_3.get().datetime) meeting_spec3.put() users = [] num_users = 20 for i in range(0, num_users): user = User(email='{}@yelp.com'.format(i), metadata={ 'department': 'dept{}'.format(i)}, subscription_preferences=[user_pref1, user_pref2, user_pref3]) user.put() MeetingRequest(user=user.key, meeting_spec=meeting_spec1.key).put() MeetingRequest(user=user.key, meeting_spec=meeting_spec2.key).put() MeetingRequest(user=user.key, meeting_spec=meeting_spec3.key).put() users.append(user) meeting1 = Meeting(meeting_spec=meeting_spec1.key, cancelled=False).put() MeetingParticipant(meeting=meeting1, user=users[1].key).put() MeetingParticipant(meeting=meeting1, user=users[0].key).put() meeting2 = Meeting(meeting_spec=meeting_spec2.key, cancelled=False).put() MeetingParticipant(meeting=meeting2, user=users[1].key).put() MeetingParticipant(meeting=meeting2, user=users[0].key).put() meeting3 = Meeting(meeting_spec=meeting_spec3.key, cancelled=False).put() MeetingParticipant(meeting=meeting3, user=users[1].key).put() MeetingParticipant(meeting=meeting3, user=users[0].key).put() for run in range(10): matches, unmatched = generate_meetings(users, meeting_spec1, prev_meeting_tuples=None, group_size=3) assert(len(matches) == 6) assert (len(unmatched) == 2) for matched_group in matches: assert(not (users[0] in matched_group and users[1] in matched_group))
def send_match_emails(): specs = get_specs_for_current_week() for spec in specs: matches = [] meetings = Meeting.query(Meeting.meeting_spec == spec.key).fetch() for meeting in meetings: participants = MeetingParticipant.query(MeetingParticipant.meeting == meeting.key).fetch() matches.append((participants[0].user.get(), participants[1].user.get())) logging.info(spec) logging.info(matches) send_batch_meeting_confirmation_email(matches, spec) return "OK"
def save_meetings(matches, spec): for match in matches: # Last element in match may be the key for meeting time matched_users = [user for user in match if isinstance(user, User)] meeting_key = Meeting(meeting_spec=spec) db.session.add(meeting_key) for user in matched_users: mp = MeetingParticipant(meeting=meeting_key, user=user) db.session.add(mp) db.session.commit() username_list = [user.get_username() for user in matched_users] logging.info(meeting_key) logging.info(', '.join(username_list))
def test_get_meeting_participants(app, database): pref = UserSubscriptionPreferences(subscription=database.sub.key, preference=database.prefs[0].key) pref.put() user1 = User(email='*****@*****.**', metadata={'department': 'dept'}, subscription_preferences=[pref.key]) user1.put() user2 = User(email='*****@*****.**', metadata={'department': 'dept'}, subscription_preferences=[pref.key]) user2.put() meeting1 = Meeting(meeting_spec=database.specs[0].key, cancelled=False).put() MeetingParticipant(meeting=meeting1, user=user1.key).put() MeetingParticipant(meeting=meeting1, user=user2.key).put() with app.test_request_context('/v1/metrics/meeting_participants'): participants = meeting_participants().json assert len(participants) == 2 assert set(participants[0].keys()) == set( ['date', 'meeting', 'meeting_title', 'participant', 'time']) assert participants[0]['date'] == database.specs[0].datetime.isoformat( ) participant_lookup = { participant['participant']: participant for participant in participants } assert participant_lookup[user1.email] == { 'date': database.specs[0].datetime.isoformat(), 'meeting': meeting1.urlsafe(), 'meeting_title': database.specs[0].meeting_subscription.get().title, 'participant': user1.email, 'time': '04:00PM', }
def metrics_api(): keys_only = ndb.QueryOptions(keys_only=True) meeting_subscriptions = MeetingSubscription.query().fetch() metrics = [] for subscription in meeting_subscriptions: data = { 'title': subscription.title, 'subscribed': UserSubscriptionPreferences.query( UserSubscriptionPreferences.subscription == subscription.key).count(options=keys_only), 'meetings': Meeting.query().count(options=keys_only), } metrics.append(data) return json.dumps(metrics)
def test_get_meeting_participants(app, database, session): pref = UserSubscriptionPreferences(subscription=database.sub, preference=database.prefs[0]) user1 = User(email='*****@*****.**', meta_data={'department': 'dept'}, subscription_preferences=[pref]) user2 = User(email='*****@*****.**', meta_data={'department': 'dept'}, subscription_preferences=[pref]) meeting1 = Meeting(meeting_spec=database.specs[0], cancelled=False) mp1 = MeetingParticipant(meeting=meeting1, user=user1) mp2 = MeetingParticipant(meeting=meeting1, user=user2) session.add(pref) session.add(user1) session.add(user2) session.add(meeting1) session.add(mp1) session.add(mp2) session.commit() with app.test_request_context('/v1/metrics/meeting_participants'): participants = meeting_participants().json assert len(participants) == 2 assert set(participants[0].keys()) == set( ['date', 'meeting', 'meeting_title', 'participant', 'time']) assert participants[0]['date'] == database.specs[0].datetime.isoformat( ) participant_lookup = { participant['participant']: participant for participant in participants } assert participant_lookup[user1.email] == { 'date': database.specs[0].datetime.isoformat(), 'meeting': meeting1.id, 'meeting_title': database.specs[0].meeting_subscription.title, 'participant': user1.email, 'time': '03:00PM', }
def test_get_meeting_participants(app, database): pref = UserSubscriptionPreferences(subscription=database.sub.key, preference=database.prefs[0].key) pref.put() user1 = User( email='*****@*****.**', metadata={'department': 'dept'}, subscription_preferences=[pref.key] ) user1.put() user2 = User( email='*****@*****.**', metadata={'department': 'dept'}, subscription_preferences=[pref.key] ) user2.put() meeting1 = Meeting(meeting_spec=database.specs[0].key, cancelled=False).put() MeetingParticipant(meeting=meeting1, user=user1.key).put() MeetingParticipant(meeting=meeting1, user=user2.key).put() with app.test_request_context('/v1/metrics/meeting_participants'): participants = json.loads(meeting_participants()) assert len(participants) == 2 assert set(participants[0].keys()) == set(['date', 'meeting', 'meeting_title', 'participant', 'time']) assert participants[0]['date'] == database.specs[0].datetime.isoformat() assert participants[0]['time'] == database.specs[0].datetime.strftime('%I:%M%p')
def test_previous_meeting_penalty(session): pref_1 = SubscriptionDateTime(datetime=datetime.now() - timedelta(weeks=MEETING_COOLDOWN_WEEKS - 1)) pref_2 = SubscriptionDateTime(datetime=datetime.now() - timedelta(weeks=MEETING_COOLDOWN_WEEKS - 2)) pref_3 = SubscriptionDateTime(datetime=datetime.now() - timedelta(weeks=MEETING_COOLDOWN_WEEKS - 3)) subscription = MeetingSubscription(title='all engineering weekly', datetime=[pref_1, pref_2, pref_3]) user_pref1 = UserSubscriptionPreferences(preference=pref_1, subscription=subscription) user_pref2 = UserSubscriptionPreferences(preference=pref_2, subscription=subscription) user_pref3 = UserSubscriptionPreferences(preference=pref_3, subscription=subscription) meeting_spec1 = MeetingSpec(meeting_subscription=subscription, datetime=pref_1.datetime) meeting_spec2 = MeetingSpec(meeting_subscription=subscription, datetime=pref_2.datetime) meeting_spec3 = MeetingSpec(meeting_subscription=subscription, datetime=pref_3.datetime) session.add(pref_1) session.add(pref_2) session.add(pref_3) session.add(subscription) session.add(user_pref1) session.add(user_pref2) session.add(user_pref3) session.add(meeting_spec1) session.add(meeting_spec2) session.add(meeting_spec3) users = [] num_users = 20 for i in range(0, num_users): user = User(email='{}@yelp.com'.format(i), meta_data={ 'department': 'dept{}'.format(i)}, subscription_preferences=[user_pref1, user_pref2, user_pref3]) session.add(user) mr1 = MeetingRequest(user=user, meeting_spec=meeting_spec1) mr2 = MeetingRequest(user=user, meeting_spec=meeting_spec2) mr3 = MeetingRequest(user=user, meeting_spec=meeting_spec3) session.add(mr1) session.add(mr2) session.add(mr3) users.append(user) meeting1 = Meeting(meeting_spec=meeting_spec1, cancelled=False) mp1 = MeetingParticipant(meeting=meeting1, user=users[1]) mp2 = MeetingParticipant(meeting=meeting1, user=users[0]) session.add(meeting1) session.add(mp1) session.add(mp2) meeting2 = Meeting(meeting_spec=meeting_spec2, cancelled=False) mp1 = MeetingParticipant(meeting=meeting2, user=users[1]) mp2 = MeetingParticipant(meeting=meeting2, user=users[0]) session.add(meeting2) session.add(mp1) session.add(mp2) meeting3 = Meeting(meeting_spec=meeting_spec3, cancelled=False) mp1 = MeetingParticipant(meeting=meeting3, user=users[1]) mp2 = MeetingParticipant(meeting=meeting3, user=users[0]) session.add(meeting3) session.add(mp1) session.add(mp2) session.commit() for run in range(10): matches, unmatched = generate_meetings(users, meeting_spec1, prev_meeting_tuples=None, group_size=3) assert(len(matches) == 6) assert (len(unmatched) == 2) for matched_group in matches: assert(not (users[0] in matched_group and users[1] in matched_group))