7 Replies Latest reply on Jan 31, 2013 3:57 PM by tmcclure

    Retrieving All Details of a Notification

    tmcclure

      This is a two-part question...

       

       

       

      PINotificationViewer displays the following columns:

      • Start Time
      • End Time
      • Duration
      • State
      • Priority
      • Acknowledged
      • Action
      • Contact
      • Contact Method
      • Comment 
      .
      .
      .
      1.  I am able to extract the data from most of these columns using code similar to this:
      foreach (ANInstance instance in instances.Instances)
      {
           string startTime = instance.StartTime.ToString():
      }
      But I have yet to discover how to extract the data from the "Action" column.  Any ideas?
      .
      .
      .
      2. I am able to extract the details relating to the contacts by using the following code:

      foreach (ANContactEvent contactEvent in instance.ContactEvents)
      {
           try
           {
                string contactName = AFNotificationContactTemplate.FindNotificationContactTemplate(_afServer, contactEvent.ContactID).Name;
           }

       

      Any ideas how to extract the data from the "Comment" column?

        • Re: Retrieving All Details of a Notification
          mhamel

          @Todd: These pieces of information are stored under the ContactEvents property of you current notification event instance. This property returns a ContactEvent object that you can query to get the time, the acknowledgement status, the actions, the comments, etc.

           

          I have added an example that I used recently to demonstrate how.

           
          'Get a reference on the database.        
          _MyDB = _PISystem.Databases("MyDB")
          
          'Contain all AFNotification objects that meet the criteria applied to the FindNotifications metho.
          Dim MyAFNotificationsList As AFNamedCollectionList(Of Notification.AFNotification) = Nothing
          'Transform the AFNotification objects from the AF to AN world.
          Dim MyANNotificationList As ANNotificationList = Nothing
          'Contain the list of all notifications accross all AFNotification objects.
          Dim NotificationInstancesList As List(Of ANNotificationInstances) = Nothing
          'Contain all avent instances of a given notification
          Dim NotificationInstances As ANNotificationInstances = Nothing
          Dim ContactEvent As ANContactEvent
          'This object will hold the events were creation/closure of a notification have taken place or
          'events were information was sent throughout delivery channel such as the e-mail.
          Dim NotificationEventInstance As ANInstance = Nothing
          Dim StartTime As Time.AFTime = New Time.AFTime("*-7d")
          Dim EndTime As Time.AFTime = New Time.AFTime("*")
          Dim ContactName As String
          Dim ActionInString As String = ""
          Dim MessageTemplate As String = "Notification {0} last triggered at {1} and is {2}"
          Dim MessageTemplate2 As String = "Action: {0} took place at {1}"
          
          'Get the AFNotification objects.
          MyAFNotificationsList = OSIsoft.AF.Notification.AFNotification.FindNotifications(_MyDB, "*", AFSearchField.Name, AFSortField.Name, AFSortOrder.Ascending, 100)
          'Transform them from AF to AN world.
          MyANNotificationList = New ANNotificationList(MyAFNotificationsList)
          'Get the list of all notification instances accross all AFNotification objects between the start and end time.
          NotificationInstancesList = MyANNotificationList.GetInstances(StartTime, EndTime)
          
          'Browse each instance when a notification took place.
          For Each NotificationInstances In NotificationInstancesList
               'Browse event instances to 
               If NotificationInstances.Instances IsNot Nothing And NotificationInstances.Instances.Count > 0 Then                
                    For Each NotificationEventInstance In NotificationInstances.Instances
                         'Process your StartTime, EndTime, IsAcknowledged properties
                         Console.WriteLine( _
                                   String.Format(MessageTemplate, _
                                                    MyAFNotificationsList(NotificationInstances.NotificationID).Name, _
                                                    NotificationEventInstance.StartTime.ToString("yyyy-MM-dd HH:mm:ss"), _
                                                    IIf(NotificationEventInstance.IsAcknowledged, "acknowledged", "unacknowledged")))
          
                         For Each ContactEvent In NotificationEventInstance.ContactEvents
                              Select Case ContactEvent.Action
                                   Case ANContactEventActions.AcknowledgedAuto
                                        ActionInString = "Instance acknowledged automatically"
                                   Case ANContactEventActions.Commented
                                        ActionInString = "Instance acknowledged automatically"
                                   Case ANContactEventActions.Sent
                                        ActionInString = "Sent"
                                   Case ANContactEventActions.AckManualNotificationWithComment
                                        ActionInString = ContactEvent.Comment
                                        'Case ...
                              End Select
          
                              'Get the contact name.
                              ContactName = OSIsoft.AF.Notification.AFNotificationContactTemplate.FindNotificationContactTemplate(_PISystem, ContactEvent.ContactID).Name
          
                              'Process the action and time.
                              Console.WriteLine(String.Format(MessageTemplate2, ActionInString, ContactEvent.Time.ToString("yyyy-MM-dd HH:mm:ss")))
                         Next
                    Next
               End If
          Next
          

           

           

           

           

          I hope this helped!

            • Re: Retrieving All Details of a Notification
              tmcclure

              This was extremely helpful!  Thank you.  I was searching under the contactEvent.ContactID because that is where I was finding everything else.  Thank you for the example as well.

                • Re: Retrieving All Details of a Notification
                  tmcclure

                  Do you know if there is a way to also display the time that a notification is acknowledged?  I am not sure if this is stored in AF.

                    • Re: Retrieving All Details of a Notification
                      mhamel

                      @Todd: Yes it is possible. An ANInstance object is generated when someone is acknowledging a notification. You will find when this action took place by looking at the ANInstance.StartTime property.

                       

                      You have different level of acknowledgment: subscription or instance level. The former one is when a contact answers to notification, but does not stop further notification from being sent to subscribers, the latter will acknowledge and stop further notification from being sent to subscribers.

                        • Re: Retrieving All Details of a Notification
                          tmcclure

                          I am still trying to discover on what level this is placed.  In my code, I dig down several levels:

                           

                          foreach (ANNotificationInstances instances in history)

                           

                                             {

                           

                                              foreach (ANInstance instance in instances.Instances)

                           

                                                         {

                           

                                                         foreach (ANContactEvent contactEvent in instance.ContactEvents)

                           

                                                             {

                           

                          I of course have code under each of those levels, but is the StartTime of the acknowledgement found under the contactEvent level?

                           

                          I already have the StartTime of the notification, and so far that is all I am able to obtain.  I am trying to get the time that someone acknowledges a notification.