Please ensure Javascript is enabled for purposes of website accessibility
Powered by Zoomin Software. For more details please contactZoomin

AF SDK Reference

AFSearch.AggregateAsync Method

  • Last UpdatedNov 18, 2025
  • 4 minute read
AFSearch.AggregateAsync Method
Performs all requested aggregates on the objects that match the search criteria asynchronously.

Namespace:  OSIsoft.AF.Search
Assembly:  OSIsoft.AFSDK (in OSIsoft.AFSDK.dll) Version: 3.1.1.1182

Syntax

public Task<AFAggregateResultCollection> AggregateAsync(
	IEnumerable<AFAggregateRequest> aggregateRequests,
	CancellationToken cancellationToken
)
Public Function AggregateAsync ( 
	aggregateRequests As IEnumerable(Of AFAggregateRequest),
	cancellationToken As CancellationToken
) As Task(Of AFAggregateResultCollection)

Dim instance As AFSearch
Dim aggregateRequests As IEnumerable(Of AFAggregateRequest)
Dim cancellationToken As CancellationToken
Dim returnValue As Task(Of AFAggregateResultCollection)

returnValue = instance.AggregateAsync(aggregateRequests, 
	cancellationToken)
public:
Task<AFAggregateResultCollection^>^ AggregateAsync(
	IEnumerable<AFAggregateRequest^>^ aggregateRequests, 
	CancellationToken cancellationToken
)
member AggregateAsync : 
        aggregateRequests : IEnumerable<AFAggregateRequest> * 
        cancellationToken : CancellationToken -> Task<AFAggregateResultCollection> 

Parameters

aggregateRequests
Type: System.Collections.GenericIEnumerableAFAggregateRequest
All aggregates that should be performed on the search results.
cancellationToken
Type: System.ThreadingCancellationToken
A token to control cancellation of the aggregation request.

Return Value

Type: TaskAFAggregateResultCollection
An AFAggregateResultCollection containing the results of the requested aggregates or errors if the aggregates could not be performed.

Remarks

Note Notes to Callers
This call might use a background task to complete some of its work. See the Threading Overview for some matters to consider when execution transitions to another thread.

Examples

// Get the Database
PISystems myPISystems = new PISystems();
PISystem myPISystem = myPISystems.DefaultPISystem;
if (myPISystem == null)
    throw new InvalidOperationException("Default PISystem was not found.");
AFDatabase myDB = myPISystem.Databases[dbName];
if (myDB == null)
    throw new InvalidOperationException("Database was not found.");

// Create a search to find all the event frames created from the 'Event'
// template in the last year.
using (AFEventFrameSearch eventSearch = new AFEventFrameSearch(myDB, "EventFrameSearch", @"Template:'Event' Start:>'t-1y'"))
{
    eventSearch.CacheTimeout = TimeSpan.FromMinutes(10);
    // Get several aggregates in one call
    var averageAndCount = new AFSummaryRequest("Duration", AFSummaryTypes.Average | AFSummaryTypes.Count);
    var binByMonth = averageAndCount.BinBy("StartTime",
        new AFTimeSpan(months: 1).GetIntervalTimes(new AFTimeRange("1-1y", "1+1mo")));
    var groupByElement = averageAndCount.GroupBy("Element");
    var overallSummaries = new AFSummaryRequest("Duration", AFSummaryTypes.Minimum | AFSummaryTypes.Average | AFSummaryTypes.Maximum);

    var aggregateResults = await eventSearch.AggregateAsync(new AFAggregateRequest[] { binByMonth, groupByElement, overallSummaries }, CancellationToken.None);

    var overallSummaryResult = aggregateResults.GetResult(overallSummaries);
    Console.WriteLine("Minimum: {0}, Average: {1}, Maximum: {2}",
        overallSummaryResult.SummaryResults[AFSummaryTypes.Minimum],
        overallSummaryResult.SummaryResults[AFSummaryTypes.Average],
        overallSummaryResult.SummaryResults[AFSummaryTypes.Maximum]);

    var binByMonthResult = aggregateResults.GetResult(binByMonth);
    foreach (var bin in binByMonthResult.BinnedResults)
    {
        Console.WriteLine("Month: {0}, Average: {1}, Count: {2}",
            bin.Key, bin.Value[AFSummaryTypes.Average], bin.Value[AFSummaryTypes.Count]);
    }

    var groupByElementResult = aggregateResults.GetResult(groupByElement);
    foreach (var group in groupByElementResult.GroupedResults)
    {
        Console.WriteLine("Element: {0}, Average: {1}, Count: {2}",
            group.Key, group.Value[AFSummaryTypes.Average], group.Value[AFSummaryTypes.Count]);
    }
}
' Get the Database
Dim myPISystems As New PISystems()
Dim myPISystem As PISystem = myPISystems.DefaultPISystem
If myPISystem Is Nothing Then
    Throw New InvalidOperationException("Default PISystem was not found.")
End If
Dim myDB As AFDatabase = myPISystem.Databases(dbName)
If myDB Is Nothing Then
    Throw New InvalidOperationException("Database was not found.")
End If

' Create a search to find all the event frames created from the 'Event'
' template in the last year.
Using eventSearch As New AFEventFrameSearch(myDB, "EventFrameSearch", "Template:'Event' Start:>'t-1y'")
    eventSearch.CacheTimeout = TimeSpan.FromMinutes(10)


    ' Get several aggregates in one call
    Dim averageAndCount As AFSummaryRequest = New AFSummaryRequest("Duration", AFSummaryTypes.Average Or AFSummaryTypes.Count)
    Dim binByMonth As AFBinningRequest(Of AFTime) = averageAndCount.BinBy("StartTime", New AFTimeSpan(months:=1).GetIntervalTimes(New AFTimeRange("1-1y", "1+1mo")))
    Dim groupByElement As AFGroupingRequest(Of Object) = averageAndCount.GroupBy("Element")
    Dim overallSummaries As AFSummaryRequest = New AFSummaryRequest("Duration", AFSummaryTypes.Minimum Or AFSummaryTypes.Average Or AFSummaryTypes.Maximum)

    Dim aggregateResults As AFAggregateResultCollection = Await eventSearch.AggregateAsync(New AFAggregateRequest() {binByMonth, groupByElement, overallSummaries}, CancellationToken.None)

    Dim overallSummaryResult As AFSummaryResult = aggregateResults.GetResult(overallSummaries)
    Console.WriteLine("Minimum: {0}, Average: {1}, Maximum: {2}", overallSummaryResult.SummaryResults(AFSummaryTypes.Minimum), overallSummaryResult.SummaryResults(AFSummaryTypes.Average), overallSummaryResult.SummaryResults(AFSummaryTypes.Maximum))

    Dim binByMonthResult As AFBinnedResult(Of AFTime) = aggregateResults.GetResult(binByMonth)
    For Each bin As KeyValuePair(Of AFRange(Of AFTime), IReadOnlyDictionary(Of AFSummaryTypes, AFValue)) In binByMonthResult.BinnedResults
        Console.WriteLine("Month: {0}, Average: {1}, Count: {2}", bin.Key, bin.Value(AFSummaryTypes.Average), bin.Value(AFSummaryTypes.Count))
    Next

    Dim groupByElementResult As AFGroupedResult(Of Object) = aggregateResults.GetResult(groupByElement)
    For Each group As KeyValuePair(Of Object, IReadOnlyDictionary(Of AFSummaryTypes, AFValue)) In groupByElementResult.GroupedResults
        Console.WriteLine("Element: {0}, Average: {1}, Count: {2}", group.Key, group.Value(AFSummaryTypes.Average), group.Value(AFSummaryTypes.Count))
    Next

    Return True
End Using

No code example is currently available or this language may not be supported.

No code example is currently available or this language may not be supported.

Version Information

AFSDK

Supported in: 3.1.1, 3.1.0, 3.0.2, 3.0.1, 3.0.0, 2.10.11, 2.10.5, 2.10.0, 2.10, 2.9.5, 2.9

See Also

In This Topic
Related Links
TitleResults for “How to create a CRG?”Also Available in