Skip to content

Commit

Permalink
refactor: removed third-party util library
Browse files Browse the repository at this point in the history
  • Loading branch information
achannarasappa committed May 31, 2021
1 parent 58c35d5 commit e18cf5a
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 95 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/muesli/reflow v0.2.1-0.20201126184510-3bcb929042f2
github.com/muesli/termenv v0.7.4
github.com/novalagung/gubrak/v2 v2.0.1
github.com/nxadm/tail v1.4.6 // indirect
github.com/onsi/ginkgo v1.15.0
github.com/onsi/gomega v1.10.5
Expand Down
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/DefinitelyMod/gocsv v0.0.0-20181205141819-acfa5f112b45 h1:+OD9vawobD89HK04zwMokunBCSEeAb08VWAHPUMg+UE=
github.com/DefinitelyMod/gocsv v0.0.0-20181205141819-acfa5f112b45/go.mod h1:+nlrAh0au59iC1KN5RA1h1NdiOQYlNOBrbtE1Plqht4=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8=
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo=
github.com/achannarasappa/term-grid v0.2.3 h1:acYVsmM33MT7xy5lvIUX3PyNAnALulRRG5JPQrtZ3Qs=
github.com/achannarasappa/term-grid v0.2.3/go.mod h1:uXEY1bCtUSooU1owBx76qEGO2m07r2VAJr2LbxAvNZo=
github.com/achannarasappa/term-grid v0.2.4 h1:4shrL7Gm/Jb5eRfB1dROEw9lVf6T/z4MUrUZ03G6v34=
github.com/achannarasappa/term-grid v0.2.4/go.mod h1:uXEY1bCtUSooU1owBx76qEGO2m07r2VAJr2LbxAvNZo=
github.com/adrg/xdg v0.3.0 h1:BO+k4wFj0IoTolBF1Apn8oZrX3LQrEbBA8+/9vyW9J4=
Expand Down Expand Up @@ -183,8 +179,6 @@ github.com/muesli/termenv v0.7.2/go.mod h1:ct2L5N2lmix82RaY3bMWwVu/jUFc9Ule0KGDC
github.com/muesli/termenv v0.7.4 h1:/pBqvU5CpkY53tU0vVn+xgs2ZTX63aH5nY+SSps5Xa8=
github.com/muesli/termenv v0.7.4/go.mod h1:pZ7qY9l3F7e5xsAOS0zCew2tME+p7bWeBkotCEcIIcc=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/novalagung/gubrak/v2 v2.0.1 h1:OmabeSZsnf8Yd4FSZfm3F6VG2XoEsQBpktwtkjiCLdg=
github.com/novalagung/gubrak/v2 v2.0.1/go.mod h1:zilliNLzP2RSdZ67Sz7NdqI4bg3j5zUTFR34tuXopA0=
github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.6 h1:11TGpSHY7Esh/i/qnq02Jo5oVrI1Gue8Slbq0ujPZFQ=
Expand Down
12 changes: 6 additions & 6 deletions internal/currency/currency.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

c "github.com/achannarasappa/ticker/internal/common"
"github.com/go-resty/resty/v2"
. "github.com/novalagung/gubrak/v2"
)

type ResponseQuote struct {
Expand Down Expand Up @@ -40,13 +39,14 @@ func transformResponseCurrency(responseQuote ResponseQuote) c.CurrencyRate {

func transformResponseCurrencies(responseQuotes []ResponseQuote) c.CurrencyRates {

currencyRates := From(responseQuotes).Reduce(func(acc c.CurrencyRates, responseQuote ResponseQuote) c.CurrencyRates {
currencyRates := c.CurrencyRates{}

for _, responseQuote := range responseQuotes {
currencyRate := transformResponseCurrency(responseQuote)
acc[currencyRate.FromCurrency] = currencyRate
return acc
}, c.CurrencyRates{}).Result()
currencyRates[currencyRate.FromCurrency] = currencyRate
}

return (currencyRates).(c.CurrencyRates)
return currencyRates

}

Expand Down
39 changes: 19 additions & 20 deletions internal/position/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
c "github.com/achannarasappa/ticker/internal/common"
"github.com/achannarasappa/ticker/internal/currency"
. "github.com/achannarasappa/ticker/internal/quote"

"github.com/novalagung/gubrak/v2"
)

type Position struct {
Expand Down Expand Up @@ -55,32 +53,33 @@ func GetLots(lots []c.Lot) map[string]AggregatedLot {
return map[string]AggregatedLot{}
}

aggregatedLots := gubrak.
From(lots).
Reduce(func(acc map[string]AggregatedLot, lot c.Lot, i int) map[string]AggregatedLot {

aggregatedLot, ok := acc[lot.Symbol]
if !ok {
acc[lot.Symbol] = AggregatedLot{
Symbol: lot.Symbol,
Cost: lot.UnitCost * lot.Quantity,
Quantity: lot.Quantity,
OrderIndex: i,
}
return acc
aggregatedLots := map[string]AggregatedLot{}

for i, lot := range lots {

aggregatedLot, ok := aggregatedLots[lot.Symbol]

if !ok {

aggregatedLots[lot.Symbol] = AggregatedLot{
Symbol: lot.Symbol,
Cost: lot.UnitCost * lot.Quantity,
Quantity: lot.Quantity,
OrderIndex: i,
}

} else {

aggregatedLot.Quantity = aggregatedLot.Quantity + lot.Quantity
aggregatedLot.Cost = aggregatedLot.Cost + (lot.Quantity * lot.UnitCost)

acc[lot.Symbol] = aggregatedLot
aggregatedLots[lot.Symbol] = aggregatedLot

return acc
}

}, make(map[string]AggregatedLot)).
Result()
}

return (aggregatedLots).(map[string]AggregatedLot)
return aggregatedLots
}

func GetSymbols(config c.Config, aggregatedLots map[string]AggregatedLot) []string {
Expand Down
142 changes: 80 additions & 62 deletions internal/sorter/sorter.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package sorter

import (
"sort"

. "github.com/achannarasappa/ticker/internal/position"
. "github.com/achannarasappa/ticker/internal/quote"

"github.com/novalagung/gubrak/v2"
)

type Sorter func(quotes []Quote, positions map[string]Position) []Quote
Expand All @@ -18,99 +18,117 @@ func NewSorter(sort string) Sorter {
}

var sortDict = map[string]Sorter{
"alpha": sortByTicker,
"alpha": sortByAlpha,
"value": sortByValue,
"user": sortByUser,
}

func sortByUser(quotes []Quote, positions map[string]Position) []Quote {
func sortByUser(q []Quote, positions map[string]Position) []Quote {

quoteCount := len(quotes)
quoteCount := len(q)

if quoteCount <= 0 {
return quotes
return q
}

result := gubrak.
From(quotes).
OrderBy(func(v Quote) int {
if p, ok := positions[v.Symbol]; ok {
return p.AggregatedLot.OrderIndex
}
return quoteCount
}).
Result()

return (result).([]Quote)
quotes := make([]Quote, quoteCount)
copy(quotes, q)

sort.SliceStable(quotes, func(i, j int) bool {

prevIndex := quoteCount
nextIndex := quoteCount

if position, ok := positions[quotes[i].Symbol]; ok {
prevIndex = position.AggregatedLot.OrderIndex
}

if position, ok := positions[quotes[j].Symbol]; ok {
nextIndex = position.AggregatedLot.OrderIndex
}

return nextIndex > prevIndex
})

return quotes

}

func sortByTicker(quotes []Quote, positions map[string]Position) []Quote {
if len(quotes) <= 0 {
return quotes
func sortByAlpha(q []Quote, positions map[string]Position) []Quote {

quoteCount := len(q)

if quoteCount <= 0 {
return q
}

result := gubrak.
From(quotes).
OrderBy(func(v Quote) string {
return v.Symbol
}).
Result()
quotes := make([]Quote, quoteCount)
copy(quotes, q)

sort.SliceStable(quotes, func(i, j int) bool {
return quotes[j].Symbol > quotes[i].Symbol
})

return (result).([]Quote)
return quotes
}

func sortByValue(quotes []Quote, positions map[string]Position) []Quote {
if len(quotes) <= 0 {
return quotes
func sortByValue(q []Quote, positions map[string]Position) []Quote {

quoteCount := len(q)

if quoteCount <= 0 {
return q
}

quotes := make([]Quote, quoteCount)
copy(quotes, q)

activeQuotes, inactiveQuotes := splitActiveQuotes(quotes)

cActiveQuotes := gubrak.From(activeQuotes)
cInactiveQuotes := gubrak.From(inactiveQuotes)
sort.SliceStable(inactiveQuotes, func(i, j int) bool {
return positions[inactiveQuotes[j].Symbol].Value < positions[inactiveQuotes[i].Symbol].Value
})

positionsSorter := func(v Quote) float64 {
return positions[v.Symbol].Value
}
sort.SliceStable(activeQuotes, func(i, j int) bool {
return positions[activeQuotes[j].Symbol].Value < positions[activeQuotes[i].Symbol].Value
})

cActiveQuotes.OrderBy(positionsSorter, false)
cInactiveQuotes.OrderBy(positionsSorter, false)
return append(activeQuotes, inactiveQuotes...)
}

result := cActiveQuotes.
Concat(cInactiveQuotes.Result()).
Result()
func sortByChange(q []Quote, positions map[string]Position) []Quote {

return (result).([]Quote)
}
quoteCount := len(q)

func sortByChange(quotes []Quote, positions map[string]Position) []Quote {
if len(quotes) <= 0 {
return quotes
if quoteCount <= 0 {
return q
}

activeQuotes, inactiveQuotes := splitActiveQuotes(quotes)
quotes := make([]Quote, quoteCount)
copy(quotes, q)

cActiveQuotes := gubrak.
From(activeQuotes)
activeQuotes, inactiveQuotes := splitActiveQuotes(quotes)

cActiveQuotes.OrderBy(func(v Quote) float64 {
return v.ChangePercent
}, false)
sort.SliceStable(activeQuotes, func(i, j int) bool {
return activeQuotes[j].ChangePercent < activeQuotes[i].ChangePercent
})

result := cActiveQuotes.
Concat(inactiveQuotes).
Result()
return append(activeQuotes, inactiveQuotes...)

return (result).([]Quote)
}

func splitActiveQuotes(quotes []Quote) (interface{}, interface{}) {
activeQuotes, inactiveQuotes, _ := gubrak.
From(quotes).
Partition(func(v Quote) bool {
return v.IsActive
}).
ResultAndError()
func splitActiveQuotes(quotes []Quote) ([]Quote, []Quote) {

activeQuotes := make([]Quote, 0)
inactiveQuotes := make([]Quote, 0)

for _, quote := range quotes {
if quote.IsActive {
activeQuotes = append(activeQuotes, quote)
} else {
inactiveQuotes = append(inactiveQuotes, quote)
}
}

return activeQuotes, inactiveQuotes
}
35 changes: 35 additions & 0 deletions internal/sorter/sorter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ var _ = Describe("Sorter", func() {
IsActive: false,
IsRegularTradingSession: false,
}
rblxQuote := Quote{
ResponseQuote: ResponseQuote{
Symbol: "RBLX",
ShortName: "Roblox",
},
Price: 85.00,
Change: 10.00,
ChangePercent: 7.32,
IsActive: false,
IsRegularTradingSession: false,
}
quotes := []Quote{
bitcoinQuote,
twQuote,
Expand Down Expand Up @@ -114,11 +125,35 @@ var _ = Describe("Sorter", func() {
It("should sort position value, with inactive quotes last", func() {
sorter := NewSorter("value")

positions := map[string]Position{
"BTC-USD": {
Value: 50000.0,
},
"GOOG": {
Value: 2523.53,
},
"RBLX": {
Value: 900.00,
},
"MSFT": {
Value: 100.00,
},
}

quotes := []Quote{
bitcoinQuote,
twQuote,
googleQuote,
msftQuote,
rblxQuote,
}

sortedQuotes := sorter(quotes, positions)
expected := []Quote{
bitcoinQuote,
googleQuote,
twQuote,
rblxQuote,
msftQuote,
}

Expand Down

0 comments on commit e18cf5a

Please sign in to comment.