feat(activitylog): store activities per resource
Signed-off-by: jkoberg <jkoberg@owncloud.com>
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
|
||||
"github.com/cs3org/reva/v2/pkg/events"
|
||||
"github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool"
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/log"
|
||||
"github.com/owncloud/ocis/v2/services/activitylog/pkg/config"
|
||||
microstore "go-micro.dev/v4/store"
|
||||
@@ -19,6 +21,7 @@ type Options struct {
|
||||
Stream events.Stream
|
||||
RegisteredEvents []events.Unmarshaller
|
||||
Store microstore.Store
|
||||
GatewaySelector pool.Selectable[gateway.GatewayAPIClient]
|
||||
}
|
||||
|
||||
// Logger configures a logger for the activitylog service
|
||||
@@ -62,3 +65,10 @@ func Store(store microstore.Store) Option {
|
||||
o.Store = store
|
||||
}
|
||||
}
|
||||
|
||||
// GatewaySelector adds a grpc client selector for the gateway service
|
||||
func GatewaySelector(gatewaySelector pool.Selectable[gateway.GatewayAPIClient]) Option {
|
||||
return func(o *Options) {
|
||||
o.GatewaySelector = gatewaySelector
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
|
||||
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
|
||||
"github.com/cs3org/reva/v2/pkg/events"
|
||||
"github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool"
|
||||
"github.com/cs3org/reva/v2/pkg/storagespace"
|
||||
"github.com/cs3org/reva/v2/pkg/utils"
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/log"
|
||||
"github.com/owncloud/ocis/v2/services/activitylog/pkg/config"
|
||||
microstore "go-micro.dev/v4/store"
|
||||
@@ -24,6 +30,7 @@ type ActivitylogService struct {
|
||||
log log.Logger
|
||||
events <-chan events.Event
|
||||
store microstore.Store
|
||||
gws pool.Selectable[gateway.GatewayAPIClient]
|
||||
}
|
||||
|
||||
// New is what you need to implement.
|
||||
@@ -51,6 +58,7 @@ func New(opts ...Option) (*ActivitylogService, error) {
|
||||
cfg: o.Config,
|
||||
events: ch,
|
||||
store: o.Store,
|
||||
gws: o.GatewaySelector,
|
||||
}
|
||||
|
||||
return s, nil
|
||||
@@ -59,10 +67,87 @@ func New(opts ...Option) (*ActivitylogService, error) {
|
||||
// Run runs the service
|
||||
func (a *ActivitylogService) Run() error {
|
||||
for e := range a.events {
|
||||
var err error
|
||||
switch ev := e.Event.(type) {
|
||||
case events.PostprocessingFinished:
|
||||
fmt.Println("PostprocessingFinished event received", ev)
|
||||
case events.UploadReady:
|
||||
err = a.addActivity(ev.FileRef, e.ID, utils.TSToTime(ev.Timestamp))
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
a.log.Error().Err(err).Interface("event", e).Msg("could not process event")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *ActivitylogService) addActivity(initRef *provider.Reference, eventID string, timestamp time.Time) error {
|
||||
gwc, err := a.gws.Next()
|
||||
if err != nil {
|
||||
return fmt.Errorf("cant get gateway client: %w", err)
|
||||
}
|
||||
|
||||
ctx, err := utils.GetServiceUserContext(a.cfg.ServiceAccount.ServiceAccountID, gwc, a.cfg.ServiceAccount.ServiceAccountSecret)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cant get service user context: %w", err)
|
||||
}
|
||||
|
||||
var info *provider.ResourceInfo
|
||||
depth, ref := 0, initRef
|
||||
for {
|
||||
if err := a.addActivityToReference(ref, eventID, depth, timestamp); err != nil {
|
||||
return fmt.Errorf("could not store activity: %w", err)
|
||||
}
|
||||
|
||||
if info != nil && utils.IsSpaceRoot(info) {
|
||||
return nil
|
||||
}
|
||||
|
||||
info, err = utils.GetResource(ctx, ref, gwc)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not get resource info: %w", err)
|
||||
}
|
||||
|
||||
depth++
|
||||
ref = &provider.Reference{ResourceId: info.GetParentId()}
|
||||
}
|
||||
}
|
||||
|
||||
func (a *ActivitylogService) addActivityToReference(ref *provider.Reference, eventID string, depth int, timestamp time.Time) error {
|
||||
fileID, err := storagespace.FormatReference(ref)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return a.storeActivity(fileID, Activity{
|
||||
EventID: eventID,
|
||||
Depth: depth,
|
||||
Timestamp: timestamp,
|
||||
})
|
||||
}
|
||||
|
||||
func (a *ActivitylogService) storeActivity(resourceID string, activity Activity) error {
|
||||
records, err := a.store.Read(resourceID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var activities []Activity
|
||||
if len(records) > 0 {
|
||||
if err := json.Unmarshal(records[0].Value, &activities); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: max len check?
|
||||
activities = append(activities, activity)
|
||||
|
||||
b, err := json.Marshal(activities)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return a.store.Write(µstore.Record{
|
||||
Key: resourceID,
|
||||
Value: b,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user