//******************************************************************************************************
// RealTimeStatistic.cs - Gbtc
//
// Copyright © 2010, 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:
// ----------------------------------------------------------------------------------------------------
// 09/28/2011 - Mehulbhai P Thakkar
// Generated original version of source code.
//
//******************************************************************************************************
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Windows.Media;
using GSF.Data;
using GSF.TimeSeries.Statistics;
using GSF.TimeSeries.UI;
namespace GSF.PhasorProtocols.UI.DataModels
{
///
/// Represents collection of statistics for the configured devices.
///
public class RealTimeStatistic : DataModelBase
{
#region [ Members ]
// Fields
private string m_sourceType;
private bool m_expanded;
private ObservableCollection m_streamStatisticList;
#endregion
#region [ Properties ]
///
/// Gets or sets 's SourceType.
///
public string SourceType
{
get
{
return m_sourceType;
}
set
{
m_sourceType = value;
OnPropertyChanged("SourceType");
}
}
///
/// Gets or sets 's SourceType.
///
public bool Expanded
{
get
{
return m_expanded;
}
set
{
m_expanded = value;
OnPropertyChanged("Expanded");
}
}
///
/// Gets or sets 's SourceType.
///
public ObservableCollection StreamStatisticList
{
get
{
return m_streamStatisticList;
}
set
{
m_streamStatisticList = value;
OnPropertyChanged("StreamStatisticList");
}
}
#endregion
#region [ Methods ]
// Static
///
/// Creates type collection of .
///
/// to connection to database.
/// Collection of .
public static ObservableCollection Load(AdoDataConnection database)
{
bool createdConnection = false;
try
{
createdConnection = CreateConnection(ref database);
ObservableCollection realTimeStatisticList = new();
DataSet resultSet = new();
resultSet.EnforceConstraints = false;
// Get PDCs and directly connected devices.
resultSet.Tables.Add(database.Connection.RetrieveData(database.AdapterType, database.ParameterizedQueryString("SELECT ID, Acronym, Name FROM DeviceDetail " +
"WHERE NodeID = {0} AND (IsConcentrator = {1} OR ParentAcronym = {2} OR ParentAcronym IS NULL) AND Enabled = {3} ORDER BY Acronym", "nodeID", "isConcentrator", "parentAcronym", "enabled"),
DefaultTimeout, database.CurrentNodeID(), database.Bool(true), string.Empty, database.Bool(true)).Copy());
resultSet.Tables[0].TableName = "DirectDevices";
// Get all the devices connected via PDC.
resultSet.Tables.Add(database.Connection.RetrieveData(database.AdapterType, database.ParameterizedQueryString("SELECT ID, Acronym, Name, ParentID, " +
"ParentAcronym FROM DeviceDetail WHERE NodeID = {0} AND IsConcentrator = {1} AND Enabled = {2} AND ParentID > 0 ORDER BY Acronym", "nodeID", "isConcentrator", "enabled"),
DefaultTimeout, database.CurrentNodeID(), database.Bool(false), database.Bool(true)).Copy());
resultSet.Tables[1].TableName = "PdcDevices";
// Get output stream information.
resultSet.Tables.Add(database.Connection.RetrieveData(database.AdapterType, database.ParameterizedQueryString("SELECT ID, Acronym, Name FROM OutputStream WHERE " +
"NodeID = {0} AND Enabled = {1} ORDER BY Acronym", "nodeID", "enabled"), DefaultTimeout, database.CurrentNodeID(), database.Bool(true)).Copy());
resultSet.Tables[2].TableName = "OutputStreams";
resultSet.Tables.Add(database.Connection.RetrieveData(database.AdapterType, database.ParameterizedQueryString("SELECT ID, AdapterName FROM CustomActionAdapter WHERE " +
"NodeID = {0} AND TypeName = {1} AND Enabled = {2} ORDER BY AdapterName", "nodeID", "typeName", "enabled"), DefaultTimeout, database.CurrentNodeID(), "GSF.TimeSeries.Transport.DataPublisher", database.Bool(true)).Copy());
resultSet.Tables[3].TableName = "DataPublishers";
// Get list of statistic measurements detail.
ObservableCollection statisticMeasurements = GetStatisticMeasurements(database);
bool expanded = (statisticMeasurements.Count < 100);
// We do this for later use in refreshing data.
StatisticMeasurements = new Dictionary();
foreach (StatisticMeasurement statisticMeasurement in statisticMeasurements)
StatisticMeasurements.Add(statisticMeasurement.SignalID, statisticMeasurement);
// Create a system statistics list.
ObservableCollection systemStatistics = new();
systemStatistics.Add(new StreamStatistic()
{
ID = 0,
Acronym = "SYSTEM",
Name = "System",
StatusColor = "Green",
Expanded = expanded,
StatisticMeasurementList = new ObservableCollection(statisticMeasurements.Where(sm => sm.SignalReference.Contains("!SYSTEM"))),
DeviceStatisticList = new ObservableCollection()
});
SystemStatistics = new Dictionary();
foreach (StreamStatistic streamStatistic in systemStatistics)
{
// We do this to associate statistic measurement to parent output stream easily.
foreach (StatisticMeasurement measurement in streamStatistic.StatisticMeasurementList)
measurement.DeviceID = streamStatistic.ID;
streamStatistic.DeviceStatisticList.Insert(0, new PdcDeviceStatistic()
{
DeviceID = 0,
DeviceAcronym = "Run-time Statistics",
DeviceName = "",
Expanded = expanded,
StatisticMeasurementList = streamStatistic.StatisticMeasurementList
});
streamStatistic.StatisticMeasurementList = null;
// We do this for later use in refreshing data.
SystemStatistics.Add(streamStatistic.ID, streamStatistic);
}
// Create an input stream statistics list.
ObservableCollection inputStreamStatistics = new
(
from stream in resultSet.Tables["DirectDevices"].AsEnumerable()
select new StreamStatistic
{
ID = Convert.ToInt32(stream.Field