|
1 | | -from string import ascii_letters, digits |
| 1 | +from string import ascii_letters, digits, Template |
2 | 2 | from datetime import datetime, timezone |
3 | 3 |
|
4 | 4 | from flask import g, current_app |
5 | | -from plotly.graph_objects import Pie, Figure |
6 | | -from plotly.express import colors |
7 | | -from plotly.io import to_image |
8 | 5 |
|
9 | 6 | from src.rq import task_queue, prepare_task, run_task |
10 | 7 | from src.http import telegram |
|
19 | 16 | ) |
20 | 17 | from ._common.responses import cancel_command |
21 | 18 | from ._common.decorators import ( |
22 | | - yd_access_token_required, |
23 | | - disabled |
| 19 | + yd_access_token_required |
24 | 20 | ) |
25 | 21 |
|
26 | 22 |
|
27 | 23 | # `plotly` uses too much RAM. |
28 | | -# Disabled at the moment, will be changed in future |
29 | | -@disabled |
| 24 | +# Disabled at the moment, will be refactored in future |
| 25 | +USE_GRAPH = False |
| 26 | + |
| 27 | + |
| 28 | +if USE_GRAPH: |
| 29 | + from plotly.graph_objects import Pie, Figure |
| 30 | + from plotly.express import colors |
| 31 | + from plotly.io import to_image |
| 32 | + |
| 33 | + |
30 | 34 | @yd_access_token_required |
31 | 35 | def handle(*args, **kwargs): |
32 | 36 | """ |
33 | 37 | Handles `/space_info` command. |
34 | 38 | """ |
| 39 | + if USE_GRAPH: |
| 40 | + handle_with_graph(*args, **kwargs) |
| 41 | + else: |
| 42 | + handle_with_text(*args, **kwargs) |
| 43 | + |
| 44 | + |
| 45 | +def handle_with_text(*args, **kwargs): |
| 46 | + """ |
| 47 | + Handles `/space_info` command using plain text. |
| 48 | + """ |
| 49 | + user = g.db_user |
| 50 | + chat_id = kwargs.get( |
| 51 | + "chat_id", |
| 52 | + g.telegram_chat.id |
| 53 | + ) |
| 54 | + access_token = user.yandex_disk_token.get_access_token() |
| 55 | + disk_info = None |
| 56 | + |
| 57 | + try: |
| 58 | + disk_info = get_disk_info(access_token) |
| 59 | + except YandexAPIRequestError as error: |
| 60 | + cancel_command( |
| 61 | + chat_telegram_id=chat_id |
| 62 | + ) |
| 63 | + |
| 64 | + raise error |
| 65 | + |
| 66 | + total_space = disk_info.get("total_space", 0) |
| 67 | + used_space = disk_info.get("used_space", 0) |
| 68 | + trash_size = disk_info.get("trash_size", 0) |
| 69 | + |
| 70 | + free_space_in_gb = b_to_gb(total_space - used_space - trash_size) |
| 71 | + total_space_in_gb = b_to_gb(total_space) |
| 72 | + used_space_in_gb = b_to_gb(used_space) |
| 73 | + trash_size_in_gb = b_to_gb(trash_size) |
| 74 | + free_space_in_p = to_percent(total_space_in_gb, free_space_in_gb) |
| 75 | + used_space_in_p = to_percent(total_space_in_gb, used_space_in_gb) |
| 76 | + trash_size_in_p = to_percent(total_space_in_gb, trash_size_in_gb) |
| 77 | + |
| 78 | + current_utc_date = get_current_utc_datetime() |
| 79 | + title = gettext( |
| 80 | + "Yandex.Disk space at %(current_utc_date)s", |
| 81 | + current_utc_date=current_utc_date |
| 82 | + ) |
| 83 | + gb_text = gettext("GB") |
| 84 | + total_text = gettext("Total") |
| 85 | + used_text = gettext("Used") |
| 86 | + free_text = gettext("Free") |
| 87 | + trash_text = gettext("Trash") |
| 88 | + |
| 89 | + space_template = Template("<b>$name:</b> $size $unit, $percent%") |
| 90 | + |
| 91 | + message = ( |
| 92 | + title + |
| 93 | + "\n\n" + |
| 94 | + space_template.substitute( |
| 95 | + name=total_text, |
| 96 | + size=f"{total_space_in_gb:.2f}", |
| 97 | + unit=gb_text, |
| 98 | + percent=100 |
| 99 | + ) + |
| 100 | + "\n" + |
| 101 | + space_template.substitute( |
| 102 | + name=free_text, |
| 103 | + size=f"{free_space_in_gb:.2f}", |
| 104 | + unit=gb_text, |
| 105 | + percent=f"{free_space_in_p:.0f}" |
| 106 | + ) + |
| 107 | + "\n" + |
| 108 | + space_template.substitute( |
| 109 | + name=used_text, |
| 110 | + size=f"{used_space_in_gb:.2f}", |
| 111 | + unit=gb_text, |
| 112 | + percent=f"{used_space_in_p:.0f}" |
| 113 | + ) + |
| 114 | + "\n" + |
| 115 | + space_template.substitute( |
| 116 | + name=trash_text, |
| 117 | + size=f"{trash_size_in_gb:.2f}", |
| 118 | + unit=gb_text, |
| 119 | + percent=f"{trash_size_in_p:.0f}" |
| 120 | + ) |
| 121 | + ) |
| 122 | + |
| 123 | + telegram.send_message( |
| 124 | + chat_id=chat_id, |
| 125 | + text=message, |
| 126 | + parse_mode="HTML" |
| 127 | + ) |
| 128 | + |
| 129 | + |
| 130 | +def handle_with_graph(*args, **kwargs): |
| 131 | + """ |
| 132 | + Handles `/space_info` command using graphs. |
| 133 | + """ |
35 | 134 | user = g.db_user |
36 | 135 | chat_id = kwargs.get( |
37 | 136 | "chat_id", |
@@ -206,6 +305,13 @@ def b_to_gb(value: int) -> int: |
206 | 305 | return (value / 1024 / 1024 / 1024) |
207 | 306 |
|
208 | 307 |
|
| 308 | +def to_percent(whole: int, part: int) -> int: |
| 309 | + """ |
| 310 | + :returns: `part` as percentage of `whole`. |
| 311 | + """ |
| 312 | + return (part * 100 / whole) |
| 313 | + |
| 314 | + |
209 | 315 | def get_current_utc_datetime() -> str: |
210 | 316 | """ |
211 | 317 | :returns: Current date as string representation. |
|
0 commit comments