-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
5oappy/feature/integration testing schedulerv2 (#306)
## Describe your changes note: merged lindas changes into this branch before Linda's was merged into main so there may be some inconsistencies that will need to be manually resolved. real changes begin at 1ecbfe0. I know its a mess but it works!! The scheduler works correctly according to this control flow: 1) api is called and.. 2) optimiser instance is created 3) optimiser invokes calculator instance 4) calculator instance retrieves all required data 5) optimiser calls calculator methods to assign varaibles 6) minizinc instance created with the model string 7) variables are assigned to minizinc model string 8) minizinc solves the model 9) minizinc returns the posssible assignments 10) possible assignments are returned to api 11) api invokes save result to persist possible assignments to database and setting relevant flags. 12) api returns {"message": "Optimisation completed successfully", "result": <minizinc result string>} Further info on calculator: - The scheduler will optimise all recently created shifts marked via the status flag being set to SUBMITTED (as in just submitted). - The scheduler will allow for users to be assigned to multiple shifts provided their skills align. - The scheduler will allow for clashes in scheduling. - The actual saving of volunteer shift will check for clashes. - `unavailability_record` is created for each successful user assigned and saved to the shift (each `shift_request_volunteer` entry added to the db). This prevents the optimiser from thinking user is available for subsequent optimisations. pictures: before calling optimiser: <img width="871" alt="Screenshot 2024-10-06 at 11 13 05 PM" src="https://github.com/user-attachments/assets/e492839f-f0e8-4e9b-9029-a73b304c466c"> after calling optimiser: <img width="871" alt="Screenshot 2024-10-06 at 11 13 20 PM" src="https://github.com/user-attachments/assets/f0711a5f-50b9-49e1-ba28-c75facb1e7e0"> logs: <img width="821" alt="Screenshot 2024-10-06 at 11 56 37 PM" src="https://github.com/user-attachments/assets/3d0c4c5b-3c4b-4970-b34e-301f6fafbb25"> <img width="186" alt="Screenshot 2024-10-07 at 12 00 01 AM" src="https://github.com/user-attachments/assets/c52f2c5d-39cd-4b7f-b963-fe5741cf0dfd"> <img width="197" alt="Screenshot 2024-10-07 at 12 01 48 AM" src="https://github.com/user-attachments/assets/ba65dc65-365e-4c8d-a658-85df00b2e4da"> <img width="193" alt="Screenshot 2024-10-06 at 10 16 14 PM" src="https://github.com/user-attachments/assets/584080c2-5f41-4a2e-92d0-9f505084e4b7"> note: the second phone has duplicate shifts because of a bug that allowed 2 shifts to be posted via post man at the same time and got persisted when the optimiser was not set up to handle clashes yet. signed: Steven, Hafizh, Linda. ## Issue ticket number and link [FIR-4](https://fireapp-emergiq-2024.atlassian.net/browse/FIR-4) [FIR-108](https://fireapp-emergiq-2024.atlassian.net/browse/FIR-108) --------- Co-authored-by: YiranLI <[email protected]> Co-authored-by: anannnchim <[email protected]> Co-authored-by: Muhammad Hafizh Hasyim <[email protected]>
- Loading branch information
1 parent
7957255
commit 55f07b1
Showing
7 changed files
with
294 additions
and
108 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
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,2 @@ | ||
from .api import * | ||
from .response_models import optimiser_response_model |
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,52 @@ | ||
from flask_restful import Resource, marshal_with, reqparse | ||
from .response_models import optimiser_response_model | ||
from repository.shift_repository import ShiftRepository | ||
from services.jwk import requires_auth, is_user_or_has_role | ||
from domain import UserType, session_scope | ||
from controllers.v2.v2_blueprint import v2_api | ||
from services.optimiser.optimiser import Optimiser | ||
import logging | ||
|
||
# Initialise parser for potential arguments in future extensions (if needed) | ||
parser = reqparse.RequestParser() | ||
parser.add_argument('debug', type=bool, required=False, help="Optional debug mode flag.") | ||
|
||
|
||
class OptimiserResource(Resource): | ||
optimiser_repository: ShiftRepository | ||
|
||
def __init__(self, optimiser_repository: ShiftRepository = ShiftRepository()): | ||
self.optimiser_repository = optimiser_repository | ||
|
||
@requires_auth | ||
@is_user_or_has_role(None, UserType.ROOT_ADMIN) | ||
@marshal_with(optimiser_response_model) # Use the marshalling model | ||
def post(self): | ||
# Parse debug argument | ||
try: | ||
args = parser.parse_args() | ||
except Exception: | ||
args = {} | ||
# Default debug to False if it's not provided or the body is empty | ||
debug = args.get('debug', False) | ||
|
||
try: | ||
with session_scope() as session: | ||
# Initialise and run the optimiser | ||
optimiser = Optimiser(session=session, repository=self.optimiser_repository, debug=debug) | ||
result = optimiser.solve() | ||
optimiser.save_result(result) | ||
|
||
# Return raw data, the marshaller will format it | ||
return { | ||
"message": "Optimisation completed successfully", | ||
"result": str(result) | ||
} | ||
|
||
except Exception as e: | ||
logging.error(f"Error running optimiser: {e}") | ||
return {"message": "Internal server error", "result": str(e)}, 500 | ||
|
||
|
||
# Register the OptimiserResource in the blueprint | ||
v2_api.add_resource(OptimiserResource, '/v2/optimiser') |
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,6 @@ | ||
from flask_restful import fields | ||
|
||
optimiser_response_model = { | ||
'message': fields.String, | ||
'result': fields.Raw | ||
} |
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.