diff --git a/internal/rdb/inspect.go b/internal/rdb/inspect.go index 7539643..2fa478b 100644 --- a/internal/rdb/inspect.go +++ b/internal/rdb/inspect.go @@ -3,6 +3,7 @@ package rdb import ( "encoding/json" "fmt" + "strings" "time" "github.com/go-redis/redis/v7" @@ -93,6 +94,23 @@ func (r *RDB) CurrentStats() (*Stats, error) { }, nil } +// RedisInfo returns a map of redis info. +func (r *RDB) RedisInfo() (map[string]string, error) { + res, err := r.client.Info().Result() + if err != nil { + return nil, err + } + info := make(map[string]string) + lines := strings.Split(res, "\r\n") + for _, l := range lines { + kv := strings.Split(l, ":") + if len(kv) == 2 { + info[kv[0]] = kv[1] + } + } + return info, nil +} + // ListEnqueued returns all enqueued tasks that are ready to be processed. func (r *RDB) ListEnqueued() ([]*EnqueuedTask, error) { data, err := r.client.LRange(base.DefaultQueue, 0, -1).Result() diff --git a/internal/rdb/inspect_test.go b/internal/rdb/inspect_test.go index 891e64e..3374bef 100644 --- a/internal/rdb/inspect_test.go +++ b/internal/rdb/inspect_test.go @@ -120,7 +120,30 @@ func TestCurrentStats(t *testing.T) { continue } } +} +func TestRedisInfo(t *testing.T) { + r := setup(t) + + info, err := r.RedisInfo() + if err != nil { + t.Fatalf("RDB.RedisInfo() returned error: %v", err) + } + + wantKeys := []string{ + "redis_version", + "uptime_in_days", + "connected_clients", + "used_memory_human", + "used_memory_peak_human", + "used_memory_peak_perc", + } + + for _, key := range wantKeys { + if _, ok := info[key]; !ok { + t.Errorf("RDB.RedisInfo() = %v is missing entry for %q", info, key) + } + } } func TestListEnqueued(t *testing.T) {