/// <summary> /// Read historian data from server. /// </summary> /// <param name="connection">openHistorian connection.</param> /// <param name="startTime">Start time of query.</param> /// <param name="stopTime">Stop time of query.</param> /// <param name="measurementIDs">Array of measurement IDs to query - or <c>null</c> for all available points.</param> /// <param name="resolution">Resolution for data query.</param> /// <returns>Enumeration of <see cref="IMeasurement"/> values read for time range.</returns> /// <remarks> /// <example> /// <code> /// using (var connection = new Connection("127.0.0.1", "PPA")) /// foreach(var measurement in GetHistorianData(connection, DateTime.UtcNow.AddMinutes(-1.0D), DateTime.UtcNow)) /// Console.WriteLine("{0}:{1} @ {2} = {3}, quality: {4}", measurement.Key.Source, measurement.Key.ID, measurement.Timestamp, measurement.Value, measurement.StateFlags); /// </code> /// </example> /// </remarks> public static IEnumerable <IMeasurement> GetHistorianData(Connection connection, DateTime startTime, DateTime stopTime, IEnumerable <ulong> measurementIDs = null, Resolution resolution = Resolution.Full) { SeekFilterBase <HistorianKey> timeFilter; MatchFilterBase <HistorianKey, HistorianValue> pointFilter = null; HistorianKey key = new HistorianKey(); HistorianValue value = new HistorianValue(); // Set data scan resolution if (resolution == Resolution.Full) { timeFilter = TimestampSeekFilter.CreateFromRange <HistorianKey>(startTime, stopTime); } else { TimeSpan resolutionInterval = resolution.GetInterval(); BaselineTimeInterval interval = BaselineTimeInterval.Second; if (resolutionInterval.Ticks < Ticks.PerMinute) { interval = BaselineTimeInterval.Second; } else if (resolutionInterval.Ticks < Ticks.PerHour) { interval = BaselineTimeInterval.Minute; } else if (resolutionInterval.Ticks == Ticks.PerHour) { interval = BaselineTimeInterval.Hour; } startTime = startTime.BaselinedTimestamp(interval); stopTime = stopTime.BaselinedTimestamp(interval); timeFilter = TimestampSeekFilter.CreateFromIntervalData <HistorianKey>(startTime, stopTime, resolutionInterval, new TimeSpan(TimeSpan.TicksPerMillisecond)); } // Setup point ID selections if (measurementIDs != null) { pointFilter = PointIdMatchFilter.CreateFromList <HistorianKey, HistorianValue>(measurementIDs); } // Start stream reader for the provided time window and selected points using (Database database = connection.OpenDatabase()) { TreeStream <HistorianKey, HistorianValue> stream = database.Read(SortedTreeEngineReaderOptions.Default, timeFilter, pointFilter); while (stream.Read(key, value)) { yield return new Measurement { Metadata = MeasurementKey.LookUpOrCreate(connection.InstanceName, (uint)key.PointID).Metadata, Timestamp = key.TimestampAsDate, Value = value.AsSingle, StateFlags = (MeasurementStateFlags)value.Value3 } } ; } }
private IEnumerable <TrendingDataPoint> Read(IEnumerable <ulong> measurementIDs, DateTime startTime, DateTime stopTime) { SeekFilterBase <HistorianKey> timeFilter = TimestampSeekFilter.CreateFromRange <HistorianKey>(startTime, stopTime); MatchFilterBase <HistorianKey, HistorianValue> pointFilter = null; HistorianKey key = new HistorianKey(); HistorianValue value = new HistorianValue(); if ((object)measurementIDs != null) { pointFilter = PointIdMatchFilter.CreateFromList <HistorianKey, HistorianValue>(measurementIDs); } // Start stream reader for the provided time window and selected points using (TreeStream <HistorianKey, HistorianValue> stream = m_database.Read(SortedTreeEngineReaderOptions.Default, timeFilter, pointFilter)) { m_treeStreams.Add(stream); while (stream.Read(key, value)) { yield return(new TrendingDataPoint() { ChannelID = (int)key.PointID.HighDoubleWord(), SeriesID = (SeriesID)(int)key.PointID.LowDoubleWord(), Timestamp = key.TimestampAsDate, Value = value.AsSingle }); } } }
/// <summary> /// Adds all of the items in the stream to this tree. Skips any dulpicate entries. /// </summary> /// <param name="stream">the stream to add.</param> public void TryAddRange(TreeStream <TKey, TValue> stream) { //TKey key = new TKey(); //TValue value = new TValue(); //while (stream.Read(key, value)) //{ // TryAdd(key, value); //} //return; InsertStreamHelper <TKey, TValue> helper = new InsertStreamHelper <TKey, TValue>(stream); LeafStorage.TryInsertSequentialStream(helper); while (helper.IsValid) { if (helper.IsKVP1) { LeafStorage.TryInsert(helper.Key1, helper.Value1); } else { LeafStorage.TryInsert(helper.Key2, helper.Value2); } helper.NextDoNotCheckSequential(); } if (IsDirty && AutoFlush) { m_header.SaveHeader(Stream); } }
/// <summary> /// Gets concentrated frames from the provided stream /// </summary> /// <param name="stream">the database to use</param> /// <returns></returns> public static SortedList <DateTime, FrameData> GetFrames(this TreeStream <HistorianKey, HistorianValue> stream) { SortedList <DateTime, FrameDataConstructor> results = new SortedList <DateTime, FrameDataConstructor>(); ulong lastTime = ulong.MinValue; FrameDataConstructor lastFrame = null; HistorianKey key = new HistorianKey(); HistorianValue value = new HistorianValue(); while (stream.Read(key, value)) { if (lastFrame == null || key.Timestamp != lastTime) { lastTime = key.Timestamp; DateTime timestamp = new DateTime((long)lastTime); if (!results.TryGetValue(timestamp, out lastFrame)) { lastFrame = new FrameDataConstructor(); results.Add(timestamp, lastFrame); } } lastFrame.PointId.Add(key.PointID); lastFrame.Values.Add(value.ToStruct()); } List <FrameData> data = new List <FrameData>(results.Count); data.AddRange(results.Values.Select(x => x.ToFrameData())); return(SortedListConstructor.Create(results.Keys, data)); }
public void TestReadPoints() { Stopwatch sw = new Stopwatch(); int pointCount = 0; HistorianKey key = new HistorianKey(); HistorianValue value = new HistorianValue(); HistorianServerDatabaseConfig settings = new HistorianServerDatabaseConfig("PPA", @"C:\Program Files\openHistorian\Archive\", true); using (HistorianServer server = new HistorianServer(settings)) { using (HistorianClient client = new HistorianClient("127.0.0.1", 12345)) using (ClientDatabaseBase <HistorianKey, HistorianValue> database = client.GetDatabase <HistorianKey, HistorianValue>(String.Empty)) { TreeStream <HistorianKey, HistorianValue> stream = database.Read(0, (ulong)DateTime.MaxValue.Ticks, new ulong[] { 1 }); while (stream.Read(key, value)) { ; } sw.Start(); stream = database.Read(0, (ulong)DateTime.MaxValue.Ticks, new ulong[] { 65, 953, 5562 }); while (stream.Read(key, value)) { pointCount++; } sw.Stop(); } } Console.WriteLine(pointCount); Console.WriteLine(sw.Elapsed.TotalSeconds.ToString()); }
public bool ScanToSnapDBPoint(ulong timestamp, ulong pointID, DataPoint point) { if ((object)m_stream != null) { m_stream.Dispose(); } // Start read at provided timestamp m_stream = m_database.Read(timestamp, ulong.MaxValue); bool success = true; // Scan to desired point do { if (!m_stream.Read(m_key, m_value)) { success = false; break; } }while (m_key.PointID != pointID); point.Timestamp = m_key.Timestamp; point.PointID = m_key.PointID; point.Value = m_value.Value1; point.Flags = m_value.Value3; return(success); }
void ScannerThread() { int threadId = Interlocked.Increment(ref ThreadNumber); try { //DateTime start = DateTime.FromBinary(Convert.ToDateTime("2/1/2014").Date.Ticks + Convert.ToDateTime("6:00:00PM").TimeOfDay.Ticks).ToUniversalTime(); while (!StopReading) { Stopwatch sw = new Stopwatch(); using (HistorianClient client = new HistorianClient("127.0.0.1", 12345)) using (ClientDatabaseBase <HistorianKey, HistorianValue> database = client.GetDatabase <HistorianKey, HistorianValue>(String.Empty)) { HistorianKey key = new HistorianKey(); HistorianValue value = new HistorianValue(); sw.Start(); TreeStream <HistorianKey, HistorianValue> scan = database.Read(0, ulong.MaxValue, new ulong[] { 65, 953, 5562 }); while (scan.Read(key, value)) { ; } sw.Stop(); } //Console.WriteLine("Thread: " + threadId.ToString() + " " + "Run Number: " + myId.ToString() + " " + (pointCount / sw.Elapsed.TotalSeconds / 1000000).ToString()); } } catch (Exception) { //Console.WriteLine(ex.ToString()); } Console.WriteLine("Thread: " + threadId.ToString() + " Quit"); }
/// <summary> /// Adds all of the points to this archive file. /// </summary> /// <param name="stream"></param> public override void AddPoints(TreeStream <TKey, TValue> stream) { if (m_disposed) { throw new ObjectDisposedException(GetType().FullName); } m_tree.TryAddRange(stream); }
/// <summary> /// Writes the tree stream to the database. /// </summary> /// <param name="stream">all of the key/value pairs to add to the database.</param> public override void Write(TreeStream <TKey, TValue> stream) { if (m_disposed) { throw new ObjectDisposedException(GetType().FullName); } m_server.Write(stream); }
/// <summary> /// Adds all of the points in the stream to the Tree /// </summary> /// <param name="stream">stream to add</param> public void AddRange(TreeStream <TKey, TValue> stream) { TKey key = new TKey(); TValue value = new TValue(); while (stream.Read(key, value)) { Add(key, value); } }
private void Write(TreeStream <TKey, TValue> stream) { TKey key = new TKey(); TValue value = new TValue(); //ToDo: Prebuffer the points in the stream. It is possible that this call may be behind a slow socket interface, therefore it will lockup the writing speed. while (stream.Read(key, value)) { Write(key, value); } }
public TreeStreamSequential(TreeStream <TKey, TValue> baseStream) { m_isEndOfStream = false; m_baseStream = baseStream; IsValid = false; CurrentKey = new TKey(); CurrentValue = new TValue(); m_baseStreamCurrentKey = new TKey(); m_baseStreamCurrentValue = new TValue(); m_baseStreamIsValid = false; }
public SnapDBClient(string hostAddress, int port, string instanceName, ulong startTime, ulong endTime, int frameRate, IEnumerable <ulong> pointIDs) { m_client = new HistorianClient(hostAddress, port); m_database = m_client.GetDatabase <HistorianKey, HistorianValue>(instanceName); m_key = new HistorianKey(); m_value = new HistorianValue(); SeekFilterBase <HistorianKey> timeFilter = TimestampSeekFilter.CreateFromRange <HistorianKey>(DataPoint.RoundTimestamp(startTime, frameRate), DataPoint.RoundTimestamp(endTime, frameRate)); MatchFilterBase <HistorianKey, HistorianValue> pointFilter = PointIdMatchFilter.CreateFromList <HistorianKey, HistorianValue>(pointIDs); m_stream = m_database.Read(SortedTreeEngineReaderOptions.Default, timeFilter, pointFilter); }
/// <summary> /// Provides a thread safe way to enqueue points. /// While points are streaming all other writes are blocked. Therefore, /// this point stream should be high speed. /// </summary> /// <param name="stream"></param> public void Enqueue(TreeStream <HistorianKey, HistorianValue> stream) { lock (m_syncWrite) { PointData data = default(PointData); while (data.Load(stream)) { m_blocks.Enqueue(data); } } m_worker.Start(); }
public bool FindSnapDBPoint(DataPoint point) { using (TreeStream <HistorianKey, HistorianValue> stream = m_database.ReadSingleValue(point.Timestamp, point.PointID)) { if (stream.Read(m_key, m_value)) { return(m_key.MillisecondTimestamp == point.Timestamp && m_key.PointID == point.PointID); } } return(false); }
public static IEnumerable GetHistorianDataSampled(SqlString historianServer, SqlString instanceName, DateTime startTime, DateTime stopTime, TimeSpan interval, [SqlFacet(MaxSize = -1)] SqlString measurementIDs) { const int DefaultHistorianPort = 38402; if (historianServer.IsNull || string.IsNullOrEmpty(historianServer.Value)) { throw new ArgumentNullException("historianServer", "Missing historian server parameter"); } if (instanceName.IsNull || string.IsNullOrEmpty(instanceName.Value)) { throw new ArgumentNullException("instanceName", "Missing historian instance name parameter"); } if (startTime > stopTime) { throw new ArgumentException("Invalid time range specified", "startTime"); } string[] parts = historianServer.Value.Split(':'); string hostName = parts[0]; int port; if (parts.Length < 2 || !int.TryParse(parts[1], out port)) { port = DefaultHistorianPort; } using (HistorianClient client = new HistorianClient(hostName, port)) using (ClientDatabaseBase <HistorianKey, HistorianValue> reader = client.GetDatabase <HistorianKey, HistorianValue>(instanceName.Value)) { var timeFilter = interval.Ticks == 0 ? TimestampSeekFilter.CreateFromRange <HistorianKey>(startTime, stopTime) : TimestampSeekFilter.CreateFromIntervalData <HistorianKey>(startTime, stopTime, interval, new TimeSpan(TimeSpan.TicksPerMillisecond)); MatchFilterBase <HistorianKey, HistorianValue> pointFilter = null; HistorianKey key = new HistorianKey(); HistorianValue value = new HistorianValue(); if (!measurementIDs.IsNull && !string.IsNullOrEmpty(measurementIDs.Value)) { pointFilter = PointIdMatchFilter.CreateFromList <HistorianKey, HistorianValue>(measurementIDs.Value.Split(',').Select(ulong.Parse)); } // Start stream reader for the provided time window and selected points using (TreeStream <HistorianKey, HistorianValue> stream = reader.Read(SortedTreeEngineReaderOptions.Default, timeFilter, pointFilter)) { while (stream.Read(key, value)) { yield return(new Measurement(key.PointID, key.TimestampAsDate, value.AsSingle)); } } } }
public SnapDBClient(string hostAddress, int port, string instanceName, ulong startTime, ulong endTime, int frameRate, IEnumerable<ulong> pointIDs) { m_client = new HistorianClient(hostAddress, port); m_database = m_client.GetDatabase<HistorianKey, HistorianValue>(instanceName); m_key = new HistorianKey(); m_value = new HistorianValue(); SeekFilterBase<HistorianKey> timeFilter = TimestampSeekFilter.CreateFromRange<HistorianKey>(DataPoint.RoundTimestamp(startTime, frameRate), DataPoint.RoundTimestamp(endTime, frameRate)); MatchFilterBase<HistorianKey, HistorianValue> pointFilter = PointIdMatchFilter.CreateFromList<HistorianKey, HistorianValue>(pointIDs); m_stream = m_database.Read(SortedTreeEngineReaderOptions.Default, timeFilter, pointFilter); }
/// <summary> /// Creates a new <see cref="ReportHistorianReader"/>. /// </summary> /// <param name="server">Snapserver to connect to <see cref="SnapServer"/>.</param> /// <param name="instanceName">Name of the instance to connect to.</param> /// <param name="startTime">Starttime.</param> /// <param name="endTime">Endtime.</param> /// <param name="frameRate">SamplingRate of the signal.</param> /// <param name="pointIDs">PointIDs to be collected.</param> public ReportHistorianReader(SnapServer server, string instanceName, DateTime startTime, DateTime endTime, int frameRate, IEnumerable <ulong> pointIDs) { m_client = SnapClient.Connect(server); m_database = m_client.GetDatabase <HistorianKey, HistorianValue>(instanceName); m_key = new HistorianKey(); m_value = new HistorianValue(); SeekFilterBase <HistorianKey> timeFilter = TimestampSeekFilter.CreateFromRange <HistorianKey>(DataPoint.RoundTimestamp(startTime, frameRate), DataPoint.RoundTimestamp(endTime, frameRate)); MatchFilterBase <HistorianKey, HistorianValue> pointFilter = PointIdMatchFilter.CreateFromList <HistorianKey, HistorianValue>(pointIDs); m_stream = m_database.Read(SortedTreeEngineReaderOptions.Default, timeFilter, pointFilter); }
/// <summary> /// Writes the provided stream to the engine. /// </summary> /// <param name="stream"></param> /// <returns>the transaction code so this write can be tracked.</returns> public long Write(TreeStream <TKey, TValue> stream) { long sequenceId = -1; TKey key = new TKey(); TValue value = new TValue(); while (stream.Read(key, value)) { sequenceId = m_prebuffer.Write(key, value); } return(sequenceId); }
public int[] MeasureBits(TreeStream<HistorianKey, HistorianValue> stream, int higherBits) { HistorianKey hkey = new HistorianKey(); HistorianValue hvalue = new HistorianValue(); int[] bucket = new int[1 << higherBits]; int shiftBits = 32 - higherBits; while (stream.Read(hkey,hvalue)) { uint value = (uint)hvalue.Value1 >> shiftBits; bucket[value]++; } return bucket; }
public void ReadData() { Logger.Console.Verbose = VerboseLevel.All; using (SnapServer server = CreateServer()) { using (SnapClient client = SnapClient.Connect(server)) using (ClientDatabaseBase <HistorianKey, HistorianValue> db = client.GetDatabase <HistorianKey, HistorianValue>("PPA")) using (TreeStream <HistorianKey, HistorianValue> stream = db.Read(null, null, null)) { Console.WriteLine(stream.Count()); } } }
/// <summary> /// Parses an entire stream to count the number of points. Notice, this will /// enumerate the list and the list will have to be reset to be enumerated again. /// </summary> /// <typeparam name="TKey">The key type</typeparam> /// <typeparam name="TValue">The value type</typeparam> /// <param name="stream">The stream to enumerate</param> /// <returns>the number of items in the stream.</returns> public static long Count <TKey, TValue>(this TreeStream <TKey, TValue> stream) where TKey : class, new() where TValue : class, new() { TKey key = new TKey(); TValue value = new TValue(); long cnt = 0; while (stream.Read(key, value)) { cnt++; } return(cnt); }
/// <summary> /// Queries the provided signals within a the provided time window [Inclusive] /// </summary> /// <param name="database"></param> /// <param name="startTime">the lower bound of the time</param> /// <param name="endTime">the upper bound of the time. [Inclusive]</param> /// <param name="signals">an IEnumerable of all of the signals to query as part of the results set.</param> /// <returns></returns> public static Dictionary <ulong, RawSignalTimeValue> GetRawSignals(this ClientDatabaseBase <HistorianKey, HistorianValue> database, DateTime startTime, DateTime endTime, IEnumerable <ulong> signals) { HistorianKey key = new HistorianKey(); HistorianValue value = new HistorianValue(); Dictionary <ulong, RawSignalTimeValue> results = signals.ToDictionary((x) => x, (x) => new RawSignalTimeValue()); TreeStream <HistorianKey, HistorianValue> stream = database.Read((ulong)startTime.Ticks, (ulong)endTime.Ticks, signals); while (stream.Read(key, value)) { results[key.PointID].Signals.Add(key.TimestampAsDate, value.ToStruct()); } return(results); }
public bool Load(TreeStream <HistorianKey, HistorianValue> stream) { HistorianKey key = new HistorianKey(); HistorianValue value = new HistorianValue(); if (stream.Read(key, value)) { Key1 = key.Timestamp; Key2 = key.PointID; Value1 = value.Value3; Value2 = value.Value1; return(true); } return(false); }
public int[] MeasureBits(TreeStream <HistorianKey, HistorianValue> stream, int higherBits) { HistorianKey hkey = new HistorianKey(); HistorianValue hvalue = new HistorianValue(); int[] bucket = new int[1 << higherBits]; int shiftBits = 32 - higherBits; while (stream.Read(hkey, hvalue)) { uint value = (uint)hvalue.Value1 >> shiftBits; bucket[value]++; } return(bucket); }
/// <summary> /// Writes the tree stream to the database. /// </summary> /// <param name="stream">all of the key/value pairs to add to the database.</param> public override void Write(TreeStream <TKey, TValue> stream) { if (m_reader != null) { throw new Exception("Sockets do not support writing while a reader is open. Dispose of reader."); } m_stream.Write((byte)ServerCommand.Write); m_encodingMode.ResetEncoder(); while (stream.Read(m_tmpKey, m_tmpValue)) { m_encodingMode.Encode(m_stream, m_tmpKey, m_tmpValue); } m_encodingMode.WriteEndOfStream(m_stream); m_stream.Flush(); }
public void TestReadData() { using (HistorianServer server = new HistorianServer(new HistorianServerDatabaseConfig("DB", @"c:\temp\Scada\", false), 1234)) { using (SnapClient client = SnapClient.Connect(server.Host)) { ClientDatabaseBase <HistorianKey, HistorianValue> database = client.GetDatabase <HistorianKey, HistorianValue>("DB"); TreeStream <HistorianKey, HistorianValue> stream = database.Read(10, 800 - 1); HistorianKey key = new HistorianKey(); HistorianValue value = new HistorianValue(); while (stream.Read(key, value)) { Console.WriteLine(key.Timestamp); } } } }
public void TestReadData() { HistorianKey key = new HistorianKey(); HistorianValue value = new HistorianValue(); var settings = new HistorianServerDatabaseConfig("PPA", @"c:\temp\Scada\", true); using (HistorianServer server = new HistorianServer(settings)) { double count = 0; DebugStopwatch sw = new DebugStopwatch(); double time = sw.TimeEvent(() => { count = 0; using (HistorianClient client = new HistorianClient("127.0.0.1", 12345)) using (ClientDatabaseBase <HistorianKey, HistorianValue> database = client.GetDatabase <HistorianKey, HistorianValue>(String.Empty)) { //IHistorianDatabase<HistorianKey, HistorianValue> database = server.GetDefaultDatabase();//.GetDatabase(); //TreeStream<HistorianKey, HistorianValue> stream = reader.Read(0, ulong.MaxValue, new ulong[] { 2 }); TreeStream <HistorianKey, HistorianValue> stream = database.Read(0, ulong.MaxValue); while (stream.Read(key, value)) { count++; } } }); Console.WriteLine((count / 1000000 / time).ToString() + " Million PPS"); } //Console.WriteLine("KeyMethodsBase calls"); //for (int x = 0; x < 23; x++) //{ // Console.WriteLine(TreeKeyMethodsBase<HistorianKey>.CallMethods[x] + "\t" + ((TreeKeyMethodsBase<HistorianKey>.Method)(x)).ToString()); //} //Console.WriteLine("ValueMethodsBase calls"); //for (int x = 0; x < 5; x++) //{ // Console.WriteLine(TreeValueMethodsBase<HistorianValue>.CallMethods[x] + "\t" + ((TreeValueMethodsBase<HistorianValue>.Method)(x)).ToString()); //} //for (int x = 0; x < 15; x++) //{ // Console.WriteLine(BinaryStreamBase.CallMethods[x] + "\t" + ((BinaryStreamBase.Method)(x)).ToString()); //} }
public void TestReadData() { HistorianKey key = new HistorianKey(); HistorianValue value = new HistorianValue(); HistorianServerDatabaseConfig settings = new HistorianServerDatabaseConfig("DB", @"c:\temp\Scada\", true); using (HistorianServer server = new HistorianServer(settings, 12345)) { using (HistorianClient client = new HistorianClient("127.0.0.1", 12345)) using (ClientDatabaseBase <HistorianKey, HistorianValue> database = client.GetDatabase <HistorianKey, HistorianValue>("DB")) { TreeStream <HistorianKey, HistorianValue> stream = database.Read(0, 1000); while (stream.Read(key, value)) { Console.WriteLine(key.Timestamp); } } } }
/// <summary> /// Read historian data from server. /// </summary> /// <param name="connection">openHistorian connection.</param> /// <param name="startTime">Start time of query.</param> /// <param name="stopTime">Stop time of query.</param> /// <param name="measurementIDs">Comma separated list of measurement IDs to query - or <c>null</c> for all available points.</param> /// <param name="resolution">Resolution for data query.</param> /// <returns>Enumeration of <see cref="IMeasurement"/> values read for time range.</returns> /// <example> /// <code> /// using (var connection = new Connection("127.0.0.1", "PPA")) /// foreach(var measurement in GetHistorianData(connection, DateTime.UtcNow.AddMinutes(-1.0D), DateTime.UtcNow)) /// Console.WriteLine("{0}:{1} @ {2} = {3}, quality: {4}", measurement.Key.Source, measurement.Key.ID, measurement.Timestamp, measurement.Value, measurement.StateFlags); /// </code> /// </example> public static IEnumerable <IMeasurement> GetHistorianData(Connection connection, DateTime startTime, DateTime stopTime, string measurementIDs = null, Resolution resolution = Resolution.Full) { SeekFilterBase <HistorianKey> timeFilter; MatchFilterBase <HistorianKey, HistorianValue> pointFilter = null; HistorianKey key = new HistorianKey(); HistorianValue value = new HistorianValue(); // Set data scan resolution if (resolution == Resolution.Full) { timeFilter = TimestampSeekFilter.CreateFromRange <HistorianKey>(startTime, stopTime); } else { timeFilter = TimestampSeekFilter.CreateFromIntervalData <HistorianKey>(startTime, stopTime, resolution.GetInterval(), new TimeSpan(TimeSpan.TicksPerMillisecond)); } // Setup point ID selections if (!string.IsNullOrEmpty(measurementIDs)) { pointFilter = PointIdMatchFilter.CreateFromList <HistorianKey, HistorianValue>(measurementIDs.Split(',').Select(ulong.Parse)); } // Start stream reader for the provided time window and selected points using (Database database = connection.OpenDatabase()) { TreeStream <HistorianKey, HistorianValue> stream = database.Read(SortedTreeEngineReaderOptions.Default, timeFilter, pointFilter); while (stream.Read(key, value)) { yield return new Measurement() { Metadata = MeasurementKey.LookUpOrCreate(connection.InstanceName, (uint)key.PointID).Metadata, Timestamp = key.TimestampAsDate, Value = value.AsSingle, StateFlags = (MeasurementStateFlags)value.Value3 } } ; } }
public void DetatchFiles() { Logger.Console.Verbose = VerboseLevel.All; using (SnapServer server = CreateServer()) { using (SnapClient client = SnapClient.Connect(server)) using (ClientDatabaseBase <HistorianKey, HistorianValue> db = client.GetDatabase <HistorianKey, HistorianValue>("PPA")) { using (TreeStream <HistorianKey, HistorianValue> stream = db.Read(null, null, null)) { Console.WriteLine(stream.Count()); } db.DetatchFiles(db.GetAllAttachedFiles().Select(x => x.Id).ToList()); using (TreeStream <HistorianKey, HistorianValue> stream = db.Read(null, null, null)) { Console.WriteLine(stream.Count()); } } } }
public InsertStreamHelper(TreeStream <TKey, TValue> stream) { Stream = stream; Key1 = new TKey(); Key2 = new TKey(); Value1 = new TValue(); Value2 = new TValue(); IsKVP1 = false; IsStillSequential = true; if (IsKVP1) { IsValid = Stream.Read(Key2, Value2); IsKVP1 = false; } else { IsValid = Stream.Read(Key1, Value1); IsKVP1 = true; } }
private IEnumerable<IDataPoint> ReadDataStream(TreeStream<HistorianKey, HistorianValue> stream) { HistorianKey key = new HistorianKey(); HistorianValue value = new HistorianValue(); List<ArchiveDataPoint> queriedData = new List<ArchiveDataPoint>(); ArchiveDataPoint point; MeasurementStateFlags stateFlags; while (stream.Read(key, value)) { point = new ArchiveDataPoint((int)key.PointID); point.Time = new TimeTag(new DateTime((long)key.Timestamp)); point.Value = BitConvert.ToSingle(value.Value1); stateFlags = (MeasurementStateFlags)value.Value3; if ((stateFlags & MeasurementStateFlags.BadData) == 0) { if ((stateFlags & MeasurementStateFlags.BadTime) == 0) point.Quality = Quality.Good; else point.Quality = Quality.Old; } else { point.Quality = Quality.SuspectData; } queriedData.Add(point); } return queriedData; }