IPIData2 Example

An example of using the various methods of the IPIData2 interface.


Private Sub Summaries2_Click()
On Error GoTo errhandler

' This example demonstrates the Summaries2 and FilteredSummaries methods in the IPIData2
' interface as well as the PercentTrue method in the IPICalculation interface.

' First use Summaries2 to compute the daily time-weighted total for sinusoid for three days.
' Then call FilteredSummaries for all summary types for the same periods with a filter expression
'     depending on a different tag.
' Also call PercentTrue for the same filter expression for the same periods.
' Demonstrate that the amounts of time when the filter expression is true computed from PercentTrue
'     and those computed from FilteredSummaries using time-weighted Count are the same.
' Demonstrate that there is a difference in the daily time-weighted total when filter is applied.
' Finally, demonstrate how to access the additional summary result attributes for daily Maximum.

Dim srv As Server
Dim pt As PIPoint
Dim pdata As PIData
Dim ipid2 As IPIData2, ipiCalc As IPICalculation
Dim nvsSum As NamedValues, nvsFilSum As NamedValues
Dim valsum As PIValues, valfilsum As PIValues, valfiltime As PIValues, valtimetrue As PIValues
Dim filexpr As String, startTime As String, endTime As String, duration As String
Dim nv As NamedValue, msg As String
Dim filtime As Double, stime As PITime, etime As PITime, maxtime As PITime
Dim stime2 As PITime, etime2 As PITime

    startTime = "y-2d" ' three days ago
    endTime = "t" ' today
    duration = "1d" ' one day calculation period
    filexpr = "('ba:phase.1' = ""phase1"")"

    Set srv = Servers("localhost")
    Set ipiCalc = srv                         ' get pointer to IPICalculation Interface
    Set pt = srv.PIPoints("sinusoid")  ' perform summary calls on tag sinusoid
    Set pdata = pt.Data
    Set ipid2 = pdata                        ' get pointer to IPIData2 Interface

' calculate daily total for three days on sinusoid
    Set nvsSum = ipid2.Summaries2(startTime, endTime, duration, asTotal, cbTimeWeighted)
' extract the pivalues for total from the result
    Set valsum = nvsSum("Total").Value
' calculate the daily summary for sinusoid only when the batches are at phase1
' do all the summary types in one call
' note that fstExpRecordedValues is only supported by PI 3.4 and above
    Set nvsFilSum = ipid2.FilteredSummaries(startTime, endTime, duration, _
                                                                  filexpr, asAll, cbTimeWeighted, fstExpRecordedValues)
' extract the time-weighted total and Count from the FilteredSummaries result
    Set valfilsum = nvsFilSum("Total").Value
' The time-weighted Count returns the number of seconds when the calculation is active
' (i.e. pass the filter test)
    Set valfiltime = nvsFilSum("Count").Value
' Calculate the actual amount of time the batches are at phase1 using the PercentTrue method
    Set valtimetrue = ipiCalc.PercentTrue(startTime, endTime, duration, filexpr, fstExpRecordedValues)

' Check and Compare the results
    If (valsum.Count <> 3 Or valfilsum.Count <> 3 Or valfiltime.Count <> 3 Or _
         valtimetrue.Count <> 3) Then
        MsgBox "Result count is not correct"
        Exit Sub
    End If
    Dim I As Integer
    For I = 1 To 3
' start time and end time of each calculation period are stored in the
' pivalue.ValueAttributes for PercentTrue
        Set nv = valtimetrue(I).ValueAttributes("EarliestTime")
        Set stime = nv.Value
        Set nv = valtimetrue(I).ValueAttributes("MostRecentTime")
        Set etime = nv.Value
' Same start time and end time information from the pivalues generated
' by the Summaries methods
        Set nv = valsum(I).ValueAttributes("EarliestTime")
        Set stime2 = nv.Value
        Set nv = valsum(I).ValueAttributes("MostRecentTime")
        Set etime2 = nv.Value
' the start time and the end time from all these pivalues should be the same
' because the time specification are the same
        If (Abs(stime.UTCSeconds - stime2.UTCSeconds) > 0.0001 Or _
             Abs(etime.UTCSeconds - etime2.UTCSeconds) > 0.0001) Then
            MsgBox "Start time or End time not match among the pivalues"
            Exit Sub
        End If
' amount of time when filter is active from PercentTrue result is (percent true) * (period) / 100.
        filtime = valtimetrue(I).Value * (etime.UTCSeconds - stime.UTCSeconds) / 100#
' compare filtime with the time-weighted Count result from FilteredSummaries call
        If (Abs(filtime - valfiltime(I).Value) > 2) Then
            msg = "At period " & CStr(I) & " filter time result does not match"
            MsgBox msg
        End If
        Debug.Print I, stime.LocalDate, etime.LocalDate, _
                          valsum(I).Value, valfilsum(I).Value, filtime, valfiltime(I).Value
' print percent good for the total in the period
        Debug.Print "percend good: ", valsum(I).ValueAttributes("Percentgood").Value, _
        Debug.Print " "
    Next I

' Examine the daily Maximum sinusoid when the batch is at phase1
    Set valfilsum = nvsFilSum("Maximum").Value
    Debug.Print "period", "Maxtime", "MaxValue"
    For I = 1 To 3
' extract the maximum time from the pivalue ValueAttributes
        Set nv = valfilsum(I).ValueAttributes("TimeOfMaxVal")
        Set maxtime = nv.Value
        Debug.Print I, maxtime.LocalDate, valfilsum(I).Value
    Next I

' Will not find Maximum in the NameValues returned by the Summaries2 call
' because we only ask for total
    On Error Resume Next
    Set nv = nvsSum("Maximum")
    If (Err.Number <> 0) Then
        MsgBox "It is OK not to find Maximum data when we only ask for Total"
        MsgBox "Something is wrong, get Maximum data when we only ask for Total"
    End If
' Will not find TimeOfMaxVal in PIvalues of a Total summary result
    Set nv = valsum(1).ValueAttributes("TimeOfMaxVal")
    If (Err.Number <> 0) Then
        MsgBox "It is OK not to find TimeOfMaxVal attribute in a PIvalue for a total"
        MsgBox "Something is wrong, get TimeOfMaxVal attribute in a PIvalue for a total"
    End If
    Exit Sub

    MsgBox (Err.Description)
    Exit Sub
End Sub

Enabling Operational Intelligence