Commit 7ad48fe6 authored by luxq's avatar luxq

add getListByPage

parent 009e92dd
...@@ -31,6 +31,24 @@ func (g baseService) List() ([]string, error) { ...@@ -31,6 +31,24 @@ func (g baseService) List() ([]string, error) {
return res, nil return res, nil
} }
func (g baseService) ListByPage(pageSize int, pageNum int) ([]string, error) {
all, err := getAll(g.rdb, g.service)
if err != nil {
return nil, err
}
sort.Sort(ServiceInfoList(all))
start := pageSize * pageNum
end := start + pageSize
if len(all) < end {
end = len(all)
}
var res []string
for _, v := range all[start:end] {
res = append(res, string(v.Message()))
}
return res, nil
}
func (g baseService) ServiceInfo(serviceid string) (string, error) { func (g baseService) ServiceInfo(serviceid string) (string, error) {
info, err := getOne(g.rdb, g.service, serviceid) info, err := getOne(g.rdb, g.service, serviceid)
if err != nil { if err != nil {
...@@ -53,12 +71,25 @@ func getAll(rdb *redis.Client, serviceType common.ServiceType) (ServiceInfoList, ...@@ -53,12 +71,25 @@ func getAll(rdb *redis.Client, serviceType common.ServiceType) (ServiceInfoList,
return nil, err return nil, err
} }
var list []ServiceInfo var list []ServiceInfo
var ctx = context.Background()
var resList = make([]*redis.MapStringStringCmd, 0)
_, err = rdb.Pipelined(ctx, func(pip redis.Pipeliner) error {
for _, key := range keys { for _, key := range keys {
res := rdb.HGetAll(context.Background(), key) cmd := pip.HGetAll(ctx, key)
resList = append(resList, cmd)
}
return nil
})
if err != nil {
return nil, err
}
for _, res := range resList {
if info := parseInfo(serviceType, res); info != nil { if info := parseInfo(serviceType, res); info != nil {
list = append(list, info) list = append(list, info)
} }
} }
return list, nil return list, nil
} }
......
...@@ -5,6 +5,7 @@ import "encoding/json" ...@@ -5,6 +5,7 @@ import "encoding/json"
type ServiceQuery interface { type ServiceQuery interface {
ModuleName() string ModuleName() string
List() ([]string, error) List() ([]string, error)
ListByPage(pageSize int, pageNum int) ([]string, error)
ServiceInfo(instance string) (string, error) ServiceInfo(instance string) (string, error)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment