3 Replies Latest reply on Nov 25, 2015 11:17 PM by Mike Zboray

    Efficient way to iterate through notifications

    DSmith1

      I'm writing a small program that will iterate through the notifications on an AF database and check which are running.

       

      I am used to just iterating through lists; but I'm trying to better follow OSI's AFSDK coding guidelines and bulk load.

      See my code example below. I connect to a database, bulk load some of the notifications and then attempt different ways to track how many are running / not running.

       

       

       

      // Connect to AF database
      
      
      Console.WriteLine("Connected to AF database: " + targetDatabase);
      
      // Collect the Fault Distance notifications and compare the total count to how many have an 'Enabled' status
      
      
      AFNamedCollectionList<AFNotification> myNotifications = AFNotification.FindNotifications(targetAFDB, "*", 
      
      AFSearchField.Name, AFSortField.Name, AFSortOrder.Ascending, 1000);
      
      //AFNamedCollectionList<AFNotification> erroredNotifications = (AFNamedCollectionList<AFNotification>)myNotifications.Where(n=>n.GetStatus().ToString().Equals("Enabled"));
      
      
      //nonRunningNotifications = erroredNotifications.Count;
      
      
      foreach (AFNotification afNote in myNotifications)
      
      if (!afNote.GetStatus().ToString().Equals("Enabled"))
      
      //This unfortunately does nothing, what is this function for if it returns void?
      
      
      AFNotification.GetStatus(myNotifications);
      

       

       

      A couple of questions:

      1. (line 11) I failed at trying to use a predicate. Can someone help me sort out how I'd collect all notifications that are not running?
      2. (line 13) How do developers use AFNotification's GetStatus function? It returns an AFStatus object, but that's just a status that says "Enabled" or something like that. Do people traditionally just call its "ToString()" function to compare?
      3. (line 16) What is the point of the AFNotification.GetStatus() function? It takes in an AFNamedCollectionList of AFnotifications but its return type is void. Is it supposed to do something to my list of notifications?
      4. Where would I get the ANNotifications class? I've seen other users on here use it and I cannot see it in my .NET project. I heard it's part of ANSDK but I cannot find that
        • Re: Efficient way to iterate through notifications
          Mike Zboray

          1. There is no way to filter Notifications by status on the server. You have to pull them into the client application and filter there.

           

          2. Usually you would would write something like

           

          var enabledNotifications = myNotifications.Where(n => n.Status == AFStatus.Enabled);
          

           

          or

           

          if (afNote.Status == AFStatus.Enabled)
          

           

          The difference between Status and GetStatus() is that the former is cached while the latter goes to the AF server to retrieve the latest status.

           

          3. The static method updates the Status property of the notifications you pass in. So yes it does something to the list. The point of this is to a) give you a way to update statuses in bulk, b) not require a full refresh of the notification to get the status.

           

          4. ANNotification and related classes are in the ANSDK which comes with the Notifications client. You reference OSIsoft.PIAnalytics.Common and OSIsoft.PIAnalytics.SDK to use it.

            • Re: Efficient way to iterate through notifications
              DSmith1

              Thank you very much for your responses.  Answer #3 doesn't make sense to me yet.

               

              The function takes in a list of AF notification elements and "gets their status". But the function returns nothing. I don't understand how that will change them because it's not doing anything or storing the data somewhere. Or is it?

               

              I can see the "setStatus" function being useful - setting statuses to AF notifications in bulk. But not getting them and nothing happens.

                • Re: Efficient way to iterate through notifications
                  Mike Zboray

                  The static method AFNotification.GetStatus will retrieve the statuses from the server (in bulk) and then it actually mutates the AFNotification object by setting Status property for each notification in the list. This does not cause a checkout or update/refresh anything else on the notifications that you passed in. I agree it's a bit confusing because the name defies standard .NET naming conventions.

                   

                  The instance method GetStatus() returns an AFStatus and also updates the Status property.