Skip to content

Commit

Permalink
feat(#143): determine how long a monitor has been up
Browse files Browse the repository at this point in the history
  • Loading branch information
firminochangani committed Jan 6, 2024
1 parent a7e8687 commit c337dfc
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 63 deletions.
3 changes: 3 additions & 0 deletions api/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ components:
last_checked_at:
type: string
format: date-time
up_since:
type: string
format: date-time
check_interval_in_seconds:
type: integer
Incident:
Expand Down
4 changes: 2 additions & 2 deletions internal/adapters/psql/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (p MonitorRepository) FindByID(ctx context.Context, id domain.ID) (*monitor
models.IncidentColumns.Cause,
models.IncidentColumns.ResponseStatus,
models.IncidentColumns.CheckedURL,
)),
), qm.OrderBy("created_at DESC")),
).One(ctx, p.db)
if errors.Is(err, sql.ErrNoRows) {
return nil, monitor.ErrMonitorNotFound
Expand Down Expand Up @@ -166,7 +166,7 @@ func (p MonitorRepository) FindAll(
models.IncidentColumns.Cause,
models.IncidentColumns.ResponseStatus,
models.IncidentColumns.CheckedURL,
))),
), qm.OrderBy("created_at DESC"))),
qm.Offset(mapPaginationParamsToOffset(params.Page, params.Limit)),
qm.Limit(params.Limit),
qm.OrderBy("created_at DESC"),
Expand Down
31 changes: 26 additions & 5 deletions internal/adapters/psql/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,22 @@ func TestMonitorRepository_Lifecycle(t *testing.T) {
assert.Equal(t, limitPerPage, result.PerPage)
assert.Equal(t, maxMonitors/limitPerPage, result.PageCount)
assert.Len(t, result.Data, limitPerPage)
assertIncidentsAreSortedInDescOrder(t, result.Data[0].Incidents())

t.Run("find_by_id", func(t *testing.T) {
expected := result.Data[0]
var found *monitor.Monitor

incident00 := tests.FixtureIncident(t, expected.ID().String())
err = incidentRepository.Create(ctx, incident00)
require.NoError(t, err)
incidents := make([]*monitor.Incident, 5)
for i := 0; i < 5; i++ {
incidents[i] = tests.FixtureIncident(t, expected.ID().String())
if i != 4 {
incidents[i].Resolve()
}

err = incidentRepository.Create(ctx, incidents[i])
require.NoError(t, err)
}

found, err = monitorRepository.FindByID(ctx, expected.ID())
require.NoError(t, err)
Expand All @@ -58,8 +66,9 @@ func TestMonitorRepository_Lifecycle(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, found.Subscribers()[0].UserID(), owner.ID())

require.Len(t, found.Incidents(), 1)
assertIncident(t, incident00, found.Incidents()[0])
require.Len(t, found.Incidents(), 5)
// assertIncident(t, incident00, found.Incidents()[0])
assertIncidentsAreSortedInDescOrder(t, found.Incidents())
})

t.Run("error_not_found_while_finding_by_id", func(t *testing.T) {
Expand Down Expand Up @@ -188,6 +197,18 @@ func saveIncident(t *testing.T, monitorID domain.ID) {
require.NoError(t, model.Insert(ctx, db, boil.Infer()))
}

func assertIncidentsAreSortedInDescOrder(t *testing.T, incidents []*monitor.Incident) {
t.Helper()
var prev *monitor.Incident

for _, current := range incidents {
if prev != nil {
require.True(t, prev.CreatedAt().After(current.CreatedAt()))
}
prev = current
}
}

func fixtureMonitor(
t *testing.T,
accID domain.ID,
Expand Down
19 changes: 19 additions & 0 deletions internal/domain/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,22 @@ func (m *Monitor) Edit(endpointUrl string, checkIntervalInSeconds time.Duration)

return nil
}

func (m *Monitor) UpSince() *time.Time {
if m.isPaused {
return nil
}

if len(m.incidents) > 0 {
mostRecentIncident := m.incidents[len(m.incidents)-1]

if !mostRecentIncident.IsResolved() {
return nil
}

return mostRecentIncident.resolvedAt
}

upSince := m.createdAt
return &upSince
}
1 change: 1 addition & 0 deletions internal/ports/http/mappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func mapMonitorToResponseItem(m *monitor.Monitor) Monitor {
IsPaused: m.IsPaused(),
LastCheckedAt: m.LastCheckedAt(),
CheckIntervalInSeconds: int(m.CheckInterval().Seconds()),
UpSince: m.UpSince(),
}
}

Expand Down
57 changes: 29 additions & 28 deletions internal/ports/http/server.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 29 additions & 28 deletions tests/client/client.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/components/monitors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func TestMonitors(t *testing.T) {
assert.NotEmpty(t, m.Id)
assert.NotEmpty(t, m.EndpointUrl)
assert.NotEmpty(t, m.CreatedAt)
assert.NotNil(t, m.UpSince)
}

resp02, err := cli.GetAllMonitorsWithResponse(ctx, &client.GetAllMonitorsParams{
Expand Down

0 comments on commit c337dfc

Please sign in to comment.