39 lines
1.2 KiB
Go
39 lines
1.2 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/company/ai-ops/internal/database"
|
|
"github.com/company/ai-ops/internal/service"
|
|
)
|
|
|
|
// PGHealingRepository 是自愈记录的 PostgreSQL 实现
|
|
type PGHealingRepository struct{}
|
|
|
|
func NewPGHealingRepository() *PGHealingRepository {
|
|
return &PGHealingRepository{}
|
|
}
|
|
|
|
func (r *PGHealingRepository) CreateHealing(ctx context.Context, h *service.HealingLog) error {
|
|
_, err := database.Pool.Exec(ctx, `
|
|
INSERT INTO ai_ops_healings (id, alert_id, action_type, config, status, dry_run, result_detail, error_code, started_at)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
|
`, h.ID, h.AlertID, h.ActionType, h.Config, h.Status, h.DryRun, h.ResultDetail, h.ErrorCode, h.StartedAt)
|
|
if err != nil {
|
|
return fmt.Errorf("insert healing: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *PGHealingRepository) UpdateHealingStatus(ctx context.Context, id, status string, result map[string]any, errCode string) error {
|
|
_, err := database.Pool.Exec(ctx, `
|
|
UPDATE ai_ops_healings SET status = $2, result_detail = $3, error_code = $4, completed_at = NOW()
|
|
WHERE id = $1
|
|
`, id, status, result, errCode)
|
|
if err != nil {
|
|
return fmt.Errorf("update healing: %w", err)
|
|
}
|
|
return nil
|
|
}
|