| 1 |
8440155 |
mahdim10 |
1y ago |
import asyncio |
| 2 |
8440155 |
mahdim10 |
1y ago |
|
| 3 |
8440155 |
mahdim10 |
1y ago |
from sqlalchemy import case |
| 4 |
8440155 |
mahdim10 |
1y ago |
from sqlalchemy.orm import Session |
| 5 |
8440155 |
mahdim10 |
1y ago |
|
| 6 |
8440155 |
mahdim10 |
1y ago |
from app.config import NOTIFY_REACHED_USAGE_PERCENT |
| 7 |
8440155 |
mahdim10 |
1y ago |
from app.db.models import User |
| 8 |
8440155 |
mahdim10 |
1y ago |
from app.models.notification import UserNotification |
| 9 |
8440155 |
mahdim10 |
1y ago |
from app.models.user import UserResponse |
| 10 |
8440155 |
mahdim10 |
1y ago |
from app.notification.notifiers import notify |
| 11 |
8440155 |
mahdim10 |
1y ago |
|
| 12 |
8440155 |
mahdim10 |
1y ago |
|
| 13 |
8440155 |
mahdim10 |
1y ago |
async def data_usage_percent_reached(db: Session, users_usage: list) -> None: |
| 14 |
8440155 |
mahdim10 |
1y ago |
""" |
| 15 |
8440155 |
mahdim10 |
1y ago |
Monitors data usage of active users and sends a notification if usage exceeds NOTIFY_REACHED_USAGE_PERCENT. |
| 16 |
8440155 |
mahdim10 |
1y ago |
|
| 17 |
8440155 |
mahdim10 |
1y ago |
This function performs the following steps: |
| 18 |
8440155 |
mahdim10 |
1y ago |
- Receives a list of user data usage, where each user has an ID and the amount of data they have used from all nodes. |
| 19 |
8440155 |
mahdim10 |
1y ago |
- If the new total data usage exceeds NOTIFY_REACHED_USAGE_PERCENT of the user's data limit, it sends a notification. |
| 20 |
8440155 |
mahdim10 |
1y ago |
|
| 21 |
8440155 |
mahdim10 |
1y ago |
This function is called when user data usage updates occur. |
| 22 |
8440155 |
mahdim10 |
1y ago |
|
| 23 |
8440155 |
mahdim10 |
1y ago |
""" |
| 24 |
8440155 |
mahdim10 |
1y ago |
|
| 25 |
8440155 |
mahdim10 |
1y ago |
users_usage_dict = {user["id"]: user["value"] for user in users_usage} |
| 26 |
8440155 |
mahdim10 |
1y ago |
|
| 27 |
8440155 |
mahdim10 |
1y ago |
case_stmt = case( |
| 28 |
8440155 |
mahdim10 |
1y ago |
*[ |
| 29 |
8440155 |
mahdim10 |
1y ago |
(User.id == user_id, User.used_traffic + increase_amount) |
| 30 |
8440155 |
mahdim10 |
1y ago |
for user_id, increase_amount in users_usage_dict.items() |
| 31 |
8440155 |
mahdim10 |
1y ago |
], |
| 32 |
8440155 |
mahdim10 |
1y ago |
else_=User.used_traffic, |
| 33 |
8440155 |
mahdim10 |
1y ago |
) |
| 34 |
8440155 |
mahdim10 |
1y ago |
|
| 35 |
8440155 |
mahdim10 |
1y ago |
exceeding_users = ( |
| 36 |
8440155 |
mahdim10 |
1y ago |
db.query(User) |
| 37 |
8440155 |
mahdim10 |
1y ago |
.filter( |
| 38 |
8440155 |
mahdim10 |
1y ago |
User.id.in_(users_usage_dict.keys()), |
| 39 |
8440155 |
mahdim10 |
1y ago |
User.data_limit.isnot(None), |
| 40 |
8440155 |
mahdim10 |
1y ago |
User.data_limit > 0, |
| 41 |
8440155 |
mahdim10 |
1y ago |
(User.used_traffic / User.data_limit) * 100 |
| 42 |
8440155 |
mahdim10 |
1y ago |
< NOTIFY_REACHED_USAGE_PERCENT, |
| 43 |
8440155 |
mahdim10 |
1y ago |
(case_stmt / User.data_limit) * 100 > NOTIFY_REACHED_USAGE_PERCENT, |
| 44 |
8440155 |
mahdim10 |
1y ago |
) |
| 45 |
8440155 |
mahdim10 |
1y ago |
.all() |
| 46 |
8440155 |
mahdim10 |
1y ago |
) |
| 47 |
8440155 |
mahdim10 |
1y ago |
|
| 48 |
8440155 |
mahdim10 |
1y ago |
for user in exceeding_users: |
| 49 |
8440155 |
mahdim10 |
1y ago |
added_traffic = users_usage_dict[user.id] |
| 50 |
8440155 |
mahdim10 |
1y ago |
user.used_traffic += added_traffic |
| 51 |
8440155 |
mahdim10 |
1y ago |
asyncio.ensure_future( |
| 52 |
8440155 |
mahdim10 |
1y ago |
notify( |
| 53 |
8440155 |
mahdim10 |
1y ago |
action=UserNotification.Action.reached_usage_percent, |
| 54 |
8440155 |
mahdim10 |
1y ago |
user=UserResponse.model_validate(user), |
| 55 |
8440155 |
mahdim10 |
1y ago |
) |
| 56 |
8440155 |
mahdim10 |
1y ago |
) |
| 57 |
8440155 |
mahdim10 |
1y ago |
|
| 58 |
8440155 |
mahdim10 |
1y ago |
db.expunge_all() |