diff options
Diffstat (limited to 'backend/internal/store/postgres.go')
| -rw-r--r-- | backend/internal/store/postgres.go | 75 |
1 files changed, 73 insertions, 2 deletions
diff --git a/backend/internal/store/postgres.go b/backend/internal/store/postgres.go index a8d072a..57fa258 100644 --- a/backend/internal/store/postgres.go +++ b/backend/internal/store/postgres.go @@ -213,15 +213,16 @@ func (s *PostgresStore) AddHeartbeat(hb model.Heartbeat) error { return nil } -func (s *PostgresStore) GetHistory(serviceID int, limit int) ([]model.Heartbeat, error) { +func (s *PostgresStore) GetHistory(serviceID int, limit int, offset int) ([]model.Heartbeat, error) { query := ` SELECT id, service_id, status_code, response_time_ms, is_up, error_message, tested_at FROM heartbeats WHERE service_id = $1 ORDER BY tested_at DESC LIMIT $2 + OFFSET $3 ` - rows, err := s.pool.Query(context.Background(), query, serviceID, limit) + rows, err := s.pool.Query(context.Background(), query, serviceID, limit, offset) if err != nil { return nil, fmt.Errorf("get history: %w", err) } @@ -234,6 +235,18 @@ func (s *PostgresStore) GetHistory(serviceID int, limit int) ([]model.Heartbeat, }) } +func (s *PostgresStore) CountHeartbeats(serviceID int) (int, error) { + var count int + err := s.pool.QueryRow(context.Background(), + `SELECT COUNT(*) FROM heartbeats WHERE service_id = $1`, + serviceID, + ).Scan(&count) + if err != nil { + return 0, fmt.Errorf("count heartbeats: %w", err) + } + return count, nil +} + func (s *PostgresStore) GetLastHeartbeat(serviceID int) (*model.Heartbeat, error) { query := ` SELECT id, service_id, status_code, response_time_ms, is_up, error_message, tested_at @@ -322,3 +335,61 @@ func (s *PostgresStore) GetActiveMaintenance(now time.Time) (*model.Maintenance, } return &m, nil } + +func (s *PostgresStore) ListMaintenances() ([]model.Maintenance, error) { + query := `SELECT id, title, start_time, end_time, is_active FROM maintenances ORDER BY start_time DESC` + rows, err := s.pool.Query(context.Background(), query) + if err != nil { + return nil, fmt.Errorf("list maintenances: %w", err) + } + defer rows.Close() + return pgx.CollectRows(rows, func(row pgx.CollectableRow) (model.Maintenance, error) { + var m model.Maintenance + err := row.Scan(&m.ID, &m.Title, &m.StartTime, &m.EndTime, &m.IsActive) + return m, err + }) +} + +func (s *PostgresStore) AddMaintenance(m model.Maintenance) (*model.Maintenance, error) { + query := ` + INSERT INTO maintenances (title, start_time, end_time, is_active) + VALUES ($1, $2, $3, $4) + RETURNING id + ` + err := s.pool.QueryRow(context.Background(), query, m.Title, m.StartTime, m.EndTime, m.IsActive). + Scan(&m.ID) + if err != nil { + return nil, fmt.Errorf("add maintenance: %w", err) + } + return &m, nil +} + +func (s *PostgresStore) UpdateMaintenance(id int, m model.Maintenance) (*model.Maintenance, error) { + query := ` + UPDATE maintenances + SET title = $1, start_time = $2, end_time = $3, is_active = $4 + WHERE id = $5 + RETURNING id, title, start_time, end_time, is_active + ` + var updated model.Maintenance + err := s.pool.QueryRow(context.Background(), query, m.Title, m.StartTime, m.EndTime, m.IsActive, id). + Scan(&updated.ID, &updated.Title, &updated.StartTime, &updated.EndTime, &updated.IsActive) + if err != nil { + if err == pgx.ErrNoRows { + return nil, fmt.Errorf("manutencao nao encontrada") + } + return nil, fmt.Errorf("update maintenance: %w", err) + } + return &updated, nil +} + +func (s *PostgresStore) DeleteMaintenance(id int) error { + tag, err := s.pool.Exec(context.Background(), "DELETE FROM maintenances WHERE id = $1", id) + if err != nil { + return fmt.Errorf("delete maintenance: %w", err) + } + if tag.RowsAffected() == 0 { + return fmt.Errorf("manutencao nao encontrada") + } + return nil +} |