-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
240 lines (196 loc) · 6.43 KB
/
main.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
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
231
232
233
234
235
236
237
238
239
240
package main
import (
"context"
"fmt"
"log"
"net"
"net/http"
"runtime"
"time"
"github.com/golang/protobuf/proto"
"github.com/EVE-Tools/element43/go/lib/transport"
"github.com/EVE-Tools/top-stations/lib/esiMarkets"
pb "github.com/EVE-Tools/top-stations/lib/topStations"
"github.com/antihax/goesi"
"github.com/boltdb/bolt"
"github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
"github.com/grpc-ecosystem/go-grpc-middleware/tags"
"github.com/kelseyhightower/envconfig"
"github.com/robfig/cron"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// Config holds the application's configuration info from the environment.
type Config struct {
Cron string `default:"@hourly" envconfig:"cron"`
DBPath string `default:"/data/top-stations.db" envconfig:"db_path"`
LogLevel string `default:"info" envconfig:"log_level"`
EsiMarketsHost string `default:"esi-markets.element43.svc.cluster.local:43000" envconfig:"esi_markets_host"`
Port string `default:"43000" envconfig:"port"`
}
// Server is a gRPC server serving location requests
type Server struct {
db *bolt.DB
}
// Global instances, move into context/parameter if project grows larger!
var db *bolt.DB
var config *Config
// GetTopStations returns the top stations from persistence.
func (server *Server) GetTopStations(context context.Context, request *pb.GetTopStationsRequest) (*pb.GetTopStationsResponse, error) {
var statsBlob []byte
// Try to get stats from BoltDB
db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte("topStations"))
statsBlob = bucket.Get([]byte("stats"))
return nil
})
if statsBlob == nil {
logrus.Error("could not get stats from BoltDB")
return nil, status.Error(codes.NotFound, "Error retrieving stats")
}
var stats pb.GetTopStationsResponse
err := proto.Unmarshal(statsBlob, &stats)
if err != nil {
logrus.WithError(err).Error("could not parse stats from BoltDB")
return nil, status.Error(codes.NotFound, "Error parsing stats")
}
return &stats, nil
}
func main() {
loadConfig()
startEndpoint()
// Terminate this goroutine, crash if all other goroutines exited
runtime.Goexit()
}
// Load configuration from environment
func loadConfig() {
config = &Config{}
envconfig.MustProcess("TOP_STATIONS", config)
logLevel, err := logrus.ParseLevel(config.LogLevel)
if err != nil {
panic(err)
}
logrus.SetLevel(logLevel)
logrus.Debugf("Config: %+v", config)
}
// Init DB and start gRPC endpoint.
func startEndpoint() {
var err error
db, err = bolt.Open(config.DBPath, 0600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
panic(err)
}
// Initialize buckets
err = db.Update(func(tx *bolt.Tx) error {
tx.CreateBucketIfNotExists([]byte("topStations"))
return nil
})
if err != nil {
panic(err)
}
// Start cron
job := cron.New()
job.AddFunc(config.Cron, updateTopStations)
job.Start()
// Inititalize gRPC server
var opts []grpc.ServerOption
var logOpts []grpc_logrus.Option
opts = append(opts, grpc_middleware.WithUnaryServerChain(
grpc_ctxtags.UnaryServerInterceptor(),
grpc_logrus.UnaryServerInterceptor(logrus.NewEntry(logrus.New()), logOpts...)))
listener, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%s", config.Port))
if err != nil {
log.Fatalf("could not listen: %v", err)
}
updateTopStations()
grpcServer := grpc.NewServer(opts...)
pb.RegisterTopStationsServer(grpcServer, &Server{db: db})
grpcServer.Serve(listener)
}
func updateTopStations() {
//
// 1. Download list of region_ids
// 2. For each region download orders and aggregate stats
// 3. Save to BoltDB
//
const userAgent string = "Element43/top-stations (element-43.com)"
const timeout time.Duration = time.Duration(time.Second * 30)
httpClientESI := &http.Client{
Timeout: timeout,
Transport: transport.NewESITransport(userAgent, timeout, 500),
}
esiClient := goesi.NewAPIClient(httpClientESI, userAgent)
regionIDs, _, err := esiClient.ESI.UniverseApi.GetUniverseRegions(nil, nil)
if err != nil {
logrus.WithError(err).Fatal("could not load regions from ESI")
}
// Set up a connection to the server.
opts := []grpc.DialOption{grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(50000000)), grpc.WithInsecure()}
conn, err := grpc.Dial(config.EsiMarketsHost, opts...)
if err != nil {
logrus.WithError(err).Fatal("could not connect to esiMarkets")
}
defer conn.Close()
esiMarketsClient := esiMarkets.NewESIMarketsClient(conn)
statsMap := make(map[uint64]*pb.StationStats)
for _, id := range regionIDs {
logrus.Infof("Processing region %d...", id)
orders, err := esiMarketsClient.GetRegion(context.Background(), &esiMarkets.GetRegionRequest{RegionId: uint64(id)})
if err != nil {
logrus.WithError(err).WithField("region_id", id).Warn("could not load orders from esiMarkets for region")
continue
}
for _, order := range orders.Orders {
if stats, ok := statsMap[order.LocationId]; ok {
stats.TotalOrders = stats.TotalOrders + 1
stats.TotalVolume = stats.TotalVolume + (order.Price * float64(order.VolumeRemain))
if order.IsBuyOrder {
stats.BidVolume = stats.BidVolume + (order.Price * float64(order.VolumeRemain))
} else {
stats.AskVolume = stats.AskVolume + (order.Price * float64(order.VolumeRemain))
}
} else {
if order.IsBuyOrder {
statsMap[order.LocationId] = &pb.StationStats{
Id: int64(order.LocationId),
AskVolume: 0,
BidVolume: order.Price * float64(order.VolumeRemain),
TotalVolume: order.Price * float64(order.VolumeRemain),
TotalOrders: 1,
}
} else {
statsMap[order.LocationId] = &pb.StationStats{
Id: int64(order.LocationId),
AskVolume: order.Price * float64(order.VolumeRemain),
BidVolume: 0,
TotalVolume: order.Price * float64(order.VolumeRemain),
TotalOrders: 1,
}
}
}
}
}
stats := pb.GetTopStationsResponse{
Stations: make([]*pb.StationStats, 0),
}
for _, value := range statsMap {
stats.Stations = append(stats.Stations, value)
}
blob, err := proto.Marshal(&stats)
if err != nil {
logrus.WithError(err).Warn("could not marshal station stats")
return
}
db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte("topStations"))
if bucket == nil {
panic("Bucket not found! This should never happen!")
}
err := bucket.Put([]byte("stats"), blob)
return err
})
logrus.Info("Done!")
}