From 9e6db8caaad377b9f64935aa44b9f0a474a2b749 Mon Sep 17 00:00:00 2001 From: Xtao_dada Date: Sat, 4 Sep 2021 16:28:38 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=E2=9A=A1=EF=B8=8F=20fix=20double?= =?UTF-8?q?=20log=20and=20add=20uptime=20(#129)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🐛 修复双重日志 ⚡️ 添加在线时间查看 --- config.gen.yml | 2 ++ languages/built-in/en.yml | 9 +++++---- languages/built-in/zh-cn.yml | 2 ++ languages/built-in/zh-tw.yml | 3 ++- pagermaid/__init__.py | 2 ++ pagermaid/interface/__init__.py | 2 +- pagermaid/modules/clock.py | 2 +- pagermaid/modules/status.py | 31 +++++++++++++++++++++++++++++-- 8 files changed, 44 insertions(+), 9 deletions(-) diff --git a/config.gen.yml b/config.gen.yml index bdf66c0..d14b5a1 100644 --- a/config.gen.yml +++ b/config.gen.yml @@ -84,3 +84,5 @@ speed_test_path: "" # 24 default time_form: "%H:%M" date_form: "%A %y/%m/%d" +# only support %m %d %H %M %S +start_form: "%m/%d %H:%M" diff --git a/languages/built-in/en.yml b/languages/built-in/en.yml index 5bddaaf..9cb82cf 100644 --- a/languages/built-in/en.yml +++ b/languages/built-in/en.yml @@ -159,6 +159,7 @@ handle_failure_error: Something went wrong oh woo woo ~ Please report this probl time_des: Display the time in a specific area. If the parameter is empty, the configuration value will be displayed by default. time_parameters: time_time: time +time_config: Something went wrong oh woo woo ~ Please check your config file. # external # translate translate_des: Translate the target message into the specified language through Google Translate. (Support reply) @@ -363,13 +364,13 @@ status_des: Output the running status of PagerMaid-Modify. status_online: online status_offline: offline status_hint: PagerMaid-Modify running status -status_name: host name -status_platform: host platform +status_name: Host name +status_platform: Host platform status_release: Kernel version status_python: Python version status_telethon: Telethon version -status_db: database status -status_dialogs: total number of conversations +status_db: Database status +status_uptime: Uptime # stats stats_des: View conversation statistics. stats_loading: Loading... diff --git a/languages/built-in/zh-cn.yml b/languages/built-in/zh-cn.yml index cb5163b..e90cf33 100644 --- a/languages/built-in/zh-cn.yml +++ b/languages/built-in/zh-cn.yml @@ -167,6 +167,7 @@ handle_failure_error: 出错了呜呜呜 ~ 请报告此问题。 time_des: 显示特定区域的时间,如果参数为空,则默认显示配置值。 time_parameters: <地区> time_time: 时间 +time_config: 出错了呜呜呜 ~ 配置文件填写错误 # external ## translate @@ -383,6 +384,7 @@ status_release: Kernel 版本 status_python: Python 版本 status_telethon: Telethon 版本 status_db: 数据库状态 +status_uptime: 运行时间 ## stats stats_des: 查看我的对话统计信息。 stats_loading: 加载中 . . . diff --git a/languages/built-in/zh-tw.yml b/languages/built-in/zh-tw.yml index c09ae77..d24b3d0 100644 --- a/languages/built-in/zh-tw.yml +++ b/languages/built-in/zh-tw.yml @@ -154,6 +154,7 @@ handle_failure_error: Error!請報告此問題。 time_des: 顯示特定區域的時間。 time_parameters: <地區> time_time: 時間 +time_config: 出錯了嗚嗚嗚 ~ 配置文件填寫錯誤 # external # translate translate_des: 透過Google翻譯把目標訊息翻譯成指定的語言。(支援回覆) @@ -364,7 +365,7 @@ status_release: Kernel版本 status_python: Python版本 status_telethon: Telethon版本 status_db: 數據庫狀態 -status_dialogs: 聊天總數 +status_uptime: 運行時間 # stats stats_des: 查看我的對話統計信息。 stats_loading: 加載中 . . . diff --git a/pagermaid/__init__.py b/pagermaid/__init__.py index 4522f52..43f3d76 100644 --- a/pagermaid/__init__.py +++ b/pagermaid/__init__.py @@ -14,6 +14,7 @@ try: except: pass from subprocess import run, PIPE +from datetime import datetime from time import time from os import getcwd, makedirs, environ from os.path import exists @@ -345,6 +346,7 @@ def before_send(event, hint): report_time = time() +start_time = datetime.utcnow() git_hash = run("git rev-parse HEAD", stdout=PIPE, shell=True).stdout.decode() sentry_sdk.init( "https://935d04099b7d4bd889e7ffac488579fc@o416616.ingest.sentry.io/5312335", diff --git a/pagermaid/interface/__init__.py b/pagermaid/interface/__init__.py index cb8d04a..f149f62 100644 --- a/pagermaid/interface/__init__.py +++ b/pagermaid/interface/__init__.py @@ -55,7 +55,7 @@ def start(): if strtobool(config['web_interface']['enable']) or environ.get('PORT'): logs.info(f"已经启动Web界面 {web_host}:{web_port}") app.logger.removeHandler(default_handler) - app.logger.addHandler(logging_handler) + # app.logger.addHandler(logging_handler) try: server.start() except OSError: diff --git a/pagermaid/modules/clock.py b/pagermaid/modules/clock.py index 204cee2..f0d8ba1 100644 --- a/pagermaid/modules/clock.py +++ b/pagermaid/modules/clock.py @@ -36,7 +36,7 @@ async def time(context): time_zone = await get_timezone(country) if not time_zone: if len(context.parameter) < 1: - await context.edit('出错了呜呜呜 ~ 配置文件填写错误') + await context.edit(lang('time_config')) return try: time_num, utc_num = int(context.parameter[0]), int(context.parameter[0]) diff --git a/pagermaid/modules/status.py b/pagermaid/modules/status.py index 6ee9159..555e75f 100644 --- a/pagermaid/modules/status.py +++ b/pagermaid/modules/status.py @@ -15,7 +15,7 @@ from telethon.tl.types import User, Chat, Channel from sys import platform from re import sub, findall from pathlib import Path -from pagermaid import log, config, redis_status +from pagermaid import log, config, redis_status, start_time from pagermaid.utils import execute, upload_attachment from pagermaid.listener import listener from pagermaid.utils import lang, alias_command @@ -83,14 +83,41 @@ async def tty(context): @listener(is_plugin=False, outgoing=True, command=alias_command("status"), description=lang('status_des')) async def status(context): + # database database = lang('status_online') if redis_status() else lang('status_offline') + # uptime https://gist.github.com/borgstrom/936ca741e885a1438c374824efb038b3 + time_units = ( + ('%m', 60 * 60 * 24 * 30), + ('%d', 60 * 60 * 24), + ('%H', 60 * 60), + ('%M', 60), + ('%S', 1) + ) + + async def human_time_duration(seconds): + parts = {} + for unit, div in time_units: + amount, seconds = divmod(int(seconds), div) + parts[unit] = str(amount) + try: + time_form = config['start_form'] + except (ValueError, KeyError): + time_form = "%m/%d %H:%M" + for key, value in parts.items(): + time_form = time_form.replace(key, value) + return time_form + + current_time = datetime.utcnow() + uptime_sec = (current_time - start_time).total_seconds() + uptime = await human_time_duration(int(uptime_sec)) text = (f"**{lang('status_hint')}** \n" f"{lang('status_name')}: `{uname().node}` \n" f"{lang('status_platform')}: `{platform}` \n" f"{lang('status_release')}: `{uname().release}` \n" f"{lang('status_python')}: `{python_version()}` \n" f"{lang('status_telethon')}: `{telethon_version.__version__}` \n" - f"{lang('status_db')}: `{database}`" + f"{lang('status_db')}: `{database}` \n" + f"{lang('status_uptime')}: `{uptime}`" ) await context.edit(text)