perf: support json format viewing for stream values #65

fix: the 'reset' parameter in 'GetKeyDetail' isn't taking effect
This commit is contained in:
Lykin 2023-11-16 00:21:54 +08:00
parent fea87662de
commit cc436ad86f
3 changed files with 49 additions and 37 deletions

View File

@ -2,6 +2,7 @@ package services
import ( import (
"context" "context"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/redis/go-redis/v9" "github.com/redis/go-redis/v9"
@ -646,8 +647,12 @@ func (b *browserService) GetKeyDetail(param types.KeyDetailParam) (resp types.JS
// load all // load all
cursor = 0 cursor = 0
loadVal, subErr = client.LRange(ctx, key, 0, -1).Result() loadVal, subErr = client.LRange(ctx, key, 0, -1).Result()
} else {
if param.Reset {
cursor = 0
} else { } else {
cursor, _ = getEntryCursor() cursor, _ = getEntryCursor()
}
scanSize := int64(Preferences().GetScanSize()) scanSize := int64(Preferences().GetScanSize())
loadVal, subErr = client.LRange(ctx, key, int64(cursor), int64(cursor)+scanSize-1).Result() loadVal, subErr = client.LRange(ctx, key, int64(cursor), int64(cursor)+scanSize-1).Result()
cursor = cursor + uint64(scanSize) cursor = cursor + uint64(scanSize)
@ -709,8 +714,12 @@ func (b *browserService) GetKeyDetail(param types.KeyDetailParam) (resp types.JS
break break
} }
} }
} else {
if param.Reset {
cursor = 0
} else { } else {
cursor, _ = getEntryCursor() cursor, _ = getEntryCursor()
}
loadedVal, cursor, subErr = client.HScan(ctx, key, cursor, matchPattern, scanSize).Result() loadedVal, cursor, subErr = client.HScan(ctx, key, cursor, matchPattern, scanSize).Result()
if subErr != nil { if subErr != nil {
return nil, false, subErr return nil, false, subErr
@ -767,8 +776,12 @@ func (b *browserService) GetKeyDetail(param types.KeyDetailParam) (resp types.JS
break break
} }
} }
} else {
if param.Reset {
cursor = 0
} else { } else {
cursor, _ = getEntryCursor() cursor, _ = getEntryCursor()
}
loadedKey, cursor, subErr = client.SScan(ctx, key, cursor, param.MatchPattern, scanSize).Result() loadedKey, cursor, subErr = client.SScan(ctx, key, cursor, param.MatchPattern, scanSize).Result()
items = make([]types.SetEntryItem, len(loadedKey)) items = make([]types.SetEntryItem, len(loadedKey))
for i, val := range loadedKey { for i, val := range loadedKey {
@ -823,8 +836,12 @@ func (b *browserService) GetKeyDetail(param types.KeyDetailParam) (resp types.JS
break break
} }
} }
} else {
if param.Reset {
cursor = 0
} else { } else {
cursor, _ = getEntryCursor() cursor, _ = getEntryCursor()
}
loadedVal, cursor, err = client.ZScan(ctx, key, cursor, param.MatchPattern, scanSize).Result() loadedVal, cursor, err = client.ZScan(ctx, key, cursor, param.MatchPattern, scanSize).Result()
loadedLen := len(loadedVal) loadedLen := len(loadedVal)
items = make([]types.ZSetEntryItem, loadedLen/2) items = make([]types.ZSetEntryItem, loadedLen/2)
@ -855,7 +872,6 @@ func (b *browserService) GetKeyDetail(param types.KeyDetailParam) (resp types.JS
case "stream": case "stream":
loadStreamHandle := func() ([]types.StreamEntryItem, bool, error) { loadStreamHandle := func() ([]types.StreamEntryItem, bool, error) {
var msgs []redis.XMessage var msgs []redis.XMessage
var items []types.StreamEntryItem
var last string var last string
if param.Full { if param.Full {
// load all // load all
@ -863,7 +879,11 @@ func (b *browserService) GetKeyDetail(param types.KeyDetailParam) (resp types.JS
msgs, err = client.XRevRange(ctx, key, "+", "-").Result() msgs, err = client.XRevRange(ctx, key, "+", "-").Result()
} else { } else {
scanSize := int64(Preferences().GetScanSize()) scanSize := int64(Preferences().GetScanSize())
if param.Reset {
last = ""
} else {
_, last = getEntryCursor() _, last = getEntryCursor()
}
if len(last) <= 0 { if len(last) <= 0 {
last = "+" last = "+"
} }
@ -882,11 +902,15 @@ func (b *browserService) GetKeyDetail(param types.KeyDetailParam) (resp types.JS
} }
} }
setEntryXLast(last) setEntryXLast(last)
for _, msg := range msgs { items := make([]types.StreamEntryItem, len(msgs))
items = append(items, types.StreamEntryItem{ for i, msg := range msgs {
ID: msg.ID, items[i].ID = msg.ID
Value: msg.Values, 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 { if err != nil {
return items, false, err return items, false, err

View File

@ -24,5 +24,6 @@ type ZSetEntryItem struct {
type StreamEntryItem struct { type StreamEntryItem struct {
ID string `json:"id"` ID string `json:"id"`
Value map[string]any `json:"value"` Value map[string]any `json:"v"`
DisplayValue string `json:"dv,omitempty"`
} }

View File

@ -83,26 +83,26 @@ const idColumn = reactive({
const valueColumn = reactive({ const valueColumn = reactive({
key: 'value', key: 'value',
title: i18n.t('common.value'), title: i18n.t('common.value'),
align: 'center', align: 'left',
titleAlign: 'center', titleAlign: 'center',
resizable: true, resizable: true,
// ellipsis: {
// tooltip: true,
// },
filterOptionValue: null, filterOptionValue: null,
filter(value, row) { filter: (value, row) => {
const v = value.toString() const v = value.toString()
if (filterType.value === 1) { if (filterType.value === 1) {
// filter key // filter key
return some(keys(row.value), (key) => includes(key, v)) return some(keys(row.v), (key) => includes(key, v))
} else { } else {
// filter value // 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, // sorter: (row1, row2) => row1.value - row2.value,
// ellipsis: {
// tooltip: true
// },
render: (row) => { 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 = { const actionColumn = {
@ -136,23 +136,10 @@ const actionColumn = {
}) })
}, },
} }
const columns = reactive([idColumn, valueColumn, actionColumn]) const columns = computed(() => [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 entries = computed(() => { const entries = computed(() => {
const len = size(tableData.value) const len = size(props.value)
return `${len} / ${Math.max(len, props.length)}` return `${len} / ${Math.max(len, props.length)}`
}) })
@ -253,7 +240,7 @@ defineExpose({
:bordered="false" :bordered="false"
:bottom-bordered="false" :bottom-bordered="false"
:columns="columns" :columns="columns"
:data="tableData" :data="props.value"
:loading="props.loading" :loading="props.loading"
:single-column="true" :single-column="true"
:single-line="false" :single-line="false"