From b05a00a8d5f5841cce23f65066a7e0e7ec6958f1 Mon Sep 17 00:00:00 2001 From: Noah Hilverling Date: Tue, 27 Apr 2021 12:11:25 +0200 Subject: [PATCH] icingaredis.Client: Add StreamLastId() --- pkg/icingaredis/client.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pkg/icingaredis/client.go b/pkg/icingaredis/client.go index 6cc84cca..777de056 100644 --- a/pkg/icingaredis/client.go +++ b/pkg/icingaredis/client.go @@ -145,3 +145,19 @@ func (c *Client) HMYield(ctx context.Context, key string, count int, concurrent return pairs, com.WaitAsync(g) } + +// StreamLastId fetches the last message of a stream and returns its ID. +func (c *Client) StreamLastId(ctx context.Context, stream string) (string, error) { + lastId := "0-0" + + messages, err := c.XRevRangeN(ctx, stream, "+", "-", 1).Result() + if err != nil { + return "", err + } + + for _, message := range messages { + lastId = message.ID + } + + return lastId, nil +}