ev/gql/graph.go

204 lines
4.5 KiB
Go
Raw Permalink Normal View History

package gql_ev
2022-09-04 08:34:22 -06:00
import (
"context"
2023-04-02 21:00:22 -06:00
"encoding"
"encoding/json"
"errors"
2022-09-04 08:34:22 -06:00
"fmt"
"net/http"
"time"
2023-07-12 17:35:02 -06:00
"go.sour.is/pkg/gql"
"go.sour.is/pkg/lg"
2023-02-26 22:33:01 -07:00
"go.sour.is/ev"
2023-09-29 10:07:24 -06:00
"go.sour.is/ev/event"
2022-09-04 08:34:22 -06:00
)
type EventResolver interface {
Events(ctx context.Context, streamID string, paging *gql.PageInput) (*gql.Connection, error)
EventAdded(ctx context.Context, streamID string, after int64) (<-chan *Event, error)
TruncateStream(ctx context.Context, streamID string, index int64) (bool, error)
2022-09-04 08:34:22 -06:00
}
type contextKey struct {
name string
}
var esKey = contextKey{"event-store"}
2022-09-04 08:34:22 -06:00
type EventStore struct {
*ev.EventStore
}
func (es *EventStore) IsResolver() {}
2022-09-04 08:34:22 -06:00
func (es *EventStore) Events(ctx context.Context, streamID string, paging *gql.PageInput) (*gql.Connection, error) {
ctx, span := lg.Span(ctx)
2022-09-04 08:34:22 -06:00
defer span.End()
lis, err := es.Read(ctx, streamID, paging.GetIdx(0), paging.GetCount(30))
if err != nil && !errors.Is(err, ev.ErrNotFound) {
2022-09-04 08:34:22 -06:00
span.RecordError(err)
return nil, err
}
edges := make([]gql.Edge, 0, len(lis))
for i := range lis {
span.AddEvent(fmt.Sprint("event ", i, " of ", len(lis)))
edges = append(edges, &Event{lis[i]})
2022-09-04 08:34:22 -06:00
}
var first, last uint64
if first, err = es.FirstIndex(ctx, streamID); err != nil {
span.RecordError(err)
return nil, err
}
if last, err = es.LastIndex(ctx, streamID); err != nil {
span.RecordError(err)
return nil, err
}
return &gql.Connection{
Paging: &gql.PageInfo{
Next: lis.Last().EventMeta().ActualPosition < last,
Prev: lis.First().EventMeta().ActualPosition > first,
Begin: lis.First().EventMeta().ActualPosition,
End: lis.Last().EventMeta().ActualPosition,
2022-09-04 08:34:22 -06:00
},
Edges: edges,
}, nil
}
func (e *EventStore) EventAdded(ctx context.Context, streamID string, after int64) (<-chan *Event, error) {
ctx, span := lg.Span(ctx)
2022-09-04 08:34:22 -06:00
defer span.End()
es := e.EventStream()
if es == nil {
return nil, fmt.Errorf("EventStore does not implement streaming")
}
sub, err := es.Subscribe(ctx, streamID, after)
if err != nil {
span.RecordError(err)
return nil, err
}
ch := make(chan *Event)
2022-09-04 08:34:22 -06:00
go func() {
ctx, span := lg.Span(ctx)
2022-09-04 08:34:22 -06:00
defer span.End()
{
ctx, span := lg.Fork(ctx)
defer func() {
defer span.End()
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()
err := sub.Close(ctx)
span.RecordError(err)
}()
}
2022-09-04 08:34:22 -06:00
for <-sub.Recv(ctx) {
2022-09-04 08:34:22 -06:00
events, err := sub.Events(ctx)
if err != nil {
span.RecordError(err)
break
}
span.AddEvent(fmt.Sprintf("received %d events", len(events)))
for i := range events {
select {
case ch <- &Event{events[i]}:
2022-09-04 08:34:22 -06:00
continue
case <-ctx.Done():
return
}
}
}
}()
return ch, nil
}
func (es *EventStore) TruncateStream(ctx context.Context, streamID string, index int64) (bool, error) {
ctx, span := lg.Span(ctx)
defer span.End()
err := es.Truncate(ctx, streamID, index)
return err == nil, err
}
func (e *EventStore) GetMiddleware() func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx, span := lg.Span(r.Context())
defer span.End()
r = r.WithContext(gql.ToContext(ctx, esKey, e))
next.ServeHTTP(w, r)
})
}
}
2022-09-04 08:34:22 -06:00
type Event struct {
2022-09-04 08:34:22 -06:00
e event.Event
}
func (e *Event) ID() string {
return fmt.Sprint(e.e.EventMeta().StreamID, "@", e.e.EventMeta().Position)
2022-09-04 08:34:22 -06:00
}
func (e *Event) EventID() string {
2022-09-04 08:34:22 -06:00
return e.e.EventMeta().GetEventID()
}
func (e *Event) StreamID() string {
return e.e.EventMeta().StreamID
}
func (e *Event) Position() uint64 {
return e.e.EventMeta().Position
}
func (e *Event) Type() string {
return event.TypeOf(e.e)
}
func (e *Event) Created() time.Time {
return e.e.EventMeta().Created()
}
func (e *Event) Values() map[string]interface{} {
2022-09-04 08:34:22 -06:00
return event.Values(e.e)
}
func (e *Event) Bytes() (string, error) {
2023-04-02 21:00:22 -06:00
switch e := e.e.(type) {
case encoding.BinaryMarshaler:
b, err := e.MarshalBinary()
return string(b), err
case encoding.TextMarshaler:
b, err := e.MarshalText()
return string(b), err
default:
b, err := json.Marshal(e)
return string(b), err
}
2022-09-04 08:34:22 -06:00
}
func (e *Event) Meta() *event.Meta {
2022-09-04 08:34:22 -06:00
meta := e.e.EventMeta()
return &meta
}
func (e *Event) Linked(ctx context.Context) (*Event, error) {
ctx, span := lg.Span(ctx)
defer span.End()
values := event.Values(e.e)
streamID, ok := values["stream_id"].(string)
if !ok {
return nil, nil
}
pos, ok := values["pos"].(uint64)
if !ok {
return nil, nil
}
2022-10-30 09:18:08 -06:00
events, err := gql.FromContext[contextKey, *EventStore](ctx, esKey).ReadN(ctx, streamID, pos)
return &Event{e: events.First()}, err
}
func (e *Event) IsEdge() {}