From 733668f18f00063ec3108c296b9a9b7092d5c6b6 Mon Sep 17 00:00:00 2001 From: peter <1674920+peterbitfly@users.noreply.github.com> Date: Thu, 31 Oct 2024 11:14:24 +0000 Subject: [PATCH 1/2] feat(notifications): add ts column to notification history tables --- ..._column_to_notification_history_tables.sql | 18 ++++++++++++++ backend/pkg/notification/queuing.go | 24 ++++++++++++------- 2 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 backend/pkg/commons/db/migrations/postgres/20241031105829_add_ts_column_to_notification_history_tables.sql diff --git a/backend/pkg/commons/db/migrations/postgres/20241031105829_add_ts_column_to_notification_history_tables.sql b/backend/pkg/commons/db/migrations/postgres/20241031105829_add_ts_column_to_notification_history_tables.sql new file mode 100644 index 000000000..2f07a469f --- /dev/null +++ b/backend/pkg/commons/db/migrations/postgres/20241031105829_add_ts_column_to_notification_history_tables.sql @@ -0,0 +1,18 @@ +-- +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; +ALTER TABLE machine_notifications_history ADD COLUMN IF NOT EXISTS ts TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP; +ALTER TABLE client_notifications_history ADD COLUMN IF NOT EXISTS ts TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP; +ALTER TABLE network_notifications_history ADD COLUMN IF NOT EXISTS ts TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP; + +-- +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; +ALTER TABLE machine_notifications_history DROP COLUMN IF EXISTS ts; +ALTER TABLE client_notifications_history DROP COLUMN IF EXISTS ts; +ALTER TABLE network_notifications_history DROP COLUMN IF EXISTS ts; +-- +goose StatementEnd diff --git a/backend/pkg/notification/queuing.go b/backend/pkg/notification/queuing.go index 5165963b2..be3ec18a5 100644 --- a/backend/pkg/notification/queuing.go +++ b/backend/pkg/notification/queuing.go @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) From f0f1fea4ad546baa08fee98ee0c39ea9494af9d6 Mon Sep 17 00:00:00 2001 From: peter <1674920+peterbitfly@users.noreply.github.com> Date: Thu, 31 Oct 2024 13:46:23 +0000 Subject: [PATCH 2/2] feat(notification): add db migration file --- ...05829_add_ts_column_to_notification_history_tables.sql | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/backend/pkg/commons/db/migrations/postgres/20241031105829_add_ts_column_to_notification_history_tables.sql b/backend/pkg/commons/db/migrations/postgres/20241031105829_add_ts_column_to_notification_history_tables.sql index 2f07a469f..b43b4ec35 100644 --- a/backend/pkg/commons/db/migrations/postgres/20241031105829_add_ts_column_to_notification_history_tables.sql +++ b/backend/pkg/commons/db/migrations/postgres/20241031105829_add_ts_column_to_notification_history_tables.sql @@ -2,9 +2,13 @@ -- +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 @@ -12,7 +16,11 @@ ALTER TABLE network_notifications_history ADD COLUMN IF NOT EXISTS ts TIMESTAMP -- +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