/
server.go
433 lines (347 loc) · 14.1 KB
/
server.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
package main
import (
"fmt"
"net/http"
"github.com/gorilla/rpc"
"io/ioutil"
"github.com/gorilla/rpc/json"
"io"
"strings"
"os"
"strconv"
"errors"
)
type StockService struct {}
//Static part of Yahoo API for each customer
const (
QuotesUrl = "http://download.finance.yahoo.com/d/quotes.csv?s="
)
var TradeIdForCustomer int
var budget float64
var isNewPurchase bool
var objStock *StockParameters
var objAllStocks StockStructre
var listStocks []StockParameters
var objStockDistribution *StockDistributionParameters
var objAllStockDistribution StockDistribution
var listDistributedStocks []StockDistributionParameters
var listResponse []StockDistribution
var objAllResponse AllResponses
var stockResponse StockResponse
//Structure of Request containing input parameters from user
//Ex: Input : GOOG:50%,YHOO:10% and Budget : 500
type StockRequest struct {
StockSymbolAndPercentage string
Budget float64
}
type StockResponse struct {
TradeId int
Stocks string
UnvestedAmount float64
}
//Structure to store a stock status having symbol,
//percentage of budget and Real time stock amount from yahoo API
type StockParameters struct {
Symbol string
Percent float64
StockAmount float64
}
//Strcure containing array of above stock parameters
type StockStructre struct {
StockParams []StockParameters
}
//Structure used to save number and amount of stocks for each symbol
type StockDistributionParameters struct {
Symbol string
NumberOfStocksForSymbol int
AmountOfStockSymbol float64
}
//Structure containing TradeId, Unvested amount and stocks information
//for each customer per trade transaction
type StockDistribution struct {
TradeId int
UninvestedAmount float64
StockDistributionArray []StockDistributionParameters
}
//In memory structure used to save all stocks
type AllResponses struct {
CustomerResponses []StockDistribution
}
type RequestPortfolio struct {
TradeId int
}
type ResponsePortfolio struct {
CurrentStocks string
CurrentMarketValue float64
CurrentUnvestedAmount float64
}
func (t *StockService) ShowPortfolio(r *http.Request, args *RequestPortfolio, reply *ResponsePortfolio) error {
var objStockSymbols string
isNewPurchase = false
previousStockStatus, err := getStockDistributionForTradeId(args.TradeId)
checkError(err)
for _, i := range previousStockStatus.StockDistributionArray {
objStockSymbols = objStockSymbols + "," + i.Symbol
}
startsWith := strings.HasPrefix(objStockSymbols, ",")
if startsWith {
objStockSymbols = strings.TrimPrefix(objStockSymbols, ",")
}
//Pass the input values of stock symbols and
//get the real time stocks in csv
response, err := getCsv(objStockSymbols)
checkError(err)
//Read the contents of response
contents, err := ioutil.ReadAll(response)
response.Close()
checkError(err)
data := strings.Split(string(contents), "\n")
objStock = new(StockParameters)
listStocks = []StockParameters{}
objAllStocks = StockStructre{listStocks}
for i := 0; i < len(data); i++ {
if data[i] != "" {
symbolsAndPercentage := strings.Split(data[i], ",")
(*objStock).Symbol = symbolsAndPercentage[0][1:len(symbolsAndPercentage[0]) - 1]
(*objStock).Percent = 0.0
objAllStocks.AddItem(*objStock)
}
}
createStockStructure(string(contents))
currentStockStatus := getStockStatus()
objTest := getCurrentMarketStatus(previousStockStatus, currentStockStatus)
*reply = *objTest
return nil
}
func getCurrentMarketStatus(previousStockStatus *StockDistribution, currentStockStatus StockDistribution) *ResponsePortfolio {
var stockStatus string
var amountSign string
var amountDifference float64
var currentTotalAmount float64
objResponsePortfolio := new(ResponsePortfolio)
for i := 0; i < len(previousStockStatus.StockDistributionArray); i++ {
for j := 0; j < len(currentStockStatus.StockDistributionArray); j++ {
if previousStockStatus.StockDistributionArray[i].Symbol == currentStockStatus.StockDistributionArray[j].Symbol {
symbol := currentStockStatus.StockDistributionArray[j].Symbol
currentValue := currentStockStatus.StockDistributionArray[j].AmountOfStockSymbol
currentTotalAmount = currentTotalAmount + currentValue * float64(previousStockStatus.StockDistributionArray[i].NumberOfStocksForSymbol)
amountDifference := previousStockStatus.StockDistributionArray[i].AmountOfStockSymbol - currentStockStatus.StockDistributionArray[j].AmountOfStockSymbol
if amountDifference == 0.0 {
amountSign = ""
} else if amountDifference > 0.0 {
amountSign = "+"
} else {
amountSign = "-"
}
stockStatus = stockStatus + symbol + ":" + strconv.Itoa(previousStockStatus.StockDistributionArray[i].NumberOfStocksForSymbol) + ":" + amountSign + "$" + strconv.FormatFloat(currentValue, 'f', -1, 64) + ","
}
}
}
endsWith := strings.HasSuffix(stockStatus, ",")
if endsWith {
stockStatus = strings.TrimSuffix(stockStatus, ",")
}
(objResponsePortfolio).CurrentMarketValue = currentTotalAmount
(objResponsePortfolio).CurrentUnvestedAmount = previousStockStatus.UninvestedAmount + amountDifference
(objResponsePortfolio).CurrentStocks = stockStatus
return objResponsePortfolio
}
func getStockDistributionForTradeId(tradeId int) (*StockDistribution, error) {
for i := 0; i < len(objAllResponse.CustomerResponses); i++ {
if objAllResponse.CustomerResponses[i].TradeId == tradeId {
objStock := &objAllResponse.CustomerResponses[i]
return objStock, nil
}
}
return nil, errors.New("Stock Information is not found!")
}
func (t *StockService) PurchaseStocks(r *http.Request, args *StockRequest, reply *StockResponse) error {
budget = args.Budget
isNewPurchase = true
var stocks string
//Get the stock symbols from input in comma seperated form, Ex: GOOG,YHOO
listInputParams := getListOfInputParameters(args.StockSymbolAndPercentage)
if strings.EqualFold(listInputParams, "Invalid Input") {
err := errors.New("Invalid Distribution of Budget. Please enter valid percentage!")
checkError(err)
} else {
//Pass the input values of stock symbols and
//get the real time stocks in csv
response, err := getCsv(listInputParams)
checkError(err)
//Read the contents of response
contents, err := ioutil.ReadAll(response)
response.Close()
checkError(err)
createStockStructure(string(contents))
stockDistribution := getStockStatus()
for _, i := range stockDistribution.StockDistributionArray {
stocks = stocks + i.Symbol + ":" + strconv.Itoa(i.NumberOfStocksForSymbol) + ":$" + strconv.FormatFloat(i.AmountOfStockSymbol, 'f', -1, 64) + ","
}
stockResponse = StockResponse{stockDistribution.TradeId, stocks, stockDistribution.UninvestedAmount}
fmt.Println(stockResponse)
*reply = stockResponse
}
return nil
}
/*
Gets the list of symbols from user input in comma seperated string value
Ex: GOOG,YHOO
Also craetes a structure to save symbol and stock percantage value
Ex: Symbol : GOOG
Percent : 50
Returns comma seperated input symbols
*/
func getListOfInputParameters(stockSymbol string) string {
objStock = new(StockParameters)
listStocks = []StockParameters{}
objAllStocks = StockStructre{listStocks}
var inputSymbols string
var totalPercentage float64
data := strings.Split(stockSymbol, ",")
//Create a structure object (objStock) with Symbol and Percent
//Add each object to an array objAllStocks to contain symbol, percent for each customer
for i := 0; i < len(data); i++ {
symbolsAndPercentage := strings.Split(data[i], ":")
(*objStock).Symbol = symbolsAndPercentage[0]
endsWith := strings.HasSuffix(symbolsAndPercentage[1], "%")
if endsWith {
symbolsAndPercentage[1] = strings.TrimSuffix(symbolsAndPercentage[1], "%")
}
percent, err := strconv.ParseFloat(symbolsAndPercentage[1], 64)
checkError(err)
(*objStock).Percent = percent
objAllStocks.AddItem(*objStock)
}
//Find the distribution of percentage
for _, i := range objAllStocks.StockParams {
totalPercentage = totalPercentage + i.Percent
}
//Throw error if the total percentage distribution > 100
if(totalPercentage > 100.0) {
err := errors.New("Invalid Distribution of Budget. Please enter valid percentage!")
checkError(err)
} else {
for _, i := range objAllStocks.StockParams {
inputSymbols = inputSymbols + "," + i.Symbol
}
startsWith := strings.HasPrefix(inputSymbols, ",")
if startsWith {
inputSymbols = strings.TrimPrefix(inputSymbols, ",")
}
}
return inputSymbols
}
/*Takes comma seperated string of input symbols and get the csv from yahoo API
Returns response body from Yahoo API containing symbol name and real time value
*/
func getCsv(inputSymbols string) (io.ReadCloser, error) {
url := QuotesUrl + inputSymbols + "&f=sa"
fmt.Println("csv: firing HTTP GET at ", url)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
return resp.Body, nil
}
/*
Takes symbol and real time value of stock for each symbol seperated by newline
and update stock structure to contain real time stock amount along with symbol and percentage
*/
func createStockStructure(contents string) {
listOfSymbolsAndStockValue := strings.Split(contents, "\n")
for i := 0; i < len(listOfSymbolsAndStockValue); i++ {
if listOfSymbolsAndStockValue[i] != "" {
symbolAndStockAmount := strings.Split(listOfSymbolsAndStockValue[i], ",")
symbol := symbolAndStockAmount[0][1:len(symbolAndStockAmount[0]) - 1]
for j := 0; j < len(objAllStocks.StockParams); j++ {
if strings.EqualFold(objAllStocks.StockParams[j].Symbol, symbol) {
amount, err := strconv.ParseFloat(symbolAndStockAmount[1], 64)
checkError(err)
objAllStocks.StockParams[j].StockAmount = amount
}
}
}
}
}
/*
Calculates budgetwise number of stocks, unvested amount and saves in array
Also creates in-memory structure to save information of all customers
*/
func getStockStatus() StockDistribution {
validTrade := false
unvestedAmout := budget
objStockDistribution = new(StockDistributionParameters)
listDistributedStocks = []StockDistributionParameters{}
objAllStockDistribution = StockDistribution{TradeIdForCustomer, unvestedAmout, listDistributedStocks}
//Calculate amount and number of stocks foe each symbol based on percentage and total budget
for i := 0; i < len(objAllStocks.StockParams); i++ {
(*objStockDistribution).Symbol = objAllStocks.StockParams[i].Symbol
totalBudgetForSymbol := (objAllStocks.StockParams[i].Percent * budget)/100
(*objStockDistribution).NumberOfStocksForSymbol = int(totalBudgetForSymbol/objAllStocks.StockParams[i].StockAmount)
(*objStockDistribution).AmountOfStockSymbol = objAllStocks.StockParams[i].StockAmount
unvestedAmout = unvestedAmout - float64((*objStockDistribution).NumberOfStocksForSymbol) * (*objStockDistribution).AmountOfStockSymbol
objAllStockDistribution.AddStockDistributionItem(*objStockDistribution)
}
//Save total unvested amount after purchasing all stocks
objAllStockDistribution.UninvestedAmount = unvestedAmout
if isNewPurchase {
if unvestedAmout >= 0.0 {
TradeIdForCustomer = TradeIdForCustomer + 1
validTrade = true
} else {
err := errors.New("Please invest more budget to purchase the stocks!")
checkError(err)
}
objAllStockDistribution.TradeId = TradeIdForCustomer
if validTrade {
objAllResponse.AddResponse(objAllStockDistribution)
}
} else {
objAllStockDistribution.TradeId = 0
}
return objAllStockDistribution
}
/*Store trade information (Trade Id, all stocks, Unvested amount) for each customer for in-memory cache
Returns array of above information for all customers
*/
func (stockResponse *AllResponses) AddResponse(customerTradeResponse StockDistribution) []StockDistribution {
stockResponse.CustomerResponses = append(stockResponse.CustomerResponses, customerTradeResponse)
return stockResponse.CustomerResponses
}
/*
Store Tradewise information (symbols, amount, number of stocks purchased) for each customer per transaction
Returns array of all stock information
*/
func (stockDistribution *StockDistribution) AddStockDistributionItem(indivisualStock StockDistributionParameters) []StockDistributionParameters {
stockDistribution.StockDistributionArray = append(stockDistribution.StockDistributionArray, indivisualStock)
return stockDistribution.StockDistributionArray
}
/*
Takes object of StockParameter structure and add in array to list all symbol information for each customer
Returns array of stock information
*/
func (stock *StockStructre) AddItem(item StockParameters) []StockParameters {
stock.StockParams = append(stock.StockParams, item)
return stock.StockParams
}
/*
Checks for the error and exit if any
*/
func checkError(err error) {
if err != nil {
fmt.Println("Fatal error ", err.Error())
os.Exit(1)
}
}
func main() {
//Initialize in-memory array of structure
listResponse = []StockDistribution{}
objAllResponse = AllResponses{listResponse}
fmt.Println("Starting Server")
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(StockService), "")
http.Handle("/stocks", s)
http.ListenAndServe(":8080", nil)
}