-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInventoryProcessor.go
377 lines (316 loc) · 10.8 KB
/
InventoryProcessor.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package main
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/aws/aws-sdk-go/aws/session"
"time"
"encoding/json"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"os"
"net/http"
"text/template"
"log"
"io"
"io/ioutil"
"strconv"
)
type Configuration struct {
Amazon struct {
AccountNumber string `json:"accountNumber"`
Endpoint string `json:"endpoint"`
QueueURL string `json:"queueUrl"`
Region string `json:"region"`
InventoryFactsQueueName string `json:"inventoryFactsQueueName"`
InventoryPoolsQueueName string `json:"inventoryPoolsQueueName"`
} `json:"amazon"`
App struct {
NumDefinitionRoutines int `json:"numDefinitionRoutines"`
NumFactsRoutines int `json:"numFactsRoutines"`
} `json:"app"`
Mongo struct {
DB string `json:"dB"`
FactsCollection string `json:"factsCollection"`
HostAndPort string `json:"hostAndPort"`
PoolsCollection string `json:"poolsCollection"`
} `json:"mongo"`
}
type InventoryPoolsDefinitionMessage struct {
InventoryPools []struct {
Brand string `json:"brand"`
Country []struct {
CountryCode string `json:"countryCode"`
Regions []string `json:"regions"`
} `json:"country"`
ID string `json:"id"`
Type string `json:"type"`
} `json:"inventoryPools"`
}
type InventoryPoolFact struct {
Brand string `json:"brand"`
DocType string `json:"docType"`
Pool string `json:"pool"`
ProductID string `json:"productId"`
Availability string `json:"availability"`
BackOrderLevel int `json:"backOrderLevel"`
Backorderable string `json:"backorderable"`
ShipmentDate int `json:"shipmentDate"`
SiteID string `json:"siteId"`
SkuID string `json:"skuId"`
StockLevel int `json:"stockLevel"`
StoreStockLevel int `json:"storeStockLevel"`
}
type InventoryPoolsFacts struct {
Brand string `json:"brand"`
DocType string `json:"docType"`
Pool string `json:"pool"`
ProductID string `json:"productId"`
Skus []struct {
Availability string `json:"availability"`
BackOrderLevel int `json:"backOrderLevel"`
Backorderable string `json:"backorderable"`
ShipmentDate int `json:"shipmentDate"`
SiteID string `json:"siteId"`
SkuID string `json:"skuId"`
StockLevel int `json:"stockLevel"`
StoreStockLevel int `json:"storeStockLevel"`
} `json:"skus"`
}
type Page struct {
Title string
ReceivedDefMsgs string
ReceivedFactsMsgs string
StoredDefMsgs string
StoredFactsMsgs string
}
var svc *sqs.SQS
var url string
var mongo_session *mgo.Session
var config Configuration
var pools_definition_messages_received_from_queue int64
var inventory_pools_facts_messages_received_from_queue int64
var pools_definition_messages_stored_in_mongo int64
var inventory_pools_facts_messages_stored_in_mongo int64
func init() {
// Load Config File
file, e := ioutil.ReadFile("./config.json")
if e != nil {
log.Printf("File error: %v\n", e)
os.Exit(1)
}
log.Printf("%s\n", string(file))
var config Configuration
json.Unmarshal(file, &config)
// Setup connections
if config.Amazon.Endpoint != "" {
svc = sqs.New(session.New(), &aws.Config{Endpoint: aws.String("http://localhost:9324"), Region: aws.String(config.Amazon.Region)})
url = config.Amazon.QueueURL
} else {
svc = sqs.New(session.New(), &aws.Config{Region: aws.String(config.Amazon.Region) })
url = config.Amazon.QueueURL + config.Amazon.AccountNumber + "/"
}
}
func startGoRoutines() {
for i:=0;i<config.App.NumDefinitionRoutines;i++ {
c := make(chan string)
go updateMongoInventoryPoolDefinition(c)
go processInventoryPoolDefinition(c)
}
for i:=0;i<config.App.NumFactsRoutines;i++ {
c := make(chan string)
go updateMongoInventoryPoolFacts(c)
go processInventoryPoolFacts(c)
}
}
/*
*
* Program entry point creates connection to SQS and Mongo then pool SQS for messages
*
*/
func main() {
counter = 0
pools_definition_messages_received_from_queue = 0
pools_definition_messages_stored_in_mongo = 0
t := time.Now()
log.Println(t.Format("2006-01-02T15:04:05.999999999Z07:00"))
file, _ := os.Open("config.json")
decoder := json.NewDecoder(file)
err := decoder.Decode(&config)
if err != nil {
log.Println("error:", err)
}
log.Printf("%+v\n", config)
startGoRoutines()
startWebServer()
}
/* ---------------------------------------------------------------------------------------------------------------- */
/* Web Server */
var templates = template.Must(template.ParseFiles("index.html"))
func startWebServer() {
http.HandleFunc("/stats", statsHandler)
http.HandleFunc("/stopserver", stopserverHandler)
log.Printf("About to listen on 10443. Go to https://127.0.0.1:10443/")
err := http.ListenAndServeTLS(":10443", "cert.pem", "key.pem", nil)
if err != nil {
log.Printf("Web Server cannot start: %v\n", err)
}
}
func stopserverHandler(w http.ResponseWriter, r *http.Request) {
log.Println("Quit signal received from web interface (/stopserver), exiting...")
io.WriteString(w, "Quit signal received from web interface (/stopserver), exiting...")
os.Exit(0)
}
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
err := templates.ExecuteTemplate(w, tmpl+".html", p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func statsHandler(w http.ResponseWriter, r *http.Request) {
p := new(Page)
p.Title = "Statistics"
p.ReceivedDefMsgs = strconv.FormatInt(pools_definition_messages_received_from_queue, 10)
p.ReceivedFactsMsgs = strconv.FormatInt(inventory_pools_facts_messages_received_from_queue, 10)
p.StoredDefMsgs = strconv.FormatInt(pools_definition_messages_stored_in_mongo, 10)
p.StoredFactsMsgs = strconv.FormatInt(inventory_pools_facts_messages_stored_in_mongo, 10)
renderTemplate(w, "index", p)
}
/* ---------------------------------------------------------------------------------------------------------------- */
/* Amazon SQS */
func getSQSMessage(svc *sqs.SQS, queueUrl string) []string {
var response []string
params := &sqs.ReceiveMessageInput{
QueueUrl: aws.String(queueUrl), // Required
MaxNumberOfMessages: aws.Int64(10),
}
resp, err := svc.ReceiveMessage(params)
if err != nil {
//log.Println(queueUrl)
//log.Println("-->> " + err.Error())
return response
}
if len(resp.Messages) > 0 {
for _, msg := range resp.Messages {
message := *msg.Body
delParams := &sqs.DeleteMessageInput{
QueueUrl: aws.String(queueUrl), // Required
ReceiptHandle: aws.String(*msg.ReceiptHandle), // Required
}
svc.DeleteMessage(delParams)
response = append(response, message)
}
return response
}
return response
}
/* ---------------------------------------------------------------------------------------------------------------- */
/* Amazon pool Def Processing */
func processInventoryPoolDefinition(c chan string) {
log.Println("processInventory")
inv_url := url + config.Amazon.InventoryPoolsQueueName
for {
messages := getSQSMessage(svc, inv_url)
if(len(messages) > 0) {
//log.Printf("%s\n", message)
for _,message := range messages {
pools_definition_messages_received_from_queue++
c <- message
}
}
}
}
/* ---------------------------------------------------------------------------------------------------------------- */
/* Mongo pool Def Processing */
var counter int
func ProcessPoolDefMsg(session *mgo.Session, poolDefStr string) {
var PoolMessage InventoryPoolsDefinitionMessage
json.Unmarshal([]byte(poolDefStr), &PoolMessage)
c := mongo_session.DB(config.Mongo.DB).C(config.Mongo.PoolsCollection)
upsertdata := bson.M{ "$set": PoolMessage.InventoryPools[0]}
_ , err2 := c.UpsertId( PoolMessage.InventoryPools[0].ID, upsertdata )
if err2 != nil {
log.Println("Error: ", err2)
}
pools_definition_messages_stored_in_mongo++
}
func updateMongoInventoryPoolDefinition(c chan string) {
log.Println("updateMongo")
var err error
mongo_session, err = mgo.Dial(config.Mongo.HostAndPort);
if err != nil {
log.Println("Error: ", err)
return
}
defer mongo_session.Close()
mongo_session.SetMode(mgo.Monotonic, true)
for {
message := <- c
ProcessPoolDefMsg(mongo_session, message)
counter++
if(counter % 1000 == 0) {
counter = 0;
t := time.Now()
log.Println(t.Format("2006-01-02T15:04:05.999999999Z07:00"))
}
}
}
/* ---------------------------------------------------------------------------------------------------------------- */
/* Amazon pool Facts Processing */
func processInventoryPoolFacts(c chan string) {
log.Println("processInventoryFacts")
facts_url := url + config.Amazon.InventoryFactsQueueName
for {
messages := getSQSMessage(svc, facts_url)
if(len(messages) > 0) {
for _,message := range messages {
inventory_pools_facts_messages_received_from_queue++
c <- message
}
}
}
}
func ProcessPoolfactsMsg(session *mgo.Session, poolDefStr string) {
var PoolFacts []InventoryPoolsFacts
json.Unmarshal([]byte(poolDefStr), &PoolFacts)
c := mongo_session.DB(config.Mongo.DB).C(config.Mongo.FactsCollection)
if len(PoolFacts) > 0 {
for i:=0;i<len(PoolFacts);i++ {
for j:=0;j<len(PoolFacts[i].Skus);j++ {
p := InventoryPoolFact{ DocType: PoolFacts[i].DocType, Brand: PoolFacts[i].Brand,
Pool : PoolFacts[i].Pool, ProductID: PoolFacts[i].ProductID, SkuID: PoolFacts[i].Skus[j].SkuID,
SiteID: PoolFacts[i].Skus[j].SiteID, StockLevel: PoolFacts[i].Skus[j].StockLevel,
StoreStockLevel: PoolFacts[i].Skus[j].StoreStockLevel, Availability: PoolFacts[i].Skus[j].Availability,
BackOrderLevel: PoolFacts[i].Skus[j].BackOrderLevel, Backorderable: PoolFacts[i].Skus[j].Backorderable,
ShipmentDate: PoolFacts[i].Skus[j].ShipmentDate }
upsertdata := bson.M{ "$set": p}
_ , err2 := c.UpsertId( p.Brand+p.Pool+p.ProductID+p.SkuID, upsertdata )
if err2 != nil {
log.Println("Error: ", err2)
}
}
}
inventory_pools_facts_messages_stored_in_mongo++
}
}
/* ---------------------------------------------------------------------------------------------------------------- */
/* Mongo pool Facts Processing */
func updateMongoInventoryPoolFacts(c chan string) {
var err error
mongo_session, err = mgo.Dial(config.Mongo.HostAndPort);
if err != nil {
log.Println("Error: ", err)
return
}
defer mongo_session.Close()
mongo_session.SetMode(mgo.Monotonic, true)
for {
message := <- c
ProcessPoolfactsMsg(mongo_session, message)
counter++
if(counter % 1000 == 0) {
counter = 0;
t := time.Now()
log.Println(t.Format("2006-01-02T15:04:05.999999999Z07:00"))
}
}
}