-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from jonodrew/set-unmatched-bonus
Change task to allow a value for unmatched bonus
- Loading branch information
Showing
17 changed files
with
219 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[bumpversion] | ||
current_version = 2.2.0 | ||
current_version = 2.3.0 | ||
commit = True | ||
tag = True | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
from app.extensions import celery | ||
from app.extensions import celery_app | ||
|
||
|
||
def make_celery(app): | ||
celery.conf.update(app.config) | ||
celery.conf.imports = ("app.tasks.tasks",) | ||
celery_app.conf.update(app.config) | ||
celery_app.conf.imports = ("app.tasks.tasks",) | ||
|
||
class ContextTask(celery.Task): | ||
class ContextTask(celery_app.Task): | ||
def __call__(self, *args, **kwargs): | ||
with app.app_context(): | ||
return self.run(*args, **kwargs) | ||
|
||
celery.Task = ContextTask | ||
return celery | ||
celery_app.Task = ContextTask | ||
return celery_app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from copy import deepcopy | ||
|
||
import celery | ||
from celery.result import AsyncResult | ||
from app.tasks.tasks import async_process_data, find_best_output | ||
from app.classes import CSMentor, CSMentee | ||
from app.helpers import base_rules | ||
|
||
|
||
def most_mentees_with_at_least_one_mentor( | ||
mentors: list[CSMentor], mentees: list[CSMentee] | ||
) -> AsyncResult: | ||
max_score = sum(rule.results.get(True) for rule in base_rules()) | ||
copies = ((deepcopy(mentors), deepcopy(mentees), i) for i in range(max_score)) | ||
task = celery.chord( | ||
(async_process_data.si(*data) for data in copies), find_best_output.s() | ||
)() | ||
return task |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,58 @@ | ||
import functools | ||
import os | ||
|
||
import sys | ||
import requests | ||
from typing import Tuple, List | ||
from app.extensions import celery | ||
from typing import Tuple, List, Sequence | ||
|
||
from app.classes import CSMentor, CSMentee | ||
from app.extensions import celery_app as celery_app | ||
from matching import process | ||
from app.classes import CSParticipantFactory | ||
from app.helpers import base_rules | ||
from matching.rules.rule import UnmatchedBonus | ||
|
||
sys.setrecursionlimit(10000) | ||
|
||
|
||
@celery.task(name="async_process_data", bind=True) | ||
@celery_app.task(name="async_process_data", bind=True) | ||
def async_process_data( | ||
self, | ||
data_to_process: Tuple[List[dict], List[dict]], | ||
) -> Tuple[List[dict], List[dict]]: | ||
mentor_data, mentee_data = data_to_process | ||
mentors = map(CSParticipantFactory.create_from_dict, mentor_data) | ||
mentees = map(CSParticipantFactory.create_from_dict, mentee_data) | ||
mentors, | ||
mentees, | ||
unmatched_bonus: int = 6, | ||
) -> Tuple[List[CSMentor], List[CSMentee], int]: | ||
all_rules = [base_rules() for _ in range(3)] | ||
for ruleset in all_rules: | ||
ruleset.append(UnmatchedBonus(unmatched_bonus)) | ||
matched_mentors, matched_mentees = process.process_data( | ||
list(mentors), list(mentees), all_rules=all_rules | ||
) | ||
matched_as_dict = [participant.to_dict() for participant in matched_mentors], [ | ||
participant.to_dict() for participant in matched_mentees | ||
] | ||
return matched_as_dict | ||
return matched_mentors, matched_mentees, unmatched_bonus | ||
|
||
|
||
@celery_app.task | ||
def find_best_output( | ||
group_result: Sequence[tuple[list[CSMentor], list[CSMentee], int]] | ||
) -> tuple[list[CSMentor], list[CSMentee], int]: | ||
highest_count = (0, 0) | ||
best_outcome = group_result[0] | ||
for participant_tuple in group_result: | ||
mentors, mentees, unmatched_bonus = participant_tuple | ||
one_connection_min_func = functools.partial( | ||
map, lambda participant: len(participant.connections) > 0 | ||
) | ||
current_count = ( | ||
sum(one_connection_min_func(mentors)), | ||
sum(one_connection_min_func(mentees)), | ||
) | ||
if all( | ||
current > highest for current, highest in zip(current_count, highest_count) # type: ignore | ||
): | ||
best_outcome = participant_tuple | ||
highest_count = current_count # type: ignore | ||
return best_outcome | ||
|
||
|
||
@celery.task(name="delete_mailing_lists_after_period", bind=True) | ||
@celery_app.task(name="delete_mailing_lists_after_period", bind=True) | ||
def delete_mailing_lists_after_period(self, task_id: str): | ||
url = f"{os.environ.get('SERVICE_URL', 'http://app:5000')}/tasks/{task_id}" | ||
return requests.delete(url).status_code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.