File size: 1,932 Bytes
d6f631f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | package adapter
import (
"context"
"encoding/json"
"fmt"
"github.com/redis/go-redis/v9"
"github.com/openmeterio/openmeter/openmeter/progressmanager/entity"
"github.com/openmeterio/openmeter/pkg/models"
)
// keyPrefix is the prefix for progress data in the Redis store.
// All progress keys will be stored as "progress:<namespace>:<id>"
const staticKeyPrefix = "progress:"
// GetProgress retrieves the progress
func (a *adapter) GetProgress(ctx context.Context, input entity.GetProgressInput) (*entity.Progress, error) {
if err := input.Validate(); err != nil {
return nil, fmt.Errorf("validate get progress input: %w", err)
}
var progress entity.Progress
cmd := a.redis.Get(ctx, a.getKey(input.ProgressID))
if cmd.Err() != nil {
if cmd.Err() == redis.Nil {
return nil, models.NewGenericNotFoundError(
fmt.Errorf("progress not found for id: %s", input.ProgressID.ID),
)
}
return nil, fmt.Errorf("get progress: %w", cmd.Err())
}
if err := json.Unmarshal([]byte(cmd.Val()), &progress); err != nil {
return nil, fmt.Errorf("unmarshal progress: %w", err)
}
return &progress, nil
}
// UpsertProgress updates the progress
func (a *adapter) UpsertProgress(ctx context.Context, input entity.UpsertProgressInput) error {
if err := input.Validate(); err != nil {
return fmt.Errorf("validate upsert progress input: %w", err)
}
data, err := json.Marshal(input.Progress)
if err != nil {
return fmt.Errorf("marshal progress: %w", err)
}
cmd := a.redis.Set(ctx, a.getKey(input.ProgressID), data, a.expiration)
if cmd.Err() != nil {
return fmt.Errorf("set progress: %w", cmd.Err())
}
return nil
}
// getKey returns the key for the KV store
func (a *adapter) getKey(id entity.ProgressID) string {
if a.keyPrefix == "" {
return fmt.Sprintf("%s:%s:%s", staticKeyPrefix, id.Namespace, id.ID)
}
return fmt.Sprintf("%s:%s:%s:%s", a.keyPrefix, staticKeyPrefix, id.Namespace, id.ID)
}
|