Skip to main content

read_history_processed (Aggregate)

The most powerful mode HistoryRead, used in conjunction with standard aggregation functions. Given an interval, an aggregation function, and the width of each subinterval, the server returns the aggregated result for each subinterval.

Preliminary / Supporting
  • To pull all original points, please use ReadRaw.
  • For interpolation at a specific point in time, please use ReadAtTime.

Signature​

pub fn Session::read_history_processed(
&self,
node_id: &str,
start: SystemTime,
end: SystemTime,
processing_interval_ms: f64,
aggregate_node_id: &str,
) -> Result<Vec<DataValue>, OpcUaError>;

Standard Aggregate Function Constants​

darra_opcua::history_aggregate Module:

ConstantsNodeIdMeaning
AVERAGEi=2342Arithmetic Mean
TIME_AVERAGEi=11285Time-Weighted Average
TOTALi=2348Cumulative (Algebraic Sum)
MINIMUMi=2346Minimum Value
MAXIMUMi=2347Maximum value
COUNTi=2352Number of "Good" status points within the interval
STANDARD_DEVIATION_SAMPLEi=11427Sample standard deviation
RANGEi=2353Max − Min
DELTAi=11286End of Interval − Start of Interval

For a complete list, see the OPC UA Aggregate Functions Standard.

Usage​

Hourly average of the 24-hour temperature​

use std::time::{Duration, SystemTime};
use darra_opcua::history_aggregate;

let now = SystemTime::now();
let yesterday = now - Duration::from_secs(86400);

let values = s.read_history_processed(
"ns=2;s=Temperature",
yesterday, now,
/*processing_interval_ms*/ 3_600_000.0, // 1 hour
history_aggregate::AVERAGE,
)?;
// Returns 24 values DataValue, each representing the average for that hour
for v in &values {
println!("avg = {}", v.variant().try_get_f64().unwrap_or(0.0));
}

Highest Temperature Each Day of the Week​

let week_ago = now - Duration::from_secs(7 * 86400);
let maxes = s.read_history_processed(
"ns=2;s=Temperature",
week_ago, now,
86_400_000.0, // 1 day
history_aggregate::MAXIMUM,
)?;

Total traffic in one hour​

let one_hour_ago = now - Duration::from_secs(3600);
let totals = s.read_history_processed(
"ns=2;s=FlowRate",
one_hour_ago, now,
3_600_000.0,
history_aggregate::TOTAL,
)?;

Performance Advantages​

  • Avoid fetching millions of data points and processing them on the client side
  • Servers often index historical data, enabling extremely fast aggregation.
  • Standard Solutions for Cross-Day and Cross-Week Reports

Server Requirements​

  • Historian must implement this aggregate function (the supported functions are listed in the ServerCapabilities/AggregateFunctions column on the server).
  • The server returns a protocol code for unsupported aggregations BadAggregateNotSupported — SDK No constant StatusCode has been defined for this code (For a complete list of constants, see StatusCode Associated Constants); when making a determination, do not simply copy from StatusCode::X, but use status code values / help functions: e.status.is_bad() / e.status.raw() / darra_opcua::status_name(e.status)

Parameter Validation​

Internal API validation:

  • aggregate_node_id Cannot be empty
  • processing_interval_ms Must be > 0

Otherwise, return Err(OpcUaError::with_message(StatusCode::INVALID_ARGUMENT, ...)).

Custom Aggregations NodeId​

If the server has a non-standard aggregate, simply pass its string NodeId:

s.read_history_processed(
"ns=2;s=Temperature",
yesterday, now,
60_000.0, // 1 minute
"ns=2;s=Custom_RMS", // Custom RMS Aggregation
)?;

Next Step​