NTP Analyzer  0.8.2
Analyze the operation of time servers
PeerReadingDatabaseMapper.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.Diagnostics.CodeAnalysis;
25 using Ntp.Analyzer.Data.Log;
26 using Ntp.Analyzer.Objects;
27 using Ntp.Common.Log;
28 using Ntp.Data.Log;
29 
30 namespace Ntp.Analyzer.Data.Sql
31 {
35  public sealed class PeerReadingDatabaseMapper : FilteredSqlDatabaseMapper<PeerReading>
36  {
38  HostDatabaseMapper hostMapper,
39  PeerDatabaseMapper peerMapper,
40  LogBase log)
41  : base(log)
42  {
43  this.hostMapper = hostMapper;
44  this.peerMapper = peerMapper;
45  }
46 
47  private const string SelectSql =
48  "SELECT " +
49  "id, time, zone, hostId, peerId, last, reach, delay, [offset], jitter " +
50  "FROM peerReading";
51 
52  private const string InsertSql =
53  "INSERT INTO peerReading ( time, zone, hostId, peerId, last, reach, delay, [offset], jitter ) " +
54  "VALUES ( @time, @zone, @hostId, @peerId, @last, @reach, @delay, @offset, @jitter );{0};";
55 
56  private readonly HostDatabaseMapper hostMapper;
57  private readonly PeerDatabaseMapper peerMapper;
58 
59  protected override bool UseCache => false;
60 
61  protected override string TableName => "peerReading";
62 
63  protected override string CreateSql => "CREATE TABLE peerReading ( " +
64  " id {0} PRIMARY KEY, " +
65  " time TIMESTAMP NOT NULL, " +
66  " zone INT NOT NULL, " +
67  " hostId INT NOT NULL, " +
68  " peerId INT NOT NULL, " +
69  " last INT NOT NULL, " +
70  " reach INT NOT NULL, " +
71  " delay DOUBLE PRECISION NOT NULL, " +
72  " [offset] DOUBLE PRECISION NOT NULL, " +
73  " jitter DOUBLE PRECISION NOT NULL, " +
74  " FOREIGN KEY (hostId) REFERENCES host(id), " +
75  " FOREIGN KEY (peerId) REFERENCES peer(id) " +
76  "){1};";
77 
82  [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
83  public override IEnumerator<PeerReading> GetEnumerator()
84  {
85  lock (MapperLocker)
86  {
87  bool error = false;
88 
89  try
90  {
91  Open();
92  Command.CommandText = PrepareSql(SelectSql);
93  Log.SqlExecute(Command.CommandText);
94  Reader = Command.ExecuteReader();
95  }
96  catch (Exception e)
97  {
98  Log.ReadError(TableName, e);
99  error = true;
100  }
101 
102  if (error)
103  yield break;
104 
105  while (Reader.Read())
106  {
107  int id = Convert.ToInt32(Reader["id"]);
108  var time = Convert.ToDateTime(Reader["time"]);
109  int zone = Convert.ToInt32(Reader["zone"]);
110  int hostId = Convert.ToInt32(Reader["hostId"]);
111  var host = hostMapper[hostId];
112  int peerId = Convert.ToInt32(Reader["peerId"]);
113  var peer = peerMapper[peerId];
114  int last = Convert.ToInt32(Reader["last"]);
115  int reach = Convert.ToInt32(Reader["reach"]);
116  double delay = Convert.ToDouble(Reader["delay"]);
117  double offset = Convert.ToDouble(Reader["offset"]);
118  double jitter = Convert.ToDouble(Reader["jitter"]);
119 
120  var reading = new PeerReading(
121  id, time, zone, host, peer,
122  last, reach, delay, offset, jitter);
123 
124  yield return reading;
125  }
126 
127  Close();
128  }
129  }
130 
131  [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
132  protected override void Insert(PeerReading item)
133  {
134  lock (MapperLocker)
135  {
136  try
137  {
138  Open();
139  Command.CommandText = PrepareInsertSql(InsertSql);
140  Command.Parameters.Add(CreateParameter("@time", item.Time));
141  Command.Parameters.Add(CreateParameter("@zone", item.UtcOffset));
142  Command.Parameters.Add(CreateParameter("@hostId", item.Host.Id));
143  Command.Parameters.Add(CreateParameter("@peerId", item.Peer.Id));
144  Command.Parameters.Add(CreateParameter("@last", item.LastPoll));
145  Command.Parameters.Add(CreateParameter("@reach", item.Reach));
146  Command.Parameters.Add(CreateParameter("@delay", item.Delay));
147  Command.Parameters.Add(CreateParameter("@offset", item.Offset));
148  Command.Parameters.Add(CreateParameter("@jitter", item.Jitter));
149  Command.Prepare();
150  Log.SqlExecute(Command.CommandText, Command.Parameters);
151  var idObject = Command.ExecuteScalar();
152  item.SetId(Convert.ToInt32(idObject));
153  }
154  catch (Exception e)
155  {
156  Log.InsertError(TableName, e);
157  }
158  finally
159  {
160  Close();
161  }
162  }
163  }
164 
165  protected override void ReadContent()
166  {
167  throw new NotSupportedException(LogMessages.DatabaseCacheError);
168  }
169 
170  protected override void Update(PeerReading item)
171  {
172  throw new NotSupportedException(string.Format(LogMessages.DatabaseNoUpdate, TableName));
173  }
174  }
175 }
OR/M mapper for tables with a large amount of rows.
void SetId(int id)
Sets the identifier after the object have been stored in persistent storage.
var e
Definition: bootstrap.min.js:6
PeerReadingDatabaseMapper(HostDatabaseMapper hostMapper, PeerDatabaseMapper peerMapper, LogBase log)
override IEnumerator< PeerReading > GetEnumerator()
Read all data from table in a sequential manner.