Skip to content

Commit 40e72a2

Browse files
committed
added failed assertion check endpoint
1 parent 20e8fc2 commit 40e72a2

File tree

6 files changed

+104
-0
lines changed

6 files changed

+104
-0
lines changed

internal/check/repository.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Repository interface {
1919
GetMonitorOverviewsByTeamID(ctx context.Context, teamID uint) (*[]MonitorOverviews, error)
2020
GetMonitorStatsByMonitorID(ctx context.Context, monitorID uint) (*MonitorStats, error)
2121
GetMonitorOverviewStatsByTeamID(ctx context.Context, teamID uint) (*[]MonitorOverviewStats, error)
22+
GetMonitorIDAndAssertions(ctx context.Context, monitorID uint, assertions []string) (*[]Check, error)
2223
}
2324

2425
type RepositoryImpl struct {
@@ -188,3 +189,27 @@ func (r *RepositoryImpl) GetMonitorOverviewStatsByTeamID(ctx context.Context, te
188189

189190
return &overviews, err
190191
}
192+
193+
func (r *RepositoryImpl) GetMonitorIDAndAssertions(ctx context.Context, monitorID uint, assertions []string) (*[]Check, error) {
194+
var checks []Check
195+
err := r.db.WithContext(
196+
ctx,
197+
).Where(
198+
Check{
199+
MonitorID: uint64(monitorID),
200+
},
201+
).Where(assertions[0]).Order(
202+
"created_at desc",
203+
).Find(
204+
&checks,
205+
).Error
206+
if err != nil {
207+
if errors.Is(err, gorm.ErrRecordNotFound) {
208+
return &[]Check{}, nil
209+
}
210+
211+
return nil, err
212+
}
213+
214+
return &checks, nil
215+
}

internal/check/service.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ type Service interface {
1616
GetMonitorMetricsByMonitorID(ctx context.Context, monitorID uint) (*[]AggMetric, error)
1717
GetMonitorStatsByMonitorID(ctx context.Context, monitorID uint) (*MonitorStats, error)
1818
GetMonitorOverviewsByTeamID(ctx context.Context, teamID uint) (*[]MonitorOverviews, error)
19+
20+
GetMonitorIDAndAssertions(ctx context.Context, monitorID uint, assertions []string) (*[]Check, error)
1921
}
2022

2123
type ServiceImpl struct {
@@ -68,3 +70,7 @@ func (s *ServiceImpl) GetMonitorOverviewsByTeamID(ctx context.Context, teamID ui
6870

6971
return overviews, err
7072
}
73+
74+
func (s *ServiceImpl) GetMonitorIDAndAssertions(ctx context.Context, monitorID uint, assertions []string) (*[]Check, error) {
75+
return s.repository.GetMonitorIDAndAssertions(ctx, monitorID, assertions)
76+
}

internal/monitor/repository.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type Repository interface {
1515
GetMonitorAndSettingsByTeamIDAndID(ctx context.Context, teamID uint, monitorID uint) (*entities.Monitor, error)
1616
GetMonitorsAndSettingsByTeamID(ctx context.Context, teamID uint, offset *int, limit *int, query *string) (*[]MonitorWithTotalCount, error)
1717
GetMonitorsAndIncidentsByTeamID(ctx context.Context, teamID uint) (*[]entities.Monitor, error)
18+
GetMonitorAssertionByID(ctx context.Context, monitorAssertionID uint) (*entities.MonitorAssertion, error)
1819
SetState(ctx context.Context, teamID, monitorID uint, state entities.MonitorState) error
1920
Create(ctx context.Context, monitor *entities.Monitor) error
2021
Update(ctx context.Context, teamID, monitorID uint, monitor *entities.Monitor) error
@@ -102,6 +103,20 @@ func (r *RepositoryImpl) GetMonitorsAndIncidentsByTeamID(ctx context.Context, te
102103
return &monitors, err
103104
}
104105

106+
func (r *RepositoryImpl) GetMonitorAssertionByID(ctx context.Context, monitorAssertionID uint) (*entities.MonitorAssertion, error) {
107+
var monitorAssertion entities.MonitorAssertion
108+
err := r.db.WithContext(
109+
ctx,
110+
).Where(entities.MonitorAssertion{
111+
ID: monitorAssertionID,
112+
}).First(&monitorAssertion).Error
113+
if errors.Is(err, gorm.ErrRecordNotFound) {
114+
return nil, ErrNotFound
115+
}
116+
117+
return &monitorAssertion, err
118+
}
119+
105120
func (r *RepositoryImpl) SetState(ctx context.Context, teamID, monitorID uint, state entities.MonitorState) error {
106121
err := r.db.WithContext(ctx).Model(
107122
&entities.Monitor{},

internal/monitor/service.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type Service interface {
1414
GetMonitorAndSettingsByTeamIDAndID(ctx context.Context, teamID uint, monitorID uint) (*entities.Monitor, error)
1515
GetMonitorsAndSettingsByTeamID(ctx context.Context, teamID uint, offset *int, limit *int, query *string) (*[]MonitorWithTotalCount, error)
1616
GetMonitorsAndIncidentsByTeamID(ctx context.Context, teamID uint) (*[]entities.Monitor, error)
17+
GetMonitorAssertionByID(ctx context.Context, monitorAssertionID uint) (*entities.MonitorAssertion, error)
1718
SetState(ctx context.Context, teamID, monitorID uint, state entities.MonitorState) error
1819
Create(ctx context.Context, monitor *entities.Monitor) error
1920
Update(ctx context.Context, teamID, monitorID uint, monitor *entities.Monitor) error
@@ -44,6 +45,10 @@ func (s *ServiceImpl) GetMonitorsAndIncidentsByTeamID(ctx context.Context, teamI
4445
return s.repository.GetMonitorsAndIncidentsByTeamID(ctx, teamID)
4546
}
4647

48+
func (s *ServiceImpl) GetMonitorAssertionByID(ctx context.Context, monitorAssertionID uint) (*entities.MonitorAssertion, error) {
49+
return s.repository.GetMonitorAssertionByID(ctx, monitorAssertionID)
50+
}
51+
4752
func (s *ServiceImpl) SetState(ctx context.Context, teamID, monitorID uint, state entities.MonitorState) error {
4853
err := s.repository.SetState(ctx, teamID, monitorID, state)
4954
if err != nil {

internal/rest/controllers/monitors/checks.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,55 @@ func (h *Handlers) GetMonitorCheck(c hs.AuthenticatedContext) error {
166166

167167
return c.JSON(http.StatusOK, resp)
168168
}
169+
170+
type GeFailedMonitorCheckRequest struct {
171+
TeamID uint `param:"teamId" validate:"required,numeric,gte=0"`
172+
MonitorID uint `param:"monitorId" validate:"required,numeric,gte=0"`
173+
MonitorAssertionID uint `param:"monitorAssertionId" validate:"required,numeric,gte=0"`
174+
Offset *int `query:"offset" validate:"omitempty,numeric,gte=0"`
175+
Limit *int `query:"limit" validate:"omitempty,numeric,gte=0"`
176+
}
177+
178+
type GetFailedMonitorCheckResponse struct {
179+
GetMonitorChecksResponseCheck
180+
}
181+
182+
func (h *Handlers) GetFailedMonitorChecks(c hs.AuthenticatedContext) error {
183+
req, err := helpers.Bind[GeFailedMonitorCheckRequest](c)
184+
if err != nil {
185+
c.Log.WithError(err).Debug("failed to bind GetMonitorsRequest")
186+
187+
return echo.ErrBadRequest
188+
}
189+
190+
ctx := c.Request().Context()
191+
192+
monitorAssertion, err := h.MonitorService.GetMonitorAssertionByID(ctx, req.MonitorAssertionID)
193+
if err != nil {
194+
c.Log.WithError(err).Error("failed to get monitorAssertion")
195+
return echo.ErrInternalServerError
196+
}
197+
198+
// map monitorassertion to filter
199+
200+
result, err := h.CheckService.GetMonitorIDAndAssertions(
201+
ctx,
202+
monitorAssertion.MonitorID,
203+
[]string{"status_code == 200"},
204+
)
205+
if err != nil {
206+
if errors.Is(err, check.ErrNotFound) {
207+
c.Log.WithError(err).Debug("check not found")
208+
209+
return echo.ErrNotFound
210+
}
211+
212+
c.Log.WithError(err).Error("failed to get monitor check")
213+
214+
return echo.ErrInternalServerError
215+
}
216+
217+
resp := h.newGetMonitorChecksResponse(result)
218+
219+
return c.JSON(http.StatusOK, resp)
220+
}

internal/rest/controllers/monitors/routes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func Register(
5252
monitorsGroup.PUT("/:monitorId/state", AuthHandler(h.PutMonitorState), AllowedRoles(mw.UserRoleOwner, mw.UserRoleAdmin))
5353

5454
monitorsGroup.GET("/:monitorId/checks", AuthHandler(h.GetMonitorChecks))
55+
monitorsGroup.GET("/:monitorId/checks/failed/:monitorAssertionId", AuthHandler(h.GetFailedMonitorChecks))
5556
monitorsGroup.GET("/:monitorId/checks/:checkId", AuthHandler(h.GetMonitorCheck))
5657

5758
monitorsGroup.GET("/:monitorId/metrics", AuthHandler(h.GetMonitorMetrics))

0 commit comments

Comments
 (0)