Skip to content

Commit 1493966

Browse files
committed
added get monitor incidents endpoint
1 parent bcc7336 commit 1493966

File tree

4 files changed

+92
-3
lines changed

4 files changed

+92
-3
lines changed

internal/incident/repository.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var ErrNotFound = errors.New("incident not found")
1515
type Repository interface {
1616
GetByID(ctx context.Context, id uint) (*entities.Incident, error)
1717
GetByTeamIDPaginated(ctx context.Context, teamID uint, offset, limit *int) (*[]entities.Incident, error)
18+
GetByMonitorIDPaginated(ctx context.Context, monitorID uint, offset, limit *int) (*[]entities.Incident, error)
1819
Upsert(ctx context.Context, incidents *[]entities.Incident) error
1920
Create(ctx context.Context, incidents *[]entities.Incident) error
2021
Update(ctx context.Context, incident *entities.Incident) error
@@ -63,6 +64,23 @@ func (r *RepositoryImpl) GetByTeamIDPaginated(ctx context.Context, teamID uint,
6364
return &incidents, nil
6465
}
6566

67+
func (r *RepositoryImpl) GetByMonitorIDPaginated(ctx context.Context, monitorID uint, offset, limit *int) (*[]entities.Incident, error) {
68+
var incidents []entities.Incident
69+
if err := r.db.WithContext(
70+
ctx,
71+
).Where(entities.Incident{
72+
MonitorID: monitorID,
73+
}).Order(
74+
"created_at desc",
75+
).Scopes(
76+
postgres.Paginated(offset, limit),
77+
).Find(&incidents).Error; err != nil {
78+
return nil, err
79+
}
80+
81+
return &incidents, nil
82+
}
83+
6684
func (r *RepositoryImpl) Upsert(ctx context.Context, incidents *[]entities.Incident) error {
6785
return r.db.WithContext(ctx).Clauses(clause.OnConflict{
6886
Columns: []clause.Column{{Name: "monitor_assertion_id"}, {Name: "resolved"}},

internal/incident/service.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
type Service interface {
1010
GetByID(ctx context.Context, id uint) (*entities.Incident, error)
1111
GetByTeamIDPaginated(ctx context.Context, teamID uint, offset, limit *int) (*[]entities.Incident, error)
12+
GetByMonitorIDPaginated(ctx context.Context, monitorID uint, offset, limit *int) (*[]entities.Incident, error)
1213
Upsert(ctx context.Context, incidents *[]entities.Incident) error
1314
Create(ctx context.Context, incidents *[]entities.Incident) error
1415
Update(ctx context.Context, incident *entities.Incident) error
@@ -33,6 +34,10 @@ func (s *ServiceImpl) GetByTeamIDPaginated(ctx context.Context, teamID uint, off
3334
return s.repository.GetByTeamIDPaginated(ctx, teamID, offset, limit)
3435
}
3536

37+
func (s *ServiceImpl) GetByMonitorIDPaginated(ctx context.Context, monitorID uint, offset, limit *int) (*[]entities.Incident, error) {
38+
return s.repository.GetByMonitorIDPaginated(ctx, monitorID, offset, limit)
39+
}
40+
3641
func (s *ServiceImpl) Upsert(ctx context.Context, incidents *[]entities.Incident) error {
3742
return s.repository.Upsert(ctx, incidents)
3843
}

internal/rest/controllers/incidents/incidents.go

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type GetIncidentsRequest struct {
1616
}
1717

1818
type GetIncidentsResponse struct {
19-
Checks []GetIncidentsResponseIncident `json:"incidents"`
19+
Incidents []GetIncidentsResponseIncident `json:"incidents"`
2020
}
2121

2222
type GetIncidentsResponseIncident struct {
@@ -56,11 +56,11 @@ func (h *Handlers) GetIncidents(c hs.AuthenticatedContext) error {
5656

5757
func (h *Handlers) newGetIncidentResponse(incidents *[]entities.Incident) *GetIncidentsResponse {
5858
resp := &GetIncidentsResponse{
59-
Checks: make([]GetIncidentsResponseIncident, len(*incidents)),
59+
Incidents: make([]GetIncidentsResponseIncident, len(*incidents)),
6060
}
6161

6262
for i, incident := range *incidents {
63-
resp.Checks[i] = GetIncidentsResponseIncident{
63+
resp.Incidents[i] = GetIncidentsResponseIncident{
6464
ID: incident.ID,
6565
TeamID: incident.TeamID,
6666
MonitorID: incident.MonitorID,
@@ -135,3 +135,68 @@ func (h *Handlers) newGetIncidentOverviewResponse(incidents *[]entities.Incident
135135

136136
return resp
137137
}
138+
139+
type GetMonitorIncidentsRequest struct {
140+
TeamID uint `param:"teamId" validate:"required,numeric,gte=0"`
141+
MonitorID uint `param:"monitorId" validate:"required,numeric,gte=0"`
142+
Offset *int `query:"offset" validate:"omitempty,numeric,gte=0"`
143+
Limit *int `query:"limit" validate:"omitempty,numeric,gte=0,max=255"`
144+
}
145+
146+
type GetMonitorIncidentsResponse struct {
147+
Incidents []GetMonitorIncidentsResponseIncident `json:"incidents"`
148+
}
149+
150+
type GetMonitorIncidentsResponseIncident struct {
151+
ID uint `json:"id"`
152+
TeamID uint `json:"teamId"`
153+
MonitorID uint `json:"monitorId"`
154+
Title string `json:"title"`
155+
Description string `json:"description"`
156+
CreatedAt string `json:"createdAt"`
157+
}
158+
159+
func (h *Handlers) GetMonitorIncidents(c hs.AuthenticatedContext) error {
160+
req, err := helpers.Bind[GetMonitorIncidentsRequest](c)
161+
if err != nil {
162+
c.Log.WithError(err).Debug("failed to bind GetMonitorIncidentsRequest")
163+
164+
return echo.ErrBadRequest
165+
}
166+
167+
ctx := c.Request().Context()
168+
169+
incidents, err := h.IncidentService.GetByTeamIDPaginated(
170+
ctx,
171+
req.TeamID,
172+
req.Offset,
173+
req.Limit)
174+
if err != nil {
175+
c.Log.WithError(err).Error("failed to get incidents")
176+
177+
return echo.ErrInternalServerError
178+
}
179+
180+
resp := h.newGetIncidentResponse(incidents)
181+
182+
return c.JSON(http.StatusOK, resp)
183+
}
184+
185+
func (h *Handlers) GetMonitorIncidentsResponse(incidents *[]entities.Incident) *GetMonitorIncidentsResponse {
186+
resp := &GetMonitorIncidentsResponse{
187+
Incidents: make([]GetMonitorIncidentsResponseIncident, len(*incidents)),
188+
}
189+
190+
for i, incident := range *incidents {
191+
resp.Incidents[i] = GetMonitorIncidentsResponseIncident{
192+
ID: incident.ID,
193+
TeamID: incident.TeamID,
194+
MonitorID: incident.MonitorID,
195+
Title: incident.Title,
196+
Description: *incident.Description,
197+
CreatedAt: incident.CreatedAt.Format("2006-01-02T15:04:05Z07:00"),
198+
}
199+
}
200+
201+
return resp
202+
}

internal/rest/controllers/incidents/routes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ func Register(
3737

3838
monitorsGroup.GET("", AuthHandler(h.GetIncidents))
3939
monitorsGroup.GET("/overview", AuthHandler(h.GetIncidents))
40+
monitorsGroup.GET("/monitor/:monitorId", AuthHandler(h.GetIncidents))
4041
}

0 commit comments

Comments
 (0)