7999d24f81
this commit updates our monkit dependency to the v3 version where it outputs in an influx style. this makes discovery much easier as many tools are built to look at it this way. graphite and rothko will suffer some due to no longer being a tree based on dots. hopefully time will exist to update rothko to index based on the new metric format. it adds an influx output for the statreceiver so that we can write to influxdb v1 or v2 directly. Change-Id: Iae9f9494a6d29cfbd1f932a5e71a891b490415ff
94 lines
2.0 KiB
Go
94 lines
2.0 KiB
Go
// Copyright (C) 2019 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package notifications
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
|
"github.com/spacemonkeygo/monkit/v3"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
var (
|
|
mon = monkit.Package()
|
|
)
|
|
|
|
// Service is the notification service between storage nodes and satellites.
|
|
// architecture: Service
|
|
type Service struct {
|
|
log *zap.Logger
|
|
db DB
|
|
}
|
|
|
|
// NewService creates a new notification service.
|
|
func NewService(log *zap.Logger, db DB) *Service {
|
|
return &Service{
|
|
log: log,
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
// Receive - receives notifications from satellite and Insert them into DB.
|
|
func (service *Service) Receive(ctx context.Context, newNotification NewNotification) (Notification, error) {
|
|
notification, err := service.db.Insert(ctx, newNotification)
|
|
if err != nil {
|
|
return Notification{}, err
|
|
}
|
|
|
|
return notification, nil
|
|
}
|
|
|
|
// Read - change notification status to Read by ID.
|
|
func (service *Service) Read(ctx context.Context, notificationID uuid.UUID) (err error) {
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
err = service.db.Read(ctx, notificationID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// ReadAll - change status of all user's notifications to Read.
|
|
func (service *Service) ReadAll(ctx context.Context) (err error) {
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
err = service.db.ReadAll(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// List - shows the list of paginated notifications.
|
|
func (service *Service) List(ctx context.Context, cursor Cursor) (_ Page, err error) {
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
notificationPage, err := service.db.List(ctx, cursor)
|
|
if err != nil {
|
|
return Page{}, err
|
|
}
|
|
|
|
if notificationPage.Notifications == nil {
|
|
notificationPage = Page{Notifications: []Notification{}}
|
|
}
|
|
|
|
return notificationPage, nil
|
|
}
|
|
|
|
// UnreadAmount - returns amount on notifications with value is_read = nil.
|
|
func (service *Service) UnreadAmount(ctx context.Context) (_ int, err error) {
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
amount, err := service.db.UnreadAmount(ctx)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return amount, nil
|
|
}
|