Mercurial > silverbladetech
annotate Stocks/Stocks.Service/StocksService.cs @ 46:8e6458270599
Initial load
author | Steven Hollidge <stevenhollidge@hotmail.com> |
---|---|
date | Fri, 06 Apr 2012 17:13:35 +0100 |
parents | 399398841fd0 |
children |
rev | line source |
---|---|
0 | 1 using System; |
2 using System.Collections.Generic; | |
3 using System.Diagnostics; | |
4 using System.Linq; | |
5 using System.Threading.Tasks; | |
6 using NLog; | |
7 using Stocks.Common; | |
6
c812bca7b1ac
"Restore packages on build" enabled by Nuget 1.6
stevenh7776 stevenhollidge@hotmail.com
parents:
4
diff
changeset
|
8 using Stocks.Common.Core; |
2
29ed98d659e9
Adding WebClientShim files
stevenh7776 stevenhollidge@hotmail.com
parents:
0
diff
changeset
|
9 using Stocks.Common.Events; |
0 | 10 using Stocks.Common.Models; |
11 | |
12 namespace Stocks.Service | |
23
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
13 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
14 public class StocksService : IStocksService |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
15 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
16 #region Delegates |
0 | 17 |
23
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
18 public delegate void PriceChangedEventHandler(object sender, PriceChangedEventArgs e); |
0 | 19 |
23
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
20 #endregion |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
21 |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
22 private static readonly Logger Log = LogManager.GetCurrentClassLogger(); |
0 | 23 |
23
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
24 private readonly IConfigurationService _configurationService; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
25 private readonly IWebClientShim _webClient; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
26 private IList<Company> _companies; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
27 private List<Price> _currentPrices; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
28 private string _serviceUrl; |
2
29ed98d659e9
Adding WebClientShim files
stevenh7776 stevenhollidge@hotmail.com
parents:
0
diff
changeset
|
29 |
23
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
30 public StocksService( |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
31 IConfigurationService configurationService, |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
32 IWebClientShim webClientShim) |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
33 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
34 new AssemblyInit(); |
0 | 35 |
23
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
36 _webClient = webClientShim; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
37 _configurationService = configurationService; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
38 Stats = new SummaryStats(); |
0 | 39 |
23
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
40 GetCompanyList(); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
41 } |
0 | 42 |
23
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
43 public SummaryStats Stats { get; private set; } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
44 |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
45 #region IStocksService Members |
0 | 46 |
23
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
47 public bool IsActive { get; private set; } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
48 |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
49 public void Start() |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
50 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
51 PrepareForServiceStarting(); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
52 Task.Factory.StartNew(DownloadPrices); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
53 } |
0 | 54 |
23
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
55 public void Stop() |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
56 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
57 IsActive = false; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
58 Log.Debug("StockService stopped"); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
59 } |
0 | 60 |
23
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
61 #endregion |
0 | 62 |
23
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
63 private void GetCompanyList() |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
64 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
65 _companies = _configurationService.GetCompanies(); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
66 |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
67 string symbolsCsv = _companies.Select( |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
68 c => c.Symbol).Aggregate((c, d) => c + "," + d); |
0 | 69 |
23
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
70 _serviceUrl = _configurationService.GetServiceUrl(symbolsCsv); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
71 } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
72 |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
73 public event PriceChangedEventHandler PriceChanged; |
0 | 74 |
23
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
75 protected virtual void OnPriceChanged(Price price) |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
76 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
77 Stats.PriceChangeEvents++; |
0 | 78 |
23
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
79 if (PriceChanged != null) |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
80 PriceChanged(this, new PriceChangedEventArgs {Price = price}); |
0 | 81 } |
82 | |
23
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
83 private void PrepareForServiceStarting() |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
84 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
85 Log.Debug("StockService starting"); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
86 IsActive = true; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
87 Stats.Reset(); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
88 _currentPrices = new List<Price>(_companies.Count); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
89 } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
90 |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
91 private void DownloadPrices() |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
92 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
93 try |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
94 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
95 while (IsActive) |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
96 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
97 Stopwatch timeToDownload; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
98 string webResponse = TimedDelegates.Execute( |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
99 _webClient.DownloadString, |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
100 _serviceUrl, |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
101 out timeToDownload); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
102 |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
103 PopulatePricesFromWebResponse(webResponse); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
104 UpdateStats(timeToDownload, webResponse); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
105 } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
106 } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
107 catch (Exception e) |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
108 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
109 Log.Error("Exception during DownloadPrices()"); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
110 Log.Error("Stack Trace {0}: /r/nException Message: {1}", e.StackTrace, e.Message); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
111 Stop(); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
112 } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
113 } |
0 | 114 |
23
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
115 private void PopulatePricesFromWebResponse(string webResponse) |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
116 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
117 string[] webPrices = webResponse.Split( |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
118 new[] {"\n", "\r\n"}, |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
119 StringSplitOptions.RemoveEmptyEntries); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
120 |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
121 foreach (string webPriceData in webPrices) |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
122 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
123 Price webPrice = Factory.CreatePrice(webPriceData); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
124 Price localPrice = _currentPrices.Find(x => x.Symbol == webPrice.Symbol); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
125 |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
126 if (localPrice == null) |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
127 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
128 _currentPrices.Add(new Price(webPrice.Symbol, webPrice.CurrentPrice, webPrice.PreviousPrice)); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
129 continue; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
130 } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
131 |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
132 if (!localPrice.Equals(webPrice)) |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
133 UpdateLocalPrice(webPrice, localPrice); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
134 } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
135 } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
136 |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
137 private void UpdateLocalPrice(Price webPrice, Price localPrice) |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
138 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
139 localPrice.PreviousPrice = localPrice.CurrentPrice; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
140 localPrice.CurrentPrice = webPrice.CurrentPrice; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
141 OnPriceChanged(localPrice); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
142 } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
143 |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
144 private void UpdateStats(Stopwatch timeToDownload, string webResponse) |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
145 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
146 Stats.LastWebRequest.Duration = (int) timeToDownload.ElapsedMilliseconds; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
147 Stats.LastWebRequest.PricesDownloaded = _currentPrices.Count; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
148 Stats.LastWebRequest.Response = webResponse; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
149 Stats.LastWebRequest.Request = _serviceUrl; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
150 Stats.LastWebRequest.SymbolCount = _companies.Count; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
151 Stats.NumberOfRequests++; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
152 Log.Trace(Stats); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
8
diff
changeset
|
153 } |
2
29ed98d659e9
Adding WebClientShim files
stevenh7776 stevenhollidge@hotmail.com
parents:
0
diff
changeset
|
154 } |
0 | 155 } |