private static async Task RunAlertDispatcher(ILogger log, CloudTable subscribersTable, IAsyncCollector <Alert> outputQueueItem, bool trackDailyChange) { TickerHistory tickerHistory = await QueryTickerService(log); foreach (Datum data in tickerHistory.Data) { log.LogInformation($"USD to BTC at {data.Time.ToString()}: {data.Close}"); } await SendAlertsToSubscribers(log, subscribersTable, outputQueueItem, trackDailyChange, tickerHistory); }
private static async Task <TickerHistory> QueryTickerService(ILogger log) { HttpResponseMessage response = await httpClient.GetAsync(BuildTickerServiceUri()); HttpContent responseContent = response.Content; using (StreamReader reader = new StreamReader(await responseContent.ReadAsStreamAsync())) { string result = await reader.ReadToEndAsync(); TickerHistory history = JsonConvert.DeserializeObject <TickerHistory>(result); return(history); } }
private static async Task SendAlertsToSubscribers(ILogger log, CloudTable subscribersTable, IAsyncCollector <Alert> outputQueueItem, bool trackDailyChange, TickerHistory history) { decimal latest = history.Data[history.Data.Length - 1].Close; decimal lastHour = history.Data[history.Data.Length - 2].Open; decimal lastDay = history.Data[0].Close; decimal lastHourChange = (latest - lastHour) / lastHour; decimal lastDayChange = (latest - lastDay) / lastDay; log.LogInformation($"Last hour change: {decimal.Round(lastHourChange * 100, 2)}%"); log.LogInformation($"Last day change: {decimal.Round(lastDayChange * 100, 2)}%"); decimal trackedChange = (trackDailyChange ? Math.Max(Math.Abs(lastHourChange), Math.Abs(lastDayChange)) : Math.Abs(lastHourChange)) * 100; if (trackedChange >= 1) { foreach (int levelOfAlert in Enumerable.Range(1, (int)trackedChange)) { await SendAlertsForLevel(log, subscribersTable, outputQueueItem, lastHourChange, lastDayChange, latest, levelOfAlert); } } }