Skip to content

Commit

Permalink
Move meassurement to abstract_user
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Paul committed Dec 2, 2021
1 parent e8cff50 commit dce3a70
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
16 changes: 11 additions & 5 deletions mautrix_telegram/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class TelegramBridge(Bridge):

periodic_sync_task: asyncio.Task = None

latest_telegram_update_timestamp: float

def prepare_db(self) -> None:
super().prepare_db()
init_db(self.db)
Expand Down Expand Up @@ -165,13 +167,14 @@ def is_bridge_ghost(self, user_id: UserID) -> bool:
async def count_logged_in_users(self) -> int:
return len([user for user in User.by_tgid.values() if user.tgid])

def update_bridge_readiness(self):
for portal in Portal.all():
if portal.latest_event_timestamp and portal.latest_event_timestamp < time() - PORTAL_INACTIVE_THRESHOLD:
self.az.live = False
return
def update_bridge_liveness(self):
self.latest_telegram_update_timestamp = time()
self.az.live = True

def check_bridge_liveness(self):
if self.latest_telegram_update_timestamp and self.latest_telegram_update_timestamp < time() - PORTAL_INACTIVE_THRESHOLD:
self.az.live = False

async def _update_active_puppet_metric(self) -> None:
active_users = UserActivity.get_active_count(
self.config['bridge.limits.min_puppet_activity_days'],
Expand Down Expand Up @@ -208,6 +211,9 @@ async def manhole_global_namespace(self, user_id: UserID) -> Dict[str, Any]:
"Puppet": Puppet,
}

def update_bridge_readiness(self):
time()

@property
def manhole_banner_program_version(self) -> str:
return f"{super().manhole_banner_program_version} and Telethon {__telethon_version__}"
Expand Down
1 change: 1 addition & 0 deletions mautrix_telegram/abstract_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ async def _update_catch(self, update: TypeUpdate) -> None:
self.log.exception("Failed to handle Telegram update")
UPDATE_ERRORS.labels(update_type=update_type).inc()
UPDATE_TIME.labels(update_type=update_type).observe(time.time() - start_time)
self.brige.update_bridge_readiness()

@property
@abstractmethod
Expand Down
5 changes: 0 additions & 5 deletions mautrix_telegram/portal/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from typing import Awaitable, Dict, List, Optional, Tuple, Union, NamedTuple, TYPE_CHECKING
from abc import ABC
from time import time
import random
import mimetypes
import codecs
Expand Down Expand Up @@ -66,8 +65,6 @@


class PortalTelegram(BasePortal, ABC):
latest_event_timestamp: float = time()

async def handle_telegram_typing(self, user: p.Puppet, update: UpdateTyping) -> None:
if user.is_real_user:
# Ignore typing notifications from double puppeted users to avoid echoing
Expand Down Expand Up @@ -616,8 +613,6 @@ async def _backfill_messages(self, source: 'AbstractUser', min_id: Optional[int]

async def handle_telegram_message(self, source: 'AbstractUser', sender: p.Puppet,
evt: Message) -> None:
self.latest_event_timestamp = time()

if self.bridge.is_blocked:
self.log.debug(f"Bridge is blocked, dropping telegram message {evt.id}")
return
Expand Down

0 comments on commit dce3a70

Please sign in to comment.