Tracking Push Notifications in a structured logger (notifications.log) (#10823)

* Remove NotificationRegistry table and use structured logging

* Fix ackId for notification sent

* Notification logger at server level

* Remove unused i18n strings
This commit is contained in:
Elias Nahum
2019-05-13 10:53:46 -04:00
committed by Christopher Speller
parent 30061df036
commit 5b252e8736
23 changed files with 206 additions and 578 deletions

View File

@@ -195,10 +195,6 @@ func (s *LayeredStore) LinkMetadata() LinkMetadataStore {
return s.DatabaseLayer.LinkMetadata()
}
func (s *LayeredStore) NotificationRegistry() NotificationRegistryStore {
return s.DatabaseLayer.NotificationRegistry()
}
func (s *LayeredStore) MarkSystemRanUnitTests() {
s.DatabaseLayer.MarkSystemRanUnitTests()
}

View File

@@ -1,81 +0,0 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package sqlstore
import (
"net/http"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/store"
)
type SqlNotificationRegistryStore struct {
SqlStore
}
func NewSqlNotificationRegistryStore(sqlStore SqlStore) store.NotificationRegistryStore {
s := &SqlNotificationRegistryStore{sqlStore}
for _, db := range sqlStore.GetAllConns() {
table := db.AddTableWithName(model.NotificationRegistry{}, "NotificationRegistry").SetKeys(false, "AckId")
table.ColMap("AckId").SetMaxSize(26)
table.ColMap("UserId").SetMaxSize(26)
table.ColMap("PostId").SetMaxSize(26)
table.ColMap("DeviceId").SetMaxSize(512)
table.ColMap("Type").SetMaxSize(26)
table.ColMap("SendStatus").SetMaxSize(4096)
}
return s
}
func (s *SqlNotificationRegistryStore) CreateIndexesIfNotExists() {
s.CreateIndexIfNotExists("idx_notification_ack_id", "NotificationRegistry", "AckId")
s.CreateIndexIfNotExists("idx_notification_create_at", "NotificationRegistry", "CreateAt")
s.CreateIndexIfNotExists("idx_notification_receive_at", "NotificationRegistry", "ReceiveAt")
s.CreateIndexIfNotExists("idx_notification_post_id", "NotificationRegistry", "PostId")
s.CreateIndexIfNotExists("idx_notification_user_id", "NotificationRegistry", "UserId")
s.CreateIndexIfNotExists("idx_notification_type", "NotificationRegistry", "Type")
}
func (s *SqlNotificationRegistryStore) Save(notification *model.NotificationRegistry) (*model.NotificationRegistry, *model.AppError) {
notification.PreSave()
appErr := notification.IsValid()
if appErr != nil {
return nil, appErr
}
err := s.GetMaster().Insert(notification)
if err != nil {
appErr = model.NewAppError("SqlNotificationRegistryStore.Save", "store.sql_notification.save.app_error", nil, "id="+notification.AckId+", "+err.Error(), http.StatusInternalServerError)
return nil, appErr
}
return notification, nil
}
func (s *SqlNotificationRegistryStore) MarkAsReceived(ackId string, time int64) *model.AppError {
result, err := s.GetMaster().Exec("UPDATE NotificationRegistry SET ReceiveAt = :ReceiveAt WHERE AckId = :AckId AND ReceiveAt = 0", map[string]interface{}{"AckId": ackId, "ReceiveAt": time})
if err != nil {
return model.NewAppError("SqlNotificationRegistryStore.Save", "store.sql_notification.mark_as_received.app_error", nil, "id="+ackId+", "+err.Error(), http.StatusInternalServerError)
}
affected, err := result.RowsAffected()
if err != nil {
return model.NewAppError("SqlNotificationRegistryStore.Save", "store.sql_notification.mark_as_received.app_error", nil, "id="+ackId+", "+err.Error(), http.StatusInternalServerError)
} else if affected != 1 {
return model.NewAppError("SqlNotificationRegistryStore.Save", "store.sql_notification.mark_as_received.app_error", nil, "id="+ackId+", Message already received", http.StatusInternalServerError)
}
return nil
}
func (s *SqlNotificationRegistryStore) UpdateSendStatus(ackId, status string) *model.AppError {
_, err := s.GetMaster().Exec("UPDATE NotificationRegistry SET SendStatus = :Status WHERE AckId = :AckId", map[string]interface{}{"AckId": ackId, "Status": status})
if err != nil {
return model.NewAppError("SqlNotificationRegistryStore.Save", "store.sql_notification.update_status.app_error", nil, "id="+ackId+", "+err.Error(), http.StatusInternalServerError)
}
return nil
}

View File

@@ -1,14 +0,0 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package sqlstore
import (
"testing"
"github.com/mattermost/mattermost-server/store/storetest"
)
func TestNotificationRegistryStore(t *testing.T) {
StoreTest(t, storetest.TestNotificationRegistryStore)
}

View File

@@ -99,5 +99,4 @@ type SqlStore interface {
UserTermsOfService() store.UserTermsOfServiceStore
LinkMetadata() store.LinkMetadataStore
getQueryBuilder() sq.StatementBuilderType
NotificationRegistry() store.NotificationRegistryStore
}

View File

@@ -99,7 +99,6 @@ type SqlSupplierOldStores struct {
group store.GroupStore
UserTermsOfService store.UserTermsOfServiceStore
linkMetadata store.LinkMetadataStore
notificationRegistry store.NotificationRegistryStore
}
type SqlSupplier struct {
@@ -152,7 +151,6 @@ func NewSqlSupplier(settings model.SqlSettings, metrics einterfaces.MetricsInter
supplier.oldStores.TermsOfService = NewSqlTermsOfServiceStore(supplier, metrics)
supplier.oldStores.UserTermsOfService = NewSqlUserTermsOfServiceStore(supplier)
supplier.oldStores.linkMetadata = NewSqlLinkMetadataStore(supplier)
supplier.oldStores.notificationRegistry = NewSqlNotificationRegistryStore(supplier)
initSqlSupplierReactions(supplier)
initSqlSupplierRoles(supplier)
@@ -1055,10 +1053,6 @@ func (ss *SqlSupplier) LinkMetadata() store.LinkMetadataStore {
return ss.oldStores.linkMetadata
}
func (ss *SqlSupplier) NotificationRegistry() store.NotificationRegistryStore {
return ss.oldStores.notificationRegistry
}
func (ss *SqlSupplier) DropAllTables() {
ss.master.TruncateTables()
}

View File

@@ -78,7 +78,6 @@ type Store interface {
TotalMasterDbConnections() int
TotalReadDbConnections() int
TotalSearchDbConnections() int
NotificationRegistry() NotificationRegistryStore
}
type TeamStore interface {
@@ -605,9 +604,3 @@ type LinkMetadataStore interface {
Save(linkMetadata *model.LinkMetadata) StoreChannel
Get(url string, timestamp int64) StoreChannel
}
type NotificationRegistryStore interface {
Save(notification *model.NotificationRegistry) (*model.NotificationRegistry, *model.AppError)
MarkAsReceived(ackId string, time int64) *model.AppError
UpdateSendStatus(ackId, status string) *model.AppError
}

View File

@@ -780,22 +780,6 @@ func (_m *LayeredStoreDatabaseLayer) Next() store.LayeredStoreSupplier {
return r0
}
// NotificationRegistry provides a mock function with given fields:
func (_m *LayeredStoreDatabaseLayer) NotificationRegistry() store.NotificationRegistryStore {
ret := _m.Called()
var r0 store.NotificationRegistryStore
if rf, ok := ret.Get(0).(func() store.NotificationRegistryStore); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(store.NotificationRegistryStore)
}
}
return r0
}
// OAuth provides a mock function with given fields:
func (_m *LayeredStoreDatabaseLayer) OAuth() store.OAuthStore {
ret := _m.Called()

View File

@@ -1,70 +0,0 @@
// Code generated by mockery v1.0.0. DO NOT EDIT.
// Regenerate this file using `make store-mocks`.
package mocks
import mock "github.com/stretchr/testify/mock"
import model "github.com/mattermost/mattermost-server/model"
// NotificationRegistryStore is an autogenerated mock type for the NotificationRegistryStore type
type NotificationRegistryStore struct {
mock.Mock
}
// MarkAsReceived provides a mock function with given fields: ackId, time
func (_m *NotificationRegistryStore) MarkAsReceived(ackId string, time int64) *model.AppError {
ret := _m.Called(ackId, time)
var r0 *model.AppError
if rf, ok := ret.Get(0).(func(string, int64) *model.AppError); ok {
r0 = rf(ackId, time)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*model.AppError)
}
}
return r0
}
// Save provides a mock function with given fields: notification
func (_m *NotificationRegistryStore) Save(notification *model.NotificationRegistry) (*model.NotificationRegistry, *model.AppError) {
ret := _m.Called(notification)
var r0 *model.NotificationRegistry
if rf, ok := ret.Get(0).(func(*model.NotificationRegistry) *model.NotificationRegistry); ok {
r0 = rf(notification)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*model.NotificationRegistry)
}
}
var r1 *model.AppError
if rf, ok := ret.Get(1).(func(*model.NotificationRegistry) *model.AppError); ok {
r1 = rf(notification)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
}
// UpdateSendStatus provides a mock function with given fields: ackId, status
func (_m *NotificationRegistryStore) UpdateSendStatus(ackId string, status string) *model.AppError {
ret := _m.Called(ackId, status)
var r0 *model.AppError
if rf, ok := ret.Get(0).(func(string, string) *model.AppError); ok {
r0 = rf(ackId, status)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*model.AppError)
}
}
return r0
}

View File

@@ -482,22 +482,6 @@ func (_m *SqlStore) MarkSystemRanUnitTests() {
_m.Called()
}
// NotificationRegistry provides a mock function with given fields:
func (_m *SqlStore) NotificationRegistry() store.NotificationRegistryStore {
ret := _m.Called()
var r0 store.NotificationRegistryStore
if rf, ok := ret.Get(0).(func() store.NotificationRegistryStore); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(store.NotificationRegistryStore)
}
}
return r0
}
// OAuth provides a mock function with given fields:
func (_m *SqlStore) OAuth() store.OAuthStore {
ret := _m.Called()

View File

@@ -256,22 +256,6 @@ func (_m *Store) MarkSystemRanUnitTests() {
_m.Called()
}
// NotificationRegistry provides a mock function with given fields:
func (_m *Store) NotificationRegistry() store.NotificationRegistryStore {
ret := _m.Called()
var r0 store.NotificationRegistryStore
if rf, ok := ret.Get(0).(func() store.NotificationRegistryStore); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(store.NotificationRegistryStore)
}
}
return r0
}
// OAuth provides a mock function with given fields:
func (_m *Store) OAuth() store.OAuthStore {
ret := _m.Called()

View File

@@ -1,106 +0,0 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package storetest
import (
"testing"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/store"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNotificationRegistryStore(t *testing.T, ss store.Store) {
t.Run("Save", func(t *testing.T) { testNotificationRegistryStoreSave(t, ss) })
t.Run("MarkAsReceived", func(t *testing.T) { testNotificationRegistryStoreMarkAsReceived(t, ss) })
t.Run("UpdateSendStatus", func(t *testing.T) { testNotificationRegistryStoreUpdateSendStatus(t, ss) })
}
func testNotificationRegistryStoreSave(t *testing.T, ss store.Store) {
t.Run("should save notification registry", func(t *testing.T) {
notificationRegistry := &model.NotificationRegistry{
AckId: model.NewId(),
DeviceId: "apple_or_android_device_identifier",
PostId: model.NewId(),
UserId: model.NewId(),
Type: model.PUSH_TYPE_MESSAGE,
}
result, err := ss.NotificationRegistry().Save(notificationRegistry)
require.Nil(t, err)
require.IsType(t, notificationRegistry, result)
assert.NotZero(t, result.CreateAt)
})
t.Run("should save notification registry without AckId", func(t *testing.T) {
notificationRegistry := &model.NotificationRegistry{
DeviceId: "apple_or_android_device_identifier",
PostId: model.NewId(),
UserId: model.NewId(),
Type: model.PUSH_TYPE_MESSAGE,
}
result, err := ss.NotificationRegistry().Save(notificationRegistry)
require.Nil(t, err)
require.IsType(t, notificationRegistry, result)
assert.NotEmpty(t, result.AckId)
assert.NotZero(t, result.CreateAt)
})
t.Run("should fail to save invalid notification registry", func(t *testing.T) {
notificationRegistry := &model.NotificationRegistry{
DeviceId: "apple_or_android_device_identifier",
PostId: model.NewId(),
UserId: model.NewId(),
Type: "garbage",
}
_, err := ss.NotificationRegistry().Save(notificationRegistry)
assert.NotNil(t, err)
})
}
func testNotificationRegistryStoreMarkAsReceived(t *testing.T, ss store.Store) {
notificationRegistry := &model.NotificationRegistry{
DeviceId: "apple_or_android_device_identifier",
PostId: model.NewId(),
UserId: model.NewId(),
Type: model.PUSH_TYPE_MESSAGE,
}
t.Run("should update when the notification was received", func(t *testing.T) {
result, err := ss.NotificationRegistry().Save(notificationRegistry)
if err != nil {
t.Fatal("Notification should have been saved")
}
err = ss.NotificationRegistry().MarkAsReceived(result.AckId, model.GetMillis())
assert.Nil(t, err)
})
}
func testNotificationRegistryStoreUpdateSendStatus(t *testing.T, ss store.Store) {
notificationRegistry := &model.NotificationRegistry{
DeviceId: "apple_or_android_device_identifier",
UserId: model.NewId(),
Type: model.PUSH_TYPE_CLEAR,
}
t.Run("should update when the notification send status", func(t *testing.T) {
result, err := ss.NotificationRegistry().Save(notificationRegistry)
if err != nil {
t.Fatal("Notification should have been saved")
}
err = ss.NotificationRegistry().UpdateSendStatus(result.AckId, "Some Status")
assert.Nil(t, err)
})
}

View File

@@ -50,28 +50,24 @@ type Store struct {
GroupStore mocks.GroupStore
UserTermsOfServiceStore mocks.UserTermsOfServiceStore
LinkMetadataStore mocks.LinkMetadataStore
NotificationRegistryStore mocks.NotificationRegistryStore
}
func (s *Store) Team() store.TeamStore { return &s.TeamStore }
func (s *Store) Channel() store.ChannelStore { return &s.ChannelStore }
func (s *Store) Post() store.PostStore { return &s.PostStore }
func (s *Store) User() store.UserStore { return &s.UserStore }
func (s *Store) Bot() store.BotStore { return &s.BotStore }
func (s *Store) Audit() store.AuditStore { return &s.AuditStore }
func (s *Store) ClusterDiscovery() store.ClusterDiscoveryStore { return &s.ClusterDiscoveryStore }
func (s *Store) Compliance() store.ComplianceStore { return &s.ComplianceStore }
func (s *Store) Session() store.SessionStore { return &s.SessionStore }
func (s *Store) OAuth() store.OAuthStore { return &s.OAuthStore }
func (s *Store) System() store.SystemStore { return &s.SystemStore }
func (s *Store) Webhook() store.WebhookStore { return &s.WebhookStore }
func (s *Store) Command() store.CommandStore { return &s.CommandStore }
func (s *Store) CommandWebhook() store.CommandWebhookStore { return &s.CommandWebhookStore }
func (s *Store) Preference() store.PreferenceStore { return &s.PreferenceStore }
func (s *Store) License() store.LicenseStore { return &s.LicenseStore }
func (s *Store) NotificationRegistry() store.NotificationRegistryStore {
return &s.NotificationRegistryStore
}
func (s *Store) Team() store.TeamStore { return &s.TeamStore }
func (s *Store) Channel() store.ChannelStore { return &s.ChannelStore }
func (s *Store) Post() store.PostStore { return &s.PostStore }
func (s *Store) User() store.UserStore { return &s.UserStore }
func (s *Store) Bot() store.BotStore { return &s.BotStore }
func (s *Store) Audit() store.AuditStore { return &s.AuditStore }
func (s *Store) ClusterDiscovery() store.ClusterDiscoveryStore { return &s.ClusterDiscoveryStore }
func (s *Store) Compliance() store.ComplianceStore { return &s.ComplianceStore }
func (s *Store) Session() store.SessionStore { return &s.SessionStore }
func (s *Store) OAuth() store.OAuthStore { return &s.OAuthStore }
func (s *Store) System() store.SystemStore { return &s.SystemStore }
func (s *Store) Webhook() store.WebhookStore { return &s.WebhookStore }
func (s *Store) Command() store.CommandStore { return &s.CommandStore }
func (s *Store) CommandWebhook() store.CommandWebhookStore { return &s.CommandWebhookStore }
func (s *Store) Preference() store.PreferenceStore { return &s.PreferenceStore }
func (s *Store) License() store.LicenseStore { return &s.LicenseStore }
func (s *Store) Token() store.TokenStore { return &s.TokenStore }
func (s *Store) Emoji() store.EmojiStore { return &s.EmojiStore }
func (s *Store) Status() store.StatusStore { return &s.StatusStore }
@@ -116,7 +112,6 @@ func (s *Store) AssertExpectations(t mock.TestingT) bool {
&s.CommandWebhookStore,
&s.PreferenceStore,
&s.LicenseStore,
&s.NotificationRegistryStore,
&s.TokenStore,
&s.EmojiStore,
&s.StatusStore,