def test_get_previous_meetings_multi_subscription(minimal_database): pref_1 = SubscriptionDateTime(datetime=datetime.now() - timedelta(weeks=MEETING_COOLDOWN_WEEKS - 1)).put() subscription1 = MeetingSubscription(title='all engineering weekly', datetime=[pref_1]).put() subscription2 = MeetingSubscription(title='all sales weekly', datetime=[pref_1]).put() user_pref1 = UserSubscriptionPreferences(preference=pref_1, subscription=subscription1).put() user_pref2 = UserSubscriptionPreferences(preference=pref_1, subscription=subscription2).put() user1 = User(email='*****@*****.**', metadata={'department': 'dept'}, subscription_preferences=[user_pref1, user_pref2]).put() user2 = User(email='*****@*****.**', metadata={'department': 'dept2'}, subscription_preferences=[user_pref1, user_pref2]).put() meeting_spec1 = MeetingSpec(meeting_subscription=subscription1, datetime=pref_1.get().datetime).put() meeting = Meeting(meeting_spec=meeting_spec1, cancelled=False).put() MeetingParticipant(meeting=meeting, user=user2).put() MeetingParticipant(meeting=meeting, user=user1).put() assert get_previous_meetings(subscription1) == set([(user1.id(), user2.id())]) assert get_previous_meetings(subscription2) == set([])
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 get_previous_meetings_counts(users, subscription): previous_meetings = get_previous_meetings(subscription) counts_for_pairs = get_counts_for_pairs(previous_meetings) userids = sorted([user.key.id() for user in users]) all_pairs_counts = {pair: 0 for pair in itertools.combinations(userids, 2)} for pair in counts_for_pairs: all_pairs_counts[pair] = counts_for_pairs[pair] return all_pairs_counts
def test_get_previous_meetings_no_specs(database_no_specs): 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() assert get_previous_meetings(subscription) == set([])
def get_previous_meetings_counts(users, subscription_key): """ Given users for a subscription, return the number of times two people have matched :param users: id of user :param subscription_key: Key referencing the subscription model entity :return: Tuple of user id's matched to count ie. {(4L, 5L): 5} """ previous_meetings = get_previous_meetings(subscription_key) counts_for_pairs = get_counts_for_pairs(previous_meetings) user_ids = sorted([user.key.id() for user in users]) all_pairs_counts = { pair: 0 for pair in itertools.combinations(user_ids, 2) } for pair in counts_for_pairs: all_pairs_counts[pair] = counts_for_pairs[pair] return all_pairs_counts
def generate_pair_meetings(users, spec, prev_meeting_tuples=None): """ Returns 2 tuples: - meetings: list of dicts of the same type as prev_meetings, to indicate this iteration's found meetings - unmatched_user_ids: users with no matches. """ if prev_meeting_tuples is None: prev_meeting_tuples = get_previous_meetings(spec.meeting_subscription) uid_to_users = {user.key.id(): user for user in users} user_ids = sorted(uid_to_users.keys()) # Determine matches that should not happen disallowed_meeting_set = get_disallowed_meetings( users, prev_meeting_tuples, spec ) graph_matches = construct_graph(user_ids, disallowed_meeting_set) # matching returns (1,4) and (4,1) this de-dupes graph_matches = dict((a, b) if a <= b else (b, a) for a, b in graph_matches.iteritems()) matches = [] for uid_a, uid_b in graph_matches.items(): user_a = uid_to_users[uid_a] user_b = uid_to_users[uid_b] time = user_preference(user_a, spec) matches.append((user_a, user_b, time)) logging.info('{} employees matched'.format(len(matches) * 2)) logging.info([(meeting[0].get_username(), meeting[1].get_username()) for meeting in matches]) unmatched = [ uid_to_users[user] for user in user_ids if user not in graph_matches.keys() if user not in graph_matches.values() ] logging.info('{} employees unmatched'.format(len(unmatched))) logging.info([user.get_username() for user in unmatched]) return matches, unmatched