Skip to content

Commit

Permalink
feat: Add limit to GetTimeline query
Browse files Browse the repository at this point in the history
  • Loading branch information
wesbillman committed Sep 11, 2023
1 parent 42c1517 commit 416daf9
Show file tree
Hide file tree
Showing 6 changed files with 317 additions and 245 deletions.
27 changes: 22 additions & 5 deletions backend/controller/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (c *ConsoleService) GetModules(ctx context.Context, req *connect.Request[pb
}

func (c *ConsoleService) GetCalls(ctx context.Context, req *connect.Request[pbconsole.GetCallsRequest]) (*connect.Response[pbconsole.GetCallsResponse], error) {
events, err := c.dal.QueryEvents(ctx, dal.FilterCall(types.None[string](), req.Msg.Module, types.Some(req.Msg.Verb)))
events, err := c.dal.QueryEvents(ctx, nil, dal.FilterCall(types.None[string](), req.Msg.Module, types.Some(req.Msg.Verb)))
if err != nil {
return nil, errors.WithStack(err)
}
Expand All @@ -119,7 +119,7 @@ func (c *ConsoleService) GetRequestCalls(ctx context.Context, req *connect.Reque
return nil, errors.WithStack(err)
}

events, err := c.dal.QueryEvents(ctx, dal.FilterRequests(requestKey))
events, err := c.dal.QueryEvents(ctx, nil, dal.FilterRequests(requestKey))
if err != nil {
return nil, errors.WithStack(err)
}
Expand All @@ -134,12 +134,29 @@ func (c *ConsoleService) GetTimeline(ctx context.Context, req *connect.Request[p
if err != nil {
return nil, errors.WithStack(err)
}
results, err := c.dal.QueryEvents(ctx, query...)

limit := 100 // Default to 100 events if not specified.
if req.Msg.Limit > 0 {
limit = int(req.Msg.Limit)
}

// Get 1 more than the requested limit to determine if there are more results.
limitPlusOne := limit + 1

results, err := c.dal.QueryEvents(ctx, &limitPlusOne, query...)
if err != nil {
return nil, errors.WithStack(err)
}

more := len(results) > limit
// Return only the requested number of results.
if len(results) > limit {
results = results[:limit]
}

response := &pbconsole.GetTimelineResponse{
Events: slices.Map(results, eventDALToProto),
More: more,
}
return connect.NewResponse(response), nil
}
Expand Down Expand Up @@ -168,7 +185,7 @@ func (c *ConsoleService) StreamTimeline(ctx context.Context, req *connect.Reques

for {
thisRequestTime := time.Now()
events, err := c.dal.QueryEvents(ctx, append(query, dal.FilterTimeRange(thisRequestTime, lastEventTime))...)
events, err := c.dal.QueryEvents(ctx, nil, append(query, dal.FilterTimeRange(thisRequestTime, lastEventTime))...)
if err != nil {
return errors.WithStack(err)
}
Expand Down Expand Up @@ -210,7 +227,7 @@ func (c *ConsoleService) StreamLogs(ctx context.Context, req *connect.Request[pb
lastLogTime := req.Msg.AfterTime.AsTime()
for {
thisRequestTime := time.Now()
events, err := c.dal.QueryEvents(ctx, append(query, dal.FilterTimeRange(thisRequestTime, lastLogTime))...)
events, err := c.dal.QueryEvents(ctx, nil, append(query, dal.FilterTimeRange(thisRequestTime, lastLogTime))...)
if err != nil {
return errors.WithStack(err)
}
Expand Down
8 changes: 4 additions & 4 deletions backend/controller/internal/dal/dal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,25 +255,25 @@ func TestDAL(t *testing.T) {

t.Run("QueryEvents", func(t *testing.T) {
t.Run("NoFilters", func(t *testing.T) {
events, err := dal.QueryEvents(ctx)
events, err := dal.QueryEvents(ctx, nil)
assert.NoError(t, err)
assertEventsEqual(t, []Event{expectedDeploymentEvent, callEvent, logEvent}, events)
})

t.Run("ByDeployment", func(t *testing.T) {
events, err := dal.QueryEvents(ctx, FilterDeployments(deploymentName))
events, err := dal.QueryEvents(ctx, nil, FilterDeployments(deploymentName))
assert.NoError(t, err)
assertEventsEqual(t, []Event{expectedDeploymentEvent, callEvent, logEvent}, events)
})

t.Run("ByCall", func(t *testing.T) {
events, err := dal.QueryEvents(ctx, FilterTypes(EventTypeCall), FilterCall(types.None[string](), "time", types.None[string]()))
events, err := dal.QueryEvents(ctx, nil, FilterTypes(EventTypeCall), FilterCall(types.None[string](), "time", types.None[string]()))
assert.NoError(t, err)
assertEventsEqual(t, []Event{callEvent}, events)
})

t.Run("ByLogLevel", func(t *testing.T) {
events, err := dal.QueryEvents(ctx, FilterTypes(EventTypeLog), FilterLogLevel(log.Trace))
events, err := dal.QueryEvents(ctx, nil, FilterTypes(EventTypeLog), FilterLogLevel(log.Trace))
assert.NoError(t, err)
assertEventsEqual(t, []Event{logEvent}, events)
})
Expand Down
5 changes: 4 additions & 1 deletion backend/controller/internal/dal/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ type eventRow struct {
RequestKey types.Option[model.IngressRequestKey]
}

func (d *DAL) QueryEvents(ctx context.Context, filters ...EventFilter) ([]Event, error) {
func (d *DAL) QueryEvents(ctx context.Context, limit *int, filters ...EventFilter) ([]Event, error) {
// Build query.
q := `SELECT e.id AS id,
d.name AS deployment_name,
Expand Down Expand Up @@ -253,6 +253,9 @@ func (d *DAL) QueryEvents(ctx context.Context, filters ...EventFilter) ([]Event,
}

q += " ORDER BY time_stamp ASC"
if limit != nil {
q += fmt.Sprintf(" LIMIT %d", *limit)
}

// Issue query.
rows, err := d.db.Conn().Query(ctx, q, args...)
Expand Down
53 changes: 37 additions & 16 deletions console/client/src/protos/xyz/block/ftl/v1/console/console_pb.ts

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

Loading

0 comments on commit 416daf9

Please sign in to comment.