Skip to content

Commit

Permalink
feat: add botx metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiruha01 committed Sep 18, 2023
1 parent 2eb8f16 commit f1a558e
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 0 deletions.
33 changes: 33 additions & 0 deletions pybotx/bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@
UploadFileMethod,
)
from pybotx.client.get_token import get_token
from pybotx.client.mertics_api.add_bot_function import (
AddBotFunctionMethod,
BotXAPIAddBotFunctionRequestPayload,
)
from pybotx.client.notifications_api.direct_notification import (
BotXAPIDirectNotificationRequestPayload,
DirectNotificationMethod,
Expand Down Expand Up @@ -1778,6 +1782,35 @@ async def refresh_access_token(
)
await method.execute(payload)

# - Metrics API -
async def collect_metric(
self,
bot_id: UUID,
bot_function: str,
huids: List[UUID],
chat_id: UUID,
) -> None:
"""Collect a new use of the bot function.
:param bot_id: Bot which should perform the request.
:param bot_function: Name of the bot function.
:param huids: Users involved in using the function.
:param chat_id: Chat in which the function was used.
"""

method = AddBotFunctionMethod(
bot_id,
self._httpx_client,
self._bot_accounts_storage,
)

payload = BotXAPIAddBotFunctionRequestPayload.from_domain(
bot_function=bot_function,
huids=huids,
chat_id=chat_id,
)
await method.execute(payload)

@staticmethod
def _build_main_collector(
collectors: Sequence[HandlerCollector],
Expand Down
Empty file.
43 changes: 43 additions & 0 deletions pybotx/client/mertics_api/add_bot_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from typing import List, Literal
from uuid import UUID

from pybotx.client.authorized_botx_method import AuthorizedBotXMethod
from pybotx.models.api_base import UnverifiedPayloadBaseModel, VerifiedPayloadBaseModel


class BotXAPIAddBotFunctionRequestPayload(UnverifiedPayloadBaseModel):
group_chat_id: UUID
user_huids: List[UUID]
bot_function: str

@classmethod
def from_domain(
cls,
chat_id: UUID,
huids: List[UUID],
bot_function: str,
) -> "BotXAPIAddBotFunctionRequestPayload":
return cls(group_chat_id=chat_id, user_huids=huids, bot_function=bot_function)


class BotXAPIAddBotFunctionResponsePayload(VerifiedPayloadBaseModel):
status: Literal["ok"]
result: bool


class AddBotFunctionMethod(AuthorizedBotXMethod):
async def execute(
self,
payload: BotXAPIAddBotFunctionRequestPayload,
) -> None:
path = "/api/v3/botx/metrics/bot_function"

response = await self._botx_method_call(
"POST",
self._build_url(path),
json=payload.jsonable_dict(),
)
self._verify_and_extract_api_model(
BotXAPIAddBotFunctionResponsePayload,
response,
)
Empty file.
54 changes: 54 additions & 0 deletions tests/client/test_metrics_api/test_collect_metric.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from http import HTTPStatus
from uuid import UUID

import httpx
import pytest
from respx import MockRouter

from pybotx import Bot, BotAccountWithSecret, HandlerCollector, lifespan_wrapper

pytestmark = [
pytest.mark.asyncio,
pytest.mark.mock_authorization,
pytest.mark.usefixtures("respx_mock"),
]


async def test__collect_bot_function_metric__success(
respx_mock: MockRouter,
host: str,
bot_id: UUID,
bot_account: BotAccountWithSecret,
) -> None:
# - Arrange -
endpoint = respx_mock.post(
f"https://{host}/api/v3/botx/metrics/bot_function",
headers={"Authorization": "Bearer token", "Content-Type": "application/json"},
json={
"user_huids": ["33bd8924-da34-4615-b8ea-f8f7139bf4ef"],
"group_chat_id": "3c11c28e-fbe1-4080-86e8-58e03c217dae",
"bot_function": "email_sent",
},
).mock(
return_value=httpx.Response(
HTTPStatus.ACCEPTED,
json={
"status": "ok",
"result": True,
},
),
)

built_bot = Bot(collectors=[HandlerCollector()], bot_accounts=[bot_account])

# - Act -
async with lifespan_wrapper(built_bot) as bot:
await bot.collect_metric(
bot_id=bot_id,
bot_function="email_sent",
huids=[UUID("33bd8924-da34-4615-b8ea-f8f7139bf4ef")],
chat_id=UUID("3c11c28e-fbe1-4080-86e8-58e03c217dae"),
)

# - Assert -
assert endpoint.called

0 comments on commit f1a558e

Please sign in to comment.