mirror of
https://github.com/nezhahq/nezha.git
synced 2025-01-22 12:48:14 -05:00
refactor: improve performance
Some checks are pending
CodeQL / Analyze (go) (push) Waiting to run
CodeQL / Analyze (javascript) (push) Waiting to run
Contributors / contributors (push) Waiting to run
Sync / sync-to-jihulab (push) Waiting to run
Run Tests / tests (macos) (push) Waiting to run
Run Tests / tests (ubuntu) (push) Waiting to run
Run Tests / tests (windows) (push) Waiting to run
Some checks are pending
CodeQL / Analyze (go) (push) Waiting to run
CodeQL / Analyze (javascript) (push) Waiting to run
Contributors / contributors (push) Waiting to run
Sync / sync-to-jihulab (push) Waiting to run
Run Tests / tests (macos) (push) Waiting to run
Run Tests / tests (ubuntu) (push) Waiting to run
Run Tests / tests (windows) (push) Waiting to run
This commit is contained in:
parent
a550709c95
commit
7c8ac7ae5a
@ -8,6 +8,7 @@ import (
|
|||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
|
||||||
"github.com/nezhahq/nezha/model"
|
"github.com/nezhahq/nezha/model"
|
||||||
|
"github.com/nezhahq/nezha/pkg/utils"
|
||||||
"github.com/nezhahq/nezha/service/singleton"
|
"github.com/nezhahq/nezha/service/singleton"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -208,7 +209,7 @@ func batchBlockOnlineUser(c *gin.Context) (any, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := singleton.BlockByIPs(list); err != nil {
|
if err := singleton.BlockByIPs(utils.Unique(list)); err != nil {
|
||||||
return nil, newGormError("%v", err)
|
return nil, newGormError("%v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
"github.com/nezhahq/nezha/model"
|
"github.com/nezhahq/nezha/model"
|
||||||
|
"github.com/nezhahq/nezha/pkg/utils"
|
||||||
"github.com/nezhahq/nezha/service/singleton"
|
"github.com/nezhahq/nezha/service/singleton"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -68,7 +69,7 @@ func batchDeleteBlockedAddress(c *gin.Context) (any, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := model.BatchClearIP(singleton.DB, list); err != nil {
|
if err := model.BatchClearIP(singleton.DB, utils.Unique(list)); err != nil {
|
||||||
return nil, newGormError("%v", err)
|
return nil, newGormError("%v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,3 +152,15 @@ func MapValuesToSlice[Map ~map[K]V, K comparable, V any](m Map) []V {
|
|||||||
s := make([]V, 0, len(m))
|
s := make([]V, 0, len(m))
|
||||||
return slices.AppendSeq(s, maps.Values(m))
|
return slices.AppendSeq(s, maps.Values(m))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Unique[T comparable](s []T) []T {
|
||||||
|
m := make(map[T]struct{})
|
||||||
|
ret := make([]T, 0, len(s))
|
||||||
|
for _, v := range s {
|
||||||
|
if _, ok := m[v]; !ok {
|
||||||
|
m[v] = struct{}{}
|
||||||
|
ret = append(ret, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
@ -134,3 +134,49 @@ func TestBinaryToIPString(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUnique(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
input []string
|
||||||
|
output []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
input: []string{"a", "b", "c", "a", "b", "c"},
|
||||||
|
output: []string{"a", "b", "c"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: []string{"a", "b", "c"},
|
||||||
|
output: []string{"a", "b", "c"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: []string{"a", "a", "a"},
|
||||||
|
output: []string{"a"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"},
|
||||||
|
output: []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "a"},
|
||||||
|
output: []string{"a", "b", "c", "d", "e", "f", "g", "h", "i"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "a", "b", "c", "d", "e", "f", "g", "h", "i"},
|
||||||
|
output: []string{"a", "b", "c", "d", "e", "f", "g", "h", "i"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: []string{},
|
||||||
|
output: []string{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: []string{"a"},
|
||||||
|
output: []string{"a"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cases {
|
||||||
|
if !reflect.DeepEqual(Unique(c.input), c.output) {
|
||||||
|
t.Fatalf("Expected %v, but got %v", c.output, Unique(c.input))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user