1
0
mirror of https://bitbucket.org/anguist/ntpa synced 2025-11-23 20:09:20 +00:00
Files
ntpa/Ntp.Analyzer/Graph/PeerGraph.cs

198 lines
7.3 KiB
C#
Raw Normal View History

2016-08-06 16:19:35 +02:00
//
// Copyright (c) 2013-2016 Carsten Sonne Larsen <cs@innolan.dk>
//
2016-04-02 11:22:42 +02:00
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
2016-08-06 16:19:35 +02:00
//
2016-04-02 11:22:42 +02:00
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
2016-08-06 16:19:35 +02:00
//
2016-04-02 11:22:42 +02:00
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
2016-05-22 23:40:47 +02:00
2016-04-02 11:22:42 +02:00
using System;
using System.Collections.Generic;
using System.Drawing;
using NPlot;
using Ntp.Analyzer.Data;
2016-04-28 19:29:22 +02:00
using Ntp.Analyzer.Data.Sql;
2016-05-22 23:40:47 +02:00
using Ntp.Analyzer.Interface;
using Ntp.Analyzer.Objects;
2016-04-02 11:22:42 +02:00
namespace Ntp.Analyzer.Graph
{
public sealed class PeerGraph : DispersionGraph
{
2016-05-22 23:40:47 +02:00
public PeerGraph(IPeerGraphConfiguration configuratation, Host host, Peer peer)
2016-04-02 11:22:42 +02:00
: base(configuratation)
{
config = configuratation;
this.host = host;
this.peer = peer;
2016-05-22 23:40:47 +02:00
timedReading = new Dictionary<DateTime, HostReading>();
2016-04-02 11:22:42 +02:00
}
2016-08-06 16:19:35 +02:00
private readonly List<double> balance = new List<double>();
2016-05-22 23:40:47 +02:00
private readonly IPeerGraphConfiguration config;
2016-08-06 16:19:35 +02:00
private readonly List<double> delay = new List<double>();
2016-05-22 23:40:47 +02:00
private readonly Host host;
private readonly Peer peer;
private readonly Dictionary<DateTime, HostReading> timedReading;
2016-08-06 16:19:35 +02:00
protected override string YLabel => "Milliseconds";
2016-04-02 11:22:42 +02:00
2016-08-18 19:18:52 +02:00
protected override void AddPlots()
{
base.AddPlots();
2016-08-27 14:17:47 +02:00
if (config.Delay.HasValue)
2016-08-18 19:18:52 +02:00
{
var delayPlot = SetupPlot("Delay", Color.DarkOrange, Time, delay);
Surface.Add(delayPlot, PlotSurface2D.XAxisPosition.Bottom, PlotSurface2D.YAxisPosition.Left);
}
2016-08-27 14:17:47 +02:00
if (config.Balance.HasValue)
2016-08-18 19:18:52 +02:00
{
var balancePlot = SetupPlot("Balanced offset", Color.DarkViolet, Time, balance);
Surface.Add(balancePlot, PlotSurface2D.XAxisPosition.Bottom, PlotSurface2D.YAxisPosition.Left);
}
}
2016-05-22 23:40:47 +02:00
protected override void LoadData()
2016-04-02 11:22:42 +02:00
{
2016-04-28 19:29:22 +02:00
FilteredSqlDatabaseMapper<PeerReading> peerMapper = DataFace.Instance.PeerReadings;
2016-04-02 11:22:42 +02:00
peerMapper.FilterHost = host;
peerMapper.FilterPeer = peer;
2016-09-08 21:28:21 +02:00
peerMapper.FilterTime = DateTime.UtcNow.Subtract(GraphTimeSpan);
2016-05-22 23:40:47 +02:00
2016-08-27 14:17:47 +02:00
if (config.Balance.HasValue)
2016-05-22 23:40:47 +02:00
{
2016-08-06 16:19:35 +02:00
foreach (var reading in peerMapper)
2016-05-22 23:40:47 +02:00
{
2016-09-08 21:28:21 +02:00
Time.Add(config.GraphTime == DateTimeKind.Local ? reading.LocalTime : reading.UtcTime);
2016-08-27 14:17:47 +02:00
if (config.Offset.HasValue) Offset.Add(reading.Offset*config.Offset.Value);
else Offset.Add(0.0);
if (config.Jitter.HasValue) Jitter.Add(reading.Jitter*config.Jitter.Value);
else Jitter.Add(0.0);
if (config.Delay.HasValue) delay.Add(reading.Delay*config.Delay.Value);
else delay.Add(0.0);
balance.Add(0.0);
2016-04-02 11:22:42 +02:00
}
2016-05-22 23:40:47 +02:00
}
else
{
2016-04-28 19:29:22 +02:00
FilteredSqlDatabaseMapper<HostReading> hostMapper = DataFace.Instance.HostReadings;
2016-04-02 11:22:42 +02:00
hostMapper.FilterHost = host;
2016-09-08 21:28:21 +02:00
hostMapper.FilterTime = DateTime.UtcNow.Subtract(GraphTimeSpan);
2016-04-02 11:22:42 +02:00
// Prepare balance data
2016-08-06 16:19:35 +02:00
foreach (var hostReading in hostMapper)
2016-05-22 23:40:47 +02:00
{
2016-09-08 21:28:21 +02:00
var indexTime = config.GraphTime == DateTimeKind.Local
? hostReading.RoundedLocalTime
: hostReading.RoundedUtcTime;
if (!timedReading.ContainsKey(indexTime))
timedReading.Add(indexTime, hostReading);
2016-04-02 11:22:42 +02:00
}
// Add
2016-08-06 16:19:35 +02:00
foreach (var reading in peerMapper)
2016-05-22 23:40:47 +02:00
{
2016-09-08 21:28:21 +02:00
Time.Add(config.GraphTime == DateTimeKind.Local ? reading.LocalTime : reading.UtcTime);
2016-08-27 14:17:47 +02:00
if (config.Offset.HasValue) Offset.Add(reading.Offset*config.Offset.Value);
else Offset.Add(0.0);
if (config.Jitter.HasValue) Jitter.Add(reading.Jitter*config.Jitter.Value);
else Jitter.Add(0.0);
if (config.Delay.HasValue) delay.Add(reading.Delay*config.Delay.Value);
else delay.Add(0.0);
2016-05-22 23:40:47 +02:00
2016-09-08 21:28:21 +02:00
var indexTime = config.GraphTime == DateTimeKind.Local
? reading.RoundedLocalTime
: reading.RoundedUtcTime;
if (timedReading.ContainsKey(indexTime))
balance.Add(reading.Offset - timedReading[indexTime].Offset);
2016-05-22 23:40:47 +02:00
else
2016-08-27 14:17:47 +02:00
balance.Add(0.0);
2016-04-02 11:22:42 +02:00
}
}
2016-08-27 14:17:47 +02:00
if (config.FilterFactor.HasValue)
2016-05-22 23:40:47 +02:00
CleanSeries();
2016-04-02 11:22:42 +02:00
}
2016-05-22 23:40:47 +02:00
protected override void PreRender()
2016-04-02 11:22:42 +02:00
{
2016-05-22 23:40:47 +02:00
base.PreRender();
2016-04-02 11:22:42 +02:00
2016-05-22 23:40:47 +02:00
Surface.Title = config.GetTitle(peer);
2016-04-02 11:22:42 +02:00
}
/// <summary>
/// Cleans the value series from values out side boundaries.
/// </summary>
2016-05-22 23:40:47 +02:00
private void CleanSeries()
2016-04-02 11:22:42 +02:00
{
double totalOffset = 0.0;
double totalJitter = 0.0;
double totalDelay = 0.0;
double totalBalance = 0.0;
// Calculate mean value
2016-05-22 23:40:47 +02:00
for (int i = 0; i < Time.Count; i++)
{
totalOffset += Math.Abs(Offset[i]);
totalJitter += Math.Abs(Jitter[i]);
totalDelay += Math.Abs(delay[i]);
totalBalance += Math.Abs(balance[i]);
2016-04-02 11:22:42 +02:00
}
2016-05-22 23:40:47 +02:00
double avgOffset = totalOffset/Time.Count;
double avgJitter = totalJitter/Time.Count;
double avgDelay = totalDelay/Time.Count;
double avgBalance = totalBalance/Time.Count;
2016-04-02 11:22:42 +02:00
2016-08-06 16:19:35 +02:00
var indexes = new List<int>();
2016-04-02 11:22:42 +02:00
// Find invalid values
2016-05-22 23:40:47 +02:00
for (int i = 0; i < Time.Count; i++)
{
if (Math.Abs(Offset[i]) > avgOffset*config.FilterFactor ||
Math.Abs(Jitter[i]) > avgJitter*config.FilterFactor ||
Math.Abs(delay[i]) > avgDelay*config.FilterFactor ||
Math.Abs(balance[i]) > avgBalance*config.FilterFactor)
{
indexes.Add(i);
2016-04-02 11:22:42 +02:00
}
}
// Remove invalid values
2016-05-22 23:40:47 +02:00
for (int i = indexes.Count - 1; i >= 0; i--)
{
Time.RemoveAt(indexes[i]);
Offset.RemoveAt(indexes[i]);
Jitter.RemoveAt(indexes[i]);
delay.RemoveAt(indexes[i]);
balance.RemoveAt(indexes[i]);
2016-04-02 11:22:42 +02:00
}
}
}
2016-03-05 15:25:01 +01:00
}