public static AdoDataConnection GetDatabaseConnection() { string connectionString = s_configurationString; string dataProviderString = s_dataProviderString; if (string.IsNullOrWhiteSpace(connectionString) || string.IsNullOrWhiteSpace(dataProviderString)) { XDocument serviceConfig = XDocument.Load(ArchiveLocator.GetConfigurationFileName()); connectionString = serviceConfig .Descendants("systemSettings") .SelectMany(systemSettings => systemSettings.Elements("add")) .Where(element => "ConnectionString".Equals((string)element.Attribute("name"), StringComparison.OrdinalIgnoreCase)) .Select(element => (string)element.Attribute("value")) .FirstOrDefault(); dataProviderString = serviceConfig .Descendants("systemSettings") .SelectMany(systemSettings => systemSettings.Elements("add")) .Where(element => "DataProviderString".Equals((string)element.Attribute("name"), StringComparison.OrdinalIgnoreCase)) .Select(element => (string)element.Attribute("value")) .FirstOrDefault(); } if (string.IsNullOrWhiteSpace(connectionString) || string.IsNullOrWhiteSpace(dataProviderString)) { return(null); } return(new AdoDataConnection(connectionString, dataProviderString)); }
// Reads statistics from the archive and stores them in a format that can // be easily manipulated to determine which devices belong in which levels. private void ReadStatistics() { ArchiveLocator locator; DateTime startTime; DateTime endTime; Dictionary<int, Aggregate> aggregateLookup; Aggregate currentAggregate; int currentHistorianID; Dictionary<string, DeviceStats> deviceStatsLookup; DeviceStats deviceStats; SignalStats signalStats; string signalName; int index; deviceStatsLookup = new Dictionary<string, DeviceStats>(); // Create the statistics reader for reading statistics from the archive using (StatisticsReader statisticsReader = new StatisticsReader()) { // Create the archive locator to // determine the location of the archive locator = new ArchiveLocator() { ArchiveLocation = m_archiveLocation, ArchiveLocationName = "Statistics", ArchiveName = "STAT" }; endTime = m_reportDate.ToUniversalTime() + TimeSpan.FromDays(1); startTime = endTime - TimeSpan.FromDays(ReportDays); // Set up and open the statistics reader statisticsReader.StartTime = startTime; statisticsReader.EndTime = endTime; statisticsReader.ArchiveFilePath = locator.ArchiveFilePath; statisticsReader.Open(); // Create lookup tables for each aggregateLookup = new Dictionary<int, Aggregate>(); foreach (MetadataRecord record in statisticsReader.MetadataRecords) { if (IsDesiredDeviceStat(record.Synonym1)) { deviceStats = deviceStatsLookup.GetOrAdd(GetDeviceNameForStat(record.Synonym1), name => new DeviceStats() { Name = name, MeasurementsExpected = new Aggregate(ReportDays), MeasurementsReceived = new Aggregate(ReportDays), SignalStatsLookup = new Dictionary<string, SignalStats>() }); if (record.Synonym1.EndsWith("!PMU-ST5", StringComparison.Ordinal)) aggregateLookup[record.HistorianID] = deviceStats.MeasurementsExpected; else if (record.Synonym1.EndsWith("!PMU-ST4", StringComparison.Ordinal)) aggregateLookup[record.HistorianID] = deviceStats.MeasurementsReceived; } else if (IsDesiredSignalStat(record.Synonym1)) { signalName = GetSignalName(record.Synonym1); deviceStats = deviceStatsLookup.GetOrAdd(GetDeviceNameForSignal(signalName), name => new DeviceStats() { Name = name, MeasurementsExpected = new Aggregate(ReportDays), MeasurementsReceived = new Aggregate(ReportDays), SignalStatsLookup = new Dictionary<string, SignalStats>() }); signalStats = deviceStats.SignalStatsLookup.GetOrAdd(signalName, name => new SignalStats() { Name = name, MeasurementsLatched = new Aggregate(ReportDays), MeasurementsUnreasonable = new Aggregate(ReportDays) }); if (record.Synonym1.StartsWith("980!")) aggregateLookup[record.HistorianID] = signalStats.MeasurementsLatched; else if (record.Synonym1.StartsWith("900!")) aggregateLookup[record.HistorianID] = signalStats.MeasurementsUnreasonable; } } currentAggregate = null; currentHistorianID = -1; foreach (IDataPoint dataPoint in statisticsReader.Read(aggregateLookup.Keys)) { index = (dataPoint.Time.ToDateTime() - startTime).Days; if (index < 0 || index >= ReportDays) continue; if (dataPoint.HistorianID != currentHistorianID) { aggregateLookup.TryGetValue(dataPoint.HistorianID, out currentAggregate); currentHistorianID = dataPoint.HistorianID; } if ((object)currentAggregate != null) currentAggregate.Add(index, dataPoint.Value); } } // Store the statistics data to be used in the report m_deviceStatsList = deviceStatsLookup.Values.ToList(); }
// Reads statistics from the archive and stores them in a format that can // be easily manipulated to determine which devices belong in which levels. private void ReadStatistics() { Dictionary<string, DeviceStats> deviceStatsLookup = new Dictionary<string, DeviceStats>(); DateTime startTime; DateTime endTime; ArchiveLocator locator; Dictionary<MetadataRecord, IEnumerable<IDataPoint>> measurementsReceived; Dictionary<MetadataRecord, IEnumerable<IDataPoint>> measurementsExpected; Dictionary<MetadataRecord, IEnumerable<IDataPoint>> dataQualityErrors; Dictionary<MetadataRecord, IEnumerable<IDataPoint>> timeQualityErrors; MetadataRecord dataQualityRecord; MetadataRecord timeQualityRecord; DeviceStats deviceStats; string signalReference; string deviceName; int index; // Create the statistics reader for reading statistics from the archive using (StatisticsReader statisticsReader = new StatisticsReader()) { // Create the archive locator to // determine the location of the archive locator = new ArchiveLocator() { ArchiveLocation = m_archiveLocation, ArchiveLocationName = "Statistics", ArchiveName = "STAT" }; endTime = m_reportDate + TimeSpan.FromDays(1); startTime = endTime - TimeSpan.FromDays(ReportDays); // Set up and open the statistics reader statisticsReader.StartTime = startTime; statisticsReader.EndTime = endTime; statisticsReader.ArchiveFilePath = locator.ArchiveFilePath; statisticsReader.Open(); measurementsReceived = statisticsReader.Read("PMU", 4); measurementsExpected = statisticsReader.Read("PMU", 5); dataQualityErrors = statisticsReader.Read("PMU", 1); timeQualityErrors = statisticsReader.Read("PMU", 2); // Determine which devices in the archive have stats for both measurements received and measurements expected foreach (Tuple<MetadataRecord, MetadataRecord> tuple in measurementsReceived.Keys.Join(measurementsExpected.Keys, GetDeviceName, GetDeviceName, Tuple.Create)) { signalReference = tuple.Item1.Synonym1; deviceName = GetDeviceName(tuple.Item1); // Ignore statistics that were calculated by an intermediate gateway if (!signalReference.StartsWith("LOCAL$") && signalReference.Contains("LOCAL$")) continue; // Make sure LOCAL$ statistics take precedence over other statistics calculated for the same device if (deviceStatsLookup.ContainsKey(deviceName) && !signalReference.StartsWith("LOCAL$")) continue; dataQualityRecord = dataQualityErrors.Keys.FirstOrDefault(record => GetDeviceName(record) == deviceName); timeQualityRecord = timeQualityErrors.Keys.FirstOrDefault(record => GetDeviceName(record) == deviceName); // Create arrays to hold the total sum of the stats for each day being reported deviceStats = new DeviceStats() { Name = deviceName, DataQualityErrors = new double[ReportDays], TimeQualityErrors = new double[ReportDays], MeasurementsReceived = new double[ReportDays], MeasurementsExpected = new double[ReportDays] }; if ((object)dataQualityRecord != null) { // Calculate the total data quality errors for each day being reported foreach (IDataPoint dataPoint in dataQualityErrors[dataQualityRecord]) { index = (dataPoint.Time.ToDateTime() - startTime).Days; if (index >= 0 && index < ReportDays) deviceStats.DataQualityErrors[index] += dataPoint.Value; } } if ((object)timeQualityRecord != null) { // Calculate the total time quality errors for each day being reported foreach (IDataPoint dataPoint in timeQualityErrors[timeQualityRecord]) { index = (dataPoint.Time.ToDateTime() - startTime).Days; if (index >= 0 && index < ReportDays) deviceStats.TimeQualityErrors[index] += dataPoint.Value; } } // Calculate the total measurements received for each day being reported foreach (IDataPoint dataPoint in measurementsReceived[tuple.Item1]) { index = (dataPoint.Time.ToDateTime() - startTime).Days; if (index >= 0 && index < ReportDays) deviceStats.MeasurementsReceived[index] += dataPoint.Value; } // Calculate the total measurements expected for each day being reported foreach (IDataPoint dataPoint in measurementsExpected[tuple.Item2]) { index = (dataPoint.Time.ToDateTime() - startTime).Days; if (index >= 0 && index < ReportDays) deviceStats.MeasurementsExpected[index] += dataPoint.Value; } // Store the calculated stats per device deviceStatsLookup[deviceName] = deviceStats; } } // Store the statistics data to be used in the report m_deviceStatsList = deviceStatsLookup.Values.ToList(); }
// Reads statistics from the archive and stores them in a format that can // be easily manipulated to determine which devices belong in which levels. private void ReadStatistics() { ArchiveLocator locator; DateTime startTime; DateTime endTime; Dictionary <int, Aggregate> aggregateLookup; Aggregate currentAggregate; int currentHistorianID; Dictionary <string, DeviceStats> deviceStatsLookup; DeviceStats deviceStats; SignalStats signalStats; string signalName; int index; deviceStatsLookup = new Dictionary <string, DeviceStats>(); // Create the statistics reader for reading statistics from the archive using (StatisticsReader statisticsReader = new StatisticsReader()) { // Create the archive locator to // determine the location of the archive locator = new ArchiveLocator() { ArchiveLocation = m_archiveLocation, ArchiveLocationName = "Statistics", ArchiveName = "STAT" }; endTime = m_reportDate.ToUniversalTime() + TimeSpan.FromDays(1); startTime = endTime - TimeSpan.FromDays(ReportDays); // Set up and open the statistics reader statisticsReader.StartTime = startTime; statisticsReader.EndTime = endTime; statisticsReader.ArchiveFilePath = locator.ArchiveFilePath; statisticsReader.Open(); // Create lookup tables for each aggregateLookup = new Dictionary <int, Aggregate>(); foreach (MetadataRecord record in statisticsReader.MetadataRecords) { if (IsDesiredDeviceStat(record.Synonym1)) { deviceStats = deviceStatsLookup.GetOrAdd(GetDeviceNameForStat(record.Synonym1), name => new DeviceStats() { Name = name, MeasurementsExpected = new Aggregate(ReportDays), MeasurementsReceived = new Aggregate(ReportDays), SignalStatsLookup = new Dictionary <string, SignalStats>() }); if (record.Synonym1.EndsWith("!PMU-ST5", StringComparison.Ordinal)) { aggregateLookup[record.HistorianID] = deviceStats.MeasurementsExpected; } else if (record.Synonym1.EndsWith("!PMU-ST4", StringComparison.Ordinal)) { aggregateLookup[record.HistorianID] = deviceStats.MeasurementsReceived; } } else if (IsDesiredSignalStat(record.Synonym1)) { signalName = GetSignalName(record.Synonym1); deviceStats = deviceStatsLookup.GetOrAdd(GetDeviceNameForSignal(signalName), name => new DeviceStats() { Name = name, MeasurementsExpected = new Aggregate(ReportDays), MeasurementsReceived = new Aggregate(ReportDays), SignalStatsLookup = new Dictionary <string, SignalStats>() }); signalStats = deviceStats.SignalStatsLookup.GetOrAdd(signalName, name => new SignalStats() { Name = name, MeasurementsLatched = new Aggregate(ReportDays), MeasurementsUnreasonable = new Aggregate(ReportDays) }); if (record.Synonym1.StartsWith("980!")) { aggregateLookup[record.HistorianID] = signalStats.MeasurementsLatched; } else if (record.Synonym1.StartsWith("900!")) { aggregateLookup[record.HistorianID] = signalStats.MeasurementsUnreasonable; } } } currentAggregate = null; currentHistorianID = -1; foreach (IDataPoint dataPoint in statisticsReader.Read(aggregateLookup.Keys)) { index = (dataPoint.Time.ToDateTime() - startTime).Days; if (index < 0 || index >= ReportDays) { continue; } if (dataPoint.HistorianID != currentHistorianID) { aggregateLookup.TryGetValue(dataPoint.HistorianID, out currentAggregate); currentHistorianID = dataPoint.HistorianID; } if ((object)currentAggregate != null) { currentAggregate.Add(index, dataPoint.Value); } } } // Store the statistics data to be used in the report m_deviceStatsList = deviceStatsLookup.Values.ToList(); }