From c4a709491d2ed635d9371ae9ed09d22309c207e1 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sat, 12 Nov 2022 13:42:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BA=20loginReport=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=BA=86=E7=89=88=E6=9C=AC=E5=8F=B7=EF=BC=8C=E9=87=8D=E6=9E=84?= =?UTF-8?q?=20version/runtime=20=E7=9A=84=E5=86=85=E9=83=A8=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E9=93=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改了 system 的显示方式将 os名称/版本/架构 移为了一行 - 将 version/runtime 的执行体移动到了 MornyInformations 当中 - MornyInformations 中对一些复杂的 version/runtime 部件为其独立出了一个方法 --- gradle.properties | 2 +- .../cono/morny/bot/command/MornyCommands.java | 100 +----------- .../morny/bot/command/MornyInformations.java | 147 ++++++++++++++++-- .../cono/morny/daemon/MornyReport.java | 9 +- 4 files changed, 144 insertions(+), 114 deletions(-) diff --git a/gradle.properties b/gradle.properties index 50b2c53..c087a4f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,7 +5,7 @@ MORNY_ARCHIVE_NAME = morny-coeur MORNY_CODE_STORE = https://github.com/Eyre-S/Coeur-Morny-Cono MORNY_COMMIT_PATH = https://github.com/Eyre-S/Coeur-Morny-Cono/commit/%s -VERSION = 1.0.0-beta1 +VERSION = 1.0.0-beta2 USE_DELTA = false VERSION_DELTA = diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java index e50db1b..5e14712 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java @@ -1,8 +1,6 @@ package cc.sukazyo.cono.morny.bot.command; -import cc.sukazyo.cono.morny.BuildConfig; import cc.sukazyo.cono.morny.MornyCoeur; -import cc.sukazyo.cono.morny.MornySystem; import cc.sukazyo.cono.morny.bot.event.OnUniMeowTrigger; import cc.sukazyo.cono.morny.daemon.MornyReport; import cc.sukazyo.cono.morny.data.MornyJrrp; @@ -21,13 +19,9 @@ import org.jetbrains.annotations.NotNull; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.net.InetAddress; -import java.net.UnknownHostException; import java.util.*; import static cc.sukazyo.cono.morny.Log.logger; -import static cc.sukazyo.cono.morny.util.CommonFormat.formatDate; -import static cc.sukazyo.cono.morny.util.CommonFormat.formatDuration; import static cc.sukazyo.cono.morny.util.tgapi.formatting.MsgEscape.escapeHtml; public class MornyCommands { @@ -234,33 +228,7 @@ public class MornyCommands { @Nullable @Override public String[] getAliases () { return null; } @Nonnull @Deprecated public String getParamRule () { return ""; } @Nonnull @Deprecated public String getDescription () { return "检查 Bot 版本信息"; } - @Override public void execute (@Nonnull InputCommand command, @Nonnull Update event) { onCommandVersionExec(event); } - } - public static void onCommandVersionExec (@Nonnull Update event) { - MornyCoeur.extra().exec(new SendMessage( - event.message().chat().id(), - String.format( - """ - version: - - Morny %s - - %s%s%s - core md5_hash: - - %s - coding timestamp: - - %d - - %s [UTC]""", - escapeHtml(MornySystem.CODENAME.toUpperCase()), - escapeHtml(MornySystem.VERSION_BASE), - MornySystem.isUseDelta() ? String.format("-δ%s", escapeHtml(Objects.requireNonNull(MornySystem.VERSION_DELTA))) : "", - MornySystem.isGitBuild()?"\n- git "+ (MornySystem.currentCodePath()==null ? - String.format("%s", BuildConfig.COMMIT.substring(0, 8)) : - String.format("%s", MornySystem.currentCodePath(), BuildConfig.COMMIT.substring(0, 8)) - ) + (MornySystem.isCleanBuild() ? "" : ".δ") : "", - escapeHtml(MornySystem.getJarMd5()), - BuildConfig.CODE_TIMESTAMP, - escapeHtml(formatDate(BuildConfig.CODE_TIMESTAMP, 0)) - ) - ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); + @Override public void execute (@Nonnull InputCommand command, @Nonnull Update event) { MornyInformations.echoVersion(event); } } private static class MornyRuntime implements ISimpleCommand { @@ -268,71 +236,7 @@ public class MornyCommands { @Nullable @Override public String[] getAliases () { return null; } @Nonnull @Deprecated public String getParamRule () { return ""; } @Nonnull @Deprecated public String getDescription () { return "获取 Bot 运行时信息(包括版本号)"; } - @Override public void execute (@Nonnull InputCommand command, @Nonnull Update event) { onCommandRuntimeExec(event); } - } - /** - * @since 0.4.1.2 - */ - public static void onCommandRuntimeExec (@Nonnull Update event) { - String hostname; - try { - hostname = InetAddress.getLocalHost().getHostName(); - } catch (UnknownHostException e) { - hostname = ""; - } - MornyCoeur.extra().exec(new SendMessage( - event.message().chat().id(), - String.format(""" - system: - - %s - - %s - - %s - java runtime: - - %s - - %s - vm memory: - - %d / %d MB - - %d cores - coeur version: - - %s%s%s*%s - - %s - - %s [UTC] - - [%d] - continuous: - - %s - - [%d] - - %s [UTC] - - [%d]""", - // system - escapeHtml(hostname), - escapeHtml(String.format("%s (%s)", System.getProperty("os.name"), System.getProperty("os.arch"))), - escapeHtml(System.getProperty("os.version")), - // java - escapeHtml(System.getProperty("java.vm.vendor")+"."+System.getProperty("java.vm.name")), - escapeHtml(System.getProperty("java.vm.version")), - // memory - Runtime.getRuntime().totalMemory() / 1024 / 1024, - Runtime.getRuntime().maxMemory() / 1024 / 1024, - Runtime.getRuntime().availableProcessors(), - // version - escapeHtml(MornySystem.VERSION_BASE), - MornySystem.isUseDelta() ? String.format("-δ%s", escapeHtml(Objects.requireNonNull(MornySystem.VERSION_DELTA))) : "", - MornySystem.isGitBuild()?String.format("-git.%s%s", MornySystem.currentCodePath()==null ? ( - String.format("%s", BuildConfig.COMMIT.substring(0, 8)) - ) : ( - String.format("%s", MornySystem.currentCodePath(), BuildConfig.COMMIT.substring(0, 8)) - ), MornySystem.isCleanBuild()?"":".δ"):"", - escapeHtml(MornySystem.CODENAME.toUpperCase()), - escapeHtml(MornySystem.getJarMd5()), - escapeHtml(formatDate(BuildConfig.CODE_TIMESTAMP, 0)), - BuildConfig.CODE_TIMESTAMP, - // continuous - escapeHtml(formatDuration(System.currentTimeMillis() - MornyCoeur.coeurStartTimestamp)), - System.currentTimeMillis() - MornyCoeur.coeurStartTimestamp, - escapeHtml(formatDate(MornyCoeur.coeurStartTimestamp, 0)), - MornyCoeur.coeurStartTimestamp - ) - ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); + @Override public void execute (@Nonnull InputCommand command, @Nonnull Update event) { MornyInformations.echoRuntime(event); } } private static class Jrrp implements ITelegramCommand { diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyInformations.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyInformations.java index 5558f6e..e9a6ad2 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyInformations.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyInformations.java @@ -1,14 +1,25 @@ package cc.sukazyo.cono.morny.bot.command; +import cc.sukazyo.cono.morny.BuildConfig; import cc.sukazyo.cono.morny.MornyCoeur; +import cc.sukazyo.cono.morny.MornySystem; import cc.sukazyo.cono.morny.data.TelegramStickers; import cc.sukazyo.cono.morny.util.tgapi.ExtraAction; import cc.sukazyo.cono.morny.util.tgapi.InputCommand; import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.model.request.ParseMode; +import com.pengrad.telegrambot.request.SendMessage; import com.pengrad.telegrambot.request.SendSticker; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.Objects; + +import static cc.sukazyo.cono.morny.util.CommonFormat.formatDate; +import static cc.sukazyo.cono.morny.util.CommonFormat.formatDuration; +import static cc.sukazyo.cono.morny.util.tgapi.formatting.MsgEscape.escapeHtml; public class MornyInformations implements ITelegramCommand { @@ -26,7 +37,7 @@ public class MornyInformations implements ITelegramCommand { public void execute (@Nonnull InputCommand command, @Nonnull Update event) { if (!command.hasArgs()) { - MornyCommands.onCommandRuntimeExec(event); + echoRuntime(event); return; } @@ -35,9 +46,9 @@ public class MornyInformations implements ITelegramCommand { if (action.startsWith(SUB_STICKER)) { echoStickers(command, event); } else if (action.equals(SUB_RUNTIME)) { - echoRuntime(command, event); + echoRuntime(event); } else if (action.equals(SUB_VERSION) || action.equals(SUB_VERSION_2)) { - echoVersion(command, event); + echoVersion(event); } else { echo404(event); } @@ -85,20 +96,134 @@ public class MornyInformations implements ITelegramCommand { * /info 子命令 {@value #SUB_RUNTIME} * @since 1.0.0-alpha4 */ - public static void echoRuntime (@Nonnull InputCommand command, @Nonnull Update event) { - if (command.getArgs().length == 1) - MornyCommands.onCommandRuntimeExec(event); - else echo404(event); + public static void echoRuntime (@Nonnull Update event) { + MornyCoeur.extra().exec(new SendMessage( + event.message().chat().id(), + String.format(""" + system: + - %s + - %s (%s) %s + java runtime: + - %s + - %s + vm memory: + - %d / %d MB + - %d cores + coeur version: + - %s + - %s + - %s [UTC] + - [%d] + continuous: + - %s + - [%d] + - %s [UTC] + - [%d]""", + // system + escapeHtml(getRuntimeHostName()==null ? "" : getRuntimeHostName()), + escapeHtml(System.getProperty("os.name")), + escapeHtml(System.getProperty("os.arch")), + escapeHtml(System.getProperty("os.version")), + // java + escapeHtml(System.getProperty("java.vm.vendor")+"."+System.getProperty("java.vm.name")), + escapeHtml(System.getProperty("java.vm.version")), + // memory + Runtime.getRuntime().totalMemory() / 1024 / 1024, + Runtime.getRuntime().maxMemory() / 1024 / 1024, + Runtime.getRuntime().availableProcessors(), + // version + getVersionAllFullTagHtml(), + escapeHtml(MornySystem.getJarMd5()), + escapeHtml(formatDate(BuildConfig.CODE_TIMESTAMP, 0)), + BuildConfig.CODE_TIMESTAMP, + // continuous + escapeHtml(formatDuration(System.currentTimeMillis() - MornyCoeur.coeurStartTimestamp)), + System.currentTimeMillis() - MornyCoeur.coeurStartTimestamp, + escapeHtml(formatDate(MornyCoeur.coeurStartTimestamp, 0)), + MornyCoeur.coeurStartTimestamp + ) + ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); } /** * /info 子命令 {@value #SUB_VERSION} * @since 1.0.0-alpha4 */ - public static void echoVersion (@Nonnull InputCommand command, @Nonnull Update event) { - if (command.getArgs().length == 1) - MornyCommands.onCommandVersionExec(event); - else echo404(event); + public static void echoVersion (@Nonnull Update event) { + MornyCoeur.extra().exec(new SendMessage( + event.message().chat().id(), + String.format( + """ + version: + - Morny %s + - %s%s%s + core md5_hash: + - %s + coding timestamp: + - %d + - %s [UTC]""", + escapeHtml(MornySystem.CODENAME.toUpperCase()), + escapeHtml(MornySystem.VERSION_BASE), + MornySystem.isUseDelta() ? String.format("-δ%s", escapeHtml(Objects.requireNonNull(MornySystem.VERSION_DELTA))) : "", + MornySystem.isGitBuild() ? "\n- git "+getVersionGitTagHtml() : "", + escapeHtml(MornySystem.getJarMd5()), + BuildConfig.CODE_TIMESTAMP, + escapeHtml(formatDate(BuildConfig.CODE_TIMESTAMP, 0)) + ) + ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); + } + + /** + * 取得 {@link MornySystem} 的 git commit 相关版本信息的 HTML 格式化标签. + * @return 格式类似于 {@code 28e8c82a.δ} 的以 HTML 方式格式化的版本号组件。 + * 其中 {@code .δ} 对应着 {@link MornySystem#isCleanBuild}; + * commit tag 字段如果支援 {@link MornySystem#currentCodePath} 则会以链接形式解析,否则则为 code 格式 + * 为了对 telegram api html 格式兼容所以不支援嵌套链接与code标签。 + * 如果 {@link MornySystem#isGitBuild} 为 {@link false},则方法会返回 {@link ""} + * @since 1.0.0-beta2 + */ + @Nonnull + public static String getVersionGitTagHtml () { + if (!MornySystem.isGitBuild()) return ""; + final StringBuilder g = new StringBuilder(); + final String cp = MornySystem.currentCodePath(); + if (cp == null) g.append(String.format("%s", BuildConfig.COMMIT.substring(0, 8))); + else g.append(String.format("%s", MornySystem.currentCodePath(), BuildConfig.COMMIT.substring(0, 8))); + if (!MornySystem.isCleanBuild()) g.append(".δ"); + return g.toString(); + } + + /** + * 取得完整 Morny 版本的 HTML 格式化标签. + *

+ * 相比于 {@link MornySystem#VERSION_FULL},这个版本号还包含了 {@link MornySystem#CODENAME 版本 codename}。 + * 各个部分也被以 HTML 的格式进行了格式化以可以更好的在富文本中插入使用. + * @return 基于 HTML 标签进行了格式化了的类似于 + * {@link MornySystem#VERSION_BASE 5.38.2-alpha1}{@link MornySystem#isUseDelta() -δ}{@link MornySystem#VERSION_DELTA tt}{@link MornySystem#isGitBuild() +git.}{@link #getVersionGitTagHtml() 28e8c82a.δ}*{@link MornySystem#CODENAME TOKYO} + * 的版本号。 + * @since 1.0.0-beta2 + */ + @Nonnull + public static String getVersionAllFullTagHtml () { + final StringBuilder v = new StringBuilder(); + v.append("").append(MornySystem.VERSION_BASE).append(""); + if (MornySystem.isUseDelta()) v.append("-δ").append(MornySystem.VERSION_DELTA).append(""); + if (MornySystem.isGitBuild()) v.append("+git.").append(getVersionGitTagHtml()); + v.append("*").append(MornySystem.CODENAME.toUpperCase()).append(""); + return v.toString(); + } + + /** + * 获取 coeur 运行时的宿主机的主机名 + * @return coeur 宿主机主机名,或者 {@link null} 表示获取失败 + */ + @Nullable + public static String getRuntimeHostName () { + try { + return InetAddress.getLocalHost().getHostName(); + } catch (UnknownHostException e) { + return null; + } } private static void echo404 (@Nonnull Update event) { diff --git a/src/main/java/cc/sukazyo/cono/morny/daemon/MornyReport.java b/src/main/java/cc/sukazyo/cono/morny/daemon/MornyReport.java index c79b223..b4f7ae5 100644 --- a/src/main/java/cc/sukazyo/cono/morny/daemon/MornyReport.java +++ b/src/main/java/cc/sukazyo/cono/morny/daemon/MornyReport.java @@ -1,8 +1,7 @@ package cc.sukazyo.cono.morny.daemon; -import cc.sukazyo.cono.morny.Log; -import cc.sukazyo.cono.morny.MornyCoeur; -import cc.sukazyo.cono.morny.MornyConfig; +import cc.sukazyo.cono.morny.*; +import cc.sukazyo.cono.morny.bot.command.MornyInformations; import cc.sukazyo.cono.morny.util.tgapi.event.EventRuntimeException; import cc.sukazyo.cono.morny.util.tgapi.formatting.TGToString; import com.google.gson.GsonBuilder; @@ -74,16 +73,18 @@ public class MornyReport { * morny 登陆时的报告发送,包含已登录的账号 id 以及启动配置。 * @since 1.0.0-alpha6 */ - public static void onMornyLogIn () { + static void onMornyLogIn () { executeReport(new SendMessage( MornyCoeur.config().reportToChat, String.format(""" ▌Morny Logged in + -v %s as user @%s as config fields: %s """, + MornyInformations.getVersionAllFullTagHtml(), MornyCoeur.getUsername(), sectionConfigFields(MornyCoeur.config()) )