/* ======================================================================== * Copyright (c) 2005-2025 The OPC Foundation, Inc. All rights reserved. * * OPC Foundation MIT License 1.00 * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * The complete license agreement can be found here: * http://opcfoundation.org/License/MIT/1.00/ * ======================================================================*/ using Opc.Ua; using DataValue = ZB.MOM.WW.OtOpcUa.OpcUaServer.Compatibility.Aggregates.LegacyDataValue; using System; using System.Collections.Generic; using Microsoft.Extensions.Logging; namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.Compatibility.Aggregates { /// /// Calculates the value of an aggregate. /// internal class AggregateCalculator { /// /// Create calculator /// [Obsolete("Use constructor with ITelemetryContext")] public AggregateCalculator( NodeId aggregateId, DateTime startTime, DateTime endTime, double processingInterval, bool stepped, AggregateConfiguration configuration) : this(aggregateId, startTime, endTime, processingInterval, stepped, configuration, null) { } /// /// Initializes the calculation stream. /// /// The aggregate function to apply. /// The start time. /// The end time. /// The processing interval. /// Whether to use stepped interpolation. /// The aggregate configuration. /// The telemetry context to use to create obvservability instruments public AggregateCalculator( NodeId aggregateId, DateTime startTime, DateTime endTime, double processingInterval, bool stepped, AggregateConfiguration configuration, ITelemetryContext? telemetry) { m_logger = telemetry.CreateLogger(); AggregateId = aggregateId; StartTime = startTime; EndTime = endTime; ProcessingInterval = processingInterval; Stepped = stepped; Configuration = configuration; TimeFlowsBackward = endTime < startTime; if (processingInterval == 0) { if (endTime == DateTime.MinValue || startTime == DateTime.MinValue) { throw new ArgumentException( "Non-zero processingInterval required.", nameof(processingInterval)); } ProcessingInterval = Math.Abs((endTime - startTime).TotalMilliseconds); } m_values = new LinkedList(); } /// /// The aggregate function applied by the calculator. /// public NodeId AggregateId { get; } /// /// Queues a raw value for processing. /// /// The data value to process. /// True if successful, false if the timestamp has been superseded by values already in the stream. public bool QueueRawValue(DataValue value) { // ignore bad data. if (value == null) { return false; } // ignore placeholders in the stream. if (value.StatusCode.CodeBits == StatusCodes.BadNoData || value.StatusCode.CodeBits == StatusCodes.BadBoundNotFound) { return true; } // check for start of data. if (m_startOfData == DateTime.MinValue) { m_startOfData = value.SourceTimestamp; } // update end of data. m_endOfData = value.SourceTimestamp; // ensure values are being queued in the right order. if (TimeFlowsBackward) { if (m_values.First != null && CompareTimestamps(value, m_values.First) > 0) { return false; } } else if (m_values.Last != null && CompareTimestamps(value, m_values.Last) < 0) { return false; } // ensure value list is always ordered from past to future. if (TimeFlowsBackward) { m_values.AddFirst(value); } else { m_values.AddLast(value); } return true; } /// /// Returns the next processed value. /// /// If true a partial interval should be processed. /// The processed value. Null if nothing available and returnPartial is false. public DataValue? GetProcessedValue(bool returnPartial) { // check if all done. if (Complete) { return null; } // update the slice. if (CurrentSlice == null) { CurrentSlice = CreateSlice(null); } else { UpdateSlice(CurrentSlice); } // check if a value can be produced. if (!CurrentSlice.Complete && !returnPartial) { return null; } // check if the slice extends beyond the range of available data. DateTime earlyTime = CurrentSlice.StartTime; DateTime lateTime = CurrentSlice.EndTime; if (CompareTimestamps(lateTime, m_values.First) < 0 || CompareTimestamps(earlyTime, m_values.Last) > 0) { CurrentSlice.OutOfDataRange = true; } m_logger.LogTrace("Computing Aggregate {StartTime:HH:mm:ss.fff}", CurrentSlice.StartTime); // compute the value. DataValue value = ComputeValue(CurrentSlice); // check if overlapping the start of data. if (SetPartialBit) { if (m_startOfData > earlyTime && m_startOfData < lateTime) { value.StatusCode = value.StatusCode.WithAggregateBits( value.StatusCode.AggregateBits | AggregateBits.Partial); } if (!UsingExtrapolation && !TimeFlowsBackward && m_endOfData >= earlyTime && m_endOfData < lateTime) { value.StatusCode = value.StatusCode.WithAggregateBits( value.StatusCode.AggregateBits | AggregateBits.Partial); } } // force value to null if status code is bad. if (StatusCode.IsBad(value.StatusCode)) { value.WrappedValue = Variant.Null; } // delete unneeded data. if (TimeFlowsBackward) { if (CurrentSlice.LateBound != null) { LinkedListNode? ii = CurrentSlice.LateBound.Next; while (ii != null) { LinkedListNode? next = ii.Next; m_values.Remove(ii); ii = next; } } } else if (CurrentSlice.EarlyBound != null) { LinkedListNode? ii = CurrentSlice.EarlyBound.Previous; if (CurrentSlice.SecondEarlyBound != null) { ii = CurrentSlice.SecondEarlyBound.Previous; } while (ii != null) { LinkedListNode? next = ii.Previous; m_values.Remove(ii); ii = next; } } // check if more to be done. Complete = (!TimeFlowsBackward && CurrentSlice.EndTime >= EndTime) || (TimeFlowsBackward && CurrentSlice.StartTime <= EndTime); if (Complete) { // check if overlapping the end of data. if (SetPartialBit && !UsingExtrapolation && !TimeFlowsBackward && m_endOfData >= earlyTime && m_endOfData < lateTime) { value.StatusCode = value.StatusCode.WithAggregateBits( value.StatusCode.AggregateBits | AggregateBits.Partial); } } else { CurrentSlice = CreateSlice(CurrentSlice); } // return the processed value. return value; } /// /// Returns true if the specified time is later than the end of the current interval. /// /// Return true if time flows forward and the time is later than the end time. public bool HasEndTimePassed(DateTime currentTime) { if (CurrentSlice == null) { return false; } if (TimeFlowsBackward) { return CurrentSlice.EndTime >= currentTime; } return CurrentSlice.EndTime <= currentTime; } /// /// The start time for the request. /// protected DateTime StartTime { get; } /// /// The end time for the request. /// protected DateTime EndTime { get; } /// /// The processing interval for the request. /// protected double ProcessingInterval { get; } /// /// True if the data series requires stepped interpolation. /// protected bool Stepped { get; } /// /// The configuration to use when processing. /// protected AggregateConfiguration Configuration { get; } /// /// Whether to use the server timestamp for all processing. /// protected bool UseServerTimestamp { get; } /// /// True if data is being processed in reverse order. /// protected bool TimeFlowsBackward { get; } /// /// Whether to use the server timestamp for all processing. /// protected TimeSlice? CurrentSlice { get; private set; } /// /// True if all values required for the request have been received and processed /// protected bool Complete { get; private set; } /// /// True if the GetProcessedValue method should set the Partial bit when appropriate. /// protected bool SetPartialBit { get; set; } /// /// True if data is extrapolated after the end of data. /// protected bool UsingExtrapolation { get; set; } /// /// Compares timestamps for two DataValues according to the current UseServerTimestamp setting. /// /// The first value to compare. /// The second value to compare. /// Less than 0 if value1 is earlier than value2; 0 if they are equal; Greater than zero otherwise. protected int CompareTimestamps(DataValue value1, DataValue value2) { if (value1 == null) { return value2 == null ? 0 : -1; } if (value2 == null) { return +1; } if (UseServerTimestamp) { int result = value1.ServerTimestamp.CompareTo(value2.ServerTimestamp); if (result == 0) { return value1.ServerPicoseconds.CompareTo(value2.ServerPicoseconds); } return result; } else { int result = value1.SourceTimestamp.CompareTo(value2.SourceTimestamp); if (result == 0) { return value1.SourcePicoseconds.CompareTo(value2.SourcePicoseconds); } return result; } } /// /// Compares timestamps for two DataValues according to the current UseServerTimestamp setting. /// /// The first value to compare. /// The second value to compare. /// Less than 0 if value1 is earlier than value2; 0 if they are equal; Greater than zero otherwise. protected int CompareTimestamps(DataValue value1, LinkedListNode? value2) { if (value2 == null) { return value1 == null ? 0 : +1; } return CompareTimestamps(value1, value2.Value); } /// /// Compares timestamps for two DataValues according to the current UseServerTimestamp setting. /// /// The first value to compare. /// The second value to compare. /// Less than 0 if value1 is earlier than value2; 0 if they are equal; Greater than zero otherwise. protected int CompareTimestamps( LinkedListNode? value1, LinkedListNode? value2) { if (value1 == null) { return value2 == null ? 0 : -1; } if (value2 == null) { return +1; } return CompareTimestamps(value1.Value, value2.Value); } /// /// Compares timestamps for a timestamp to a DataValue according to the current UseServerTimestamp setting. /// /// The timestamp to compare. /// The data value to compare. /// Less than 0 if value1 is earlier than value2; 0 if they are equal; Greater than zero otherwise. protected int CompareTimestamps(DateTime value1, LinkedListNode? value2) { if (value2 == null || value2.Value == null) { return +1; } if (UseServerTimestamp) { return value1.CompareTo(value2.Value.ServerTimestamp); } return value1.CompareTo(value2.Value.SourceTimestamp); } /// /// Checks if the value is good according to the configuration rules. /// /// The value to test. /// True if the value is good. protected bool IsGood(DataValue value) { if (value == null) { return false; } if (Configuration.TreatUncertainAsBad) { if (StatusCode.IsNotGood(value.StatusCode)) { return false; } } else if (StatusCode.IsBad(value.StatusCode)) { return false; } return true; } /// /// Stores information about a slice of data to be processed. /// protected class TimeSlice { /// /// The start time for the slice. /// public DateTime StartTime { get; set; } /// /// The end time for the slice. /// public DateTime EndTime { get; set; } /// /// True if the slice is a partial interval. /// public bool Partial { get; set; } /// /// True if all of the data required to process the slice has been collected. /// public bool Complete { get; set; } /// /// True if the slice includes times that are outside of the available dataset. /// public bool OutOfDataRange { get; set; } /// /// The first early bound for the slice. /// public LinkedListNode? EarlyBound { get; set; } /// /// The latest non-bad value before the slice. /// internal LinkedListNode? NonBadEarlyBound { get; set; } /// /// The second early bound for the slice (always earlier than the first). /// public LinkedListNode? SecondEarlyBound { get; set; } /// /// The beginning of the slice. /// public LinkedListNode? Begin { get; set; } /// /// The end of the slice. /// public LinkedListNode? End { get; set; } /// /// The late bound for the slice. /// public LinkedListNode? LateBound { get; set; } /// /// The last value which was processed. /// public LinkedListNode? LastProcessedValue { get; set; } } /// /// Creates a new time slice to process. /// /// The previous processed slice. /// The new time slice. protected TimeSlice CreateSlice(TimeSlice? previousSlice) { var slice = new TimeSlice(); // ensure slice is oriented from past to future even if request is going backwards. if (TimeFlowsBackward) { if (previousSlice == null) { slice.EndTime = StartTime; } else { slice.EndTime = previousSlice.StartTime; } slice.StartTime = slice.EndTime.AddMilliseconds(-ProcessingInterval); // check for end of request. if (slice.StartTime < EndTime) { slice.StartTime = EndTime; slice.Partial = true; } } else { if (previousSlice == null) { slice.StartTime = StartTime; } else { slice.StartTime = previousSlice.EndTime; } slice.EndTime = slice.StartTime.AddMilliseconds(ProcessingInterval); // check for end of request. if (slice.EndTime > EndTime) { slice.EndTime = EndTime; slice.Partial = true; } } // update the slice with current data. UpdateSlice(slice); return slice; } /// /// Creates a new time slice to process. /// /// The slice to update. /// True if the slice is complete. protected bool UpdateSlice(TimeSlice slice) { // check if nothing to do. if (m_values.First == null) { return slice.Complete; } // restart processing from where it left off. LinkedListNode? start = m_values.First; if (!TimeFlowsBackward && slice.LastProcessedValue != null) { start = slice.LastProcessedValue.Next; } // reset the begin bound each time we go through the values. if (TimeFlowsBackward) { slice.Begin = null; } // initialize slice from value list. for (LinkedListNode? ii = start; ii != null; ii = ii.Next) { if (TimeFlowsBackward) { // check if before the beginning of the slice. if (CompareTimestamps(slice.StartTime, ii) >= 0) { if (StatusCode.IsNotBad(ii.Value.StatusCode)) { slice.NonBadEarlyBound = ii; } if (IsGood(ii.Value)) { slice.SecondEarlyBound = slice.EarlyBound; slice.EarlyBound = ii; } continue; } // check if after the end if the slice. if (CompareTimestamps(slice.EndTime, ii) < 0) { if (IsGood(ii.Value)) { slice.LateBound = ii; break; } continue; } // save first value in the slice. slice.End ??= ii; // save end of slice. if (slice.Begin == null) { slice.Begin = ii; slice.LastProcessedValue = ii; } } else { // check if before the beginning of the slice. if (CompareTimestamps(slice.StartTime, ii) > 0) { if (StatusCode.IsNotBad(ii.Value.StatusCode)) { slice.NonBadEarlyBound = ii; } if (IsGood(ii.Value)) { slice.SecondEarlyBound = slice.EarlyBound; slice.EarlyBound = ii; slice.LastProcessedValue = ii; } continue; } // check if after the end if the slice. if (CompareTimestamps(slice.EndTime, ii) < 0) { if (IsGood(ii.Value)) { slice.LateBound = ii; slice.LastProcessedValue = ii; break; } continue; } // save first value in the slice. slice.Begin ??= ii; // save end of slice. slice.End = ii; slice.LastProcessedValue = ii; } } // check if no more data needs to be collected. LinkedListNode? requiredBound; if (TimeFlowsBackward) { // only need second early bound if using sloped extrapolation and there is no late bound. if (Configuration.UseSlopedExtrapolation && slice.LateBound == null) { requiredBound = slice.SecondEarlyBound; } else { requiredBound = slice.EarlyBound; } } else { requiredBound = slice.LateBound; } // all done if required bound exists. if (requiredBound != null) { slice.Complete = true; } return slice.Complete; } /// /// Calculates the value for the timeslice. /// /// The slice to process. /// The processed value. protected virtual DataValue ComputeValue(TimeSlice slice) { return Interpolate(slice); } /// /// Calculate the interpolate aggregate for the timeslice. /// protected DataValue Interpolate(TimeSlice slice) { if (TimeFlowsBackward) { return Interpolate(slice.EndTime, slice); } return Interpolate(slice.StartTime, slice); } /// /// Return a value indicating there is no data in the time slice. /// protected DataValue GetNoDataValue(TimeSlice slice) { if (TimeFlowsBackward) { return GetNoDataValue(slice.EndTime); } return GetNoDataValue(slice.StartTime); } /// /// Returns the timestamp to use for the slice value. /// protected DateTime GetTimestamp(TimeSlice slice) { if (TimeFlowsBackward) { return slice.EndTime; } return slice.StartTime; } /// /// Return a value indicating there is no data in the time slice. /// protected DataValue GetNoDataValue(DateTime timestamp) { return new DataValue(Variant.Null, StatusCodes.BadNoData, timestamp, timestamp); } /// /// Interpolates a value at the timestamp. /// /// The timestamp. /// The timeslice that contains the timestamp. /// The interpolated value. protected DataValue Interpolate(DateTime timestamp, TimeSlice reference) { for (LinkedListNode? ii = m_values.First; ii != null; ii = ii.Next) { int comparison = CompareTimestamps(timestamp, ii); if (comparison == 0) { if (StatusCode.IsNotBad(ii.Value.StatusCode)) { return ii.Value; } break; } if (comparison < 0) { break; } } var slice = new TimeSlice { StartTime = timestamp, EndTime = timestamp }; UpdateSlice(slice); // check for value at the timestamp. if (slice.Begin != null && IsGood(slice.Begin.Value)) { return slice.Begin.Value; } bool stepped = Stepped; DataValue dataValue; // check if the required bounds are available. if (!Stepped) { // check if sloped interpolation is possible. if (slice.EarlyBound != null && slice.LateBound != null) { dataValue = SlopedInterpolate( timestamp, slice.EarlyBound.Value, slice.LateBound.Value); if (!ReferenceEquals(slice.EarlyBound.Next, slice.LateBound)) { dataValue.StatusCode = dataValue.StatusCode .WithCodeBits(StatusCodes.UncertainDataSubNormal); } return dataValue; } // check if extrapolation is possible. if (slice.EarlyBound != null) { if (Configuration.UseSlopedExtrapolation && slice.SecondEarlyBound != null) { UsingExtrapolation = true; dataValue = SlopedInterpolate( timestamp, slice.SecondEarlyBound.Value, slice.EarlyBound.Value); dataValue.StatusCode = dataValue.StatusCode .WithCodeBits(StatusCodes.UncertainDataSubNormal); return dataValue; } // do stepped extrapolation. stepped = true; } } // do stepped interpolation. if (stepped && slice.EarlyBound != null) { dataValue = SteppedInterpolate(timestamp, slice.EarlyBound.Value); if (slice.EarlyBound.Next == null || CompareTimestamps(timestamp, slice.EarlyBound.Next) > 0) { UsingExtrapolation = true; dataValue.StatusCode = dataValue.StatusCode .WithCodeBits(StatusCodes.UncertainDataSubNormal); } return dataValue; } // no data found. return GetNoDataValue(timestamp); } /// /// Calculate the value at the timestamp using slopped interpolation. /// public static DataValue SteppedInterpolate(DateTime timestamp, DataValue earlyBound) { // can't interpolate if no start bound. if (StatusCode.IsBad(earlyBound.StatusCode)) { return new DataValue(Variant.Null, StatusCodes.BadNoData, timestamp, timestamp); } var dataValue = new DataValue { WrappedValue = earlyBound.WrappedValue, SourceTimestamp = timestamp, ServerTimestamp = timestamp, StatusCode = StatusCodes.Good }; // update status code. if (StatusCode.IsBad(earlyBound.StatusCode)) { dataValue.StatusCode = StatusCodes.BadNoData; } // update status code. if (StatusCode.IsNotGood(earlyBound.StatusCode)) { dataValue.StatusCode = StatusCodes.UncertainDataSubNormal; } dataValue.StatusCode = dataValue.StatusCode .WithAggregateBits(AggregateBits.Interpolated); return dataValue; } /// /// Calculate the value at the timestamp using slopped interpolation. /// public static DataValue SlopedInterpolate( DateTime timestamp, DataValue earlyBound, DataValue lateBound) { try { // can't interpolate if no start bound. if (StatusCode.IsBad(earlyBound.StatusCode)) { return new DataValue(Variant.Null, StatusCodes.BadNoData, timestamp, timestamp); } // revert to stepped if no end bound. if (StatusCode.IsBad(lateBound.StatusCode)) { DataValue dataValue2 = SteppedInterpolate(timestamp, earlyBound); if (StatusCode.IsNotBad(dataValue2.StatusCode)) { dataValue2.StatusCode = dataValue2.StatusCode .WithCodeBits(StatusCodes.UncertainDataSubNormal); } return dataValue2; } // convert to doubles. double earlyValue = CastToDouble(earlyBound); double lateValue = CastToDouble(lateBound); // do interpolation. double range = (lateBound.SourceTimestamp - earlyBound.SourceTimestamp) .TotalMilliseconds; double slope = (lateValue - earlyValue) / range; double calculatedValue = (slope * (timestamp - earlyBound.SourceTimestamp).TotalMilliseconds) + earlyValue; // convert back to original type. var dataValue = new DataValue { WrappedValue = CastToOriginalType(calculatedValue, earlyBound), SourceTimestamp = timestamp, ServerTimestamp = timestamp, StatusCode = StatusCodes.Good }; // update status code. if (StatusCode.IsNotGood(earlyBound.StatusCode) || StatusCode.IsNotGood(lateBound.StatusCode)) { dataValue.StatusCode = StatusCodes.UncertainDataSubNormal; } dataValue.StatusCode = dataValue.StatusCode .WithAggregateBits(AggregateBits.Interpolated); return dataValue; } // exception occurs on data conversion errors. catch (Exception) { return new DataValue( Variant.Null, StatusCodes.BadTypeMismatch, timestamp, timestamp); } } /// /// Converts the value to a double for use in calculations (throws exceptions if conversion fails). /// private protected static object LegacyCast(object? value, TypeInfo source, BuiltInType target) { // Representation conversion only; baseline interval/quality/state algorithms are retained below. return SdkValueBoundary.ToClr(SdkValueBoundary.FromClr(value).ConvertTo(target))!; } protected static double CastToDouble(DataValue value) { return (double)LegacyCast( value.Value, value.WrappedValue.TypeInfo, BuiltInType.Double); } /// /// Converts the value back to its original type (throws exceptions if conversion fails). /// protected static Variant CastToOriginalType(double value, DataValue original) { object castValue = LegacyCast( value, TypeInfo.Scalars.Double, original.WrappedValue.TypeInfo.BuiltInType); return SdkValueBoundary.FromClr(castValue); } /// /// Returns the simple bound for the timestamp. /// protected DataValue? GetSimpleBound(DateTime timestamp, TimeSlice slice) { // choose the start point LinkedListNode? start = slice.EarlyBound ?? m_values.First; // look for a raw value at or immediately before the timestamp. LinkedListNode? startBound = start; for (LinkedListNode? ii = start; ii != null; ii = ii.Next) { // check for an exact match. if (CompareTimestamps(timestamp, ii) == 0) { return new DataValue(ii.Value); } // looking for an end bound. if (CompareTimestamps(timestamp, ii) < 0) { // only can find an end bound. if (ii.Previous == null) { return GetNoDataValue(timestamp); } startBound = ii.Previous; break; } // update start bound. startBound = ii; } // check if no data found or if start bound is bad.. if (startBound == null || !IsGood(startBound.Value)) { return GetNoDataValue(timestamp); } // look for an end bound. bool revertToStepped = false; LinkedListNode? endBound = startBound.Next; if (!Stepped) { if (endBound != null) { // do sloped interpolation if two good bounds exist. if (IsGood(endBound.Value)) { return SlopedInterpolate(timestamp, startBound.Value, endBound.Value); } } // have to use stepped because end bound is not good. revertToStepped = true; } // check if end of data. if (startBound.Next == null) { return GetNoDataValue(timestamp); } // do stepped interpolation for all other cases. DataValue value = SteppedInterpolate(timestamp, startBound.Value); // need to make it uncertain if interpolation was required but not used. if (StatusCode.IsGood(value.StatusCode) && revertToStepped) { value.StatusCode = StatusCodes.UncertainDataSubNormal; value.StatusCode = value.StatusCode.WithAggregateBits(AggregateBits.Interpolated); } return value; } /// /// Returns the values in the list with simple bounds. /// protected List? GetValuesWithSimpleBounds(TimeSlice slice) { // check if slice is beyond end of available data. if (CompareTimestamps(slice.StartTime, m_values.Last) > 0 || CompareTimestamps(slice.EndTime, m_values.First) < 0) { return null; } var values = new List(); // add the start point. DataValue? startBound = GetSimpleBound(slice.StartTime, slice); if (startBound != null) { values.Add(startBound); } // initialize slice from value list. for (LinkedListNode? ii = slice.Begin; ii != null; ii = ii.Next) { if (CompareTimestamps(slice.EndTime, ii) <= 0) { break; } if (CompareTimestamps(slice.StartTime, ii) < 0) { values.Add(ii.Value); } } // add the end point. DataValue? endBound = GetSimpleBound(slice.EndTime, slice); if (endBound != null) { values.Add(endBound); } return values; } /// /// Returns the values between the start time and the end time for the slice. /// protected List? GetValues(TimeSlice slice) { // check if slice is beyond end of available data. if (CompareTimestamps(slice.StartTime, m_values.Last) > 0 || CompareTimestamps(slice.EndTime, m_values.First) < 0) { return null; } var values = new List(); // initialize slice from value list. for (LinkedListNode? ii = slice.Begin; ii != null; ii = ii.Next) { if (TimeFlowsBackward) { if (CompareTimestamps(slice.EndTime, ii) < 0) { break; } if (CompareTimestamps(slice.StartTime, ii) < 0) { values.Add(ii.Value); } } else { if (CompareTimestamps(slice.EndTime, ii) <= 0) { break; } if (CompareTimestamps(slice.StartTime, ii) <= 0) { values.Add(ii.Value); } } } return values; } /// /// Returns the values in the list with interpolated bounds. /// protected List? GetValuesWithInterpolatedBounds(TimeSlice slice) { // check if slice is before the available data. if (CompareTimestamps(slice.EndTime, m_values.First) < 0) { return null; } var values = new List(); // add the start point. DataValue startBound = Interpolate(slice.StartTime, slice); if (startBound != null) { values.Add(startBound); } // initialize slice from value list. for (LinkedListNode? ii = slice.Begin; ii != null; ii = ii.Next) { if (CompareTimestamps(slice.EndTime, ii) <= 0) { break; } if (CompareTimestamps(slice.StartTime, ii) <= 0) { values.Add(ii.Value); } } // add the end point. DataValue endBound = Interpolate(slice.EndTime, slice); if (endBound != null) { values.Add(endBound); } return values; } /// /// A subset of a slice bounded by two raw data points. /// protected class SubRegion { /// /// The value at the start of the region. /// public double StartValue { get; set; } /// /// The value at the end of the region. /// public double EndValue { get; set; } /// /// The timestamp at the start of the region. /// public DateTime StartTime; /// /// The length of the region. /// public double Duration { get; set; } /// /// The status for the region. /// public StatusCode StatusCode; /// /// The data point at the start of the region. /// public required DataValue DataPoint; } /// /// Returns the values in the list with simple bounds. /// [return: System.Diagnostics.CodeAnalysis.NotNullIfNotNull(nameof(values))] protected List? GetRegionsInValueSet( List? values, bool ignoreBadData, bool useSteppedCalculations) { // nothing to do if no data. if (values == null) { return null; } SubRegion? currentRegion = null; var regions = new List(); for (int ii = 0; ii < values.Count; ii++) { double currentValue = 0; DateTime currentTime = values[ii].SourceTimestamp; StatusCode currentStatus = values[ii].StatusCode; // convert to doubles to facilitate numeric calculations. if (StatusCode.IsNotBad(currentStatus)) { try { currentValue = CastToDouble(values[ii]); } catch (Exception) { currentStatus = StatusCodes.BadTypeMismatch; } } else { // use the previous value if end of region is bad. if (currentRegion != null) { currentValue = currentRegion.StartValue; } } // some aggregates ignore bad data so remove them from the set. if (ignoreBadData) { // always keep the first region. if (currentRegion != null) { if (!IsGood(values[ii])) { // set the status to sub normal if bad end data ignored. if (StatusCode.IsNotBad(currentRegion.StatusCode)) { currentRegion.StatusCode = StatusCodes.UncertainDataSubNormal; } // skip everything but the endpoint. if (ii < values.Count - 1) { continue; } } else if (!useSteppedCalculations && StatusCode.IsNotGood(values[ii].StatusCode)) { currentRegion.StatusCode = StatusCodes.UncertainDataSubNormal; } } } if (currentRegion != null) { // if using stepped calculations the end value is not used. if (useSteppedCalculations) { currentRegion.EndValue = currentRegion.StartValue; } // using interpolated calculations means the end affects the status of the current region. else if (IsGood(values[ii])) { // handle case with uncertain end point. if (StatusCode.IsNotGood(values[ii].StatusCode) && StatusCode.IsNotBad(currentRegion.StatusCode)) { currentRegion.StatusCode = StatusCodes.UncertainDataSubNormal; } currentRegion.EndValue = currentValue; } else { if (StatusCode.IsNotBad(currentRegion.StatusCode)) { currentRegion.StatusCode = StatusCodes.UncertainDataSubNormal; } if (ignoreBadData && StatusCode.IsNotBad(currentStatus)) { currentRegion.EndValue = currentValue; } } // if at end of data then duration is 1 tick. // must be end of data if start of region is good yet end bound is bad. if (!ignoreBadData && IsGood(currentRegion.DataPoint) && currentStatus == StatusCodes.BadNoData && ii == values.Count - 1) { currentRegion.Duration = 1; } // calculate region span. else { // set uncertain status to bad if treat uncertain as bad is true. if (StatusCode.IsUncertain(currentStatus) && !IsGood(values[ii])) { currentStatus = StatusCodes.BadNoData; } currentRegion.Duration = (currentTime - currentRegion.StartTime) .TotalMilliseconds; } regions.Add(currentRegion); } // start a new region. currentRegion = new SubRegion { StartValue = currentValue, EndValue = currentValue, StartTime = currentTime, StatusCode = currentStatus, DataPoint = values[ii] }; } return regions; } /// /// Calculates the value based status code for the slice /// protected StatusCode GetValueBasedStatusCode( TimeSlice slice, List values, StatusCode statusCode) { // compute the total good/bad/uncertain. double badCount = 0; double goodCount = 0; double totalCount = 0; for (int ii = 0; ii < values.Count; ii++) { totalCount++; if (StatusCode.IsBad(values[ii].StatusCode)) { badCount++; continue; } if (StatusCode.IsGood(values[ii].StatusCode)) { goodCount++; } } if (totalCount == 0 || goodCount / totalCount * 100 >= Configuration.PercentDataGood) { // good if the good count is greater than or equal to the configured threshold. statusCode = statusCode.WithCodeBits(StatusCodes.Good); } else if (badCount / totalCount * 100 >= Configuration.PercentDataBad) { // bad if the bad count is greater than or equal to the configured threshold. statusCode = StatusCodes.Bad; } else { // uncertain if did not meet the Good or Bad requirements statusCode = statusCode.WithCodeBits(StatusCodes.UncertainDataSubNormal); } return statusCode; } /// /// Calculates the status code for the slice /// protected StatusCode GetTimeBasedStatusCode( TimeSlice slice, List values, StatusCode defaultCode) { // get the regions in the slice. List regions = GetRegionsInValueSet(values, false, Stepped); if (regions == null || regions.Count == 0) { return StatusCodes.BadNoData; } return GetTimeBasedStatusCode(regions, defaultCode); } /// /// Calculates the status code for the slice /// protected StatusCode GetTimeBasedStatusCode(List regions, StatusCode statusCode) { // check for empty set. if (regions == null || regions.Count == 0) { return StatusCodes.BadNoData; } // compute the total good/bad/uncertain. double badDuration = 0; double goodDuration = 0; double totalDuration = 0; foreach (SubRegion region in regions) { totalDuration += region.Duration; if (StatusCode.IsBad(region.StatusCode)) { badDuration += region.Duration; continue; } // Take into account the Uncertain status code if (StatusCode.IsGood(region.StatusCode) || (!Configuration.TreatUncertainAsBad && StatusCode.IsUncertain(region.StatusCode))) { goodDuration += region.Duration; } } if (totalDuration == 0 || goodDuration / totalDuration * 100 >= Configuration.PercentDataGood) { // good if the good duration is greater than or equal to the configured threshold. statusCode = statusCode.WithCodeBits(StatusCodes.Good); } else if (badDuration / totalDuration * 100 >= Configuration.PercentDataBad) { // bad if the bad duration is greater than or equal to the configured threshold. statusCode = StatusCodes.Bad; } else { // uncertain if did not meet the Good or Bad requirements statusCode = statusCode.WithCodeBits(StatusCodes.UncertainDataSubNormal); } // always calculated. return statusCode; } private readonly ILogger m_logger; private readonly LinkedList m_values; private DateTime m_startOfData; private DateTime m_endOfData; } }