Refactor seen service to use database

This commit is contained in:
Ben Toogood
2021-02-04 11:18:33 +00:00
parent b02d4dacb5
commit 6ca55ba18d
6 changed files with 64 additions and 358 deletions

View File

@@ -1,93 +0,0 @@
package domain
import (
"time"
"github.com/micro/micro/v3/service/model"
"github.com/micro/micro/v3/service/store"
)
// Seen is the object which represents a user seeing a resource
type Seen struct {
ID string
UserID string
ResourceID string
ResourceType string
Timestamp time.Time
}
type Domain struct {
db model.Model
}
var (
userIDIndex = model.ByEquality("UserID")
resourceIDIndex = model.ByEquality("ResourceID")
resourceTypeIndex = model.ByEquality("ResourceType")
)
func New(store store.Store) *Domain {
db := model.New(store, Seen{}, []model.Index{
userIDIndex, resourceIDIndex, resourceTypeIndex,
}, &model.ModelOptions{})
return &Domain{db: db}
}
// Create a seen object in the store
func (d *Domain) Create(s Seen) error {
return d.db.Create(s)
}
// Delete a seen object from the store
func (d *Domain) Delete(s Seen) error {
// load all the users objects and then delete only the ones which match the resource, unfortunately
// the model doesn't yet support querying by multiple columns
var all []Seen
if err := d.db.Read(model.Equals("UserID", s.UserID), &all); err != nil {
return err
}
for _, a := range all {
if s.ResourceID != a.ResourceID {
continue
}
if s.ResourceType != s.ResourceType {
continue
}
q := model.Equals("ID", a.ID)
q.Order.Type = model.OrderTypeUnordered
if err := d.db.Delete(q); err != nil {
return err
}
}
return nil
}
// Read the timestamps from the store
func (d *Domain) Read(userID, resourceType string, resourceIDs []string) (map[string]time.Time, error) {
// load all the users objects and then return only the timestamps for the ones which match the
// resource, unfortunately the model doesn't yet support querying by multiple columns
var all []Seen
if err := d.db.Read(model.Equals("UserID", userID), &all); err != nil {
return nil, err
}
result := map[string]time.Time{}
for _, a := range all {
if a.ResourceType != resourceType {
continue
}
for _, id := range resourceIDs {
if id != a.ResourceID {
continue
}
result[id] = a.Timestamp
break
}
}
return result, nil
}

View File

@@ -4,8 +4,7 @@ import (
"context"
"time"
"github.com/google/uuid"
"github.com/micro/services/seen/domain"
"gorm.io/gorm"
"github.com/micro/micro/v3/service/errors"
"github.com/micro/micro/v3/service/logger"
@@ -22,7 +21,14 @@ var (
)
type Seen struct {
Domain *domain.Domain
DB *gorm.DB
}
type SeenInstance struct {
UserID string `gorm:"uniqueIndex:user_resource"`
ResourceID string `gorm:"uniqueIndex:user_resource"`
ResourceType string `gorm:"uniqueIndex:user_resource"`
Timestamp time.Time
}
// Set a resource as seen by a user. If no timestamp is provided, the current time is used.
@@ -44,13 +50,12 @@ func (s *Seen) Set(ctx context.Context, req *pb.SetRequest, rsp *pb.SetResponse)
}
// write the object to the store
err := s.Domain.Create(domain.Seen{
ID: uuid.New().String(),
err := s.DB.Create(SeenInstance{
UserID: req.UserId,
ResourceID: req.ResourceId,
ResourceType: req.ResourceType,
Timestamp: req.Timestamp.AsTime(),
})
}).Error
if err != nil {
logger.Errorf("Error with store: %v", err)
return ErrStore
@@ -74,11 +79,11 @@ func (s *Seen) Unset(ctx context.Context, req *pb.UnsetRequest, rsp *pb.UnsetRes
}
// delete the object from the store
err := s.Domain.Delete(domain.Seen{
err := s.DB.Delete(SeenInstance{}, SeenInstance{
UserID: req.UserId,
ResourceID: req.ResourceId,
ResourceType: req.ResourceType,
})
}).Error
if err != nil {
logger.Errorf("Error with store: %v", err)
return ErrStore
@@ -103,16 +108,18 @@ func (s *Seen) Read(ctx context.Context, req *pb.ReadRequest, rsp *pb.ReadRespon
}
// query the store
data, err := s.Domain.Read(req.UserId, req.ResourceType, req.ResourceIds)
if err != nil {
q := s.DB.Where(SeenInstance{UserID: req.UserId, ResourceType: req.ResourceType})
q = q.Where("resource_id IN (?)", req.ResourceIds)
var data []SeenInstance
if err := q.Find(&data).Error; err != nil {
logger.Errorf("Error with store: %v", err)
return ErrStore
}
// serialize the response
rsp.Timestamps = make(map[string]*timestamppb.Timestamp, len(data))
for uid, ts := range data {
rsp.Timestamps[uid] = timestamppb.New(ts)
for _, i := range data {
rsp.Timestamps[i.ResourceID] = timestamppb.New(i.Timestamp)
}
return nil

View File

@@ -5,20 +5,33 @@ import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/google/uuid"
"github.com/micro/micro/v3/service/store/memory"
"github.com/micro/services/seen/domain"
"github.com/micro/services/seen/handler"
pb "github.com/micro/services/seen/proto"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/timestamppb"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
func newHandler() *handler.Seen {
return &handler.Seen{
Domain: domain.New(memory.NewStore()),
func testHandler(t *testing.T) *handler.Seen {
// connect to the database
db, err := gorm.Open(postgres.Open("postgresql://postgres@localhost:5433/postgres?sslmode=disable"), &gorm.Config{})
if err != nil {
t.Fatalf("Error connecting to database: %v", err)
}
// migrate the database
if err := db.AutoMigrate(&handler.SeenInstance{}); err != nil {
t.Fatalf("Error migrating database: %v", err)
}
// clean any data from a previous run
if err := db.Exec("TRUNCATE TABLE seen_instances CASCADE").Error; err != nil {
t.Fatalf("Error cleaning database: %v", err)
}
return &handler.Seen{DB: db}
}
func TestSet(t *testing.T) {
@@ -63,7 +76,7 @@ func TestSet(t *testing.T) {
},
}
h := newHandler()
h := testHandler(t)
for _, tc := range tt {
t.Run(tc.Name, func(t *testing.T) {
err := h.Set(context.TODO(), &pb.SetRequest{
@@ -79,7 +92,7 @@ func TestSet(t *testing.T) {
}
func TestUnset(t *testing.T) {
// seed some test data
h := newHandler()
h := testHandler(t)
seed := &pb.SetRequest{
UserId: uuid.New().String(),
ResourceId: uuid.New().String(),
@@ -142,7 +155,7 @@ func TestUnset(t *testing.T) {
func TestRead(t *testing.T) {
tn := time.Now()
h := newHandler()
h := testHandler(t)
// seed some test data
td := []struct {

View File

@@ -1,15 +1,18 @@
package main
import (
"github.com/micro/services/seen/domain"
"github.com/micro/services/seen/handler"
pb "github.com/micro/services/seen/proto"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"github.com/micro/micro/v3/service"
"github.com/micro/micro/v3/service/config"
"github.com/micro/micro/v3/service/logger"
"github.com/micro/micro/v3/service/store"
)
var dbAddress = "postgresql://postgres@localhost:5432/seen?sslmode=disable"
func main() {
// Create service
srv := service.New(
@@ -17,10 +20,22 @@ func main() {
service.Version("latest"),
)
// Connect to the database
cfg, err := config.Get("seen.database")
if err != nil {
logger.Fatalf("Error loading config: %v", err)
}
addr := cfg.String(dbAddress)
db, err := gorm.Open(postgres.Open(addr), &gorm.Config{})
if err != nil {
logger.Fatalf("Error connecting to database: %v", err)
}
if err := db.AutoMigrate(&handler.SeenInstance{}); err != nil {
logger.Fatalf("Error migrating database: %v", err)
}
// Register handler
pb.RegisterSeenHandler(srv.Server(), &handler.Seen{
Domain: domain.New(store.DefaultStore),
})
pb.RegisterSeenHandler(srv.Server(), &handler.Seen{DB: db})
// Run service
if err := srv.Run(); err != nil {