From 6a54183d91cbfe51a42a6a1192df27708d851838 Mon Sep 17 00:00:00 2001
From: tiny-craft <137850705+tiny-craft@users.noreply.github.com>
Date: Mon, 3 Jul 2023 23:30:04 +0800
Subject: [PATCH] feat: support comparison operator to filter ordered set by
score
---
.../content_value/ContentValueHash.vue | 26 ++---
.../content_value/ContentValueZSet.vue | 102 ++++++++++++++++--
frontend/src/langs/en.json | 1 +
frontend/src/langs/zh-cn.json | 1 +
frontend/src/style.scss | 4 +
5 files changed, 112 insertions(+), 22 deletions(-)
diff --git a/frontend/src/components/content_value/ContentValueHash.vue b/frontend/src/components/content_value/ContentValueHash.vue
index 905dfea..bd6ee1a 100644
--- a/frontend/src/components/content_value/ContentValueHash.vue
+++ b/frontend/src/components/content_value/ContentValueHash.vue
@@ -11,18 +11,6 @@ import useConnectionStore from '../../stores/connections.js'
const i18n = useI18n()
-const filterOption = [
- {
- value: 1,
- label: i18n.t('field'),
- },
- {
- value: 2,
- label: i18n.t('value'),
- },
-]
-const filterType = ref(1)
-
const props = defineProps({
name: String,
db: Number,
@@ -34,6 +22,18 @@ const props = defineProps({
value: Object,
})
+const filterOption = computed(() => [
+ {
+ value: 1,
+ label: i18n.t('field'),
+ },
+ {
+ value: 2,
+ label: i18n.t('value'),
+ },
+])
+const filterType = ref(1)
+
const connectionStore = useConnectionStore()
const dialogStore = useDialogStore()
const keyType = redisTypes.HASH
@@ -206,10 +206,12 @@ const filterValue = ref('')
const onFilterInput = (val) => {
switch (filterType.value) {
case filterOption[0].value:
+ // filter field
valueColumn.filterOptionValue = null
fieldColumn.filterOptionValue = val
break
case filterOption[1].value:
+ // filter value
fieldColumn.filterOptionValue = null
valueColumn.filterOptionValue = val
break
diff --git a/frontend/src/components/content_value/ContentValueZSet.vue b/frontend/src/components/content_value/ContentValueZSet.vue
index 2195638..6e7da07 100644
--- a/frontend/src/components/content_value/ContentValueZSet.vue
+++ b/frontend/src/components/content_value/ContentValueZSet.vue
@@ -6,11 +6,12 @@ import AddLink from '../icons/AddLink.vue'
import { NButton, NCode, NIcon, NInput, NInputNumber, useMessage } from 'naive-ui'
import { types, types as redisTypes } from '../../consts/support_redis_type.js'
import EditableTableColumn from '../common/EditableTableColumn.vue'
-import { isEmpty } from 'lodash'
+import { isEmpty, replace } from 'lodash'
import useDialogStore from '../../stores/dialog.js'
import useConnectionStore from '../../stores/connections.js'
const i18n = useI18n()
+
const props = defineProps({
name: String,
db: Number,
@@ -22,6 +23,18 @@ const props = defineProps({
value: Object,
})
+const filterOption = computed(() => [
+ {
+ value: 1,
+ label: i18n.t('value'),
+ },
+ {
+ value: 2,
+ label: i18n.t('score'),
+ },
+])
+const filterType = ref(1)
+
const connectionStore = useConnectionStore()
const dialogStore = useDialogStore()
const keyType = redisTypes.ZSET
@@ -36,6 +49,40 @@ const scoreColumn = reactive({
align: 'center',
titleAlign: 'center',
resizable: true,
+ filterOptionValue: null,
+ filter(value, row) {
+ const score = parseFloat(row.score)
+ if (isNaN(score)) {
+ return true
+ }
+
+ const regex = /^(>=|<=|>|<|=|!=)?(\d+(\.\d*)?)?$/
+ const matches = value.match(regex)
+ if (matches) {
+ const operator = matches[1] || ''
+ const filterScore = parseFloat(matches[2] || '')
+ if (!isNaN(filterScore)) {
+ switch (operator) {
+ case '>=':
+ return score >= filterScore
+ case '<=':
+ return score <= filterScore
+ case '>':
+ return score > filterScore
+ case '<':
+ return score < filterScore
+ case '=':
+ return score === filterScore
+ case '!=':
+ return score !== filterScore
+ }
+ }
+ } else {
+ return !!~row.value.indexOf(value.toString())
+ }
+
+ return true
+ },
render: (row) => {
const isEdit = currentEditRow.value.no === row.no
if (isEdit) {
@@ -180,7 +227,18 @@ const onAddRow = () => {
const filterValue = ref('')
const onFilterInput = (val) => {
- valueColumn.filterOptionValue = val
+ switch (filterType.value) {
+ case filterOption[0].value:
+ // filter value
+ scoreColumn.filterOptionValue = null
+ valueColumn.filterOptionValue = val
+ break
+ case filterOption[1].value:
+ // filter score
+ valueColumn.filterOptionValue = null
+ scoreColumn.filterOptionValue = val
+ break
+ }
}
const onChangeFilterType = (type) => {
@@ -189,10 +247,20 @@ const onChangeFilterType = (type) => {
const clearFilter = () => {
valueColumn.filterOptionValue = null
+ scoreColumn.filterOptionValue = null
}
const onUpdateFilter = (filters, sourceColumn) => {
- valueColumn.filterOptionValue = filters[sourceColumn.key]
+ switch (filterType.value) {
+ case filterOption[0].value:
+ // filter value
+ valueColumn.filterOptionValue = filters[sourceColumn.key]
+ break
+ case filterOption[1].value:
+ // filter score
+ scoreColumn.filterOptionValue = filters[sourceColumn.key]
+ break
+ }
}
@@ -201,13 +269,27 @@ const onUpdateFilter = (filters, sourceColumn) => {