NTP Analyzer  0.8.2
Analyze the operation of time servers
PeerStatJob.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) 2013-2017 Carsten Sonne Larsen <cs@innolan.net>
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 using System;
23 using System.Collections.Generic;
24 using System.Linq;
25 using Ntp.Analyzer.Config.Node;
26 using Ntp.Analyzer.Data;
27 using Ntp.Analyzer.Import;
28 using Ntp.Analyzer.Objects;
29 using Ntp.Analyzer.Process.Log;
30 using Ntp.Common.Log;
31 using Ntp.Common.Process;
32 
33 namespace Ntp.Analyzer.Process.Description
34 {
38  public sealed class PeerStatJob : JobDescription
39  {
41  : base(config, log)
42  {
43  this.config = config;
44  readings = new List<PeerReading>();
45  activities = new List<PeerActivity>();
46  entries = new List<AssociationEntry>();
47  }
48 
49  private readonly List<PeerActivity> activities;
50 
51  private readonly StatConfiguration config;
52  private readonly List<AssociationEntry> entries;
53  private readonly List<PeerReading> readings;
54  private Host host;
55  private List<Peer> peers;
56 
57  public override ThreadType ThreadType => ThreadType.MultiThreaded;
58 
59  public override string JobType => "Peer statistics";
60 
61  public override int Priority => 2;
62 
63  protected override void InternalExecute()
64  {
65  try
66  {
67  Initialize();
68  Import();
69  SaveResult();
70  }
71  catch (Exception e)
72  {
73  Log.PeerImportError(e);
74  }
75  }
76 
80  private void Import()
81  {
82  var bulk = config.Bulk == null
83  ? new ReadingBulk(host.Name, config.TimeStamp)
84  : DataFace.Instance.ReadingBulks.Single(b => b.Name == config.Bulk.Name);
85 
86  var importer = ImportFactory.CreatePeerImporter(config.ServerName, config.ServerType, host, Log);
87  entries.AddRange(importer);
88 
89  // Read values
90  try
91  {
92  importer.Execute();
93  }
94  catch (Exception e)
95  {
96  Log.PeerImportError(e);
97  return;
98  }
99 
100  // Generate statistics
101  foreach (var entry in entries)
102  {
103  // Find peer in database.
104  var currentEntry = entry;
105  IEnumerable<Peer> peerList = peers.Where(p => p.Ip == currentEntry.Remote).ToList();
106  Peer peer;
107 
108  if (peerList.Count() == 1)
109  {
110  peer = peerList.Single();
111  }
112  else if (!peerList.Any())
113  {
114  Log.PeerIpNotFound(entry);
115  continue;
116  }
117  else
118  {
119  Log.PeerIpAmbiguous(entry);
120  continue;
121  }
122 
123  var reading = new PeerReading(host, peer, bulk, entry);
124  readings.Add(reading);
125 
126  // Update timestamp
127  var activity = DataFace.Instance.PeerActivities.
128  SingleOrDefault(a => Equals(a.Host, reading.Host) && Equals(a.Peer, reading.Peer));
129 
130  if (activity != null)
131  {
132  activity.LastActive = bulk.Time;
133  }
134  else
135  {
136  activity = new PeerActivity(reading.Peer, host, bulk.Time);
137  }
138 
139  activities.Add(activity);
140  }
141  }
142 
146  private void Initialize()
147  {
148  // Clear any previous readings
149  entries.Clear();
150  readings.Clear();
151  activities.Clear();
152 
153  peers = DataFace.Instance.Peers.ToList();
154  host = DataFace.Instance.Hosts.SingleOrDefault(h => h.Id == config.HostId);
155 
156  if (host == null)
157  {
158  Log.HostNotFound(config.HostId);
159  }
160  }
161 
165  private void SaveResult()
166  {
167  foreach (var reading in readings)
168  {
169  DataFace.Instance.PeerReadings.Save(reading);
170  }
171 
172  foreach (var activity in activities)
173  {
174  DataFace.Instance.PeerActivities.Save(activity);
175  }
176 
177  foreach (var entry in entries)
178  {
179  DataFace.Instance.AssociationEntries.Delete(entry.Remote, entry.HostId);
181  }
182  }
183  }
184 }
HostDatabaseMapper Hosts
Gets the host mapper.
Definition: DataFace.cs:57
function a
Definition: bootstrap.min.js:6
PeerReadingDatabaseMapper PeerReadings
Gets the peer reading mapper.
Definition: DataFace.cs:87
static Importer< AssociationEntry > CreatePeerImporter(string address, ServerType type, Host host, LogBase log)
override void InternalExecute()
Implementing method for descendants.
Definition: PeerStatJob.cs:63
readonly List< PeerActivity > activities
Definition: PeerStatJob.cs:49
PeerStatJob(StatConfiguration config, LogBase log)
Definition: PeerStatJob.cs:40
var e
Definition: bootstrap.min.js:6
AssociationEntryMapper AssociationEntries
Gets the association entry mapper.
Definition: DataFace.cs:111
void Initialize()
Initialize database context.
Definition: PeerStatJob.cs:146
ReadingBulkMapper ReadingBulks
Gets the reading bulk mapper.
Definition: DataFace.cs:105
PeerDatabaseMapper Peers
Gets the peer mapper.
Definition: DataFace.cs:63
Singleton facade class used to access memory persistent data.
Definition: DataFace.cs:30
readonly List< PeerReading > readings
Definition: PeerStatJob.cs:53
PeerActivityDatabaseMapper PeerActivities
Gets the peer activity mapper.
Definition: DataFace.cs:99
Base class for jobs following the GoF Command Pattern.
Job which read statistics about peers and saves the result to a database.
Definition: PeerStatJob.cs:38
readonly List< AssociationEntry > entries
Definition: PeerStatJob.cs:52
void SaveResult()
Saves the result of the import to database.
Definition: PeerStatJob.cs:165
static DataFace Instance
Gets the Singleton instance.
Definition: DataFace.cs:51
var b
Definition: bootstrap.min.js:6