//******************************************************************************************************
// StatisticsReader.cs - Gbtc
//
// Copyright © 2012, 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:
// ----------------------------------------------------------------------------------------------------
// 12/14/2010 - Stephen C. Wills
// Generated original version of source code.
// 12/13/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using GSF.Historian;
using GSF.Historian.Files;
namespace StatHistorianReportGenerator
{
///
/// Reads certain device and input stream statistics from the statistics archive.
/// The statistics that are read are the total frame count, the missing frame count,
/// connectivity, average latency, the actual data rate, data quality errors, and
/// time quality errors.
///
public class StatisticsReader : IDisposable
{
#region [ Members ]
// Fields
private ArchiveReader m_archiveReader;
private List m_metadataRecords;
private string m_archiveFilePath;
private DateTime m_startTime;
private DateTime m_endTime;
#endregion
#region [ Properties ]
///
/// Metadata records scanned from the metadata file.
///
public IReadOnlyCollection MetadataRecords
{
get
{
return m_metadataRecords.AsReadOnly();
}
}
///
/// Path to the archive file (*_archive.d).
///
public string ArchiveFilePath
{
get
{
return m_archiveFilePath;
}
set
{
m_archiveFilePath = value;
}
}
///
/// The timestamp at which to start gathering statistics from the archive.
///
public DateTime StartTime
{
get
{
return m_startTime;
}
set
{
m_startTime = value;
}
}
///
/// The timestamp at which to stop gathering statistics from the archive.
///
public DateTime EndTime
{
get
{
return m_endTime;
}
set
{
m_endTime = value;
}
}
#endregion
#region [ Methods ]
///
/// Causes the to open the archive file and retrieve the statistics.
///
public void Open()
{
m_archiveReader = new ArchiveReader();
m_archiveReader.Open(ArchiveFilePath);
m_metadataRecords = m_archiveReader.MetadataFile.Read()
.Where(record => !string.IsNullOrEmpty(record.Name))
.ToList();
}
///
/// Reads data for a specified statistic.
///
/// The suffix that defines the category of the statistic.
/// The index that defines which statistic in the given category is to be retrieved.
/// A lookup table containing a collection of data from the historian for each metadata record found with the given statistic defined.
///
/// All parameters--, , and --must
/// be set manually before attempting to read statistics from the archive.
///
public Dictionary> Read(string statisticSuffix, int signalIndex)
{
string signalReferenceEnding = string.Format("!{0}-ST{1}", statisticSuffix, signalIndex);
return m_metadataRecords
.Where(record => record.Synonym1.EndsWith(signalReferenceEnding))
.ToDictionary(record => record, record => m_archiveReader.ReadData(record.HistorianID, StartTime, EndTime));
}
///
/// Reads data for a group of historian IDs.
///
/// The historian IDs.
/// Collection of data points read from the archive.
///
/// All parameters--, , and --must
/// be set manually before attempting to read statistics from the archive.
///
public IEnumerable Read(IEnumerable historianIDs)
{
return m_archiveReader.ReadData(historianIDs, StartTime, EndTime);
}
///
/// Closes the archive file once the statistics are no longer needed.
///
public void Close()
{
if ((object)m_archiveReader == null)
return;
m_archiveReader.Dispose();
m_archiveReader = null;
m_metadataRecords = null;
}
///
/// Releases all the resources used by the object.
///
public void Dispose()
{
Close();
GC.SuppressFinalize(this);
}
#endregion
}
}