Skip to content

Commit

Permalink
Fixed sorting based on active quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
achannarasappa committed Dec 28, 2020
1 parent 27f33b3 commit fdf32f7
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 50 deletions.
59 changes: 54 additions & 5 deletions internal/quote/http-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import (
"github.com/go-resty/resty/v2"
)

type Quote struct {
Currency string `json:"currency"`
type ResponseQuote struct {
ShortName string `json:"shortName"`
Symbol string `json:"symbol"`
MarketState string `json:"marketState"`
Expand All @@ -20,19 +19,69 @@ type Quote struct {
PostMarketPrice float64 `json:"postMarketPrice"`
}

type Quote struct {
ResponseQuote
Price float64
Change float64
ChangePercent float64
IsActive bool
}

type Response struct {
QuoteResponse struct {
Quotes []Quote `json:"result"`
Error interface{} `json:"error"`
Quotes []ResponseQuote `json:"result"`
Error interface{} `json:"error"`
} `json:"quoteResponse"`
}

func transformResponseQuote(responseQuote ResponseQuote) Quote {

if responseQuote.MarketState == "REGULAR" {
return Quote{
ResponseQuote: responseQuote,
Price: responseQuote.RegularMarketPrice,
Change: responseQuote.RegularMarketChange,
ChangePercent: responseQuote.RegularMarketChangePercent,
IsActive: true,
}
}

if responseQuote.MarketState == "POST" {
return Quote{
ResponseQuote: responseQuote,
Price: responseQuote.PostMarketPrice,
Change: responseQuote.PostMarketChange,
ChangePercent: responseQuote.PostMarketChangePercent,
IsActive: true,
}
}

return Quote{
ResponseQuote: responseQuote,
Price: responseQuote.RegularMarketPrice,
Change: 0.0,
ChangePercent: 0.0,
IsActive: false,
}

}

func transformResponseQuotes(responseQuotes []ResponseQuote) []Quote {

quotes := make([]Quote, 0)
for _, responseQuote := range responseQuotes {
quotes = append(quotes, transformResponseQuote(responseQuote))
}
return quotes

}

func GetQuotes(symbols []string) []Quote {
symbolsString := strings.Join(symbols, ",")
url := fmt.Sprintf("https://query1.finance.yahoo.com/v7/finance/quote?lang=en-US&region=US&corsDomain=finance.yahoo.com&symbols=%s", symbolsString)
response, _ := resty.New().R().
SetResult(&Response{}).
Get(url)

return (response.Result().(*Response)).QuoteResponse.Quotes
return transformResponseQuotes((response.Result().(*Response)).QuoteResponse.Quotes)
}
61 changes: 24 additions & 37 deletions internal/ui/component/watchlist/watchlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/lucasb-eyer/go-colorful"
"github.com/muesli/reflow/ansi"
. "github.com/novalagung/gubrak"
)

var (
Expand Down Expand Up @@ -41,63 +42,30 @@ func (m Model) View() string {
}

func watchlist(q []quote.Quote, elementWidth int) string {
quotes := sortQuotes(q)
quoteSummaries := ""
for _, quote := range q {
for _, quote := range quotes {
quoteSummaries = quoteSummaries + "\n" + quoteSummary(quote, elementWidth)
}
return quoteSummaries
}

func quoteSummary(q quote.Quote, elementWidth int) string {

p := getPrice(q)

firstLine := lineWithGap(
styleNeutralBold(q.Symbol),
styleNeutral(convertFloatToString(p.Price)),
styleNeutral(convertFloatToString(q.Price)),
elementWidth,
)
secondLine := lineWithGap(
styleNeutralFaded(q.ShortName),
priceText(p.Change, p.ChangePercent),
priceText(q.Change, q.ChangePercent),
elementWidth,
)

return fmt.Sprintf("%s\n%s", firstLine, secondLine)
}

type quoteMeta struct {
Price float64
Change float64
ChangePercent float64
}

func getPrice(q quote.Quote) quoteMeta {

if q.MarketState == "REGULAR" {
return quoteMeta{
Price: q.RegularMarketPrice,
Change: q.RegularMarketChange,
ChangePercent: q.RegularMarketChangePercent,
}
}

if q.MarketState == "POST" {
return quoteMeta{
Price: q.PostMarketPrice,
Change: q.PostMarketChange,
ChangePercent: q.PostMarketChangePercent,
}
}

return quoteMeta{
Price: q.RegularMarketPrice,
Change: 0.0,
ChangePercent: 0.0,
}

}

func priceText(change float64, changePercent float64) string {
if change == 0.0 {
return styleNeutralFaded(" " + convertFloatToString(change) + " (" + convertFloatToString(changePercent) + "%)")
Expand Down Expand Up @@ -144,3 +112,22 @@ func getNormalizedPercentWithMax(percent float64, maxPercent float64) float64 {
return math.Abs(percent / maxPercent)

}

// Sort by change percent and keep all inactive quotes at the end
func sortQuotes(q []quote.Quote) []quote.Quote {
if len(q) <= 0 {
return q
}

activeQuotes, inactiveQuotes, _ := Partition(q, func(v quote.Quote) bool {
return v.IsActive
})

sortedActiveQuotes, _ := SortBy(activeQuotes, func(v quote.Quote) float64 {
return v.ChangePercent
})

concatQuotes, _ := Concat(sortedActiveQuotes, inactiveQuotes)

return (concatQuotes).([]quote.Quote)
}
10 changes: 2 additions & 8 deletions internal/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ui

import (
"fmt"
"sort"
"ticker-tape/internal/quote"
"ticker-tape/internal/ui/component/watchlist"
"time"
Expand Down Expand Up @@ -34,7 +33,7 @@ type Model struct {
func (m Model) updateQuotes() tea.Cmd {
return tea.Tick(time.Second*time.Duration(m.requestInterval), func(t time.Time) tea.Msg {
return QuoteMsg{
quotes: sortQuotes(m.requestQuotes(m.symbols)),
quotes: m.requestQuotes(m.symbols),
}
})
}
Expand All @@ -52,7 +51,7 @@ func (m Model) Init() tea.Cmd {
m.watchlist = watchlist.NewModel()
return func() tea.Msg {
return QuoteMsg{
quotes: sortQuotes(m.requestQuotes(m.symbols)),
quotes: m.requestQuotes(m.symbols),
}
}
}
Expand Down Expand Up @@ -115,8 +114,3 @@ func (m Model) View() string {
func footer() string {
return footerHighlightStyle(" 🚀 ticker-tape ") + helpStyle(" q: exit")
}

func sortQuotes(q []quote.Quote) []quote.Quote {
sort.Slice(q, func(i, j int) bool { return q[i].RegularMarketChangePercent < q[j].RegularMarketChangePercent })
return q
}

0 comments on commit fdf32f7

Please sign in to comment.