Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

feat(mqtt): filter the message list by direction

_Mr丶s ゛😁®fad22512cdae
diff --git a/apps/desktop/src/components/mqtt/MqttAdminConsole.vue b/apps/desktop/src/components/mqtt/MqttAdminConsole.vue
index 6bade005d8..57516b518a 100644
--- a/apps/desktop/src/components/mqtt/MqttAdminConsole.vue
+++ b/apps/desktop/src/components/mqtt/MqttAdminConsole.vue
@@ -2,7 +2,7 @@
import { ref, computed, onMounted, onUnmounted, watch, nextTick } from "vue";
import { useI18n } from "vue-i18n";
import { mqttGetBrokerInfo, mqttListTopics, mqttListSavedTopicConfigs, mqttGetMessages, mqttSubscribe, mqttUnsubscribe, mqttSaveTopicConfig, mqttDeleteTopicConfig, mqttClearMessages } from "@/lib/backend/api";
-import type { MqttBrokerInfo, MqttSavedTopic, MqttTopicNode, MqttMessage, MqttQoS } from "@/types/mqtt";
+import type { MqttBrokerInfo, MqttSavedTopic, MqttTopicNode, MqttMessage, MqttMessageDirection, MqttQoS } from "@/types/mqtt";
import { Button } from "@/components/ui/button";
import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
import TopicTreeNode from "./TopicTreeNode.vue";
@@ -25,6 +25,7 @@ interface MqttTabUiState {
displayEncoding?: PayloadEncoding;
topicSearch?: string;
payloadSearch?: string;
+ messageDirectionFilter?: MqttMessageDirectionFilter;
showSubscriptionDialog?: boolean;
formTopic?: string;
formQos?: MqttQoS;
@@ -39,6 +40,8 @@ interface MqttMessagePreview extends MqttMessage {
payloadElementId: string;
}
+type MqttMessageDirectionFilter = "all" | MqttMessageDirection;
+
const { initialState: restoredUiState, track: trackUiState } = useTabUiState<MqttTabUiState>({}, "MqttAdminConsole");
const brokerInfo = ref<MqttBrokerInfo | null>(null);
@@ -56,6 +59,7 @@ const messagesPaused = ref(restoredUiState.messagesPaused ?? false);
const displayEncoding = ref<PayloadEncoding>(restoredUiState.displayEncoding ?? "plaintext");
const topicSearch = ref(restoredUiState.topicSearch ?? "");
const payloadSearch = ref(restoredUiState.payloadSearch ?? "");
+const messageDirectionFilter = ref<MqttMessageDirectionFilter>(restoredUiState.messageDirectionFilter ?? "all");
const showSubscriptionDialog = ref(restoredUiState.showSubscriptionDialog ?? false);
const savingSubscription = ref(false);
const formTopic = ref(restoredUiState.formTopic ?? "");
@@ -73,6 +77,7 @@ trackUiState(() => ({
displayEncoding: displayEncoding.value,
topicSearch: topicSearch.value,
payloadSearch: payloadSearch.value,
+ messageDirectionFilter: messageDirectionFilter.value,
showSubscriptionDialog: showSubscriptionDialog.value,
formTopic: formTopic.value,
formQos: formQos.value,
@@ -139,10 +144,15 @@ const messagePreviews = computed<MqttMessagePreview[]>(() => {
});
});
+const directionFilteredMessages = computed(() => {
+ if (messageDirectionFilter.value === "all") return messagePreviews.value;
+ return messagePreviews.value.filter((msg) => (msg.direction ?? "received") === messageDirectionFilter.value);
+});
+
const filteredMessages = computed(() => {
const query = payloadSearch.value.trim().toLowerCase();
- if (!query) return messagePreviews.value;
- return messagePreviews.value.filter((msg) => messagePayloadText(msg).toLowerCase().includes(query));
+ if (!query) return directionFilteredMessages.value;
+ return directionFilteredMessages.value.filter((msg) => messagePayloadText(msg).toLowerCase().includes(query));
});
async function refreshData() {
@@ -425,6 +435,38 @@ onUnmounted(stopPolling);
<span v-if="selectedTopic" class="min-w-0 truncate">{{ t("connection.mqttMessagesForTopic", { topic: selectedTopic }) }}</span>
<span v-else class="min-w-0 truncate">{{ t("connection.mqttAllMessages") }}</span>
<div class="flex min-w-0 flex-1 flex-wrap items-center justify-end gap-2">
+ <div class="inline-flex h-6 shrink-0 items-center rounded-md border p-0.5" role="group" :aria-label="t('connection.mqttMessageDirectionFilterLabel')">
+ <button
+ type="button"
+ data-testid="mqtt-direction-filter-all"
+ class="h-5 rounded px-1.5 text-[10px] font-normal transition-colors"
+ :class="messageDirectionFilter === 'all' ? 'bg-accent text-accent-foreground' : 'text-muted-foreground hover:text-foreground'"
+ :aria-pressed="messageDirectionFilter === 'all'"
+ @click="messageDirectionFilter = 'all'"
+ >
+ {{ t("connection.mqttDirectionFilterAll") }}
+ </button>
+ <button
+ type="button"
+ data-testid="mqtt-direction-filter-received"
+ class="h-5 rounded px-1.5 text-[10px] font-normal transition-colors"
+ :class="messageDirectionFilter === 'received' ? 'bg-accent text-accent-foreground' : 'text-muted-foreground hover:text-foreground'"
+ :aria-pressed="messageDirectionFilter === 'received'"
+ @click="messageDirectionFilter = 'received'"
+ >
+ {{ t("connection.mqttDirectionFilterReceived") }}
+ </button>
+ <button
+ type="button"
+ data-testid="mqtt-direction-filter-sent"
+ class="h-5 rounded px-1.5 text-[10px] font-normal transition-colors"
+ :class="messageDirectionFilter === 'sent' ? 'bg-accent text-accent-foreground' : 'text-muted-foreground hover:text-foreground'"
+ :aria-pressed="messageDirectionFilter === 'sent'"
+ @click="messageDirectionFilter = 'sent'"
+ >
+ {{ t("connection.mqttDirectionFilterSent") }}
+ </button>
+ </div>
<input
v-model="payloadSearch"
data-testid="mqtt-payload-search"
@@ -433,7 +475,7 @@ onUnmounted(stopPolling);
:aria-label="t('connection.mqttSearchPayloadPlaceholder')"
/>
<span data-testid="mqtt-message-count" class="font-normal" aria-live="polite">
- <template v-if="payloadSearch.trim()">{{ filteredMessages.length }} / {{ messages.length }}</template>
+ <template v-if="payloadSearch.trim() || messageDirectionFilter !== 'all'">{{ filteredMessages.length }} / {{ messages.length }}</template>
<template v-else>{{ messages.length }}</template>
</span>
<select v-model="displayEncoding" class="h-6 rounded border bg-transparent px-1.5 text-[11px] text-muted-foreground outline-none">
diff --git a/apps/desktop/src/components/mqtt/__tests__/MqttAdminConsoleDirectionFilter.spec.ts b/apps/desktop/src/components/mqtt/__tests__/MqttAdminConsoleDirectionFilter.spec.ts
new file mode 100644
index 0000000000..2e70a8e739
--- /dev/null
+++ b/apps/desktop/src/components/mqtt/__tests__/MqttAdminConsoleDirectionFilter.spec.ts
@@ -0,0 +1,109 @@
+// @vitest-environment happy-dom
+
+import { createApp, nextTick, type App } from "vue";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import i18n, { loadLocaleMessages } from "@/i18n";
+import MqttAdminConsole from "@/components/mqtt/MqttAdminConsole.vue";
+import type { MqttBrokerInfo, MqttMessage } from "@/types/mqtt";
+
+const { mqttGetMessagesMock } = vi.hoisted(() => ({
+ mqttGetMessagesMock: vi.fn(),
+}));
+
+vi.mock("@/lib/backend/api", () => ({
+ mqttGetBrokerInfo: vi.fn(
+ async (): Promise<MqttBrokerInfo> => ({
+ brokerUrl: "mqtt://localhost:1883",
+ clientId: "dbx-test",
+ connected: true,
+ protocolVersion: "5.0",
+ subscriptionCount: 1,
+ }),
+ ),
+ mqttListTopics: vi.fn(async () => [["device/status", "atmostonce"]]),
+ mqttListSavedTopicConfigs: vi.fn(async () => [{ topic: "device/status", qos: "atmostonce", enabled: true, noLocal: false }]),
+ mqttGetMessages: mqttGetMessagesMock,
+ mqttSubscribe: vi.fn(),
+ mqttUnsubscribe: vi.fn(),
+ mqttSaveTopicConfig: vi.fn(),
+ mqttDeleteTopicConfig: vi.fn(),
+ mqttClearMessages: vi.fn(),
+}));
+
+const mountedApps: App[] = [];
+
+function message(index: number, direction: "received" | "sent"): MqttMessage {
+ return {
+ topic: "device/status",
+ payloadBase64: btoa(`payload-${index}`),
+ payloadText: `payload-${index}`,
+ qos: 0,
+ retain: false,
+ receivedAtMs: Date.now() + index,
+ direction,
+ };
+}
+
+async function settleConsole() {
+ await Promise.resolve();
+ await Promise.resolve();
+ await Promise.resolve();
+ await nextTick();
+}
+
+async function mountConsole() {
+ const container = document.createElement("div");
+ document.body.append(container);
+ const app = createApp(MqttAdminConsole, {
+ connectionId: "mqtt-connection-1",
+ initialTopic: "device/status",
+ });
+ mountedApps.push(app);
+ app.use(i18n);
+ app.mount(container);
+ await settleConsole();
+ return container;
+}
+
+async function selectDirection(container: HTMLElement, direction: "all" | "received" | "sent") {
+ const button = container.querySelector<HTMLButtonElement>(`[data-testid="mqtt-direction-filter-${direction}"]`);
+ expect(button).toBeTruthy();
+ button!.dispatchEvent(new MouseEvent("click", { bubbles: true }));
+ await settleConsole();
+}
+
+beforeEach(async () => {
+ mqttGetMessagesMock.mockReset().mockResolvedValue([message(0, "received"), message(1, "sent")]);
+ await loadLocaleMessages("zh-CN");
+ i18n.global.locale.value = "zh-CN";
+ vi.useFakeTimers();
+});
+
+afterEach(() => {
+ vi.useRealTimers();
+ for (const app of mountedApps.splice(0)) app.unmount();
+ document.body.innerHTML = "";
+});
+
+describe("MQTT 控制台按消息方向筛选 (issue #11848)", () => {
+ it("在全部、已接收和已发送之间切换消息列表", async () => {
+ const container = await mountConsole();
+
+ expect(container.querySelectorAll('[data-testid="mqtt-message"]')).toHaveLength(2);
+ expect(container.querySelector('[data-testid="mqtt-message-count"]')?.textContent).toBe("2");
+
+ await selectDirection(container, "received");
+ expect(container.querySelectorAll('[data-testid="mqtt-message"]')).toHaveLength(1);
+ expect(container.textContent).toContain("payload-0");
+ expect(container.textContent).not.toContain("payload-1");
+ expect(container.querySelector('[data-testid="mqtt-message-count"]')?.textContent).toBe("1 / 2");
+
+ await selectDirection(container, "sent");
+ expect(container.querySelectorAll('[data-testid="mqtt-message"]')).toHaveLength(1);
+ expect(container.textContent).toContain("payload-1");
+ expect(container.textContent).not.toContain("payload-0");
+
+ await selectDirection(container, "all");
+ expect(container.querySelectorAll('[data-testid="mqtt-message"]')).toHaveLength(2);
+ });
+});
diff --git a/apps/desktop/src/i18n/__tests__/featureMessages.spec.ts b/apps/desktop/src/i18n/__tests__/featureMessages.spec.ts
index b186bf0083..b21f19641e 100644
--- a/apps/desktop/src/i18n/__tests__/featureMessages.spec.ts
+++ b/apps/desktop/src/i18n/__tests__/featureMessages.spec.ts
@@ -104,6 +104,12 @@ const FEATURE_MESSAGES: FeatureMessages[] = [
},
{ feature: "Consul workspace", keys: ["consul.ui.*", "consul.tools.*"] },
{ feature: "MQTT max packet size", keys: ["connection.mqttMaxPacketSize"] },
+ {
+ feature: "MQTT message direction filter (#11848)",
+ keys: under("connection", ["mqttMessageDirectionFilterLabel", "mqttDirectionFilterAll", "mqttDirectionFilterReceived", "mqttDirectionFilterSent"]),
+ locales: ["en", "az", "es", "id", "it", "ja", "ko", "pt-BR", "ru", "tr", "zh-CN", "zh-TW"],
+ translated: true,
+ },
{ feature: "sidebar search skip hint", keys: ["sidebar.searchConnectionSkipped"] },
{ feature: "PostgreSQL legacy TLS", keys: ["connection.postgresLegacyTls", "connection.postgresLegacyTlsHint"] },
{ feature: "settings search sections", keys: ["settings.syncWebDavWebDescription", "settings.performanceSection"] },
diff --git a/apps/desktop/src/i18n/locales/az.ts b/apps/desktop/src/i18n/locales/az.ts
index 0cb2fcbd7a..a0f2e4524c 100644
--- a/apps/desktop/src/i18n/locales/az.ts
+++ b/apps/desktop/src/i18n/locales/az.ts
@@ -1014,6 +1014,10 @@ export default withEnglishFallback({
mqttAllMessages: "Mesajlar (bütün mövzular)",
mqttSent: "Göndərilib",
mqttReceived: "Qəbul edilib",
+ mqttMessageDirectionFilterLabel: "Mesaj istiqamətinə görə süzgəclə",
+ mqttDirectionFilterAll: "Hamısı",
+ mqttDirectionFilterReceived: "Qəbul edilən",
+ mqttDirectionFilterSent: "Göndərilən",
mqttRetained: "Saxlanılıb",
mqttTopic: "Mövzu",
mqttTopicExample: "məsələn, sensors/temperature",
diff --git a/apps/desktop/src/i18n/locales/en.ts b/apps/desktop/src/i18n/locales/en.ts
index 1a3379b68d..9b9bdfa6e6 100644
--- a/apps/desktop/src/i18n/locales/en.ts
+++ b/apps/desktop/src/i18n/locales/en.ts
@@ -1377,6 +1377,10 @@ export default {
mqttAllMessages: "Messages (all topics)",
mqttSent: "Sent",
mqttReceived: "Received",
+ mqttMessageDirectionFilterLabel: "Filter messages by direction",
+ mqttDirectionFilterAll: "All",
+ mqttDirectionFilterReceived: "Received",
+ mqttDirectionFilterSent: "Sent",
mqttRetained: "Retained",
mqttTopic: "Topic",
mqttTopicExample: "e.g. sensors/temperature",
diff --git a/apps/desktop/src/i18n/locales/es.ts b/apps/desktop/src/i18n/locales/es.ts
index 0283dd5327..db28b6e75f 100644
--- a/apps/desktop/src/i18n/locales/es.ts
+++ b/apps/desktop/src/i18n/locales/es.ts
@@ -1665,6 +1665,10 @@ export default withEnglishFallback({
mqttAllMessages: "Mensajes (todos los Topics)",
mqttSent: "Enviado",
mqttReceived: "Recibido",
+ mqttMessageDirectionFilterLabel: "Filtrar mensajes por dirección",
+ mqttDirectionFilterAll: "Todos",
+ mqttDirectionFilterReceived: "Recibidos",
+ mqttDirectionFilterSent: "Enviados",
mqttRetained: "Retenido",
mqttTopic: "Topic",
mqttTopicExample: "p. ej. sensors/temperature",
diff --git a/apps/desktop/src/i18n/locales/id.ts b/apps/desktop/src/i18n/locales/id.ts
index b959628334..283e696401 100644
--- a/apps/desktop/src/i18n/locales/id.ts
+++ b/apps/desktop/src/i18n/locales/id.ts
@@ -1268,6 +1268,10 @@ export default withEnglishFallback({
mqttAllMessages: "Pesan (semua topik)",
mqttSent: "Terkirim",
mqttReceived: "Diterima",
+ mqttMessageDirectionFilterLabel: "Filter berdasarkan arah pesan",
+ mqttDirectionFilterAll: "Semua",
+ mqttDirectionFilterReceived: "Diterima",
+ mqttDirectionFilterSent: "Terkirim",
mqttRetained: "Ditahan",
mqttTopic: "Topik",
mqttTopicExample: "mis. sensors/temperature",
diff --git a/apps/desktop/src/i18n/locales/it.ts b/apps/desktop/src/i18n/locales/it.ts
index 4249b84ce6..24df32330d 100644
--- a/apps/desktop/src/i18n/locales/it.ts
+++ b/apps/desktop/src/i18n/locales/it.ts
@@ -1574,6 +1574,10 @@ export default withEnglishFallback({
mqttAllMessages: "Messaggi (tutti i Topic)",
mqttSent: "Inviato",
mqttReceived: "Ricevuto",
+ mqttMessageDirectionFilterLabel: "Filtra i messaggi per direzione",
+ mqttDirectionFilterAll: "Tutti",
+ mqttDirectionFilterReceived: "Ricevuti",
+ mqttDirectionFilterSent: "Inviati",
mqttRetained: "Mantenuto",
mqttTopic: "Topic",
mqttTopicExample: "ad es. sensors/temperature",
diff --git a/apps/desktop/src/i18n/locales/ja.ts b/apps/desktop/src/i18n/locales/ja.ts
index aba97ddb99..f507805417 100644
--- a/apps/desktop/src/i18n/locales/ja.ts
+++ b/apps/desktop/src/i18n/locales/ja.ts
@@ -1574,6 +1574,10 @@ export default withEnglishFallback({
mqttAllMessages: "メッセージ(すべてのトピック)",
mqttSent: "送信",
mqttReceived: "受信",
+ mqttMessageDirectionFilterLabel: "メッセージの方向で絞り込む",
+ mqttDirectionFilterAll: "すべて",
+ mqttDirectionFilterReceived: "受信",
+ mqttDirectionFilterSent: "送信",
mqttRetained: "保持",
mqttTopic: "トピック",
mqttTopicExample: "例: sensors/temperature",
diff --git a/apps/desktop/src/i18n/locales/ko.ts b/apps/desktop/src/i18n/locales/ko.ts
index d1af6342cf..d8c1173ef1 100644
--- a/apps/desktop/src/i18n/locales/ko.ts
+++ b/apps/desktop/src/i18n/locales/ko.ts
@@ -1575,6 +1575,10 @@ export default withEnglishFallback({
pluginSshKeyEncrypted: "암호화됨",
objectCacheRefreshFailed: "개체 캐시 새로 고침 실패: {message}",
mqttMaxPacketSize: "최대 패킷 크기(바이트)",
+ mqttMessageDirectionFilterLabel: "메시지 방향으로 필터링",
+ mqttDirectionFilterAll: "전체",
+ mqttDirectionFilterReceived: "수신됨",
+ mqttDirectionFilterSent: "발신됨",
},
editor: {
duckdbDraining: "이전 DuckDB 쿼리가 아직 중지 중입니다. 잠시 후 다시 시도해 주세요.",
diff --git a/apps/desktop/src/i18n/locales/pt-BR.ts b/apps/desktop/src/i18n/locales/pt-BR.ts
index a6e9154e41..0334a3f05d 100644
--- a/apps/desktop/src/i18n/locales/pt-BR.ts
+++ b/apps/desktop/src/i18n/locales/pt-BR.ts
@@ -1575,6 +1575,10 @@ export default withEnglishFallback({
mqttAllMessages: "Mensagens (todos os Topics)",
mqttSent: "Enviado",
mqttReceived: "Recebido",
+ mqttMessageDirectionFilterLabel: "Filtrar mensagens por direção",
+ mqttDirectionFilterAll: "Todas",
+ mqttDirectionFilterReceived: "Recebidas",
+ mqttDirectionFilterSent: "Enviadas",
mqttRetained: "Retido",
mqttTopic: "Topic",
mqttTopicExample: "ex.: sensors/temperature",
diff --git a/apps/desktop/src/i18n/locales/ru.ts b/apps/desktop/src/i18n/locales/ru.ts
index 2690146274..a61a34193d 100644
--- a/apps/desktop/src/i18n/locales/ru.ts
+++ b/apps/desktop/src/i18n/locales/ru.ts
@@ -1287,6 +1287,10 @@ export default withEnglishFallback({
mqttAllMessages: "Сообщения (все топики)",
mqttSent: "Отправленные",
mqttReceived: "Полученные",
+ mqttMessageDirectionFilterLabel: "Фильтр по направлению сообщения",
+ mqttDirectionFilterAll: "Все",
+ mqttDirectionFilterReceived: "Полученные",
+ mqttDirectionFilterSent: "Отправленные",
mqttRetained: "Сохранённые",
mqttTopic: "Топик",
mqttTopicExample: "например, sensors/temperature",
diff --git a/apps/desktop/src/i18n/locales/tr.ts b/apps/desktop/src/i18n/locales/tr.ts
index d72ffcce81..14431fda7e 100644
--- a/apps/desktop/src/i18n/locales/tr.ts
+++ b/apps/desktop/src/i18n/locales/tr.ts
@@ -1014,6 +1014,10 @@ export default withEnglishFallback({
mqttAllMessages: "Mesajlar (tüm konular)",
mqttSent: "Gönderildi",
mqttReceived: "Alındı",
+ mqttMessageDirectionFilterLabel: "Mesaj yönüne göre filtrele",
+ mqttDirectionFilterAll: "Tümü",
+ mqttDirectionFilterReceived: "Alınan",
+ mqttDirectionFilterSent: "Gönderilen",
mqttRetained: "Saklanan",
mqttTopic: "Konu",
mqttTopicExample: "örn. sensors/temperature",
diff --git a/apps/desktop/src/i18n/locales/zh-CN.ts b/apps/desktop/src/i18n/locales/zh-CN.ts
index 367036df8c..4bc1518e92 100644
--- a/apps/desktop/src/i18n/locales/zh-CN.ts
+++ b/apps/desktop/src/i18n/locales/zh-CN.ts
@@ -1313,6 +1313,10 @@ export default withEnglishFallback({
mqttAllMessages: "消息(所有 Topic)",
mqttSent: "发送",
mqttReceived: "接收",
+ mqttMessageDirectionFilterLabel: "按消息方向筛选",
+ mqttDirectionFilterAll: "全部",
+ mqttDirectionFilterReceived: "已接收",
+ mqttDirectionFilterSent: "已发送",
mqttRetained: "保留",
mqttTopic: "Topic",
mqttTopicExample: "例如 sensors/temperature",
diff --git a/apps/desktop/src/i18n/locales/zh-TW.ts b/apps/desktop/src/i18n/locales/zh-TW.ts
index 6e09a76377..6883b01cd4 100644
--- a/apps/desktop/src/i18n/locales/zh-TW.ts
+++ b/apps/desktop/src/i18n/locales/zh-TW.ts
@@ -1588,6 +1588,10 @@ export default withEnglishFallback({
mqttAllMessages: "訊息(所有 Topic)",
mqttSent: "傳送",
mqttReceived: "接收",
+ mqttMessageDirectionFilterLabel: "依訊息方向篩選",
+ mqttDirectionFilterAll: "全部",
+ mqttDirectionFilterReceived: "已接收",
+ mqttDirectionFilterSent: "已傳送",
mqttRetained: "保留",
mqttTopic: "Topic",
mqttTopicExample: "例如 sensors/temperature",