Mattpdrexel

PI AF SDK - Get Most Recent Event Frame

Discussion created by Mattpdrexel on May 21, 2020
Latest reply on May 21, 2020 by Mattpdrexel

Question:

Using the PI AF SDK, what is the most efficient way to search for the most recent Event Frame instance associated with a given list of Elements or Notification Rules? 

 

Background:

I am working on an app that will allow users to view Event Frames and Notifications in a custom built UI. The UI allows the user to select an element from the hierarchy, and each Event Frame Notification rule is displayed for all elements at or below that point in the hierarchy. The goal is to make managing and viewing Event Frames easier for our analyst. 

 

In addition to displaying the notification rules, I also load the most recent Event Frame instance associated with that rule. This is proving to be the most expensive query of my code, sometimes taking a full minute to load. 

 

See below for the code I'm currently using for this purpose:

 

//elementBatch is a sample of 15 elements from the hiearchy
//I limit it to 15 since it takes too long to load code for all 300+ elements
//The rest of the elements are loaded on demand as user selects next page of results

int maxParallel = System.Environment.ProcessorCount;
Parallel.ForEach(elementBatch, new ParallelOptions { MaxDegreeOfParallelism = maxParallel }, element =>
{
   if (element != null)
   {
      AFNotificationRules rules = element.NotificationRules;
                  
      foreach (var rule in rules)
      {                       
         AFDatabase database = rule.Database;
         AFEventFrame Last_Instance = null;
         DateTime? notificationSentDate = null;

         string ruleCriteria = rule.Criteria.ToString();
         if (ruleCriteria.Contains("Analysis") & automaticallyLoadEventFramesValue == "ON")
         {
            //This is the slow part of the code
            string query = string.Format("Element:'{0}' {1}", element.GetPath(database), ruleCriteria);
            AFEventFrameSearch search = new AFEventFrameSearch(database, "", AFSearchMode.Overlapped, startTime, endTime, query); //Change AFSearchMode as desired             
            search.CacheTimeout = TimeSpan.FromMinutes(15);
            IEnumerable<AFEventFrame> instances = search.FindEventFrames(startIndex: 0, fullLoad: false, pageSize: 1);
            Last_Instance = instances.LastOrDefault();
                              
          }

 

This method can become slow on line 25, when generating the IEnumberable of Event Frames.

This search seems like the recommended way to find event frames by the community. 

 

However, it seems like there should be a more efficient way to retrieve ONLY the most recently generated Event Frames for a given list of elements or notification rules. 

 

Is there a way to use a bulk query or other alternatives to perform this query faster?

 

Any suggestions would be highly appreciated!

 

NOTE:

1.) See attached for screenshot of what the application result looks like.

2.) I'm not a professional developer, just picked this up over the last year, so may be missing something critical.  

Attachments

Outcomes