generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
console.go
372 lines (333 loc) · 11.5 KB
/
console.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package controller
import (
"context"
"encoding/json"
"time"
"connectrpc.com/connect"
"github.com/alecthomas/errors"
"github.com/alecthomas/types"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/TBD54566975/ftl/backend/common/log"
"github.com/TBD54566975/ftl/backend/common/model"
"github.com/TBD54566975/ftl/backend/common/slices"
"github.com/TBD54566975/ftl/backend/controller/internal/dal"
"github.com/TBD54566975/ftl/backend/schema"
ftlv1 "github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1"
pbconsole "github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1/console"
"github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1/console/pbconsoleconnect"
schemapb "github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1/schema"
)
type ConsoleService struct {
dal *dal.DAL
}
var _ pbconsoleconnect.ConsoleServiceHandler = (*ConsoleService)(nil)
func NewConsoleService(dal *dal.DAL) *ConsoleService {
return &ConsoleService{
dal: dal,
}
}
func (*ConsoleService) Ping(context.Context, *connect.Request[ftlv1.PingRequest]) (*connect.Response[ftlv1.PingResponse], error) {
return connect.NewResponse(&ftlv1.PingResponse{}), nil
}
func (c *ConsoleService) GetModules(ctx context.Context, req *connect.Request[pbconsole.GetModulesRequest]) (*connect.Response[pbconsole.GetModulesResponse], error) {
deployments, err := c.dal.GetActiveDeployments(ctx)
if err != nil {
return nil, errors.WithStack(err)
}
sch := &schema.Schema{
Modules: slices.Map(deployments, func(d dal.Deployment) *schema.Module {
return d.Schema
}),
}
var modules []*pbconsole.Module
for _, deployment := range deployments {
var verbs []*pbconsole.Verb
var data []*pbconsole.Data
for _, decl := range deployment.Schema.Decls {
switch decl := decl.(type) {
case *schema.Verb:
//nolint:forcetypeassert
v := decl.ToProto().(*schemapb.Verb)
verbSchema := schema.VerbToSchema(v)
dataRef := schema.DataRef{
Module: deployment.Module,
Name: verbSchema.Request.Name,
}
jsonRequestSchema, err := schema.DataToJSONSchema(sch, dataRef)
if err != nil {
return nil, errors.WithStack(err)
}
jsonData, err := json.MarshalIndent(jsonRequestSchema, "", " ")
if err != nil {
return nil, errors.WithStack(err)
}
verbs = append(verbs, &pbconsole.Verb{
Verb: v,
Schema: verbSchema.String(),
JsonRequestSchema: string(jsonData),
})
case *schema.Data:
//nolint:forcetypeassert
d := decl.ToProto().(*schemapb.Data)
data = append(data, &pbconsole.Data{
Data: d,
Schema: schema.DataToSchema(d).String(),
})
}
}
modules = append(modules, &pbconsole.Module{
Name: deployment.Module,
DeploymentName: deployment.Name.String(),
Language: deployment.Language,
Verbs: verbs,
Data: data,
})
}
return connect.NewResponse(&pbconsole.GetModulesResponse{
Modules: modules,
}), nil
}
func (c *ConsoleService) GetEvents(ctx context.Context, req *connect.Request[pbconsole.EventsQuery]) (*connect.Response[pbconsole.GetEventsResponse], error) {
query, err := eventsQueryProtoToDAL(req.Msg)
if err != nil {
return nil, errors.WithStack(err)
}
if req.Msg.Limit == 0 {
return nil, connect.NewError(connect.CodeInvalidArgument, errors.New("limit must be > 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)
}
var cursor *int64
// Return only the requested number of results.
if len(results) > limit {
results = results[:limit]
id := results[len(results)-1].GetID()
cursor = &id
}
response := &pbconsole.GetEventsResponse{
Events: slices.Map(results, eventDALToProto),
Cursor: cursor,
}
return connect.NewResponse(response), nil
}
func (c *ConsoleService) StreamEvents(ctx context.Context, req *connect.Request[pbconsole.StreamEventsRequest], stream *connect.ServerStream[pbconsole.StreamEventsResponse]) error {
// Default to 1 second interval if not specified.
updateInterval := 1 * time.Second
if req.Msg.UpdateInterval != nil && req.Msg.UpdateInterval.AsDuration() > time.Second { // Minimum 1s interval.
updateInterval = req.Msg.UpdateInterval.AsDuration()
}
if req.Msg.Query.Limit == 0 {
return connect.NewError(connect.CodeInvalidArgument, errors.New("limit must be > 0"))
}
query, err := eventsQueryProtoToDAL(req.Msg.Query)
if err != nil {
return errors.WithStack(err)
}
// Default to last 1 day of events
var lastEventTime time.Time
for {
thisRequestTime := time.Now()
newQuery := query
if !lastEventTime.IsZero() {
newQuery = append(newQuery, dal.FilterTimeRange(thisRequestTime, lastEventTime))
}
events, err := c.dal.QueryEvents(ctx, int(req.Msg.Query.Limit), newQuery...)
if err != nil {
return errors.WithStack(err)
}
for index, timelineEvent := range events {
more := len(events) > index+1
err := stream.Send(&pbconsole.StreamEventsResponse{
Event: eventDALToProto(timelineEvent),
More: more,
})
if err != nil {
return errors.WithStack(err)
}
}
lastEventTime = thisRequestTime
select {
case <-time.After(updateInterval):
case <-ctx.Done():
return nil
}
}
}
func eventsQueryProtoToDAL(pb *pbconsole.EventsQuery) ([]dal.EventFilter, error) {
var query []dal.EventFilter
if pb.Order == pbconsole.EventsQuery_DESC {
query = append(query, dal.FilterDescending())
}
for _, filter := range pb.Filters {
switch filter := filter.Filter.(type) {
case *pbconsole.EventsQuery_Filter_Deployments:
deploymentNames := make([]model.DeploymentName, 0, len(filter.Deployments.Deployments))
for _, deployment := range filter.Deployments.Deployments {
deploymentName, err := model.ParseDeploymentName(deployment)
if err != nil {
return nil, connect.NewError(connect.CodeInvalidArgument, errors.WithStack(err))
}
deploymentNames = append(deploymentNames, deploymentName)
}
query = append(query, dal.FilterDeployments(deploymentNames...))
case *pbconsole.EventsQuery_Filter_Requests:
requestNames := make([]model.RequestName, 0, len(filter.Requests.Requests))
for _, request := range filter.Requests.Requests {
_, requestName, err := model.ParseRequestName(request)
if err != nil {
return nil, connect.NewError(connect.CodeInvalidArgument, errors.WithStack(err))
}
requestNames = append(requestNames, requestName)
}
query = append(query, dal.FilterRequests(requestNames...))
case *pbconsole.EventsQuery_Filter_EventTypes:
eventTypes := make([]dal.EventType, 0, len(filter.EventTypes.EventTypes))
for _, eventType := range filter.EventTypes.EventTypes {
switch eventType {
case pbconsole.EventType_EVENT_TYPE_CALL:
eventTypes = append(eventTypes, dal.EventTypeCall)
case pbconsole.EventType_EVENT_TYPE_LOG:
eventTypes = append(eventTypes, dal.EventTypeLog)
case pbconsole.EventType_EVENT_TYPE_DEPLOYMENT_CREATED:
eventTypes = append(eventTypes, dal.EventTypeDeploymentCreated)
case pbconsole.EventType_EVENT_TYPE_DEPLOYMENT_UPDATED:
eventTypes = append(eventTypes, dal.EventTypeDeploymentUpdated)
default:
return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("unknown event type %v", eventType))
}
}
query = append(query, dal.FilterTypes(eventTypes...))
case *pbconsole.EventsQuery_Filter_LogLevel:
level := log.Level(filter.LogLevel.LogLevel)
if level < log.Trace || level > log.Error {
return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("unknown log level %v", filter.LogLevel.LogLevel))
}
query = append(query, dal.FilterLogLevel(level))
case *pbconsole.EventsQuery_Filter_Time:
var newerThan, olderThan time.Time
if filter.Time.NewerThan != nil {
newerThan = filter.Time.NewerThan.AsTime()
}
if filter.Time.OlderThan != nil {
olderThan = filter.Time.OlderThan.AsTime()
}
query = append(query, dal.FilterTimeRange(olderThan, newerThan))
case *pbconsole.EventsQuery_Filter_Id:
var lowerThan, higherThan int64
if filter.Id.LowerThan != nil {
lowerThan = *filter.Id.LowerThan
}
if filter.Id.HigherThan != nil {
higherThan = *filter.Id.HigherThan
}
query = append(query, dal.FilterIDRange(lowerThan, higherThan))
case *pbconsole.EventsQuery_Filter_Call:
var sourceModule types.Option[string]
if filter.Call.SourceModule != nil {
sourceModule = types.Some(*filter.Call.SourceModule)
}
var destVerb types.Option[string]
if filter.Call.DestVerb != nil {
destVerb = types.Some(*filter.Call.DestVerb)
}
query = append(query, dal.FilterCall(sourceModule, filter.Call.DestModule, destVerb))
default:
return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("unknown filter %T", filter))
}
}
return query, nil
}
func eventDALToProto(event dal.Event) *pbconsole.Event {
switch event := event.(type) {
case *dal.CallEvent:
var requestName *string
if r, ok := event.RequestName.Get(); ok {
rstr := r.String()
requestName = &rstr
}
var sourceVerbRef *schemapb.VerbRef
if sourceVerb, ok := event.SourceVerb.Get(); ok {
sourceVerbRef = sourceVerb.ToProto().(*schemapb.VerbRef) //nolint:forcetypeassert
}
return &pbconsole.Event{
TimeStamp: timestamppb.New(event.Time),
Id: event.ID,
Entry: &pbconsole.Event_Call{
Call: &pbconsole.CallEvent{
RequestName: requestName,
DeploymentName: event.DeploymentName.String(),
TimeStamp: timestamppb.New(event.Time),
SourceVerbRef: sourceVerbRef,
DestinationVerbRef: &schemapb.VerbRef{
Module: event.DestVerb.Module,
Name: event.DestVerb.Name,
},
Duration: durationpb.New(event.Duration),
Request: string(event.Request),
Response: string(event.Response),
Error: event.Error.Ptr(),
},
},
}
case *dal.LogEvent:
var requestName *string
if r, ok := event.RequestName.Get(); ok {
rstr := r.String()
requestName = &rstr
}
return &pbconsole.Event{
TimeStamp: timestamppb.New(event.Time),
Id: event.ID,
Entry: &pbconsole.Event_Log{
Log: &pbconsole.LogEvent{
DeploymentName: event.DeploymentName.String(),
RequestName: requestName,
TimeStamp: timestamppb.New(event.Time),
LogLevel: event.Level,
Attributes: event.Attributes,
Message: event.Message,
Error: event.Error.Ptr(),
},
},
}
case *dal.DeploymentCreatedEvent:
var replaced *string
if r, ok := event.ReplacedDeployment.Get(); ok {
rstr := r.String()
replaced = &rstr
}
return &pbconsole.Event{
TimeStamp: timestamppb.New(event.Time),
Id: event.ID,
Entry: &pbconsole.Event_DeploymentCreated{
DeploymentCreated: &pbconsole.DeploymentCreatedEvent{
Name: event.DeploymentName.String(),
Language: event.Language,
ModuleName: event.ModuleName,
MinReplicas: int32(event.MinReplicas),
Replaced: replaced,
},
},
}
case *dal.DeploymentUpdatedEvent:
return &pbconsole.Event{
TimeStamp: timestamppb.New(event.Time),
Id: event.ID,
Entry: &pbconsole.Event_DeploymentUpdated{
DeploymentUpdated: &pbconsole.DeploymentUpdatedEvent{
Name: event.DeploymentName.String(),
MinReplicas: int32(event.MinReplicas),
PrevMinReplicas: int32(event.PrevMinReplicas),
},
},
}
default:
panic(errors.Errorf("unknown event type %T", event))
}
}