|
| 1 | +package monitorYahoo |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "slices" |
| 7 | + "sync" |
| 8 | + "time" |
| 9 | + |
| 10 | + c "github.com/achannarasappa/ticker/v4/internal/common" |
| 11 | + poller "github.com/achannarasappa/ticker/v4/internal/monitor/yahoo/poller" |
| 12 | + unary "github.com/achannarasappa/ticker/v4/internal/monitor/yahoo/unary" |
| 13 | +) |
| 14 | + |
| 15 | +type MonitorYahoo struct { |
| 16 | + unaryAPI *unary.UnaryAPI |
| 17 | + poller *poller.Poller |
| 18 | + unary *unary.UnaryAPI |
| 19 | + input input |
| 20 | + productIds []string // Yahoo APIs refer to trading pairs as Product IDs which symbols ticker accepts with a -USD suffix |
| 21 | + productIdsPolling []string |
| 22 | + assetQuotesCache []c.AssetQuote // Asset quotes for all assets retrieved at start or on symbol change |
| 23 | + assetQuotesCacheLookup map[string]*c.AssetQuote // Asset quotes for all assets retrieved at least once (symbol change does not remove symbols) |
| 24 | + chanPollUpdateAssetQuote chan c.MessageUpdate[c.AssetQuote] |
| 25 | + chanError chan error |
| 26 | + mu sync.RWMutex |
| 27 | + ctx context.Context |
| 28 | + cancel context.CancelFunc |
| 29 | + isStarted bool |
| 30 | + onUpdateAssetQuote func(symbol string, assetQuote c.AssetQuote) |
| 31 | + onUpdateAssetQuotes func(assetQuotes []c.AssetQuote) |
| 32 | +} |
| 33 | + |
| 34 | +type input struct { |
| 35 | + productIds []string |
| 36 | + productIdsLookup map[string]bool |
| 37 | +} |
| 38 | + |
| 39 | +// Config contains the required configuration for the Yahoo monitor |
| 40 | +type Config struct { |
| 41 | + UnaryURL string |
| 42 | + ChanError chan error |
| 43 | +} |
| 44 | + |
| 45 | +// Option defines an option for configuring the monitor |
| 46 | +type Option func(*MonitorYahoo) |
| 47 | + |
| 48 | +func NewMonitorYahoo(config Config, opts ...Option) *MonitorYahoo { |
| 49 | + |
| 50 | + ctx, cancel := context.WithCancel(context.Background()) |
| 51 | + |
| 52 | + unaryAPI := unary.NewUnaryAPI(config.UnaryURL) |
| 53 | + |
| 54 | + monitor := &MonitorYahoo{ |
| 55 | + assetQuotesCacheLookup: make(map[string]*c.AssetQuote), |
| 56 | + assetQuotesCache: make([]c.AssetQuote, 0), |
| 57 | + chanPollUpdateAssetQuote: make(chan c.MessageUpdate[c.AssetQuote]), |
| 58 | + chanError: config.ChanError, |
| 59 | + unaryAPI: unaryAPI, |
| 60 | + ctx: ctx, |
| 61 | + cancel: cancel, |
| 62 | + onUpdateAssetQuote: func(symbol string, assetQuote c.AssetQuote) {}, |
| 63 | + onUpdateAssetQuotes: func(assetQuotes []c.AssetQuote) {}, |
| 64 | + } |
| 65 | + |
| 66 | + pollerConfig := poller.PollerConfig{ |
| 67 | + ChanUpdateAssetQuote: monitor.chanPollUpdateAssetQuote, |
| 68 | + ChanError: monitor.chanError, |
| 69 | + UnaryAPI: unaryAPI, |
| 70 | + } |
| 71 | + monitor.poller = poller.NewPoller(ctx, pollerConfig) |
| 72 | + |
| 73 | + for _, opt := range opts { |
| 74 | + opt(monitor) |
| 75 | + } |
| 76 | + |
| 77 | + return monitor |
| 78 | +} |
| 79 | + |
| 80 | +// WithRefreshInterval sets the refresh interval for the monitor |
| 81 | +func WithRefreshInterval(interval time.Duration) Option { |
| 82 | + return func(m *MonitorYahoo) { |
| 83 | + m.poller.SetRefreshInterval(interval) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// SetOnUpdate sets the onUpdate function for the monitor |
| 88 | +func (m *MonitorYahoo) SetOnUpdateAssetQuote(onUpdate func(symbol string, assetQuote c.AssetQuote)) { |
| 89 | + m.onUpdateAssetQuote = onUpdate |
| 90 | +} |
| 91 | + |
| 92 | +func (m *MonitorYahoo) SetOnUpdateAssetQuotes(onUpdate func(assetQuotes []c.AssetQuote)) { |
| 93 | + m.onUpdateAssetQuotes = onUpdate |
| 94 | +} |
| 95 | + |
| 96 | +func (m *MonitorYahoo) GetAssetQuotes(ignoreCache ...bool) ([]c.AssetQuote, error) { |
| 97 | + if len(ignoreCache) > 0 && ignoreCache[0] { |
| 98 | + assetQuotes, err := m.getAssetQuotesAndReplaceCache() |
| 99 | + if err != nil { |
| 100 | + return []c.AssetQuote{}, err |
| 101 | + } |
| 102 | + return assetQuotes, nil |
| 103 | + } |
| 104 | + |
| 105 | + m.mu.RLock() |
| 106 | + defer m.mu.RUnlock() |
| 107 | + |
| 108 | + return m.assetQuotesCache, nil |
| 109 | +} |
| 110 | + |
| 111 | +func (m *MonitorYahoo) SetSymbols(productIds []string) error { |
| 112 | + |
| 113 | + var err error |
| 114 | + |
| 115 | + m.mu.Lock() |
| 116 | + |
| 117 | + // Deduplicate productIds since input may have duplicates |
| 118 | + slices.Sort(productIds) |
| 119 | + m.productIds = slices.Compact(productIds) |
| 120 | + m.productIdsPolling = m.productIds |
| 121 | + m.input.productIds = productIds |
| 122 | + m.input.productIdsLookup = make(map[string]bool) |
| 123 | + for _, productId := range productIds { |
| 124 | + m.input.productIdsLookup[productId] = true |
| 125 | + } |
| 126 | + |
| 127 | + m.mu.Unlock() |
| 128 | + |
| 129 | + _, err = m.getAssetQuotesAndReplaceCache() |
| 130 | + if err != nil { |
| 131 | + return err |
| 132 | + } |
| 133 | + |
| 134 | + m.poller.SetSymbols(m.productIdsPolling) |
| 135 | + |
| 136 | + m.onUpdateAssetQuotes(m.assetQuotesCache) |
| 137 | + |
| 138 | + return nil |
| 139 | + |
| 140 | +} |
| 141 | + |
| 142 | +// Start the monitor |
| 143 | +func (m *MonitorYahoo) Start() error { |
| 144 | + |
| 145 | + var err error |
| 146 | + |
| 147 | + if m.isStarted { |
| 148 | + return fmt.Errorf("monitor already started") |
| 149 | + } |
| 150 | + |
| 151 | + // On start, get initial quotes from unary API |
| 152 | + _, err = m.getAssetQuotesAndReplaceCache() |
| 153 | + if err != nil { |
| 154 | + return err |
| 155 | + } |
| 156 | + |
| 157 | + err = m.poller.Start() |
| 158 | + if err != nil { |
| 159 | + return err |
| 160 | + } |
| 161 | + |
| 162 | + go m.handleUpdates() |
| 163 | + |
| 164 | + m.isStarted = true |
| 165 | + |
| 166 | + return nil |
| 167 | +} |
| 168 | + |
| 169 | +func (m *MonitorYahoo) Stop() error { |
| 170 | + |
| 171 | + if !m.isStarted { |
| 172 | + return fmt.Errorf("monitor not started") |
| 173 | + } |
| 174 | + |
| 175 | + m.cancel() |
| 176 | + return nil |
| 177 | +} |
| 178 | + |
| 179 | +func (m *MonitorYahoo) handleUpdates() { |
| 180 | + for { |
| 181 | + select { |
| 182 | + case <-m.ctx.Done(): |
| 183 | + return |
| 184 | + |
| 185 | + case updateMessage := <-m.chanPollUpdateAssetQuote: |
| 186 | + // Check if cache exists and values have changed before acquiring write lock |
| 187 | + m.mu.RLock() |
| 188 | + |
| 189 | + assetQuote, exists := m.assetQuotesCacheLookup[updateMessage.ID] |
| 190 | + |
| 191 | + if !exists { |
| 192 | + // If product id does not exist in cache, skip update |
| 193 | + // TODO: log product not found in cache - should not happen |
| 194 | + m.mu.RUnlock() |
| 195 | + continue |
| 196 | + } |
| 197 | + |
| 198 | + // Skip update if nothing has changed |
| 199 | + if assetQuote.QuotePrice.Price == updateMessage.Data.QuotePrice.Price && |
| 200 | + assetQuote.Exchange.IsActive == updateMessage.Data.Exchange.IsActive && |
| 201 | + assetQuote.QuotePrice.PriceDayHigh == updateMessage.Data.QuotePrice.PriceDayHigh { |
| 202 | + |
| 203 | + m.mu.RUnlock() |
| 204 | + continue |
| 205 | + } |
| 206 | + m.mu.RUnlock() |
| 207 | + |
| 208 | + // Price is different so update cache |
| 209 | + m.mu.Lock() |
| 210 | + |
| 211 | + assetQuote.QuotePrice.Price = updateMessage.Data.QuotePrice.Price |
| 212 | + assetQuote.QuotePrice.Change = updateMessage.Data.QuotePrice.Change |
| 213 | + assetQuote.QuotePrice.ChangePercent = updateMessage.Data.QuotePrice.ChangePercent |
| 214 | + assetQuote.QuotePrice.PriceDayHigh = updateMessage.Data.QuotePrice.PriceDayHigh |
| 215 | + assetQuote.QuotePrice.PriceDayLow = updateMessage.Data.QuotePrice.PriceDayLow |
| 216 | + assetQuote.QuotePrice.PriceOpen = updateMessage.Data.QuotePrice.PriceOpen |
| 217 | + assetQuote.QuotePrice.PricePrevClose = updateMessage.Data.QuotePrice.PricePrevClose |
| 218 | + assetQuote.QuoteExtended.FiftyTwoWeekHigh = updateMessage.Data.QuoteExtended.FiftyTwoWeekHigh |
| 219 | + assetQuote.QuoteExtended.FiftyTwoWeekLow = updateMessage.Data.QuoteExtended.FiftyTwoWeekLow |
| 220 | + assetQuote.QuoteExtended.MarketCap = updateMessage.Data.QuoteExtended.MarketCap |
| 221 | + assetQuote.QuoteExtended.Volume = updateMessage.Data.QuoteExtended.Volume |
| 222 | + assetQuote.Exchange.IsActive = updateMessage.Data.Exchange.IsActive |
| 223 | + assetQuote.Exchange.IsRegularTradingSession = updateMessage.Data.Exchange.IsRegularTradingSession |
| 224 | + |
| 225 | + m.mu.Unlock() |
| 226 | + |
| 227 | + m.onUpdateAssetQuote(assetQuote.Symbol, *assetQuote) |
| 228 | + |
| 229 | + continue |
| 230 | + |
| 231 | + default: |
| 232 | + } |
| 233 | + } |
| 234 | +} |
| 235 | + |
| 236 | +// Get asset quotes from unary API, add futures quotes, filter out assets not explicitly requested, and replace the asset quotes cache |
| 237 | +func (m *MonitorYahoo) getAssetQuotesAndReplaceCache() ([]c.AssetQuote, error) { |
| 238 | + |
| 239 | + assetQuotes, assetQuotesByProductId, err := m.unaryAPI.GetAssetQuotes(m.productIds) |
| 240 | + if err != nil { |
| 241 | + return []c.AssetQuote{}, err |
| 242 | + } |
| 243 | + |
| 244 | + // Filter asset quotes to only include explicitly requested ones |
| 245 | + assetQuotesEnriched := make([]c.AssetQuote, 0, len(m.input.productIds)) |
| 246 | + |
| 247 | + for _, quote := range assetQuotes { |
| 248 | + assetQuotesEnriched = append(assetQuotesEnriched, quote) |
| 249 | + } |
| 250 | + |
| 251 | + // Lock updates to asset quotes while symbols are changed and subscriptions updates. ensure data from unary call supercedes potentially oudated streaming data |
| 252 | + m.mu.Lock() |
| 253 | + defer m.mu.Unlock() |
| 254 | + |
| 255 | + m.assetQuotesCache = assetQuotesEnriched |
| 256 | + m.assetQuotesCacheLookup = assetQuotesByProductId |
| 257 | + |
| 258 | + return m.assetQuotesCache, nil |
| 259 | +} |
0 commit comments