annotate MetroWpf/Stocks.Service/StocksService.cs @ 21:dfc81f8bb838

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