-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathjsonout.go
More file actions
230 lines (206 loc) · 6.02 KB
/
jsonout.go
File metadata and controls
230 lines (206 loc) · 6.02 KB
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
package jsonout
import (
"encoding/json"
"fmt"
"io"
"sync"
"time"
"github.com/storacha/go-libstoracha/digestutil"
"github.com/storacha/guppy/pkg/bus"
"github.com/storacha/guppy/pkg/bus/events"
"github.com/storacha/guppy/pkg/preparation/types/id"
uploadsmodel "github.com/storacha/guppy/pkg/preparation/uploads/model"
)
// JSONEmitter writes JSON events as newline-delimited JSON (NDJSON) to a writer.
// It is safe for concurrent use.
type JSONEmitter struct {
mu sync.Mutex
enc *json.Encoder
}
// NewJSONEmitter creates a new JSONEmitter that writes to w.
func NewJSONEmitter(w io.Writer) *JSONEmitter {
return &JSONEmitter{enc: json.NewEncoder(w)}
}
// Emit writes v as a single JSON line.
func (e *JSONEmitter) Emit(v any) {
e.mu.Lock()
defer e.mu.Unlock()
_ = e.enc.Encode(v)
}
// EmitUploadStart emits a lifecycle event when an upload begins.
func (e *JSONEmitter) EmitUploadStart(u *uploadsmodel.Upload) {
e.Emit(UploadStartEvent{
Type: "upload_start",
UploadID: u.ID().String(),
SourceID: u.SourceID().String(),
})
}
// EmitUploadComplete emits a lifecycle event when an upload finishes successfully.
func (e *JSONEmitter) EmitUploadComplete(uploadID id.UploadID, rootCID string) {
e.Emit(UploadCompleteEvent{
Type: "upload_complete",
UploadID: uploadID.String(),
RootCID: rootCID,
})
}
// EmitUploadError emits a lifecycle event when an upload fails.
func (e *JSONEmitter) EmitUploadError(uploadID id.UploadID, err error, attempt int) {
e.Emit(UploadErrorEvent{
Type: "upload_error",
UploadID: uploadID.String(),
Error: err.Error(),
Attempt: attempt,
})
}
// Subscribe registers bus event handlers that emit NDJSON for each event.
func Subscribe(emitter *JSONEmitter, sub bus.Subscriber, uploads []*uploadsmodel.Upload) error {
for _, u := range uploads {
uid := u.ID()
uidStr := uid.String()
srcIDStr := u.SourceID().String()
if err := sub.Subscribe(events.TopicFsEntry(u.SourceID()), func(evt events.FSScanView) {
emitter.Emit(FSScanEvent{
Type: "fs_scan",
SourceID: srcIDStr,
UploadID: uidStr,
FSEntryID: evt.FSEntryID.String(),
Path: evt.Path,
IsDir: evt.IsDir,
Size: evt.Size,
})
}); err != nil {
return fmt.Errorf("subscribing to fs entry events: %w", err)
}
if err := sub.Subscribe(events.TopicDagScan(uid), func(evt events.DAGScanView) {
var cidStr *string
if evt.CID.Defined() {
s := evt.CID.String()
cidStr = &s
}
emitter.Emit(DAGScanEvent{
Type: "dag_scan",
UploadID: uidStr,
FSEntryID: evt.FSEntryID.String(),
Created: evt.Created.Format(time.RFC3339Nano),
Updated: evt.Updated.Format(time.RFC3339Nano),
CID: cidStr,
})
}); err != nil {
return fmt.Errorf("subscribing to dag scan events: %w", err)
}
if err := sub.Subscribe(events.TopicShard(uid), func(evt events.ShardView) {
se := ShardEvent{
Type: "shard",
ShardID: evt.ID.String(),
UploadID: evt.UploadID.String(),
Size: evt.Size,
State: string(evt.State),
}
if len(evt.Digest) > 0 {
s := digestutil.Format(evt.Digest)
se.Digest = &s
}
if evt.PieceCID.Defined() {
s := evt.PieceCID.String()
se.PieceCID = &s
}
if evt.Location != nil {
s := evt.Location.Link().String()
se.Location = &s
}
if evt.PDPAccept != nil {
s := evt.PDPAccept.Link().String()
se.PDPAccept = &s
}
emitter.Emit(se)
}); err != nil {
return fmt.Errorf("subscribing to shard events: %w", err)
}
if err := sub.Subscribe(events.TopicClientPut(uid), func(evt events.PutProgress) {
emitter.Emit(PutProgressEvent{
Type: "put_progress",
UploadID: uidStr,
BlobID: evt.BlobID.String(),
Uploaded: evt.Uploaded,
Total: evt.Total,
})
}); err != nil {
return fmt.Errorf("subscribing to put progress events: %w", err)
}
if err := sub.Subscribe(events.TopicWorker(uid), func(evt events.UploadWorkerEvent) {
we := WorkerEvent{
Type: "worker",
UploadID: uidStr,
Name: evt.Name,
Status: string(evt.Status),
}
if evt.Error != nil {
s := evt.Error.Error()
we.Error = &s
}
emitter.Emit(we)
}); err != nil {
return fmt.Errorf("subscribing to worker events: %w", err)
}
}
return nil
}
// Event types for NDJSON output. Each has a "type" discriminator field.
type FSScanEvent struct {
Type string `json:"type"`
SourceID string `json:"source_id"`
UploadID string `json:"upload_id"`
FSEntryID string `json:"fs_entry_id"`
Path string `json:"path"`
IsDir bool `json:"is_dir"`
Size uint64 `json:"size"`
}
type DAGScanEvent struct {
Type string `json:"type"`
UploadID string `json:"upload_id"`
FSEntryID string `json:"fs_entry_id"`
Created string `json:"created"`
Updated string `json:"updated"`
CID *string `json:"cid"`
}
type ShardEvent struct {
Type string `json:"type"`
ShardID string `json:"shard_id"`
UploadID string `json:"upload_id"`
Size uint64 `json:"size"`
Digest *string `json:"digest,omitempty"`
PieceCID *string `json:"piece_cid,omitempty"`
State string `json:"state"`
Location *string `json:"location,omitempty"`
PDPAccept *string `json:"pdp_accept,omitempty"`
}
type PutProgressEvent struct {
Type string `json:"type"`
UploadID string `json:"upload_id"`
BlobID string `json:"blob_id"`
Uploaded int64 `json:"uploaded"`
Total uint64 `json:"total"`
}
type WorkerEvent struct {
Type string `json:"type"`
UploadID string `json:"upload_id"`
Name string `json:"name"`
Status string `json:"status"`
Error *string `json:"error,omitempty"`
}
type UploadStartEvent struct {
Type string `json:"type"`
UploadID string `json:"upload_id"`
SourceID string `json:"source_id"`
}
type UploadCompleteEvent struct {
Type string `json:"type"`
UploadID string `json:"upload_id"`
RootCID string `json:"root_cid"`
}
type UploadErrorEvent struct {
Type string `json:"type"`
UploadID string `json:"upload_id"`
Error string `json:"error"`
Attempt int `json:"attempt"`
}