2023-10-04 23:24:55 +02:00
|
|
|
from db import logs, stats, users
|
2023-08-28 00:58:32 +02:00
|
|
|
from helpers import network
|
|
|
|
|
|
|
|
async def after_request(
|
2023-11-07 00:56:43 +01:00
|
|
|
incoming_request,
|
2023-08-28 00:58:32 +02:00
|
|
|
target_request: dict,
|
|
|
|
user: dict,
|
2023-10-16 23:34:54 +02:00
|
|
|
tokens: dict,
|
2023-08-28 00:58:32 +02:00
|
|
|
credits_cost: int,
|
|
|
|
path: str,
|
|
|
|
is_chat: bool,
|
|
|
|
model: str,
|
2023-10-16 23:34:54 +02:00
|
|
|
provider: str,
|
2023-08-28 00:58:32 +02:00
|
|
|
) -> None:
|
2023-10-12 00:03:15 +02:00
|
|
|
"""Runs after every request."""
|
|
|
|
|
2023-08-28 00:58:32 +02:00
|
|
|
if user and incoming_request:
|
2023-10-16 23:34:54 +02:00
|
|
|
await logs.log_api_request(
|
|
|
|
user=user,
|
|
|
|
incoming_request=incoming_request,
|
|
|
|
target_url=target_request['url'],
|
|
|
|
tokens=tokens,
|
|
|
|
provider=provider
|
|
|
|
)
|
2023-08-28 00:58:32 +02:00
|
|
|
|
|
|
|
if credits_cost and user:
|
|
|
|
await users.manager.update_by_id(user['_id'], {'$inc': {'credits': -credits_cost}})
|
|
|
|
|
2023-10-16 23:34:54 +02:00
|
|
|
ip_address = network.get_ip(incoming_request)
|
2023-08-28 00:58:32 +02:00
|
|
|
|
|
|
|
await stats.manager.add_date()
|
2023-10-12 00:03:15 +02:00
|
|
|
# await stats.manager.add_ip_address(ip_address)
|
2023-08-28 00:58:32 +02:00
|
|
|
await stats.manager.add_path(path)
|
|
|
|
await stats.manager.add_target(target_request['url'])
|
|
|
|
|
|
|
|
if is_chat:
|
|
|
|
await stats.manager.add_model(model)
|