-
Notifications
You must be signed in to change notification settings - Fork 10
/
ordersSave.go
160 lines (142 loc) · 3.96 KB
/
ordersSave.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
package marketwatch
import (
"log"
"sync"
"time"
"github.com/Masterminds/squirrel"
"github.com/antihax/goesi/esi"
)
/*
func (s *MarketWatch) orderChangesPump() {
for {
c := <-s.orderChangeChan
order := squirrel.Insert("evedata.marketOrderHistory").Columns("orderID", "changed", "locationID", "typeID", "volumeChange", "volumeRemain", "price", "duration", "isBuyOrder")
count := 0
for _, g := range c {
if g.VolumeChange > 0 && // Skip price changes
g.Issued.Add(time.Hour*24*time.Duration(g.Duration)).
After(time.Now().UTC()) { // Skip expired
count++
order = order.Values(g.OrderID, g.TimeChanged, g.LocationId, g.TypeID, g.VolumeChange, g.VolumeRemain, g.Price, g.Duration, g.IsBuyOrder)
}
}
// Early out if there is nothing to change
if count == 0 {
continue
}
sqlq, args, err := order.ToSql()
if err != nil {
log.Println(err)
} else {
err = s.doSQL(sqlq+" ON DUPLICATE KEY UPDATE orderID = orderID", args...)
if err != nil {
log.Println(err)
}
}
}
}*/
func (s *MarketWatch) saveAdditions(c []esi.GetMarketsRegionIdOrders200Ok) {
order := squirrel.Insert("evedata.market").Columns(
"orderID", "regionID", "stationID", "typeID", "bid", "price", "minVolume",
"remainingVolume", "enteredVolume", "issued", "duration", "reported",
)
start := time.Now()
count := 0
length := len(c)
wg := sync.WaitGroup{}
sem := make(chan bool, 8)
for _, g := range c {
count++
order = order.Values(
g.OrderId,
squirrel.Expr("evedata.regionIDByStructureID(?)", g.LocationId),
g.LocationId, g.TypeId, g.IsBuyOrder, g.Price, g.MinVolume,
g.VolumeRemain, g.VolumeTotal, g.Issued, g.Duration, squirrel.Expr("UTC_TIMESTAMP()"),
)
// If we have 80 or reached the end, dump what we have
if count%80 == 0 || count == length {
sqlq, args, err := order.ToSql()
if err != nil {
log.Println(err)
} else {
wg.Add(1)
sem <- true
go func(sqlq string, args []interface{}, start time.Time, count int) {
defer func() { <-sem; wg.Done() }()
err = s.doSQL(sqlq+`ON DUPLICATE KEY UPDATE price=VALUES(price),
remainingVolume=VALUES(remainingVolume),
issued=VALUES(issued), duration=VALUES(duration),
reported=VALUES(reported);`, args...)
if err != nil {
log.Println(err)
}
}(sqlq, args, start, count)
}
// restart the query
start = time.Now()
order = squirrel.Insert("evedata.market").Columns(
"orderID", "regionID", "stationID", "typeID", "bid", "price", "minVolume",
"remainingVolume", "enteredVolume", "issued", "duration", "reported",
)
}
}
wg.Wait()
}
func (s *MarketWatch) saveDeletions(c []OrderChange) {
or := squirrel.Or{}
for _, g := range c {
or = append(or, squirrel.Eq{"orderID": g.OrderID})
}
order := squirrel.Delete("evedata.market").Where(or)
sqlq, args, err := order.ToSql()
if err != nil {
log.Println(err)
} else {
err = s.doSQL(sqlq, args...)
if err != nil {
log.Println(err)
}
}
}
func (s *MarketWatch) saveChanges(c []OrderChange) {
for _, g := range c {
sqlq, args, err := squirrel.Update("evedata.market").
Set("price", g.Price).
Set("issued", g.Issued).
Set("remainingVolume", g.VolumeRemain).
Set("duration", g.Duration).
Set("reported", time.Now().UTC()).
Where(squirrel.Eq{"orderID": g.OrderID}).ToSql()
if err != nil {
log.Println(err)
} else {
err = s.doSQL(sqlq, args...)
if err != nil {
log.Println(err)
}
}
}
}
func (s *MarketWatch) sqlPumps() {
// Deal with change records in a separate routine as we don't have to worry about
// collisions.
//go s.orderChangesPump()
for {
select {
case v := <-s.orderChan:
s.saveAdditions(v)
case v := <-s.orderDeleteChan:
s.saveDeletions(v)
case v := <-s.orderChangeChan:
s.saveChanges(v)
case v := <-s.contractChan:
s.saveContractAdditions(v)
case v := <-s.contractDeleteChan:
s.saveContractChanges(v)
case v := <-s.contractChangeChan:
s.saveContractChanges(v)
default:
time.Sleep(time.Second)
}
}
}