From cc436ad86feae844317178779680ff1cafcd1dcd Mon Sep 17 00:00:00 2001 From: Lykin <137850705+tiny-craft@users.noreply.github.com> Date: Thu, 16 Nov 2023 00:21:54 +0800 Subject: [PATCH] perf: support json format viewing for stream values #65 fix: the 'reset' parameter in 'GetKeyDetail' isn't taking effect --- backend/services/browser_service.go | 46 ++++++++++++++----- backend/types/redis_wrapper.go | 5 +- .../content_value/ContentValueStream.vue | 35 +++++--------- 3 files changed, 49 insertions(+), 37 deletions(-) diff --git a/backend/services/browser_service.go b/backend/services/browser_service.go index ae27961..9a17a28 100644 --- a/backend/services/browser_service.go +++ b/backend/services/browser_service.go @@ -2,6 +2,7 @@ package services import ( "context" + "encoding/json" "errors" "fmt" "github.com/redis/go-redis/v9" @@ -647,7 +648,11 @@ func (b *browserService) GetKeyDetail(param types.KeyDetailParam) (resp types.JS cursor = 0 loadVal, subErr = client.LRange(ctx, key, 0, -1).Result() } else { - cursor, _ = getEntryCursor() + if param.Reset { + cursor = 0 + } else { + cursor, _ = getEntryCursor() + } scanSize := int64(Preferences().GetScanSize()) loadVal, subErr = client.LRange(ctx, key, int64(cursor), int64(cursor)+scanSize-1).Result() cursor = cursor + uint64(scanSize) @@ -710,7 +715,11 @@ func (b *browserService) GetKeyDetail(param types.KeyDetailParam) (resp types.JS } } } else { - cursor, _ = getEntryCursor() + if param.Reset { + cursor = 0 + } else { + cursor, _ = getEntryCursor() + } loadedVal, cursor, subErr = client.HScan(ctx, key, cursor, matchPattern, scanSize).Result() if subErr != nil { return nil, false, subErr @@ -768,7 +777,11 @@ func (b *browserService) GetKeyDetail(param types.KeyDetailParam) (resp types.JS } } } else { - cursor, _ = getEntryCursor() + if param.Reset { + cursor = 0 + } else { + cursor, _ = getEntryCursor() + } loadedKey, cursor, subErr = client.SScan(ctx, key, cursor, param.MatchPattern, scanSize).Result() items = make([]types.SetEntryItem, len(loadedKey)) for i, val := range loadedKey { @@ -824,7 +837,11 @@ func (b *browserService) GetKeyDetail(param types.KeyDetailParam) (resp types.JS } } } else { - cursor, _ = getEntryCursor() + if param.Reset { + cursor = 0 + } else { + cursor, _ = getEntryCursor() + } loadedVal, cursor, err = client.ZScan(ctx, key, cursor, param.MatchPattern, scanSize).Result() loadedLen := len(loadedVal) items = make([]types.ZSetEntryItem, loadedLen/2) @@ -855,7 +872,6 @@ func (b *browserService) GetKeyDetail(param types.KeyDetailParam) (resp types.JS case "stream": loadStreamHandle := func() ([]types.StreamEntryItem, bool, error) { var msgs []redis.XMessage - var items []types.StreamEntryItem var last string if param.Full { // load all @@ -863,7 +879,11 @@ func (b *browserService) GetKeyDetail(param types.KeyDetailParam) (resp types.JS msgs, err = client.XRevRange(ctx, key, "+", "-").Result() } else { scanSize := int64(Preferences().GetScanSize()) - _, last = getEntryCursor() + if param.Reset { + last = "" + } else { + _, last = getEntryCursor() + } if len(last) <= 0 { last = "+" } @@ -882,11 +902,15 @@ func (b *browserService) GetKeyDetail(param types.KeyDetailParam) (resp types.JS } } setEntryXLast(last) - for _, msg := range msgs { - items = append(items, types.StreamEntryItem{ - ID: msg.ID, - Value: msg.Values, - }) + items := make([]types.StreamEntryItem, len(msgs)) + for i, msg := range msgs { + items[i].ID = msg.ID + items[i].Value = msg.Values + if vb, merr := json.Marshal(msg.Values); merr != nil { + items[i].DisplayValue = "{}" + } else { + items[i].DisplayValue, _, _ = strutil.ConvertTo(string(vb), types.DECODE_NONE, types.FORMAT_JSON) + } } if err != nil { return items, false, err diff --git a/backend/types/redis_wrapper.go b/backend/types/redis_wrapper.go index 1b9032d..2c81a3d 100644 --- a/backend/types/redis_wrapper.go +++ b/backend/types/redis_wrapper.go @@ -23,6 +23,7 @@ type ZSetEntryItem struct { } type StreamEntryItem struct { - ID string `json:"id"` - Value map[string]any `json:"value"` + ID string `json:"id"` + Value map[string]any `json:"v"` + DisplayValue string `json:"dv,omitempty"` } diff --git a/frontend/src/components/content_value/ContentValueStream.vue b/frontend/src/components/content_value/ContentValueStream.vue index a119903..a5f50ed 100644 --- a/frontend/src/components/content_value/ContentValueStream.vue +++ b/frontend/src/components/content_value/ContentValueStream.vue @@ -83,26 +83,26 @@ const idColumn = reactive({ const valueColumn = reactive({ key: 'value', title: i18n.t('common.value'), - align: 'center', + align: 'left', titleAlign: 'center', resizable: true, + // ellipsis: { + // tooltip: true, + // }, filterOptionValue: null, - filter(value, row) { + filter: (value, row) => { const v = value.toString() if (filterType.value === 1) { // filter key - return some(keys(row.value), (key) => includes(key, v)) + return some(keys(row.v), (key) => includes(key, v)) } else { // filter value - return some(values(row.value), (val) => includes(val, v)) + return some(values(row.v), (val) => includes(val, v)) } }, // sorter: (row1, row2) => row1.value - row2.value, - // ellipsis: { - // tooltip: true - // }, render: (row) => { - return h(NCode, { language: 'json', wordWrap: true }, { default: () => JSON.stringify(row.value) }) + return h(NCode, { language: 'json', wordWrap: true, code: row.dv }) }, }) const actionColumn = { @@ -136,23 +136,10 @@ const actionColumn = { }) }, } -const columns = reactive([idColumn, valueColumn, actionColumn]) - -const tableData = computed(() => { - const data = [] - if (!isEmpty(props.value)) { - for (const elem of props.value) { - data.push({ - id: elem.id, - value: elem.value, - }) - } - } - return data -}) +const columns = computed(() => [idColumn, valueColumn, actionColumn]) const entries = computed(() => { - const len = size(tableData.value) + const len = size(props.value) return `${len} / ${Math.max(len, props.length)}` }) @@ -253,7 +240,7 @@ defineExpose({ :bordered="false" :bottom-bordered="false" :columns="columns" - :data="tableData" + :data="props.value" :loading="props.loading" :single-column="true" :single-line="false"