Skip to content

Commit 5232e76

Browse files
feat: added sync call for getting quotes from all monitors
1 parent 59fcd18 commit 5232e76

File tree

5 files changed

+30
-34
lines changed

5 files changed

+30
-34
lines changed

internal/monitor/monitor.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
c "github.com/achannarasappa/ticker/v4/internal/common"
88
monitorCoinbase "github.com/achannarasappa/ticker/v4/internal/monitor/coinbase"
99
monitorYahoo "github.com/achannarasappa/ticker/v4/internal/monitor/yahoo"
10-
"github.com/go-resty/resty/v2"
11-
"github.com/gorilla/websocket"
1210
)
1311

1412
// Monitor represents an overall monitor which manages API specific monitors
@@ -19,10 +17,8 @@ type Monitor struct {
1917

2018
// ConfigMonitor represents the configuration for the main monitor
2119
type ConfigMonitor struct {
22-
ClientHttp *resty.Client
23-
ClientWebsocket *websocket.Conn
24-
Reference c.Reference
25-
Config c.Config
20+
Reference c.Reference
21+
Config c.Config
2622
}
2723

2824
// ConfigUpdateFns represents the callback functions for when asset quotes are updated
@@ -93,14 +89,27 @@ func (m *Monitor) SetOnUpdate(config ConfigUpdateFns) error {
9389
return nil
9490
}
9591

96-
// GetMonitor returns the monitor for a given source
97-
func (m *Monitor) GetMonitor(source c.QuoteSource) c.Monitor {
98-
return m.monitors[source]
99-
}
100-
10192
// Start starts all monitors
10293
func (m *Monitor) Start() {
10394
for _, monitor := range m.monitors {
10495
monitor.Start()
10596
}
10697
}
98+
99+
// GetAssetGroupQuote synchronously gets price quotes a group of assets across all sources
100+
func (m *Monitor) GetAssetGroupQuote(assetGroup c.AssetGroup) c.AssetGroupQuote {
101+
102+
assetQuotesFromAllSources := make([]c.AssetQuote, 0)
103+
104+
for _, symbolBySource := range assetGroup.SymbolsBySource {
105+
106+
assetQuotes, _ := m.monitors[symbolBySource.Source].GetAssetQuotes(true)
107+
assetQuotesFromAllSources = append(assetQuotesFromAllSources, assetQuotes...)
108+
109+
}
110+
111+
return c.AssetGroupQuote{
112+
AssetQuotes: assetQuotesFromAllSources,
113+
AssetGroup: assetGroup,
114+
}
115+
}

internal/print/print.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/achannarasappa/ticker/v4/internal/asset"
1010
c "github.com/achannarasappa/ticker/v4/internal/common"
1111
mon "github.com/achannarasappa/ticker/v4/internal/monitor"
12-
quote "github.com/achannarasappa/ticker/v4/internal/quote"
1312
"github.com/achannarasappa/ticker/v4/internal/ui/util"
1413

1514
"github.com/spf13/cobra"
@@ -143,11 +142,10 @@ func Run(dep *c.Dependencies, ctx *c.Context, options *Options) func(*cobra.Comm
143142
return func(_ *cobra.Command, _ []string) {
144143

145144
monitors, _ := mon.NewMonitor(mon.ConfigMonitor{
146-
ClientHttp: dep.HttpClients.Default,
147-
Config: ctx.Config,
148-
Reference: ctx.Reference,
145+
Config: ctx.Config,
146+
Reference: ctx.Reference,
149147
})
150-
assetGroupQuote := quote.GetAssetGroupQuote(monitors, dep)(ctx.Groups[0])
148+
assetGroupQuote := monitors.GetAssetGroupQuote(ctx.Groups[0])
151149
assets, _ := asset.GetAssets(*ctx, assetGroupQuote)
152150

153151
if options.Format == "csv" {
@@ -165,11 +163,10 @@ func RunSummary(dep *c.Dependencies, ctx *c.Context, options *Options) func(cmd
165163
return func(_ *cobra.Command, _ []string) {
166164

167165
monitors, _ := mon.NewMonitor(mon.ConfigMonitor{
168-
ClientHttp: dep.HttpClients.Default,
169-
Config: ctx.Config,
170-
Reference: ctx.Reference,
166+
Config: ctx.Config,
167+
Reference: ctx.Reference,
171168
})
172-
assetGroupQuote := quote.GetAssetGroupQuote(monitors, dep)(ctx.Groups[0])
169+
assetGroupQuote := monitors.GetAssetGroupQuote(ctx.Groups[0])
173170
_, holdingSummary := asset.GetAssets(*ctx, assetGroupQuote)
174171

175172
if options.Format == "csv" {

internal/quote/quote.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ func getQuoteBySource(monitors *mon.Monitor, dep *c.Dependencies, symbolBySource
2424
return quoteCoincap.GetAssetQuotes(*dep.HttpClients.Default, symbolBySource.Symbols)
2525
}
2626

27-
if symbolBySource.Source == c.QuoteSourceCoinbase {
28-
// (*monitors.Coinbase).SetSymbols(symbolBySource.Symbols) this will force a refresh
29-
x, _ := monitors.GetMonitor(c.QuoteSourceCoinbase).GetAssetQuotes()
30-
return x
31-
}
32-
3327
return []c.AssetQuote{}
3428
}
3529

internal/ui/start.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ func Start(dep *c.Dependencies, ctx *c.Context) func() error {
1111
return func() error {
1212

1313
monitors, _ := mon.NewMonitor(mon.ConfigMonitor{
14-
ClientHttp: dep.HttpClients.Default,
15-
Reference: ctx.Reference,
16-
Config: ctx.Config,
14+
Reference: ctx.Reference,
15+
Config: ctx.Config,
1716
})
1817

1918
p := tea.NewProgram(

internal/ui/ui.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/achannarasappa/ticker/v4/internal/asset"
1010
c "github.com/achannarasappa/ticker/v4/internal/common"
1111
mon "github.com/achannarasappa/ticker/v4/internal/monitor"
12-
quote "github.com/achannarasappa/ticker/v4/internal/quote"
1312
"github.com/achannarasappa/ticker/v4/internal/ui/component/summary"
1413
"github.com/achannarasappa/ticker/v4/internal/ui/component/watchlist"
1514

@@ -35,7 +34,6 @@ type Model struct {
3534
ctx c.Context
3635
ready bool
3736
headerHeight int
38-
getQuotes func(c.AssetGroup) c.AssetGroupQuote
3937
nonce int
4038
requestInterval int
4139
assets []c.Asset
@@ -80,7 +78,6 @@ func NewModel(dep c.Dependencies, ctx c.Context, monitors *mon.Monitor) *Model {
8078
headerHeight: getVerticalMargin(ctx.Config),
8179
ready: false,
8280
requestInterval: ctx.Config.RefreshInterval,
83-
getQuotes: quote.GetAssetGroupQuote(monitors, &dep),
8481
nonce: 0,
8582
assets: make([]c.Asset, 0),
8683
assetQuotes: make([]c.AssetQuote, 0),
@@ -293,9 +290,9 @@ func getVerticalMargin(config c.Config) int {
293290
return 0
294291
}
295292

296-
// Send a new tick message with the nonce 100ms from now
293+
// Send a new tick message with the nonce 200ms from now
297294
func tick(nonce int) tea.Cmd {
298-
return tea.Tick(time.Second/10, func(time.Time) tea.Msg {
295+
return tea.Tick(time.Second/5, func(time.Time) tea.Msg {
299296
return tickMsg{
300297
nonce: nonce,
301298
}

0 commit comments

Comments
 (0)