-
Notifications
You must be signed in to change notification settings - Fork 18
/
events.go
122 lines (108 loc) · 2.78 KB
/
events.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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package events
import (
"context"
"fmt"
"os"
"time"
"code.vegaprotocol.io/vega/datanode/broker"
eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
)
func Run(in, out string) error {
marshaler := jsonpb.Marshaler{
EnumsAsInts: true,
OrigName: true,
Indent: " ",
}
fmt.Println("parsing event bytes from", in, "into json:", out)
f, err := os.Create(out)
if err != nil {
return err
}
defer f.Close()
eventFile, err := os.Open(in)
if err != nil {
return err
}
defer eventFile.Close()
ctx, cfunc := context.WithCancel(context.Background())
defer cfunc()
ch, ech := startFileRead(ctx, eventFile)
for {
select {
case e, ok := <-ch:
if e == nil && !ok {
return nil
}
es, err := marshaler.MarshalToString(e)
if err != nil {
return err
}
if _, err := f.WriteString(es + "\n"); err != nil {
return err
}
case err, ok := <-ech:
if err == nil && !ok {
return nil
}
return err
}
}
}
func startFileRead(ctx context.Context, eventFile *os.File) (<-chan *eventspb.BusEvent, <-chan error) {
ch := make(chan *eventspb.BusEvent, 1)
ech := make(chan error, 1)
go func() {
defer func() {
eventFile.Close()
close(ch)
close(ech)
}()
var offset, nEvents int64
now := time.Now()
for {
select {
case <-ctx.Done():
return
default:
rawEvent, _, read, err := broker.ReadRawEvent(eventFile, offset)
if err != nil {
ech <- fmt.Errorf("failed to read raw event: %w", err)
return
}
if read == 0 {
return
}
offset += int64(read)
busEvent := &eventspb.BusEvent{}
if err := proto.Unmarshal(rawEvent, busEvent); err != nil {
ech <- fmt.Errorf("failed to unmarshal bus event: %w", err)
return
}
ch <- busEvent
// if can be quite slow so lets print something out every now and again so it doesn't look like its frozen
nEvents++
if time.Since(now) > time.Second {
fmt.Println("events parsed so far:", nEvents)
now = time.Now()
}
}
}
}()
return ch, ech
}