//******************************************************************************************************
// DeviceWrapper.cs - Gbtc
//
// Copyright © 2014, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 02/24/2014 - Stephen C. Wills
// Generated original version of source code.
//
//******************************************************************************************************
using System;
using System.Threading;
namespace GSF.TimeSeries.Statistics
{
///
/// Helper class for calculating device statistics.
///
/// The type of the devices whose statistics are to be calculated.
public class DeviceStatisticsHelper where T : class, IDevice
{
#region [ Members ]
// Fields
private readonly T m_device;
private int m_expectedMeasurementsPerSecond;
private long m_lastUpdateTicks;
private int m_measurementsInSecond;
private double m_expectedMeasurements;
#endregion
#region [ Constructors ]
///
/// Creates a new instance of the class.
///
/// The device whose statistics are to be calculated using this helper.
public DeviceStatisticsHelper(T device)
{
m_device = device;
m_lastUpdateTicks = DateTime.UtcNow.Ticks;
}
#endregion
#region [ Properties ]
///
/// Gets the device whose statistics are being calculated using this helper.
///
public T Device
{
get
{
return m_device;
}
}
///
/// Gets or sets the number of measurements expected to be received from the device per second.
///
public int ExpectedMeasurementsPerSecond
{
get
{
return m_expectedMeasurementsPerSecond;
}
set
{
m_expectedMeasurementsPerSecond = value;
}
}
#endregion
#region [ Methods ]
///
/// Increases the count of the number of measurements received from the device.
///
/// The number of measurements received from the device since the last time this method was called.
///
/// Call this each time measurements have been received by the device in order
/// to properly track the statistic.
///
public void AddToMeasurementsReceived(int count)
{
Interlocked.Add(ref m_measurementsInSecond, count);
}
///
/// Updates the statistics for the number of measurements received and the number
/// of measurements expected in the wrapped by this helper.
///
///
/// Call this periodically, preferably on a slow timer (e.g., once per second),
/// in order to update the and
/// statistics.
///
public void Update()
{
Update(DateTime.UtcNow.Ticks);
}
///
/// Updates the statistics for the number of measurements received and the number
/// of measurements expected in the wrapped by this helper.
///
/// The current time, in ticks.
///
/// Call this periodically, preferably on a slow timer (e.g., once per second),
/// in order to update the and
/// statistics. This method is preferred
/// when tracking statistics for multiple s to reduce the number
/// of calls to .
///
public void Update(long nowTicks)
{
int measurementsInSecond = Interlocked.Exchange(ref m_measurementsInSecond, 0);
long expectedMeasurementsInSecond;
double diff;
if (m_lastUpdateTicks > 0L)
{
diff = (nowTicks - m_lastUpdateTicks) / (double)Ticks.PerSecond;
m_expectedMeasurements += diff * m_expectedMeasurementsPerSecond;
expectedMeasurementsInSecond = (long)m_expectedMeasurements;
m_device.MeasurementsReceived += measurementsInSecond;
m_device.MeasurementsExpected += expectedMeasurementsInSecond;
m_expectedMeasurements -= expectedMeasurementsInSecond;
}
m_lastUpdateTicks = nowTicks;
}
///
/// Resets the member variables used to track
/// statistics for the device wrapped by this helper.
///
///
/// Call this when the connection to a device has been
/// reset to mark a starting point for statistics gathering.
/// Since the method keeps track
/// of the time it was last called, it is necessary to call
/// this method during long periods of downtime where Update
/// was not being called in order to avoid momentary, unexpectedly
/// large values to be calculated for measurements expected.
///
public void Reset()
{
Reset(DateTime.UtcNow.Ticks);
}
///
/// Resets the member variables used to track
/// statistics for the device wrapped by this helper.
///
/// The current time, in ticks.
///
/// Call this when the connection to a device has been
/// reset to mark a starting point for statistics gathering.
/// Since the method keeps track
/// of the time it was last called, it is necessary to call
/// this method during long periods of downtime where Update
/// was not being called in order to avoid momentary, unexpectedly
/// large values to be calculated for measurements expected.
/// This method is preferred when tracking statistics for multiple
/// s to reduce the number of calls to
/// .
///
public void Reset(long nowTicks)
{
Interlocked.Exchange(ref m_measurementsInSecond, 0);
m_expectedMeasurements = 0.0D;
m_lastUpdateTicks = nowTicks;
}
#endregion
}
}