1
0
mirror of https://bitbucket.org/anguist/ntpa synced 2025-11-23 03:49:55 +00:00
Files
ntpa/Ntp.Analyzer.Process/Description/PeerStatJob.cs

184 lines
5.8 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-04-10 11:50:35 +02:00
2016-04-02 11:22:42 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
2016-08-06 16:19:35 +02:00
using Ntp.Analyzer.Config.Node;
2016-04-02 11:22:42 +02:00
using Ntp.Analyzer.Data;
2016-05-22 23:40:47 +02:00
using Ntp.Analyzer.Import;
2016-04-02 11:22:42 +02:00
using Ntp.Analyzer.Objects;
2016-08-27 13:28:21 +02:00
using Ntp.Analyzer.Process.Log;
2016-08-18 19:18:52 +02:00
using Ntp.Common.Log;
using Ntp.Common.Process;
2016-04-02 11:22:42 +02:00
namespace Ntp.Analyzer.Process.Description
{
/// <summary>
/// Job which read statistics about peers and saves the result to a database.
/// </summary>
public sealed class PeerStatJob : JobDescription
{
2016-08-06 16:19:35 +02:00
public PeerStatJob(StatConfiguration config, LogBase log)
2016-04-02 11:22:42 +02:00
: base(config, log)
{
this.config = config;
2016-05-22 23:40:47 +02:00
readings = new List<PeerReading>();
activities = new List<PeerActivity>();
entries = new List<AssociationEntry>();
2016-04-02 11:22:42 +02:00
}
2016-05-22 23:40:47 +02:00
private readonly List<PeerActivity> activities;
2016-08-06 16:19:35 +02:00
private readonly StatConfiguration config;
2016-05-22 23:40:47 +02:00
private readonly List<AssociationEntry> entries;
2016-04-02 11:22:42 +02:00
private readonly List<PeerReading> readings;
private Host host;
private List<Peer> peers;
2016-08-06 16:19:35 +02:00
public override ThreadType ThreadType => ThreadType.MultiThreaded;
2016-04-02 11:22:42 +02:00
2016-08-06 16:19:35 +02:00
public override string JobType => "Peer statistics";
2016-04-02 11:22:42 +02:00
2016-08-06 16:19:35 +02:00
public override int Priority => 2;
2016-04-02 11:22:42 +02:00
2016-05-22 23:40:47 +02:00
protected override void InternalExecute()
2016-04-02 11:22:42 +02:00
{
2016-05-22 23:40:47 +02:00
try
{
Initalize();
Import();
SaveResult();
}
catch (Exception e)
{
2016-08-27 13:28:21 +02:00
Log.PeerImportError(e);
2016-04-02 11:22:42 +02:00
}
}
/// <summary>
/// Import statistics.
/// </summary>
2016-05-22 23:40:47 +02:00
private void Import()
2016-04-02 11:22:42 +02:00
{
2016-08-27 13:28:21 +02:00
var bulk = config.Bulk == null
? new ReadingBulk(host.Name, config.TimeStamp)
: DataFace.Instance.ReadingBulks.Single(b => b.Name == config.Bulk.Name);
2016-05-22 23:40:47 +02:00
2016-08-27 13:28:21 +02:00
var importer = ImportFactory.CreatePeerImporter(config.ServerName, config.ServerType, host, Log);
2016-05-22 23:40:47 +02:00
entries.AddRange(importer);
// Read values
try
{
importer.Execute();
}
catch (Exception e)
{
2016-08-27 13:28:21 +02:00
Log.PeerImportError(e);
2016-05-22 23:40:47 +02:00
return;
}
2016-04-02 11:22:42 +02:00
// Generate statistics
2016-08-27 13:28:21 +02:00
foreach (var entry in entries)
2016-05-22 23:40:47 +02:00
{
2016-04-02 11:22:42 +02:00
// Find peer in database.
2016-08-27 13:28:21 +02:00
var currentEntry = entry;
IEnumerable<Peer> peerList = peers.Where(p => p.Ip == currentEntry.Remote).ToList();
2016-04-02 11:22:42 +02:00
Peer peer;
2016-05-22 23:40:47 +02:00
if (peerList.Count() == 1)
{
peer = peerList.Single();
}
else if (!peerList.Any())
{
2016-08-27 13:28:21 +02:00
Log.PeerIpNotFound(entry);
2016-04-02 11:22:42 +02:00
continue;
2016-05-22 23:40:47 +02:00
}
else
{
2016-08-27 13:28:21 +02:00
Log.PeerIpAmbiguous(entry);
2016-04-02 11:22:42 +02:00
continue;
}
2016-05-22 23:40:47 +02:00
var reading = new PeerReading(host, peer, bulk, entry);
readings.Add(reading);
// Update timestamp
2016-08-27 13:28:21 +02:00
var activity = DataFace.Instance.PeerActivities.
2016-08-18 19:18:52 +02:00
SingleOrDefault(a => Equals(a.Host, reading.Host) && Equals(a.Peer, reading.Peer));
2016-05-22 23:40:47 +02:00
if (activity != null)
{
activity.LastActive = bulk.Time;
}
else
{
activity = new PeerActivity(reading.Peer, host, bulk.Time);
2016-04-10 11:50:35 +02:00
}
2016-05-22 23:40:47 +02:00
activities.Add(activity);
2016-04-02 11:22:42 +02:00
}
}
2016-08-18 19:18:52 +02:00
/// <summary>
/// Initalize database context.
/// </summary>
private void Initalize()
{
// Clear any previouse readings
entries.Clear();
readings.Clear();
activities.Clear();
peers = DataFace.Instance.Peers.ToList();
host = DataFace.Instance.Hosts.SingleOrDefault(h => h.Id == config.HostId);
if (host == null)
{
2016-08-27 13:28:21 +02:00
Log.HostNotFound(config.HostId);
2016-08-18 19:18:52 +02:00
}
}
2016-04-02 11:22:42 +02:00
/// <summary>
/// Saves the result of the import to database.
/// </summary>
2016-05-22 23:40:47 +02:00
private void SaveResult()
2016-04-02 11:22:42 +02:00
{
2016-08-27 13:28:21 +02:00
foreach (var reading in readings)
2016-05-22 23:40:47 +02:00
{
DataFace.Instance.PeerReadings.Save(reading);
}
2016-08-27 13:28:21 +02:00
foreach (var activity in activities)
2016-05-22 23:40:47 +02:00
{
DataFace.Instance.PeerActivities.Save(activity);
2016-04-02 11:22:42 +02:00
}
2016-04-10 11:50:35 +02:00
2016-08-27 13:28:21 +02:00
foreach (var entry in entries)
2016-05-22 23:40:47 +02:00
{
DataFace.Instance.AssociationEntries.Delete(entry.Remote, entry.HostId);
DataFace.Instance.AssociationEntries.Save(entry);
2016-04-10 11:50:35 +02:00
}
2016-04-02 11:22:42 +02:00
}
}
2016-03-05 15:25:01 +01:00
}