FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
optimizer-framework/scripts/data_model_loop.py at master · ksericpro/optimizer-framework · GitHub
ksericpro
/
optimizer-framework
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
optimizer-framework
/
scripts
/
data_model_loop.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
60 lines (49 loc) · 1.99 KB
Breadcrumbs
optimizer-framework
/
scripts
/
data_model_loop.py
Copy path
File metadata and controls
60 lines (49 loc) · 1.99 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import
psycopg2
import
pandas
as
pd
import
math
from
api
.
logger_config
import
logger
from
api
.
db_config
import
get_db_params
DB_PARAMS
=
get_db_params
()
def
run_data_model_loop
():
"""
Analyzes historical performance to derive current-day parameters for the optimizer.
This fulfills the requirement of the 'Data Model' in the system architecture.
"""
try
:
conn
=
psycopg2
.
connect
(
**
DB_PARAMS
)
# 1. Fetch aggregate stats for the last 7 days
query
=
"""
SELECT
driver_id,
AVG(total_orders_completed) as avg_orders,
AVG(average_service_time) as avg_service,
AVG(efficiency_score) as avg_efficiency
FROM performance_metrics
WHERE date >= CURRENT_DATE - INTERVAL '7 days'
GROUP BY driver_id
"""
stats
=
pd
.
read_sql
(
query
,
conn
)
if
stats
.
empty
:
logger
.
warning
(
"No recent performance data found. Skipping Data Model update."
)
return
{
"status"
:
"success"
,
"drivers_updated"
:
0
}
cur
=
conn
.
cursor
()
logger
.
info
(
f"Running Data Model Loop for
{
len
(
stats
)
}
drivers..."
)
updated_count
=
0
for
_
,
row
in
stats
.
iterrows
():
base_capacity
=
row
[
'avg_orders'
]
efficiency_boost
=
1.1
if
row
[
'avg_efficiency'
]
>
0.85
else
1.0
new_max_jobs
=
math
.
ceil
(
base_capacity
*
efficiency_boost
)
cur
.
execute
(
"UPDATE drivers SET max_jobs_per_day = %s WHERE id = %s"
,
(
new_max_jobs
,
row
[
'driver_id'
])
)
updated_count
+=
1
conn
.
commit
()
cur
.
close
()
conn
.
close
()
logger
.
info
(
f"Data Model Loop complete. Updated
{
updated_count
}
drivers."
)
return
{
"status"
:
"success"
,
"drivers_updated"
:
updated_count
}
except
Exception
as
e
:
return
{
"status"
:
"error"
,
"message"
:
str
(
e
)}
if
__name__
==
"__main__"
:
run_data_model_loop
()
Back
|
FazBrowse Home
|
New Git URL