Skip to content

Commit 68d0ecb

Browse files
committed
/space_info will use text representation instead of graph one
1 parent a441c40 commit 68d0ecb

File tree

2 files changed

+115
-8
lines changed

2 files changed

+115
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ ___
1616

1717
- Changelog splitted into two files: one for end users and one for developers.
1818
- `/disk_info`, `/space_info`: change order of information from "Used space", "Trash size", "Free space" to "Used space", "Free space", "Trash size".
19+
- `/space_info`: text representation will be used instead of graph representation.
1920

2021
## Fixed
2122

src/blueprints/telegram_bot/webhook/commands/space_info.py

Lines changed: 114 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
from string import ascii_letters, digits
1+
from string import ascii_letters, digits, Template
22
from datetime import datetime, timezone
33

44
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
85

96
from src.rq import task_queue, prepare_task, run_task
107
from src.http import telegram
@@ -19,19 +16,121 @@
1916
)
2017
from ._common.responses import cancel_command
2118
from ._common.decorators import (
22-
yd_access_token_required,
23-
disabled
19+
yd_access_token_required
2420
)
2521

2622

2723
# `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+
3034
@yd_access_token_required
3135
def handle(*args, **kwargs):
3236
"""
3337
Handles `/space_info` command.
3438
"""
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+
"""
35134
user = g.db_user
36135
chat_id = kwargs.get(
37136
"chat_id",
@@ -206,6 +305,13 @@ def b_to_gb(value: int) -> int:
206305
return (value / 1024 / 1024 / 1024)
207306

208307

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+
209315
def get_current_utc_datetime() -> str:
210316
"""
211317
:returns: Current date as string representation.

0 commit comments

Comments
 (0)