Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2023-362
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
2023-362
2023-362
Commits
ece858c0
Commit
ece858c0
authored
Oct 23, 2023
by
Nirmal M.D.S
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
scheduler initial commit
parent
4cefa247
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
85 additions
and
0 deletions
+85
-0
IT20074340/TaskAllocation/Sachintha/model/scheduler.py
IT20074340/TaskAllocation/Sachintha/model/scheduler.py
+85
-0
No files found.
IT20074340/TaskAllocation/Sachintha/model/scheduler.py
0 → 100644
View file @
ece858c0
import
mysql.connector
import
datetime
import
sched
import
time
# Configure database connections
db_config
=
{
'host'
:
'localhost'
,
'user'
:
'root'
,
'password'
:
''
,
'database'
:
'rptestdb'
,
}
# Establish a connection to the MySQL database
try
:
db_connection
=
mysql
.
connector
.
connect
(
**
db_config
)
cursor
=
db_connection
.
cursor
()
except
mysql
.
connector
.
Error
as
err
:
print
(
f
"Error: {err}"
)
cursor
=
None
# Function to fetch and process data
def
fetch_and_process_data
():
# Fetch user data
cursor
.
execute
(
"SELECT user_id, stress_level FROM user"
)
user_data
=
cursor
.
fetchall
()
# Fetch task data
cursor
.
execute
(
"SELECT task_id, level FROM task"
)
task_data
=
cursor
.
fetchall
()
# Process user data
users
=
[{
"user_id"
:
user_id
,
"stress_level"
:
stress_level
}
for
user_id
,
stress_level
in
user_data
]
# Process task data
tasks
=
[{
"task_id"
:
task_id
,
"level"
:
level
}
for
task_id
,
level
in
task_data
]
# Compare stress_level and level and assign tasks accordingly
assigned_tasks
=
[]
for
user
in
users
:
for
task
in
tasks
:
if
user
[
"stress_level"
]
==
"Low"
and
task
[
"level"
]
==
"High"
:
assigned_tasks
.
append
({
"user_id"
:
user
[
"user_id"
],
"task_id"
:
task
[
"task_id"
]})
tasks
.
remove
(
task
)
# Remove the assigned task from the tasks list
break
# Exit the inner loop after assigning a task
elif
user
[
"stress_level"
]
==
"High"
and
task
[
"level"
]
==
"Low"
:
assigned_tasks
.
append
({
"user_id"
:
user
[
"user_id"
],
"task_id"
:
task
[
"task_id"
]})
tasks
.
remove
(
task
)
# Remove the assigned task from the tasks list
break
# Exit the inner loop after assigning a task
# Insert assigned tasks into the assignedtask table
for
assigned_task
in
assigned_tasks
:
cursor
.
execute
(
"INSERT INTO assignedtask (user_id, task_id) VALUES (
%
s,
%
s)"
,
(
assigned_task
[
"user_id"
],
assigned_task
[
"task_id"
]))
db_connection
.
commit
()
return
assigned_tasks
# Define the function to assign tasks
def
assign_tasks
():
assigned_tasks
=
fetch_and_process_data
()
print
(
"Assigned tasks:"
,
assigned_tasks
)
# Initialize the scheduler
s
=
sched
.
scheduler
(
time
.
time
,
time
.
sleep
)
# Schedule the assignment task to run daily at 10:30 PM
def
schedule_daily_task
():
now
=
datetime
.
datetime
.
now
()
target_time
=
datetime
.
datetime
.
combine
(
now
.
date
(),
datetime
.
time
(
10
,
19
))
# 12:00 AM
if
now
>=
target_time
:
target_time
+=
datetime
.
timedelta
(
days
=
1
)
# If the target time is already passed, schedule for the next day
time_until_next_run
=
(
target_time
-
now
)
.
total_seconds
()
s
.
enter
(
time_until_next_run
,
1
,
assign_tasks
)
s
.
run
()
# Run the scheduler loop
while
True
:
schedule_daily_task
()
time
.
sleep
(
60
)
# Wait for 1 minute before scheduling the next task
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment