Mercurial > silverbladetech
annotate Stocks/Stocks.Service/StocksService.cs @ 20:6109bc268b90
Latest
author | adminsh@apollo |
---|---|
date | Tue, 20 Mar 2012 13:37:46 +0000 |
parents | 6e84a4c92378 |
children | 399398841fd0 |
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 | |
13 { | |
14 public class StocksService : IStocksService | |
15 { | |
16 private static readonly Logger Log = LogManager.GetCurrentClassLogger(); | |
17 | |
18 private IList<Company> _companies; | |
19 private IConfigurationService _configurationService; | |
20 private List<Price> _currentPrices; | |
21 private string _serviceUrl; | |
2
29ed98d659e9
Adding WebClientShim files
stevenh7776 stevenhollidge@hotmail.com
parents:
0
diff
changeset
|
22 private IWebClientShim _webClient; |
0 | 23 |
24 public bool IsActive { get; private set; } | |
25 public SummaryStats Stats { get; private set; } | |
26 | |
2
29ed98d659e9
Adding WebClientShim files
stevenh7776 stevenhollidge@hotmail.com
parents:
0
diff
changeset
|
27 public StocksService( |
29ed98d659e9
Adding WebClientShim files
stevenh7776 stevenhollidge@hotmail.com
parents:
0
diff
changeset
|
28 IConfigurationService configurationService, |
29ed98d659e9
Adding WebClientShim files
stevenh7776 stevenhollidge@hotmail.com
parents:
0
diff
changeset
|
29 IWebClientShim webClientShim) |
0 | 30 { |
31 new AssemblyInit(); | |
32 | |
2
29ed98d659e9
Adding WebClientShim files
stevenh7776 stevenhollidge@hotmail.com
parents:
0
diff
changeset
|
33 _webClient = webClientShim; |
0 | 34 _configurationService = configurationService; |
2
29ed98d659e9
Adding WebClientShim files
stevenh7776 stevenhollidge@hotmail.com
parents:
0
diff
changeset
|
35 Stats = new SummaryStats(); |
29ed98d659e9
Adding WebClientShim files
stevenh7776 stevenhollidge@hotmail.com
parents:
0
diff
changeset
|
36 |
29ed98d659e9
Adding WebClientShim files
stevenh7776 stevenhollidge@hotmail.com
parents:
0
diff
changeset
|
37 GetCompanyList(); |
29ed98d659e9
Adding WebClientShim files
stevenh7776 stevenhollidge@hotmail.com
parents:
0
diff
changeset
|
38 } |
29ed98d659e9
Adding WebClientShim files
stevenh7776 stevenhollidge@hotmail.com
parents:
0
diff
changeset
|
39 |
29ed98d659e9
Adding WebClientShim files
stevenh7776 stevenhollidge@hotmail.com
parents:
0
diff
changeset
|
40 private void GetCompanyList() |
29ed98d659e9
Adding WebClientShim files
stevenh7776 stevenhollidge@hotmail.com
parents:
0
diff
changeset
|
41 { |
29ed98d659e9
Adding WebClientShim files
stevenh7776 stevenhollidge@hotmail.com
parents:
0
diff
changeset
|
42 _companies = _configurationService.GetCompanies(); |
0 | 43 |
44 string symbolsCsv = _companies.Select( | |
45 c => c.Symbol).Aggregate((c, d) => c + "," + d); | |
46 _serviceUrl = _configurationService.GetServiceUrl(symbolsCsv); | |
47 } | |
48 | |
8
6e84a4c92378
FakeWebClientShim added and refactoring
stevenh7776 stevenhollidge@hotmail.com
parents:
6
diff
changeset
|
49 public event PriceChangedEventHandler PriceChanged; |
0 | 50 public delegate void PriceChangedEventHandler(object sender, PriceChangedEventArgs e); |
8
6e84a4c92378
FakeWebClientShim added and refactoring
stevenh7776 stevenhollidge@hotmail.com
parents:
6
diff
changeset
|
51 protected virtual void OnPriceChanged(Price price) |
0 | 52 { |
53 Stats.PriceChangeEvents++; | |
54 | |
55 if (PriceChanged != null) | |
56 PriceChanged(this, new PriceChangedEventArgs() { Price = price }); | |
57 } | |
58 | |
59 public void Start() | |
60 { | |
61 PrepareForServiceStarting(); | |
62 Task.Factory.StartNew(() => DownloadPrices()); | |
63 } | |
64 | |
65 public void Stop() | |
66 { | |
67 IsActive = false; | |
68 Log.Debug("StockService stopped"); | |
69 } | |
70 | |
71 private void PrepareForServiceStarting() | |
72 { | |
73 Log.Debug("StockService starting"); | |
74 IsActive = true; | |
75 Stats.Reset(); | |
76 _currentPrices = new List<Price>(_companies.Count); | |
77 } | |
78 | |
79 private void DownloadPrices() | |
80 { | |
81 try | |
82 { | |
4
57f20ba55884
Fix for webClientShim to use downloadString instead of downloadData
stevenh7776 stevenhollidge@hotmail.com
parents:
2
diff
changeset
|
83 Stopwatch timeToDownload; |
0 | 84 |
85 while (IsActive) | |
86 { | |
4
57f20ba55884
Fix for webClientShim to use downloadString instead of downloadData
stevenh7776 stevenhollidge@hotmail.com
parents:
2
diff
changeset
|
87 string webResponse = TimedDelegates.Execute<string>( |
57f20ba55884
Fix for webClientShim to use downloadString instead of downloadData
stevenh7776 stevenhollidge@hotmail.com
parents:
2
diff
changeset
|
88 _webClient.DownloadString, |
57f20ba55884
Fix for webClientShim to use downloadString instead of downloadData
stevenh7776 stevenhollidge@hotmail.com
parents:
2
diff
changeset
|
89 _serviceUrl, |
57f20ba55884
Fix for webClientShim to use downloadString instead of downloadData
stevenh7776 stevenhollidge@hotmail.com
parents:
2
diff
changeset
|
90 out timeToDownload); |
0 | 91 |
92 PopulatePricesFromWebResponse(webResponse); | |
93 UpdateStats(timeToDownload, webResponse); | |
94 } | |
95 } | |
96 catch (Exception e) | |
97 { | |
98 Log.Error("Exception during DownloadPrices()"); | |
99 Log.Error("Stack Trace {0}: /r/nException Message: {1}", e.StackTrace, e.Message); | |
100 this.Stop(); | |
101 } | |
102 } | |
103 | |
104 private void PopulatePricesFromWebResponse(string webResponse) | |
105 { | |
106 string[] webPrices = webResponse.Split( | |
107 new string[] { "\n", "\r\n" }, | |
108 StringSplitOptions.RemoveEmptyEntries); | |
109 | |
110 foreach (string webPriceData in webPrices) | |
111 { | |
112 var webPrice = Factory.CreatePrice(webPriceData); | |
113 var localPrice = _currentPrices.Find(x => x.Symbol == webPrice.Symbol); | |
114 | |
115 if (localPrice == null) { | |
116 _currentPrices.Add(new Price(webPrice.Symbol, webPrice.CurrentPrice, webPrice.PreviousPrice)); | |
117 continue; | |
118 } | |
119 | |
2
29ed98d659e9
Adding WebClientShim files
stevenh7776 stevenhollidge@hotmail.com
parents:
0
diff
changeset
|
120 if (localPrice.Equals(webPrice)) |
0 | 121 continue; |
2
29ed98d659e9
Adding WebClientShim files
stevenh7776 stevenhollidge@hotmail.com
parents:
0
diff
changeset
|
122 else |
29ed98d659e9
Adding WebClientShim files
stevenh7776 stevenhollidge@hotmail.com
parents:
0
diff
changeset
|
123 UpdateLocalPrice(webPrice, localPrice); |
0 | 124 } |
125 } | |
126 | |
2
29ed98d659e9
Adding WebClientShim files
stevenh7776 stevenhollidge@hotmail.com
parents:
0
diff
changeset
|
127 private void UpdateLocalPrice(Price webPrice, Price localPrice) |
29ed98d659e9
Adding WebClientShim files
stevenh7776 stevenhollidge@hotmail.com
parents:
0
diff
changeset
|
128 { |
29ed98d659e9
Adding WebClientShim files
stevenh7776 stevenhollidge@hotmail.com
parents:
0
diff
changeset
|
129 localPrice.PreviousPrice = localPrice.CurrentPrice; |
29ed98d659e9
Adding WebClientShim files
stevenh7776 stevenhollidge@hotmail.com
parents:
0
diff
changeset
|
130 localPrice.CurrentPrice = webPrice.CurrentPrice; |
8
6e84a4c92378
FakeWebClientShim added and refactoring
stevenh7776 stevenhollidge@hotmail.com
parents:
6
diff
changeset
|
131 OnPriceChanged(localPrice); |
2
29ed98d659e9
Adding WebClientShim files
stevenh7776 stevenhollidge@hotmail.com
parents:
0
diff
changeset
|
132 } |
29ed98d659e9
Adding WebClientShim files
stevenh7776 stevenhollidge@hotmail.com
parents:
0
diff
changeset
|
133 |
0 | 134 private void UpdateStats(Stopwatch timeToDownload, string webResponse) |
135 { | |
136 Stats.LastWebRequest.Duration = (int)timeToDownload.ElapsedMilliseconds; | |
137 Stats.LastWebRequest.PricesDownloaded = _currentPrices.Count; | |
138 Stats.LastWebRequest.Response = webResponse; | |
139 Stats.LastWebRequest.Request = _serviceUrl; | |
140 Stats.LastWebRequest.SymbolCount = _companies.Count; | |
141 Stats.NumberOfRequests++; | |
142 Log.Trace(Stats); | |
143 } | |
144 } | |
145 } |