-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.go
184 lines (139 loc) · 3.96 KB
/
event.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
package main
import (
"encoding/json"
"github.com/jmoiron/sqlx"
"log"
"strconv"
"strings"
"time"
)
type EventRpc struct {
Id EventID `json:"id"`
Event
}
type EventDb struct {
EventID
Event
TimestampDb time.Time
}
type Event struct {
PackageId string `json:"packageId"` // Move package where this event was emitted.
TransactionModule string `json:"transactionModule"` // Move module where this event was emitted.
Sender string `json:"sender"` // Sender's Sui address.
EventType string `json:"type"` // Move event type.
ParsedJson json.RawMessage `json:"parsedJson,omitempty"` // Parsed json value of the event
Bcs string `json:"bcs,omitempty"` // Base 58 encoded bcs bytes of the move event
TimestampMs string `json:"timestampMs,omitempty"`
}
func NewEventDb(r EventRpc) (d EventDb) {
d.TxDigest = r.Id.TxDigest
d.EventSeq = r.Id.EventSeq
d.PackageId = r.PackageId
d.TransactionModule = r.TransactionModule
d.Sender = r.Sender
d.EventType = r.EventType
d.Bcs = r.Bcs
d.TimestampMs = r.TimestampMs
loc, err := time.LoadLocation("UTC")
if err != nil {
log.Fatal(err)
}
timestampMs, err := strconv.ParseInt(r.TimestampMs, 10, 64)
if err != nil {
log.Fatal(err)
}
d.TimestampDb = time.UnixMilli(timestampMs).In(loc)
bytes, err := json.Marshal(r.ParsedJson)
if err != nil {
log.Fatal(err)
}
s := string(bytes)
//log.Printf("s %v", s)
replaced := strings.Replace(s, `\u0000`, "", -1)
//log.Printf("replaced %v", replaced)
rawMessage := json.RawMessage(replaced)
//log.Printf("rawMessage %v", rawMessage)
d.ParsedJson = rawMessage
return
}
type EventID struct {
TxDigest string `json:"txDigest"`
EventSeq string `json:"eventSeq"`
}
type EventTypeQuery struct {
EventType string `json:"EventType"`
}
func NewEventTypeQuery(s string) *EventTypeQuery {
return &EventTypeQuery{EventType: s}
}
type AllQuery struct {
All []string `json:"All"`
}
func NewAllQuery() *AllQuery {
q := &AllQuery{}
q.All = make([]string, 0)
return q
}
func queryMaxEventID(dataSourceName string) (maxEventID *EventID) {
query := "select txDigest, eventSeq from event order by timestamp desc, txDigest, eventSeq desc limit 1"
db, err := sqlx.Open( /*"postgres"*/ "pgx", dataSourceName)
if err != nil {
log.Fatal(err)
}
eventID := EventID{}
rows, err := db.Queryx(query)
if err != nil {
log.Fatal(err)
}
if rows.Next() {
err = rows.StructScan(&eventID)
if err != nil {
log.Fatal(err)
}
log.Printf("queryMaxEventID %v", eventID)
maxEventID = &eventID
}
err = db.Close()
if err != nil {
log.Fatal(err)
}
return
}
type EventResponseResult struct {
NextCursor EventID `json:"nextCursor"`
Data []EventRpc `json:"data"`
HasNextPage bool `json:"hasNextPage"`
}
func (t *EventResponseResult) Save(dataSourceName string) (countSaved int64) {
if len(t.Data) == 0 {
log.Println("no events found")
return
}
eventsDb := make([]EventDb, 0)
for _, r := range t.Data {
d := NewEventDb(r)
eventsDb = append(eventsDb, d)
}
db, err := sqlx.Open( /*"postgres"*/ "pgx", dataSourceName)
if err != nil {
log.Fatal(err)
}
insertQuery := "insert into Event (txDigest, eventSeq, timestamp, packageId, transactionModule, sender, type, parsedJson, bcs) values (:txdigest, :eventseq, :timestampdb, :packageid, :transactionmodule, :sender, :eventtype, :parsedjson, :bcs)" // todo on conflict on constraint Event_pkey do nothing
lastEvent := eventsDb[len(eventsDb)-1]
result, err := db.NamedExec(insertQuery, eventsDb)
if err != nil {
log.Fatalf("%w", err)
}
rows, err := result.RowsAffected()
if err != nil {
log.Fatal(err)
}
log.Printf("inserted %v rows out of %v data with latest timestamp %v", rows, len(eventsDb), lastEvent.TimestampDb.UTC() /*, lastEvent*/)
err = db.Close()
if err != nil {
log.Fatal(err)
}
//countSaved = len(eventsDb)
countSaved = rows
return
}