Skip to content

Commit 5ceac77

Browse files
committed
feat(transform): Add runID to transform. Publish some events.
1 parent 1a0ef6f commit 5ceac77

File tree

13 files changed

+95
-35
lines changed

13 files changed

+95
-35
lines changed

cmd/apply.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ func (o *ApplyOptions) Run() error {
102102
Refstr: o.Refs.Ref(),
103103
Transform: &tf,
104104
ScriptOutput: o.Out,
105+
Wait: true,
105106
}
106107
res := lib.ApplyResult{}
107108
if err = o.TransformMethods.Apply(&params, &res); err != nil {

event/event.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (b *bus) Publish(ctx context.Context, typ Type, payload interface{}) error
143143
return b.publish(ctx, typ, "", payload)
144144
}
145145

146-
// Publish sends an event with a given sessionID to the bus
146+
// PublishID sends an event with a given sessionID to the bus
147147
func (b *bus) PublishID(ctx context.Context, typ Type, sessionID string, payload interface{}) error {
148148
return b.publish(ctx, typ, sessionID, payload)
149149
}
@@ -209,7 +209,7 @@ func (b *bus) SubscribeID(handler Handler, sessionID string) {
209209
b.idSubs[sessionID] = append(b.idSubs[sessionID], handler)
210210
}
211211

212-
// SubscribeAll requets all events from the bus
212+
// SubscribeAll requests all events from the bus
213213
func (b *bus) SubscribeAll(handler Handler) {
214214
b.lk.Lock()
215215
defer b.lk.Unlock()

event/transform.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package event
2+
3+
const (
4+
// ETTransformStart signals the start a transform execution
5+
ETTransformStart = Type("transform:Start")
6+
// ETTransformComplete signals the successful completion of a transform execution
7+
ETTransformComplete = Type("transform:Complete")
8+
// ETTransformFailure signals the failure of a transform execution
9+
ETTransformFailure = Type("transform:Failure")
10+
)

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/gofrs/flock v0.7.1 // indirect
1515
github.com/google/flatbuffers v1.12.1-0.20200706154056-969d0f7a6317
1616
github.com/google/go-cmp v0.5.3
17+
github.com/google/uuid v1.1.1
1718
github.com/ipfs/go-cid v0.0.7
1819
github.com/ipfs/go-datastore v0.4.4
1920
github.com/ipfs/go-ipfs v0.6.0
@@ -38,9 +39,8 @@ require (
3839
github.com/multiformats/go-multihash v0.0.14
3940
github.com/olekukonko/tablewriter v0.0.4
4041
github.com/pkg/errors v0.9.1
41-
github.com/qri-io/dataset v0.2.1-0.20201201155506-9b4fc79ffde8
4242
github.com/qri-io/dag v0.2.2-0.20201208212257-ae00241c4b48
43-
github.com/qri-io/dataset v0.2.1-0.20201124144731-82162a0f76e6
43+
github.com/qri-io/dataset v0.2.1-0.20210119191645-a8a3aa840464
4444
github.com/qri-io/deepdiff v0.2.1-0.20200807143746-d02d9f531f5b
4545
github.com/qri-io/didmod v0.0.0-20201123165422-8b2e224c993a
4646
github.com/qri-io/doggos v0.1.0

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,10 @@ github.com/qri-io/dataset v0.2.1-0.20201124144731-82162a0f76e6 h1:a9CYZQ+DCzwqg8
11321132
github.com/qri-io/dataset v0.2.1-0.20201124144731-82162a0f76e6/go.mod h1:HtwGskdCECbOON0iVQHEEm6fykwDqharlqabc1ssj3Y=
11331133
github.com/qri-io/dataset v0.2.1-0.20201201155506-9b4fc79ffde8 h1:/9pbWabRT9BbjFp1AjdAsXKT2NQp+mGmyvnTylPyEHY=
11341134
github.com/qri-io/dataset v0.2.1-0.20201201155506-9b4fc79ffde8/go.mod h1:HtwGskdCECbOON0iVQHEEm6fykwDqharlqabc1ssj3Y=
1135+
github.com/qri-io/dataset v0.2.1-0.20210118153421-4b6176e58a47 h1:8VYob4qYMMqvaGuJ54Js+8VrUOF8O8TrOJ/aJOeihbA=
1136+
github.com/qri-io/dataset v0.2.1-0.20210118153421-4b6176e58a47/go.mod h1:HtwGskdCECbOON0iVQHEEm6fykwDqharlqabc1ssj3Y=
1137+
github.com/qri-io/dataset v0.2.1-0.20210119191645-a8a3aa840464 h1:EzaPWYsCUEJnx6cniZpeF5g3JZJ2yOhsICjRVP9z7Yk=
1138+
github.com/qri-io/dataset v0.2.1-0.20210119191645-a8a3aa840464/go.mod h1:HtwGskdCECbOON0iVQHEEm6fykwDqharlqabc1ssj3Y=
11351139
github.com/qri-io/deepdiff v0.2.1-0.20200807143746-d02d9f531f5b h1:T8qEIv+qLi5mVWvSS329wJ+HbN7cfMwCWjRVzh/+upo=
11361140
github.com/qri-io/deepdiff v0.2.1-0.20200807143746-d02d9f531f5b/go.mod h1:NrL/b7YvexgpGb4HEO3Rlx5RrMLDfxuKDf/XDAq5ac0=
11371141
github.com/qri-io/didmod v0.0.0-20201123165422-8b2e224c993a h1:40BIa59lae2xZ7iieb3UU4/X57jZsWZ6QgqwdjDQhig=

lib/datasets.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/qri-io/qri/dscache/build"
2727
"github.com/qri-io/qri/dsref"
2828
qrierr "github.com/qri-io/qri/errors"
29+
"github.com/qri-io/qri/event"
2930
"github.com/qri-io/qri/fsi"
3031
"github.com/qri-io/qri/fsi/linkfile"
3132
"github.com/qri-io/qri/repo"
@@ -622,8 +623,17 @@ func (m *DatasetMethods) Save(p *SaveParams, res *dataset.Dataset) error {
622623
// string and control how transform functions
623624
loader := NewParseResolveLoadFunc("", m.inst.defaultResolver(), m.inst)
624625

626+
// allocate an ID for the transform, for now just log the events it produces
627+
runID := transform.NewRunID()
628+
m.inst.bus.SubscribeID(func(ctx context.Context, e event.Event) error {
629+
when := time.Unix(e.Timestamp/1000000000, e.Timestamp%1000000000)
630+
log.Infof("[%s] event %s: %s", when, e.Type, e.Payload)
631+
return nil
632+
}, runID)
633+
625634
// apply the transform
626-
err := transform.Apply(ctx, ds, loader, str, scriptOut, secrets)
635+
shouldWait := true
636+
err := transform.Apply(ctx, ds, loader, runID, m.inst.bus, shouldWait, str, scriptOut, secrets)
627637
if err != nil {
628638
return err
629639
}

lib/transform.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import (
44
"context"
55
"fmt"
66
"io"
7+
"time"
78

89
"github.com/qri-io/dataset"
910
"github.com/qri-io/qri/base"
1011
"github.com/qri-io/qri/dsref"
12+
"github.com/qri-io/qri/event"
1113
"github.com/qri-io/qri/transform"
1214
)
1315

@@ -30,12 +32,10 @@ func NewTransformMethods(inst *Instance) *TransformMethods {
3032
type ApplyParams struct {
3133
Refstr string
3234
Transform *dataset.Transform
33-
Source string
3435
Secrets map[string]string
36+
Wait bool
3537

36-
// TODO(dustmop): add `Wait bool`, if false, run the save asynchronously
37-
// and return events on the bus that provide the progress of the save operation
38-
38+
Source string
3939
ScriptOutput io.Writer
4040
}
4141

@@ -49,9 +49,8 @@ func (p *ApplyParams) Valid() error {
4949

5050
// ApplyResult is the result of an apply command
5151
type ApplyResult struct {
52-
Data *dataset.Dataset
53-
// TODO(dustmop): Make Apply asynchronous, running the transform in a go-routine.
54-
// Return a channel that will send progress on the execution.
52+
Data *dataset.Dataset
53+
RunID string `json:"runID"`
5554
}
5655

5756
// Apply runs a transform script
@@ -87,15 +86,26 @@ func (m *TransformMethods) Apply(p *ApplyParams, res *ApplyResult) error {
8786
str := m.inst.node.LocalStreams
8887
loader := NewParseResolveLoadFunc("", m.inst.defaultResolver(), m.inst)
8988

89+
// allocate an ID for the transform, for now just log the events it produces
90+
runID := transform.NewRunID()
91+
m.inst.bus.SubscribeID(func(ctx context.Context, e event.Event) error {
92+
when := time.Unix(e.Timestamp/1000000000, e.Timestamp%1000000000)
93+
log.Infof("[%s] event %s: %s", when, e.Type, e.Payload)
94+
return nil
95+
}, runID)
96+
9097
scriptOut := p.ScriptOutput
91-
err = transform.Apply(ctx, ds, loader, str, scriptOut, p.Secrets)
98+
err = transform.Apply(ctx, ds, loader, runID, m.inst.bus, p.Wait, str, scriptOut, p.Secrets)
9299
if err != nil {
93100
return err
94101
}
95102

96-
if err = base.InlineJSONBody(ds); err != nil && err != base.ErrNoBodyToInline {
97-
return err
103+
if p.Wait {
104+
if err = base.InlineJSONBody(ds); err != nil && err != base.ErrNoBodyToInline {
105+
return err
106+
}
107+
res.Data = ds
98108
}
99-
res.Data = ds
109+
res.RunID = runID
100110
return nil
101111
}

lib/transform_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func TestApplyTransform(t *testing.T) {
2727
Transform: &dataset.Transform{
2828
ScriptPath: "testdata/cities_2/add_city.star",
2929
},
30+
Wait: true,
3031
})
3132
if err != nil {
3233
t.Error(err)

lib/websocket.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ func (h *wsHandler) WSConnectionHandler(w http.ResponseWriter, r *http.Request)
6464
func (h *wsHandler) wsMessageHandler(_ context.Context, e event.Event) error {
6565
ctx := context.Background()
6666
evt := map[string]interface{}{
67-
"type": string(e.Type),
68-
"data": e.Payload,
67+
"type": string(e.Type),
68+
"ts": e.Timestamp,
69+
"sessionID": e.SessionID,
70+
"data": e.Payload,
6971
}
7072

7173
log.Debugf("sending event %q to %d websocket conns", e.Type, len(h.conns))

transform/apply.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,41 @@ import (
66
"fmt"
77
"io"
88

9+
"github.com/google/uuid"
10+
golog "github.com/ipfs/go-log"
911
"github.com/qri-io/dataset"
1012
"github.com/qri-io/ioes"
1113
"github.com/qri-io/qri/dsref"
14+
"github.com/qri-io/qri/event"
1215
"github.com/qri-io/qri/transform/startf"
1316
)
1417

15-
// TODO(dustmop): Tests. Especially once the `apply` command exists.
18+
var log = golog.Logger("transform")
1619

1720
// Apply applies the transform script to order to modify the changing dataset
1821
func Apply(
1922
ctx context.Context,
2023
ds *dataset.Dataset,
2124
loader dsref.ParseResolveLoad,
25+
runID string,
26+
pub event.Publisher,
27+
wait bool,
2228
str ioes.IOStreams,
2329
scriptOut io.Writer,
2430
secrets map[string]string,
25-
) (err error) {
31+
) error {
2632
var (
2733
target = ds
2834
head *dataset.Dataset
35+
err error
2936
)
3037

3138
if target.Transform == nil || target.Transform.ScriptFile() == nil {
3239
return errors.New("apply requires a transform component with a script file")
3340
}
41+
if runID == "" {
42+
return errors.New("apply requires a runID")
43+
}
3444

3545
if ds.Name != "" {
3646
head, err = loader(ctx, fmt.Sprintf("%s/%s", ds.Peername, ds.Name))
@@ -54,11 +64,18 @@ func Apply(
5464
startf.AddDatasetLoader(loader),
5565
}
5666

67+
pub.PublishID(ctx, event.ETTransformStart, runID, "")
68+
5769
if err = startf.ExecScript(ctx, target, head, opts...); err != nil {
70+
pub.PublishID(ctx, event.ETTransformFailure, runID, "")
5871
return err
5972
}
6073

61-
str.PrintErr("✅ transform complete\n")
62-
74+
pub.PublishID(ctx, event.ETTransformComplete, runID, "")
6375
return nil
6476
}
77+
78+
// NewRunID creates a run identifier
79+
func NewRunID() string {
80+
return uuid.New().String()
81+
}

0 commit comments

Comments
 (0)