Compare commits

...

7 Commits

12 changed files with 239 additions and 135 deletions

View File

@ -291,7 +291,8 @@ func (b *browserService) getRedisClient(server string, db int) (item *connection
var ok bool var ok bool
var client redis.UniversalClient var client redis.UniversalClient
if item, ok = b.connMap[server]; ok { if item, ok = b.connMap[server]; ok {
if item.db == db { if item.db == db || db < 0 {
// return without switch database directly
return return
} }

View File

@ -0,0 +1,69 @@
<script setup>
import { isNumber } from 'lodash'
const props = defineProps({
loading: {
type: Boolean,
default: false,
},
on: {
type: Boolean,
default: false,
},
defaultValue: {
type: Number,
default: 2,
},
interval: {
type: Number,
default: 2,
},
onRefresh: {
type: Function,
default: () => {},
},
})
const emit = defineEmits(['toggle', 'update:on', 'update:interval'])
const onToggle = (on) => {
emit('update:on', on === true)
if (on) {
let interval = props.interval
if (!isNumber(interval)) {
interval = props.defaultValue
}
interval = Math.max(1, interval)
emit('update:interval', interval)
emit('toggle', true)
} else {
emit('toggle', false)
}
}
</script>
<template>
<n-form :show-feedback="false" label-align="right" label-placement="left" label-width="auto" size="small">
<n-form-item :label="$t('interface.auto_refresh')">
<n-switch :loading="props.loading" :value="props.on" @update:value="onToggle" />
</n-form-item>
<n-form-item :label="$t('interface.refresh_interval')">
<n-input-number
:autofocus="false"
:default-value="props.defaultValue"
:disabled="props.on"
:max="9999"
:min="1"
:show-button="false"
:value="props.interval"
style="max-width: 100px"
@update:value="(val) => emit('update:interval', val)">
<template #suffix>
{{ $t('common.unit_second') }}
</template>
</n-input-number>
</n-form-item>
</n-form>
</template>
<style lang="scss" scoped></style>

View File

@ -107,7 +107,8 @@ const loadHistory = async () => {
data.history = list || [] data.history = list || []
} finally { } finally {
data.loading = false data.loading = false
tableRef.value?.scrollTo({ top: 999999 }) await nextTick()
tableRef.value?.scrollTo({ position: 'bottom' })
} }
} }
@ -118,7 +119,8 @@ const cleanHistory = async () => {
const success = await browserStore.cleanCmdHistory() const success = await browserStore.cleanCmdHistory()
if (success) { if (success) {
data.history = [] data.history = []
tableRef.value?.scrollTo({ top: 0 }) await nextTick()
tableRef.value?.scrollTo({ position: 'top' })
$message.success(i18n.t('dialogue.handle_succ')) $message.success(i18n.t('dialogue.handle_succ'))
} }
} finally { } finally {

View File

@ -1,20 +1,27 @@
<script setup> <script setup>
import { get, isEmpty, map, mapValues, pickBy, split, sum, toArray, toNumber } from 'lodash' import { get, isEmpty, map, mapValues, pickBy, split, sum, toArray, toNumber } from 'lodash'
import { computed, onMounted, onUnmounted, ref } from 'vue' import { computed, onMounted, onUnmounted, reactive, ref } from 'vue'
import IconButton from '@/components/common/IconButton.vue' import IconButton from '@/components/common/IconButton.vue'
import Filter from '@/components/icons/Filter.vue' import Filter from '@/components/icons/Filter.vue'
import Refresh from '@/components/icons/Refresh.vue' import Refresh from '@/components/icons/Refresh.vue'
import useBrowserStore from 'stores/browser.js' import useBrowserStore from 'stores/browser.js'
import { timeout } from '@/utils/promise.js'
import AutoRefreshForm from '@/components/common/AutoRefreshForm.vue'
import { NIcon, useThemeVars } from 'naive-ui'
const props = defineProps({ const props = defineProps({
server: String, server: String,
}) })
const browserStore = useBrowserStore() const browserStore = useBrowserStore()
const themeVars = useThemeVars()
const serverInfo = ref({}) const serverInfo = ref({})
const autoRefresh = ref(false) const pageState = reactive({
const loading = ref(false) // loading status for refresh autoRefresh: false,
const autoLoading = ref(false) // loading status for auto refresh refreshInterval: 5,
loading: false, // loading status for refresh
autoLoading: false, // loading status for auto refresh
})
/** /**
* refresh server status info * refresh server status info
@ -23,32 +30,58 @@ const autoLoading = ref(false) // loading status for auto refresh
*/ */
const refreshInfo = async (force) => { const refreshInfo = async (force) => {
if (force) { if (force) {
loading.value = true pageState.loading = true
} else { } else {
autoLoading.value = true pageState.autoLoading = true
} }
if (!isEmpty(props.server) && browserStore.isConnected(props.server)) { if (!isEmpty(props.server) && browserStore.isConnected(props.server)) {
try { try {
serverInfo.value = await browserStore.getServerInfo(props.server) serverInfo.value = await browserStore.getServerInfo(props.server)
} finally { } finally {
loading.value = false pageState.loading = false
autoLoading.value = false pageState.autoLoading = false
} }
} }
} }
let intervalID const isLoading = computed(() => {
return pageState.loading || pageState.autoLoading
})
const startAutoRefresh = async () => {
let lastExec = Date.now()
do {
if (!pageState.autoRefresh) {
break
}
await timeout(100)
if (pageState.loading || pageState.autoLoading || Date.now() - lastExec < pageState.refreshInterval * 1000) {
continue
}
lastExec = Date.now()
await refreshInfo()
} while (true)
stopAutoRefresh()
}
const stopAutoRefresh = () => {
pageState.autoRefresh = false
}
const onToggleRefresh = (on) => {
if (on) {
startAutoRefresh()
} else {
stopAutoRefresh()
}
}
onMounted(() => { onMounted(() => {
refreshInfo() refreshInfo()
intervalID = setInterval(() => {
if (autoRefresh.value === true) {
refreshInfo()
}
}, 5000)
}) })
onUnmounted(() => { onUnmounted(() => {
clearInterval(intervalID) stopAutoRefresh()
}) })
const scrollRef = ref(null) const scrollRef = ref(null)
@ -128,7 +161,7 @@ const infoFilter = ref('')
<n-tag size="small" type="primary">{{ redisMode }}</n-tag> <n-tag size="small" type="primary">{{ redisMode }}</n-tag>
</template> </template>
</n-tooltip> </n-tooltip>
<n-tooltip v-if="redisMode"> <n-tooltip v-if="role">
Role Role
<template #trigger> <template #trigger>
<n-tag size="small" type="primary">{{ role }}</n-tag> <n-tag size="small" type="primary">{{ role }}</n-tag>
@ -137,27 +170,36 @@ const infoFilter = ref('')
</n-space> </n-space>
</template> </template>
<template #header-extra> <template #header-extra>
<n-space align="center" inline> <n-popover keep-alive-on-hover placement="bottom-end" trigger="hover">
{{ $t('status.auto_refresh') }}
<n-switch v-model:value="autoRefresh" :loading="autoLoading" />
<n-tooltip>
{{ $t('status.refresh') }}
<template #trigger> <template #trigger>
<n-button <n-button
:loading="autoLoading" :loading="pageState.loading"
:type="isLoading ? 'primary' : 'default'"
circle circle
size="small" size="small"
tertiary tertiary
@click="refreshInfo(true)"> @click="refreshInfo(true)">
<template #icon> <template #icon>
<n-icon :component="Refresh" /> <n-icon :size="props.size">
<refresh
:class="{
'auto-rotate': pageState.autoRefresh || isLoading,
}"
:color="pageState.autoRefresh ? themeVars.primaryColor : undefined"
:stroke-width="pageState.autoRefresh ? 6 : 3" />
</n-icon>
</template> </template>
</n-button> </n-button>
</template> </template>
</n-tooltip> <auto-refresh-form
</n-space> v-model:interval="pageState.refreshInterval"
v-model:on="pageState.autoRefresh"
:default-value="5"
:loading="pageState.autoLoading"
@toggle="onToggleRefresh" />
</n-popover>
</template> </template>
<n-spin :show="loading"> <n-spin :show="pageState.loading">
<n-grid style="min-width: 500px" x-gap="5"> <n-grid style="min-width: 500px" x-gap="5">
<n-gi :span="6"> <n-gi :span="6">
<n-statistic :label="$t('status.uptime')" :value="uptime[0]"> <n-statistic :label="$t('status.uptime')" :value="uptime[0]">
@ -192,7 +234,7 @@ const infoFilter = ref('')
</template> </template>
</n-input> </n-input>
</template> </template>
<n-spin :show="loading"> <n-spin :show="pageState.loading">
<n-tabs default-value="CPU" placement="left" type="line"> <n-tabs default-value="CPU" placement="left" type="line">
<n-tab-pane v-for="(v, k) in serverInfo" :key="k" :disabled="isEmpty(v)" :name="k"> <n-tab-pane v-for="(v, k) in serverInfo" :key="k" :disabled="isEmpty(v)" :name="k">
<n-data-table <n-data-table

View File

@ -1,11 +1,13 @@
<script setup> <script setup>
import { computed, h, onMounted, onUnmounted, reactive, ref } from 'vue' import { computed, h, nextTick, onMounted, onUnmounted, reactive, ref } from 'vue'
import Refresh from '@/components/icons/Refresh.vue' import Refresh from '@/components/icons/Refresh.vue'
import { debounce, isEmpty, map, size, split } from 'lodash' import { debounce, isEmpty, map, size, split } from 'lodash'
import { useI18n } from 'vue-i18n' import { useI18n } from 'vue-i18n'
import { useThemeVars } from 'naive-ui' import { useThemeVars } from 'naive-ui'
import dayjs from 'dayjs' import dayjs from 'dayjs'
import useBrowserStore from 'stores/browser.js' import useBrowserStore from 'stores/browser.js'
import { timeout } from '@/utils/promise.js'
import AutoRefreshForm from '@/components/common/AutoRefreshForm.vue'
const themeVars = useThemeVars() const themeVars = useThemeVars()
@ -21,12 +23,16 @@ const props = defineProps({
}, },
}) })
const autoRefresh = reactive({
on: false,
interval: 5,
})
const data = reactive({ const data = reactive({
list: [], list: [],
sortOrder: 'descend', sortOrder: 'descend',
listLimit: 20, listLimit: 20,
loading: false, loading: false,
autoLoading: false,
client: '', client: '',
keyword: '', keyword: '',
}) })
@ -125,26 +131,45 @@ const _loadSlowLog = () => {
.then((list) => { .then((list) => {
data.list = list || [] data.list = list || []
}) })
.finally(() => { .finally(async () => {
data.loading = false data.loading = false
tableRef.value?.scrollTo({ top: data.sortOrder === 'ascend' ? 999999 : 0 }) await nextTick()
tableRef.value?.scrollTo({ position: data.sortOrder === 'ascend' ? 'bottom' : 'top' })
}) })
} }
const loadSlowLog = debounce(_loadSlowLog, 1000, { leading: true, trailing: true }) const loadSlowLog = debounce(_loadSlowLog, 1000, { leading: true, trailing: true })
let intervalID const startAutoRefresh = async () => {
onMounted(() => { let lastExec = Date.now()
loadSlowLog() do {
intervalID = setInterval(() => { if (!autoRefresh.on) {
if (data.autoLoading === true) { break
loadSlowLog()
} }
}, 5000) await timeout(100)
}) if (data.loading || Date.now() - lastExec < autoRefresh.interval * 1000) {
continue
}
lastExec = Date.now()
loadSlowLog()
} while (true)
stopAutoRefresh()
}
onUnmounted(() => { const stopAutoRefresh = () => {
clearInterval(intervalID) autoRefresh.on = false
}) }
onMounted(() => loadSlowLog())
onUnmounted(() => stopAutoRefresh())
const onToggleRefresh = (on) => {
if (on) {
startAutoRefresh()
} else {
stopAutoRefresh()
}
}
const onListLimitChanged = (limit) => { const onListLimitChanged = (limit) => {
loadSlowLog() loadSlowLog()
@ -161,23 +186,28 @@ const onListLimitChanged = (limit) => {
style="width: 120px" style="width: 120px"
@update:value="onListLimitChanged" /> @update:value="onListLimitChanged" />
</n-form-item> </n-form-item>
<n-form-item :label="$t('slog.auto_refresh')"> <n-form-item :label="$t('slog.filter')">
<n-switch v-model:value="data.autoLoading" :loading="data.loading" /> <n-input v-model:value="data.keyword" clearable placeholder="" />
</n-form-item> </n-form-item>
<n-form-item label="&nbsp;"> <n-form-item label="&nbsp;">
<n-tooltip> <n-popover :delay="500" keep-alive-on-hover placement="bottom" trigger="hover">
{{ $t('slog.refresh') }}
<template #trigger> <template #trigger>
<n-button :loading="data.loading" circle size="small" tertiary @click="_loadSlowLog"> <n-button :loading="data.loading" circle size="small" tertiary @click="_loadSlowLog">
<template #icon> <template #icon>
<n-icon :component="Refresh" /> <refresh
:class="{ 'auto-rotate': autoRefresh.on }"
:color="autoRefresh.on ? themeVars.primaryColor : undefined"
:stroke-width="autoRefresh.on ? 6 : 3" />
</template> </template>
</n-button> </n-button>
</template> </template>
</n-tooltip> <auto-refresh-form
</n-form-item> v-model:interval="autoRefresh.interval"
<n-form-item :label="$t('slog.filter')"> v-model:on="autoRefresh.on"
<n-input v-model:value="data.keyword" clearable placeholder="" /> :default-value="5"
:loading="data.loading"
@toggle="onToggleRefresh" />
</n-popover>
</n-form-item> </n-form-item>
</n-form> </n-form>
<n-data-table <n-data-table

View File

@ -14,6 +14,7 @@ import { computed, onUnmounted, reactive, watch } from 'vue'
import { padStart } from 'lodash' import { padStart } from 'lodash'
import { NIcon, useThemeVars } from 'naive-ui' import { NIcon, useThemeVars } from 'naive-ui'
import { timeout } from '@/utils/promise.js' import { timeout } from '@/utils/promise.js'
import AutoRefreshForm from '@/components/common/AutoRefreshForm.vue'
const props = defineProps({ const props = defineProps({
server: String, server: String,
@ -70,14 +71,6 @@ const ttlString = computed(() => {
}) })
const startAutoRefresh = async () => { const startAutoRefresh = async () => {
if (autoRefresh.on) {
return
}
autoRefresh.on = true
if (!isNaN(autoRefresh.interval)) {
autoRefresh.interval = 2
}
autoRefresh.interval = Math.min(autoRefresh.interval, 1)
let lastExec = Date.now() let lastExec = Date.now()
do { do {
if (!autoRefresh.on) { if (!autoRefresh.on) {
@ -147,42 +140,19 @@ const onTTL = () => {
<template #trigger> <template #trigger>
<icon-button :loading="props.loading" size="18" @click="emit('reload')"> <icon-button :loading="props.loading" size="18" @click="emit('reload')">
<n-icon :size="props.size"> <n-icon :size="props.size">
<component <refresh
:is="Refresh" :class="{ 'auto-rotate': autoRefresh.on }"
:class="{ 'auto-refreshing': autoRefresh.on }"
:color="autoRefresh.on ? themeVars.primaryColor : undefined" :color="autoRefresh.on ? themeVars.primaryColor : undefined"
:stroke-width="autoRefresh.on ? 5 : 3" /> :stroke-width="autoRefresh.on ? 6 : 3" />
</n-icon> </n-icon>
</icon-button> </icon-button>
</template> </template>
<n-form <auto-refresh-form
:show-feedback="false" v-model:interval="autoRefresh.interval"
label-align="right" v-model:on="autoRefresh.on"
label-placement="left"
label-width="auto"
size="small">
<n-form-item :label="$t('interface.auto_refresh')">
<n-switch
:loading="props.loading"
:value="autoRefresh.on"
@update:value="onToggleRefresh" />
</n-form-item>
<n-form-item :label="$t('interface.refresh_interval')">
<n-input-number
v-model:value="autoRefresh.interval"
:autofocus="false"
:default-value="2" :default-value="2"
:disabled="autoRefresh.on" :loading="props.loading"
:max="9999" @toggle="onToggleRefresh" />
:min="1"
:show-button="false"
style="max-width: 100px">
<template #suffix>
{{ $t('common.unit_second') }}
</template>
</n-input-number>
</n-form-item>
</n-form>
</n-popover> </n-popover>
</template> </template>
</n-input> </n-input>
@ -226,14 +196,4 @@ const onTTL = () => {
align-items: center; align-items: center;
gap: 5px; gap: 5px;
} }
.auto-refreshing {
animation: rotate 2s linear infinite;
}
@keyframes rotate {
100% {
transform: rotate(360deg);
}
}
</style> </style>

View File

@ -16,14 +16,14 @@ const props = defineProps({
<path <path
:stroke-width="props.strokeWidth" :stroke-width="props.strokeWidth"
d="M36.7279 36.7279C33.4706 39.9853 28.9706 42 24 42C14.0589 42 6 33.9411 6 24C6 14.0589 14.0589 6 24 6C28.9706 6 33.4706 8.01472 36.7279 11.2721C38.3859 12.9301 42 17 42 17" d="M36.7279 36.7279C33.4706 39.9853 28.9706 42 24 42C14.0589 42 6 33.9411 6 24C6 14.0589 14.0589 6 24 6C28.9706 6 33.4706 8.01472 36.7279 11.2721C38.3859 12.9301 42 17 42 17"
stroke="currentColor" :stroke="color"
stroke-linecap="round" stroke-linecap="round"
stroke-linejoin="round" /> stroke-linejoin="round" />
<path <path
:stroke-width="props.strokeWidth" :stroke-width="props.strokeWidth"
class="default-stroke" class="default-stroke"
d="M42 8V17H33" d="M42 8V17H33"
stroke="currentColor" :stroke="color"
stroke-linecap="round" stroke-linecap="round"
stroke-linejoin="round" /> stroke-linejoin="round" />
</svg> </svg>

View File

@ -359,9 +359,7 @@
"connected_clients": "Clients", "connected_clients": "Clients",
"total_keys": "Keys", "total_keys": "Keys",
"memory_used": "Memory", "memory_used": "Memory",
"all_info": "Information", "all_info": "Information"
"refresh": "Refresh",
"auto_refresh": "Auto Refresh"
}, },
"slog": { "slog": {
"title": "Slow Log", "title": "Slow Log",
@ -370,9 +368,7 @@
"exec_time": "Time", "exec_time": "Time",
"client": "Client", "client": "Client",
"cmd": "Command", "cmd": "Command",
"cost_time": "Cost", "cost_time": "Cost"
"refresh": "Refresh Now",
"auto_refresh": "Auto Refresh"
}, },
"monitor": { "monitor": {
"title": "Monitor Commands", "title": "Monitor Commands",

View File

@ -291,9 +291,7 @@
"connected_clients": "Clientes Conectados", "connected_clients": "Clientes Conectados",
"total_keys": "Chaves Totais", "total_keys": "Chaves Totais",
"memory_used": "Memória Usada", "memory_used": "Memória Usada",
"all_info": "Informações", "all_info": "Informações"
"refresh": "Atualizar",
"auto_refresh": "Atualização Automática"
}, },
"slog": { "slog": {
"title": "Registro de Operações Lentas", "title": "Registro de Operações Lentas",
@ -302,8 +300,6 @@
"exec_time": "Tempo", "exec_time": "Tempo",
"client": "Cliente", "client": "Cliente",
"cmd": "Comando", "cmd": "Comando",
"cost_time": "Custo", "cost_time": "Custo"
"refresh": "Atualizar Agora",
"auto_refresh": "Atualização Automática"
} }
} }

View File

@ -359,9 +359,7 @@
"connected_clients": "已连客户端", "connected_clients": "已连客户端",
"total_keys": "键总数", "total_keys": "键总数",
"memory_used": "内存使用", "memory_used": "内存使用",
"all_info": "全部信息", "all_info": "全部信息"
"refresh": "立即刷新",
"auto_refresh": "自动刷新"
}, },
"slog": { "slog": {
"title": "慢日志", "title": "慢日志",
@ -370,9 +368,7 @@
"exec_time": "执行时间", "exec_time": "执行时间",
"client": "客户端", "client": "客户端",
"cmd": "命令", "cmd": "命令",
"cost_time": "耗时", "cost_time": "耗时"
"refresh": "立即刷新",
"auto_refresh": "自动刷新"
}, },
"monitor": { "monitor": {
"title": "监控命令", "title": "监控命令",

View File

@ -347,7 +347,7 @@ const useBrowserStore = defineStore('browser', {
*/ */
async getServerInfo(server) { async getServerInfo(server) {
try { try {
const { success, data } = await ServerInfo(server) const { success, data, msg } = await ServerInfo(server)
if (success) { if (success) {
/** @type {RedisServerState} **/ /** @type {RedisServerState} **/
const serverInst = this.servers[server] const serverInst = this.servers[server]
@ -355,6 +355,8 @@ const useBrowserStore = defineStore('browser', {
serverInst.stats = data serverInst.stats = data
} }
return data return data
} else if (!isEmpty(msg)) {
$message.warning(msg)
} }
} finally { } finally {
} }

View File

@ -168,3 +168,13 @@ body {
.fade-leave-to { .fade-leave-to {
opacity: 0; opacity: 0;
} }
.auto-rotate {
animation: rotate 2s linear infinite;
}
@keyframes rotate {
100% {
transform: rotate(360deg);
}
}