Skip to content

Commit

Permalink
🐛 Detailed analytics by category does not exist is fixed
Browse files Browse the repository at this point in the history
If no costs for category in range - bot cann't handle the error properly
  • Loading branch information
parfeniukink committed Jun 18, 2023
1 parent 3a87f4a commit f51c7e7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 36 deletions.
46 changes: 25 additions & 21 deletions src/application/errors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import traceback
from functools import wraps
from functools import partial, wraps

from loguru import logger
from telebot import types
Expand All @@ -19,38 +19,42 @@ async def inner(m: types.Message | types.CallbackQuery, *args, **kwargs):
regular_message = isinstance(m, types.Message)
chat_id = m.chat.id if regular_message else m.message.chat.id

error_message_coro = partial(
Messages.send,
chat_id=chat_id,
keyboard=default_keyboard(),
**DEFAULT_SEND_SETTINGS,
)

try:
return await coro(m, *args, **kwargs)
except AccessForbiden as error:
return await Messages.send(
chat_id,
text=str(error),
keyboard=default_keyboard(),
**DEFAULT_SEND_SETTINGS,
)
return await error_message_coro(text=str(error))
except UserError as error:
if not regular_message:
return await CallbackMessages.edit(q=m, text=str(error))
try:
return await CallbackMessages.edit(q=m, text=str(error))
except Exception:
# NOTE: Sometimes the callback to chagne could be removed
# before this operation, so that's why it is
# better to send the message in case of failure
return await error_message_coro(text=str(error))
else:
return await Messages.send(
chat_id,
text=str(error),
keyboard=default_keyboard(),
**DEFAULT_SEND_SETTINGS,
)
return await error_message_coro(text=str(error))
except Exception as error:
traceback.print_exception(error)
logger.error(error)
text = r"Something went wrong ¯\_(ツ)_/¯"

if not regular_message:
return await CallbackMessages.edit(q=m, text=text)
try:
return await CallbackMessages.edit(q=m, text=text)
except Exception:
# NOTE: Sometimes the callback to chagne could be removed
# before this operation, so that's why it is
# better to send the message in case of failure
return await error_message_coro(text=text)
else:
return await Messages.send(
chat_id,
text=text,
keyboard=default_keyboard(),
**DEFAULT_SEND_SETTINGS,
)
return await error_message_coro(text=text)

return inner
35 changes: 20 additions & 15 deletions src/handlers/analytics/root.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""j
"""
This module includes handlers that take on
routing between analytics operations.
"""
Expand Down Expand Up @@ -28,7 +28,7 @@
ANALYTICS_BASIC_OPTIONS_KEYBOARD_ELEMENTS,
ANALYTICS_DETAILED_OPTIONS_KEYBOARD_ELEMENTS,
)
from src.keyboards.default import default_keyboard, restart_keyboard
from src.keyboards.default import default_keyboard
from src.keyboards.models import CallbackItem
from src.keyboards.patterns import callback_patterns_keyboard

Expand Down Expand Up @@ -77,7 +77,6 @@ async def basic_level_selected_callback(contract: CallbackQueryContract):
keyboard=default_keyboard(),
)

state.messages_to_delete.add(contract.q.id)
await Messages.delete(contract.user.chat_id, *state.messages_to_delete)


Expand All @@ -96,14 +95,22 @@ async def category_selected_callback(contract: CallbackQueryContract):
category_id=category_id,
)

no_frames = True

async for frame in frames:
no_frames = False

await Messages.send(
chat_id=contract.user.chat_id,
text=frame,
keyboard=default_keyboard(),
)

await Messages.delete(contract.user.chat_id, *state.messages_to_delete)
if no_frames:
await Messages.delete(contract.user.chat_id, *state.messages_to_delete)
raise UserError(\\_(ツ)_/¯ Nothing to show")
else:
await Messages.delete(contract.user.chat_id, *state.messages_to_delete)


@transaction
Expand Down Expand Up @@ -148,9 +155,7 @@ async def detailed_level_selected_callback(contract: CallbackQueryContract):

# Prepare categories names with callback data
keyboard_patterns: list[CallbackItem] = []
filtered_categories = (
await categories_services.get_all()
)
filtered_categories = await categories_services.get_all()
for category in filtered_categories:
_callback_data = "".join(
(
Expand Down Expand Up @@ -182,6 +187,7 @@ async def detailed_level_selected_callback(contract: CallbackQueryContract):
)

if no_frames:
state.messages_to_delete.add(contract.q.id)
raise UserError(\\_(ツ)_/¯ Nothing to show")

state.messages_to_delete.add(contract.q.id)
Expand Down Expand Up @@ -216,10 +222,10 @@ async def pattern_entered_callback(contract: MessageContract):
"""User can enter the date range in different ways:
2022: Annually analytics with basic detail level support
2022-2023: Annually range analytics with basic detail level support
04-2022: Montly analytics with different detail level support
04-2022 - 03-2023: Specific range analytics
2022-04: Montly analytics with different detail level support
2022-04 - 2023-03: Specific range analytics
with only basic analytics support
01-04-2022 - 03-04-2022: Specific range analytics
2022-04-01 - 2022-04-03: Specific range analytics
with different detail level support.
If the delta of the dates range is <= 31 days (1 month)
Expand Down Expand Up @@ -326,14 +332,13 @@ async def analytics_action_selected_callback(contract: CallbackQueryContract):
),
)
case AnalyticsRootOption.BY_PATTERN:
message = await Messages.send(
chat_id=contract.user.chat_id,
await CallbackMessages.edit(
q=contract.q,
text="⤵️ Enter the pattern or range and press Enter",
keyboard=restart_keyboard(),
keyboard=None,
)

state.next_callback = pattern_entered_callback
state.messages_to_delete.add(message.id)
state.messages_to_delete.add(contract.q.id)
case _:
raise ValueError("Incorrect analytics submenu payload")

Expand Down

0 comments on commit f51c7e7

Please sign in to comment.