Skip to content

Commit

Permalink
Merge pull request #1057 from gobitfly/BEDS-397/add_ts_col_to_notific…
Browse files Browse the repository at this point in the history
…ation_history_tables

Beds 397/add ts col to notification history tables
  • Loading branch information
peterbitfly authored Oct 31, 2024
2 parents e66a3ce + 4e99362 commit 5ba21d7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- +goose Up
-- +goose StatementBegin
SELECT 'add ts column to notification history tables';
ALTER TABLE users_val_dashboards_notifications_history ADD COLUMN IF NOT EXISTS ts TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP;
CREATE INDEX IF NOT EXISTS idx_user_id_ts_dashboard_id_group_id_event_type ON users_val_dashboards_notifications_history (user_id, ts, dashboard_id, group_id, event_type);
ALTER TABLE machine_notifications_history ADD COLUMN IF NOT EXISTS ts TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP;
CREATE INDEX IF NOT EXISTS idx_user_id_ts_machine_id_machine_name_event_type ON machine_notifications_history (user_id, ts, machine_id, machine_name, event_type);
ALTER TABLE client_notifications_history ADD COLUMN IF NOT EXISTS ts TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP;
CREATE INDEX IF NOT EXISTS idx_user_id_ts_client ON client_notifications_history (user_id, ts, client);
ALTER TABLE network_notifications_history ADD COLUMN IF NOT EXISTS ts TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP;
CREATE INDEX IF NOT EXISTS idx_user_id_epoch_network_event_type ON network_notifications_history (user_id, epoch, network, event_type);

-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
SELECT 'remove ts column from notification history tables';
ALTER TABLE users_val_dashboards_notifications_history DROP COLUMN IF EXISTS ts;
DROP INDEX IF EXISTS idx_user_id_ts_dashboard_id_group_id_event_type;
ALTER TABLE machine_notifications_history DROP COLUMN IF EXISTS ts;
DROP INDEX IF EXISTS idx_user_id_ts_machine_id_machine_name_event_type;
ALTER TABLE client_notifications_history DROP COLUMN IF EXISTS ts;
DROP INDEX IF EXISTS idx_user_id_ts_client;
ALTER TABLE network_notifications_history DROP COLUMN IF EXISTS ts;
DROP INDEX IF EXISTS idx_user_id_epoch_network_event_type;
-- +goose StatementEnd
24 changes: 15 additions & 9 deletions backend/pkg/notification/queuing.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,12 @@ func queueNotifications(epoch uint64, notificationsByUserID types.NotificationsP
}

func ExportNotificationHistory(epoch uint64, notificationsByUserID types.NotificationsPerUserId) error {
epochTs := utils.EpochToTime(epoch)

dashboardNotificationHistoryInsertStmt, err := db.WriterDb.Preparex(`
INSERT INTO users_val_dashboards_notifications_history
(user_id, dashboard_id, group_id, epoch, event_type, event_count, details)
VALUES ($1, $2, $3, $4, $5, $6, $7)
(user_id, dashboard_id, group_id, epoch, event_type, event_count, details, ts)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
`)
if err != nil {
return fmt.Errorf("error preparing insert statement for dashboard notifications history: %w", err)
Expand All @@ -130,8 +132,8 @@ func ExportNotificationHistory(epoch uint64, notificationsByUserID types.Notific

machineNotificationHistoryInsertStmt, err := db.FrontendWriterDB.Preparex(`
INSERT INTO machine_notifications_history
(user_id, epoch, machine_id, machine_name, event_type, event_threshold)
VALUES ($1, $2, $3, $4, $5, $6)
(user_id, epoch, machine_id, machine_name, event_type, event_threshold, ts)
VALUES ($1, $2, $3, $4, $5, $6, $7)
`)
if err != nil {
return fmt.Errorf("error preparing insert statement for machine notifications history: %w", err)
Expand All @@ -140,8 +142,8 @@ func ExportNotificationHistory(epoch uint64, notificationsByUserID types.Notific

clientNotificationHistoryInsertStmt, err := db.FrontendWriterDB.Preparex(`
INSERT INTO client_notifications_history
(user_id, epoch, client, client_version, client_url)
VALUES ($1, $2, $3, $4, $5)
(user_id, epoch, client, client_version, client_url, ts)
VALUES ($1, $2, $3, $4, $5, $6)
`)
if err != nil {
return fmt.Errorf("error preparing insert statement for client notifications history: %w", err)
Expand All @@ -150,11 +152,11 @@ func ExportNotificationHistory(epoch uint64, notificationsByUserID types.Notific

networktNotificationHistoryInsertStmt, err := db.FrontendWriterDB.Preparex(`
INSERT INTO network_notifications_history
(user_id, epoch, network, event_type, event_threshold)
VALUES ($1, $2, $3, $4, $5)
(user_id, epoch, network, event_type, event_threshold, ts)
VALUES ($1, $2, $3, $4, $5, $6)
`)
if err != nil {
return fmt.Errorf("error preparing insert statement for client notifications history: %w", err)
return fmt.Errorf("error preparing insert statement for network notifications history: %w", err)
}
defer utils.ClosePreparedStatement(networktNotificationHistoryInsertStmt)

Expand All @@ -177,6 +179,7 @@ func ExportNotificationHistory(epoch uint64, notificationsByUserID types.Notific
eventName,
len(notifications),
details,
epochTs,
)
if err != nil {
log.Error(err, "error inserting into dashboard notifications history", 0)
Expand All @@ -195,6 +198,7 @@ func ExportNotificationHistory(epoch uint64, notificationsByUserID types.Notific
nTyped.MachineName,
eventName,
nTyped.EventThreshold,
epochTs,
)
if err != nil {
log.Error(err, "error inserting into machine notifications history", 0)
Expand All @@ -213,6 +217,7 @@ func ExportNotificationHistory(epoch uint64, notificationsByUserID types.Notific
nTyped.EthClient,
"",
"",
epochTs,
)
if err != nil {
log.Error(err, "error inserting into client notifications history", 0)
Expand All @@ -226,6 +231,7 @@ func ExportNotificationHistory(epoch uint64, notificationsByUserID types.Notific
utils.Config.Chain.Name,
eventName,
0,
epochTs,
)
if err != nil {
log.Error(err, "error inserting into network notifications history", 0)
Expand Down

0 comments on commit 5ba21d7

Please sign in to comment.