58 lines
1.7 KiB
Go
58 lines
1.7 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/company/ai-ops/internal/database"
|
|
"github.com/company/ai-ops/internal/domain/model"
|
|
)
|
|
|
|
// PGNotificationLogRepository 是基于 PostgreSQL 的通知日志存储实现。
|
|
type PGNotificationLogRepository struct{}
|
|
|
|
func NewPGNotificationLogRepository() *PGNotificationLogRepository {
|
|
return &PGNotificationLogRepository{}
|
|
}
|
|
|
|
func (r *PGNotificationLogRepository) CreateLog(ctx context.Context, log *model.NotificationLog) error {
|
|
if log.ID == "" {
|
|
log.ID = newUUID()
|
|
}
|
|
if log.Status == "" {
|
|
log.Status = "pending"
|
|
}
|
|
_, err := database.Pool.Exec(ctx, `
|
|
INSERT INTO ai_ops_notification_logs (id, event_id, channel_id, channel_type, status, retry_count, error_message, sent_at, created_at)
|
|
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,NOW())
|
|
`, log.ID, log.EventID, log.ChannelID, log.ChannelType, log.Status, log.RetryCount, log.ErrorMessage, log.SentAt)
|
|
if err != nil {
|
|
return fmt.Errorf("insert notification log: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *PGNotificationLogRepository) MarkSent(ctx context.Context, id string) error {
|
|
_, err := database.Pool.Exec(ctx, `
|
|
UPDATE ai_ops_notification_logs
|
|
SET status='sent', sent_at=NOW(), error_message=NULL
|
|
WHERE id=$1
|
|
`, id)
|
|
if err != nil {
|
|
return fmt.Errorf("mark notification sent: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *PGNotificationLogRepository) MarkFailed(ctx context.Context, id string, retryCount int, errMessage string) error {
|
|
_, err := database.Pool.Exec(ctx, `
|
|
UPDATE ai_ops_notification_logs
|
|
SET status='failed', retry_count=$2, error_message=$3
|
|
WHERE id=$1
|
|
`, id, retryCount, errMessage)
|
|
if err != nil {
|
|
return fmt.Errorf("mark notification failed: %w", err)
|
|
}
|
|
return nil
|
|
}
|