-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
682 lines (535 loc) · 18.5 KB
/
app.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
package main
import (
"bufio"
"flag"
"fmt"
"os"
"strconv"
"strings"
"github.com/distributeddesigns/currency"
"github.com/op/go-logging"
"github.com/distributeddesigns/milestone1/accounts"
"github.com/distributeddesigns/milestone1/auditlogger"
"github.com/distributeddesigns/milestone1/autorequests"
"github.com/distributeddesigns/milestone1/commands"
"github.com/distributeddesigns/milestone1/quotecache"
)
// Globals
var (
consoleLog = logging.MustGetLogger("console")
logLevel = flag.String("loglevel", "WARNING", "CRITICAL, ERROR, WARNING, NOTICE, INFO, DEBUG")
accountStore = accounts.NewAccountStore()
autoBuyRequestStore = autorequests.NewAutoRequestStore()
autoSellRequestStore = autorequests.NewAutoRequestStore()
)
func main() {
flag.Parse()
consoleLoggingInit()
closeAuditLogger := auditlogger.Init()
defer closeAuditLogger()
// Find the workload file and open it
// - Read each line and:
// - parse the command
// - execute it
// - log it
// naïvely assume this is the workload file
infile := flag.Arg(0)
if _, err := os.Stat(infile); os.IsNotExist(err) {
// can't find input; bail!
consoleLog.Critical(err.Error())
os.Exit(1)
} else if err != nil {
consoleLog.Error(err.Error())
}
// open the file
file, err := os.Open(infile)
if err != nil {
consoleLog.Critical(err.Error())
os.Exit(1)
}
defer file.Close()
consoleLog.Debugf("Opened %s", file.Name())
// process all lines
scanner := bufio.NewScanner(file)
for scanner.Scan() {
cmd := parseCommand(scanner.Text())
if err := executeCommand(cmd); err != nil {
// if it fails, log and continue on
consoleLog.Errorf("Command execution error! cmd # %3d message: %s", cmd.ID, err.Error())
}
// Record command in the audit log
auditlogger.LogCommand(cmd)
}
// catch read errors
if err := scanner.Err(); err != nil {
consoleLog.Critical(err.Error())
os.Exit(1)
}
consoleLog.Debugf("Done!")
}
func consoleLoggingInit() {
// TODO: DONE 1. Make a logger that outputs to console
// TODO: DONE 2. Set variable output levels based on runtime flag
// TODO: 3. Log stuff into a file
// Create a default backend
consoleBackend := logging.NewLogBackend(os.Stdout, "", 0)
// Add output formatting
var consoleFormat = logging.MustStringFormatter(
`%{time:15:04:05.000} %{color}▶ %{level:8s}%{color:reset} %{id:03d} %{shortfile} %{message}`,
)
consoleBackendFormatted := logging.NewBackendFormatter(consoleBackend, consoleFormat)
// Add leveled logging
level, err := logging.LogLevel(*logLevel)
if err != nil {
fmt.Println("Bad log level. Using default.") // ERROR
}
consoleBackendFormattedAndLeveled := logging.AddModuleLevel(consoleBackendFormatted)
consoleBackendFormattedAndLeveled.SetLevel(level, "")
// Attach the backend
logging.SetBackend(consoleBackendFormattedAndLeveled)
}
func parseCommand(s string) commands.Command {
consoleLog.Debugf("Parsing: %s", s)
// Convert to a proper .csv, then parse
// change `[100] STUFF,...` -> `100,STUFF,...`
csv := strings.Replace(s, "[", "", 1)
csv = strings.Replace(csv, "] ", ",", 1)
// commands have mystery spaces?!
csv = strings.Replace(csv, " ", "", -1)
// Break it apart
parts := strings.Split(csv, ",")
// Almost all commands will follow this format
// TODO: Deal with the final "DUMPLOG,./testLOG"
ID, _ := strconv.Atoi(parts[0])
name, _ := commands.ToCommandType(parts[1])
parsed := commands.Command{
ID: ID,
Name: name,
UserID: parts[2],
Args: parts[3:],
}
consoleLog.Debugf("Parsed as: %+v", parsed)
return parsed
}
func executeCommand(cmd commands.Command) error {
// Each command will return true if everything is okay, false if error
var status bool
// Filter based on the "enum" of command names
switch cmd.Name {
case commands.Add:
status = executeAdd(cmd)
case commands.Quote:
status = executeQuote(cmd)
case commands.Buy:
status = executeBuy(cmd)
case commands.CommitBuy:
status = executeCommitBuy(cmd)
case commands.CancelBuy:
status = executeCancelBuy(cmd)
case commands.Sell:
status = executeSell(cmd)
case commands.CommitSell:
status = executeCommitSell(cmd)
case commands.CancelSell:
status = executeCancelSell(cmd)
case commands.SetBuyAmount:
status = executeSetBuyAmount(cmd)
case commands.SetSellAmount:
status = executeSetSellAmount(cmd)
case commands.CancelSetBuy:
status = executeCancelSetBuy(cmd)
case commands.CancelSetSell:
status = executeCancelSetSell(cmd)
case commands.SetBuyTrigger:
status = executeSetBuyTrigger(cmd)
case commands.SetSellTrigger:
status = executeSetSellTrigger(cmd)
default:
consoleLog.Warningf("Not implemented: %s", cmd.Name)
return nil
}
// report our status
if status {
consoleLog.Debugf("Finished command %d", cmd.ID)
} else {
consoleLog.Debugf("Finished command %d with errors", cmd.ID)
}
return nil
}
// Add funds to the user's account
func executeAdd(cmd commands.Command) bool {
// Finish parsing the rest of the command.
// ADD should have an amount passed
// Sanitize the command
if len(cmd.Args) != 1 {
// too many
consoleLog.Errorf("Wrong number of commands: `%s`", cmd.Args)
return false
} else if cmd.Args[0] == "" {
// missing
consoleLog.Error("No amount passed to ADD")
return false
}
// Convert to a centInt
amount, err := currency.NewFromString(cmd.Args[0])
if err != nil {
// Bail on parse failure
consoleLog.Error("Failed to parse currency")
return false
}
// Create an account if the user needs one
if !accountStore.HasAccount(cmd.UserID) {
consoleLog.Noticef("Creating account for %s", cmd.UserID)
if err := accountStore.CreateAccount(cmd.UserID); err != nil {
consoleLog.Error(err.Error())
return false
}
}
// Add the amount
consoleLog.Infof("Adding %s to %s", amount, cmd.UserID)
accountStore.Accounts[cmd.UserID].AddFunds(amount)
balance := accountStore.Accounts[cmd.UserID].Balance
consoleLog.Infof("New balance for %s is %s", cmd.UserID, balance)
return true
}
// Gets a quote from the quoteserver
func executeQuote(cmd commands.Command) bool {
// Get the stock from the command
stock := cmd.Args[0]
account := accountStore.GetAccount(cmd.UserID)
if stock == "" {
consoleLog.Error("No stock passed to QUOTE")
return false
}
// get a quote for the stock. (cache will determine if a fresh one is needed)
quote, err := quotecache.GetQuote(cmd.UserID, stock, cmd.ID)
if err != nil {
consoleLog.Error(err.Error())
return false
}
consoleLog.Noticef("Got quote: %+v", quote)
//Check auto buy sell
for _, v := range (*autoBuyRequestStore)[stock] {
//fmt.Printf("key[%s] value[%s]\n", k, v)
if v.Trigger.ToFloat() <= quote.Price.ToFloat() {
//Fulfil buy action
wholeShares, cashRemainder := quote.Price.FitsInto(v.Amount)
if wholeShares == 0 {
continue
}
account.AddStockToPortfolio(stock, wholeShares)
account.AddFunds(cashRemainder)
consoleLog.Infof("Buy trigger fired for stock %s at price %s for user %s", stock, quote.Price, cmd.UserID)
}
}
for _, v := range (*autoSellRequestStore)[stock] {
//fmt.Printf("key[%s] value[%s]\n", k, v)
if v.Trigger.ToFloat() >= quote.Price.ToFloat() {
//Fulfil buy action
wholeShares, cashRemainder := quote.Price.FitsInto(v.Amount)
if !account.RemoveStockFromPortfolio(stock, wholeShares) {
consoleLog.Infof("User does not have enough stock to sell")
continue
}
v.Amount.Sub(cashRemainder)
account.AddFunds(v.Amount)
consoleLog.Infof("Sell trigger fired for stock %s at price %s for user %s", stock, quote.Price, cmd.UserID)
}
}
consoleLog.Noticef("Got quote: %+v", quote)
// send the quote to the user
return true
}
func executeBuy(cmd commands.Command) bool {
//Gotta check users money and add a reserved portion
account := accountStore.GetAccount(cmd.UserID)
if account == nil {
consoleLog.Noticef("User %s does not have an account", account)
return false
}
stockSymbol := cmd.Args[0]
dollarAmount, err := currency.NewFromString(cmd.Args[1])
if err != nil {
consoleLog.Noticef("Dollar amount %s is invalid", cmd.Args[1])
return false
}
//User wants to buy y worth of x shares.
userQuote, err := quotecache.GetQuote(cmd.UserID, stockSymbol, cmd.ID)
if err != nil {
consoleLog.Noticef("Quote of stock %s for user %s is invalid", stockSymbol, cmd.UserID)
return false
}
wholeShares, cashRemainder := userQuote.Price.FitsInto(dollarAmount)
if wholeShares == 0 {
consoleLog.Notice("Amount specified to buy less than single stock unit")
return true
}
consoleLog.Infof("User %s set purchase order for %d shares of stock %s", cmd.UserID, wholeShares, stockSymbol)
// Remove the funds from user now to prevent double spending
dollarAmount.Sub(cashRemainder)
account.RemoveFunds(dollarAmount)
return account.AddToBuyQueue(stockSymbol, wholeShares, userQuote.Price)
}
func executeCommitBuy(cmd commands.Command) bool {
account := accountStore.GetAccount(cmd.UserID)
if account == nil {
consoleLog.Infof("User %s does not have an account", cmd.UserID)
return false
}
// CommitBuy has no additional args to parse! Everything is in cmd.
// Get the most recent Buy from the user and shrink the buy queue
newestBuy, found := account.BuyQueue.PopNewest()
// If there's no Buy or it's expired, don't change the user account
// and log the command failure.
if !found || newestBuy.IsExpired() {
consoleLog.Infof("No active buys to commit for %s", cmd.UserID)
return false
}
// If there is an active Buy give the user the stock quantity.
consoleLog.Infof("Committing buy for user %s for %d unit of %s", cmd.UserID, newestBuy.Units, newestBuy.Stock)
consoleLog.Debugf("Before, user has %d of %s", account.GetPortfolioStockUnits(newestBuy.Stock), newestBuy.Stock)
account.AddStockToPortfolio(newestBuy.Stock, newestBuy.Units)
consoleLog.Debugf("After, user has %d of %s", account.GetPortfolioStockUnits(newestBuy.Stock), newestBuy.Stock)
return true
}
func executeCancelBuy(cmd commands.Command) bool {
account := accountStore.GetAccount(cmd.UserID)
if account == nil {
consoleLog.Infof("User %s does not have an account", cmd.UserID)
return false
}
// CommitBuy has no additional args to parse! Everything is in cmd.
// Pop the latest Buy to get it out of the queue
newestBuy, found := account.BuyQueue.PopNewest()
if !found {
consoleLog.Infof("No active buys to cancel for %s", cmd.UserID)
return false
}
// Return the reserve amount to the user's balance
var reserve currency.Currency
reserve.Add(newestBuy.UnitPrice)
reserve.Mul(float64(newestBuy.Units))
consoleLog.Infof("Cancel buy for %s. Adding back %s", newestBuy.Stock, reserve)
consoleLog.Debugf("Before, user balance %s", account.Balance)
account.AddFunds(reserve)
consoleLog.Debugf("After, user balance %s", account.Balance)
return true
}
func executeSell(cmd commands.Command) bool {
account := accountStore.GetAccount(cmd.UserID)
if account == nil {
consoleLog.Noticef("User %s does not have an account", account)
return false
}
stockSymbol := cmd.Args[0]
dollarAmount, err := currency.NewFromString(cmd.Args[1])
if err != nil {
consoleLog.Noticef("Dollar amount %s is invalid", cmd.Args[1])
return false
}
userQuote, err := quotecache.GetQuote(cmd.UserID, stockSymbol, cmd.ID)
if err != nil {
consoleLog.Noticef("Quote of stock %s for user %s is invalid", stockSymbol, cmd.UserID)
return false
}
wholeShares, _ := userQuote.Price.FitsInto(dollarAmount)
if wholeShares == 0 {
consoleLog.Notice("Amount specified to sell less than single stock unit")
return true
}
consoleLog.Infof("User %s set sale order for %d shares of stock %s at %s", cmd.UserID, wholeShares, stockSymbol, userQuote.Price)
// Remove stock now to prevent double selling
if stockWasRemoved := account.RemoveStockFromPortfolio(stockSymbol, wholeShares); !stockWasRemoved {
return false
}
// Make the new sell order and report success
return account.AddToSellQueue(stockSymbol, wholeShares, userQuote.Price)
}
func executeCommitSell(cmd commands.Command) bool {
account := accountStore.GetAccount(cmd.UserID)
if account == nil {
consoleLog.Infof("User %s does not have an account", cmd.UserID)
return false
}
// CommitSell has no additional args to parse! Everything is in cmd.
// Pop the latest Sell to get it out of the queue
newestSell, found := account.SellQueue.PopNewest()
if !found {
consoleLog.Infof("No active sells to cancel for %s", cmd.UserID)
return false
}
// Add the profit of the sale to the user's account
var profit currency.Currency
profit.Add(newestSell.UnitPrice)
profit.Mul(float64(newestSell.Units))
consoleLog.Infof("Commit sell for %s of %d units of %s at %s. Adding %s",
cmd.UserID, newestSell.Units, newestSell.Stock, newestSell.UnitPrice, profit,
)
consoleLog.Debugf("Before, user balance %s", account.Balance)
account.AddFunds(profit)
consoleLog.Debugf("After, user balance %s", account.Balance)
return true
}
func executeCancelSell(cmd commands.Command) bool {
account := accountStore.GetAccount(cmd.UserID)
if account == nil {
consoleLog.Infof("User %s does not have an account", cmd.UserID)
return false
}
// CancelSell has no additional args to parse! Everything is in cmd.
newestSell, found := account.SellQueue.PopNewest()
if !found {
consoleLog.Infof("No active sells to cancel for %s", cmd.UserID)
return false
}
// Add the stock back to the user's portfolio
consoleLog.Infof("Cancel sell for %s. Adding back %d units", newestSell.Stock, newestSell.Units)
consoleLog.Debugf("Before, user portfolio: %d x %s", account.Portfolio[newestSell.Stock], newestSell.Stock)
account.AddStockToPortfolio(newestSell.Stock, newestSell.Units)
consoleLog.Debugf("After, user portfolio: %d x %s", account.Portfolio[newestSell.Stock], newestSell.Stock)
return true
}
func executeSetBuyAmount(cmd commands.Command) bool {
userID := cmd.UserID
strAmount := cmd.Args[1]
stock := cmd.Args[0]
account := accountStore.GetAccount(userID)
if account == nil {
consoleLog.Infof("User %s does not have an account", userID)
return false
}
amount, err := currency.NewFromString(strAmount)
if err != nil {
consoleLog.Error("Failed to parse currency")
return false
}
err = account.RemoveFunds(amount)
if err != nil {
consoleLog.Errorf("User had insufficient funds to set buy amount of %s", amount)
return false
}
autoBuyRequestStore.AddAutorequest(stock, cmd.UserID, amount)
consoleLog.Infof("User %s set automated buy amount for %s dollars of stock %s", userID, amount, stock)
return true
}
func executeSetSellAmount(cmd commands.Command) bool {
userID := cmd.UserID
strAmount := cmd.Args[1]
stock := cmd.Args[0]
account := accountStore.GetAccount(userID)
if account == nil {
consoleLog.Infof("User %s does not have an account", userID)
return false
}
amount, err := currency.NewFromString(strAmount)
if err != nil {
consoleLog.Error("Failed to parse currency")
return false
}
autoSellRequestStore.AddAutorequest(stock, userID, amount)
consoleLog.Infof("User %s set automated sell amount for %s dollars of stock %s", userID, amount, stock)
return true
}
func executeCancelSetBuy(cmd commands.Command) bool {
userID := cmd.UserID
stock := cmd.Args[0]
account := accountStore.GetAccount(userID)
if account == nil {
consoleLog.Infof("User %s does not have an account", userID)
return false
}
refundCurrency, err := autoBuyRequestStore.CancelAutorequest(stock, userID)
if err != nil {
consoleLog.Infof("Automated buy for stock %s was not found for user %s", stock, userID)
} else {
consoleLog.Infof("User %s cancelled automated buy for %s", userID, stock)
account.AddFunds(refundCurrency)
}
return true
}
func executeCancelSetSell(cmd commands.Command) bool {
userID := cmd.UserID
stock := cmd.Args[0]
account := accountStore.GetAccount(userID)
if account == nil {
consoleLog.Infof("User %s does not have an account", userID)
return false
}
_, err := autoSellRequestStore.CancelAutorequest(stock, userID)
if err != nil {
consoleLog.Infof("Automated sell for stock %s was not found for user %s", stock, userID)
} else {
//TODO, refund users stock. We have to wait on the triggers for this
consoleLog.Infof("User %s cancelled automated sell for %s", userID, stock)
}
return true
}
func executeSetBuyTrigger(cmd commands.Command) bool {
//Check that a sell amount exists in the store
userID := cmd.UserID
stock := cmd.Args[0]
strAmount := cmd.Args[1]
account := accountStore.GetAccount(userID)
if account == nil {
consoleLog.Infof("User %s does not have an account", userID)
return false
}
stockTriggerCost, err := currency.NewFromString(strAmount)
if err != nil {
consoleLog.Error("Failed to parse currency")
return false
}
userAutorequest, err := autoBuyRequestStore.GetAutorequest(stock, userID)
if err != nil {
consoleLog.Infof("User %s does not have an auto request pending", userID)
return false
}
stockTotalValue := userAutorequest.Amount
wholeShares, cashRemainder := stockTriggerCost.FitsInto(stockTotalValue)
if wholeShares == 0 {
consoleLog.Notice("Amount specified to buy less than single stock unit")
return true
}
consoleLog.Infof("User %s set purchase order for %d shares of stock %s", cmd.UserID, wholeShares, stock)
// Remove the funds from user now to prevent double spending
stockTotalValue.Sub(cashRemainder)
account.RemoveFunds(stockTotalValue)
userAutorequest.Trigger = stockTriggerCost
// Add stocks to user portfolio
return true
}
func executeSetSellTrigger(cmd commands.Command) bool {
//Check that a sell amount exists in the store
userID := cmd.UserID
stock := cmd.Args[0]
strAmount := cmd.Args[1]
account := accountStore.GetAccount(userID)
if account == nil {
consoleLog.Infof("User %s does not have an account", userID)
return false
}
stockTriggerCost, err := currency.NewFromString(strAmount)
if err != nil {
consoleLog.Error("Failed to parse currency")
return false
}
userAutorequest, err := autoSellRequestStore.GetAutorequest(stock, userID)
if err != nil {
consoleLog.Infof("User %s does not have an auto request pending", userID)
return false
}
stockTotalValue := userAutorequest.Amount
wholeShares, cashRemainder := stockTriggerCost.FitsInto(stockTotalValue)
if wholeShares == 0 {
consoleLog.Notice("Amount specified to buy less than single stock unit")
return true
}
consoleLog.Infof("User %s set purchase order for %d shares of stock %s", cmd.UserID, wholeShares, stock)
// Remove the funds from user now to prevent double spending
stockTotalValue.Sub(cashRemainder)
account.AddFunds(stockTotalValue)
userAutorequest.Trigger = stockTriggerCost
// Remove stocks from user portfolio
account.RemoveStockFromPortfolio(stock, wholeShares)
return true
}