Skip to content

Commit 6b4ba38

Browse files
committed
feat: Add ACLLog to rueidiscompat
1 parent 569ab9f commit 6b4ba38

File tree

4 files changed

+108
-1
lines changed

4 files changed

+108
-1
lines changed

message.go

+76
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,15 @@ func (r RedisResult) AsClientInfo() (v *ClientInfo, err error) {
487487
return
488488
}
489489

490+
func (r RedisResult) AsACLLogEntry() (v []*ACLLogEntry, err error) {
491+
if r.err != nil {
492+
err = r.err
493+
} else {
494+
v, err = r.val.AsACLLogEntry()
495+
}
496+
return
497+
}
498+
490499
// IsCacheHit delegates to RedisMessage.IsCacheHit
491500
func (r RedisResult) IsCacheHit() bool {
492501
return r.val.IsCacheHit()
@@ -1557,6 +1566,73 @@ func (m *RedisMessage) AsClientInfo() (*ClientInfo, error) {
15571566
return info, nil
15581567
}
15591568

1569+
type ACLLogEntry struct {
1570+
Count int64
1571+
Reason string
1572+
Context string
1573+
Object string
1574+
Username string
1575+
AgeSeconds float64
1576+
ClientInfo *ClientInfo
1577+
EntryID int64
1578+
TimestampCreated int64
1579+
TimestampLastUpdated int64
1580+
}
1581+
1582+
func (m *RedisMessage) AsACLLogEntry() ([]*ACLLogEntry, error) {
1583+
arr, err := m.ToArray()
1584+
if err != nil {
1585+
return nil, err
1586+
1587+
}
1588+
logEntries := make([]*ACLLogEntry, 0, len(arr))
1589+
1590+
for _, msg := range arr {
1591+
log, err := msg.AsMap()
1592+
if err != nil {
1593+
return nil, err
1594+
}
1595+
entry := ACLLogEntry{}
1596+
1597+
if attr, ok := log["count"]; ok {
1598+
entry.Count, err = attr.AsInt64()
1599+
}
1600+
if attr, ok := log["reason"]; ok {
1601+
entry.Reason, err = attr.ToString()
1602+
}
1603+
if attr, ok := log["context"]; ok {
1604+
entry.Context, err = attr.ToString()
1605+
}
1606+
if attr, ok := log["object"]; ok {
1607+
entry.Object, err = attr.ToString()
1608+
}
1609+
if attr, ok := log["username"]; ok {
1610+
entry.Username, err = attr.ToString()
1611+
}
1612+
if attr, ok := log["age-seconds"]; ok {
1613+
entry.AgeSeconds, err = attr.AsFloat64()
1614+
}
1615+
if attr, ok := log["client-info"]; ok {
1616+
entry.ClientInfo, err = attr.AsClientInfo()
1617+
}
1618+
if attr, ok := log["entry-id"]; ok {
1619+
entry.EntryID, err = attr.AsInt64()
1620+
}
1621+
if attr, ok := log["timestamp-created"]; ok {
1622+
entry.TimestampCreated, err = attr.AsInt64()
1623+
}
1624+
if attr, ok := log["timestamp-last-updated"]; ok {
1625+
entry.TimestampLastUpdated, err = attr.AsInt64()
1626+
}
1627+
1628+
if err != nil {
1629+
return nil, err
1630+
}
1631+
logEntries = append(logEntries, &entry)
1632+
}
1633+
return logEntries, nil
1634+
}
1635+
15601636
// ToMap check if message is a redis RESP3 map response, and return it
15611637
func (m *RedisMessage) ToMap() (map[string]RedisMessage, error) {
15621638
if m.IsMap() {

rueidiscompat/adapter.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ type CoreCmdable interface {
415415
GeoHash(ctx context.Context, key string, members ...string) *StringSliceCmd
416416

417417
ACLDryRun(ctx context.Context, username string, command ...any) *StringCmd
418-
// TODO ACLLog(ctx context.Context, count int64) *ACLLogCmd
418+
ACLLog(ctx context.Context, count int64) *ACLLogCmd
419419
// TODO ACLLogReset(ctx context.Context) *StatusCmd
420420

421421
ModuleLoadex(ctx context.Context, conf *ModuleLoadexConfig) *StringCmd
@@ -3202,6 +3202,12 @@ func (c *Compat) ACLDryRun(ctx context.Context, username string, command ...any)
32023202
return newStringCmd(resp)
32033203
}
32043204

3205+
func (c *Compat) ACLLog(ctx context.Context, count int64) *ACLLogCmd {
3206+
cmd := c.client.B().AclLog().Count(count).Build()
3207+
resp := c.client.Do(ctx, cmd)
3208+
return newACLLogCmd(resp)
3209+
}
3210+
32053211
func (c *Compat) doPrimaries(ctx context.Context, fn func(c rueidis.Client) error) error {
32063212
if c.pOnly {
32073213
return fn(c.client)

rueidiscompat/command.go

+19
Original file line numberDiff line numberDiff line change
@@ -4611,6 +4611,25 @@ func (cmd *ClientInfoCmd) from(res rueidis.RedisResult) {
46114611
cmd.SetVal(info)
46124612
}
46134613

4614+
type ACLLogCmd struct {
4615+
baseCmd[[]*rueidis.ACLLogEntry]
4616+
}
4617+
4618+
func (cmd *ACLLogCmd) from(res rueidis.RedisResult) {
4619+
log, err := res.AsACLLogEntry()
4620+
if err != nil {
4621+
cmd.SetErr(err)
4622+
return
4623+
}
4624+
cmd.SetVal(log)
4625+
}
4626+
4627+
func newACLLogCmd(res rueidis.RedisResult) *ACLLogCmd {
4628+
cmd := &ACLLogCmd{}
4629+
cmd.from(res)
4630+
return cmd
4631+
}
4632+
46144633
// ModuleLoadexConfig struct is used to specify the arguments for the MODULE LOADEX command of redis.
46154634
// `MODULE LOADEX path [CONFIG name value [CONFIG name value ...]] [ARGS args [args ...]]`
46164635
type ModuleLoadexConfig struct {

rueidiscompat/pipeline.go

+6
Original file line numberDiff line numberDiff line change
@@ -2089,6 +2089,12 @@ func (c *Pipeline) ACLDryRun(ctx context.Context, username string, command ...an
20892089
return ret
20902090
}
20912091

2092+
func (c *Pipeline) ACLLog(ctx context.Context, count int64) *ACLLogCmd {
2093+
ret := c.comp.ACLLog(ctx, count)
2094+
c.rets = append(c.rets, ret)
2095+
return ret
2096+
}
2097+
20922098
func (c *Pipeline) TFunctionLoad(ctx context.Context, lib string) *StatusCmd {
20932099
ret := c.comp.TFunctionLoad(ctx, lib)
20942100
c.rets = append(c.rets, ret)

0 commit comments

Comments
 (0)