Determining Collector Downtime

The following code sample determines the amount of downtime, in seconds, that the Calculation collector has experienced over the last day. Downtime occurs when there are two consecutive bad quality data points for the pulse tag. If the last known data point for the pulse tag is bad quality, all the time between its timestamp and the current time is regarded as downtime. In the following sample, the pulse tag is configured to be polled, with a collection interval of one day.
Dim pulseTag, totalDownTime, startTime, endTime
Dim prevTime, prevQuality, lastPrevTime, lastPrevQuality 
pulseTag = "calcPulseTag"
totalDownTime = 0 
endTime = CurrentTime()
startTime = DateAdd("d", -1, endTime)
lastPrevTime = curTime lastPrevQuality = 0
Do
  'get the timestamp and quality of the tag value previous to the last one we checked
  On Error Resume Next
  prevTime = PreviousTime(pulseTag, lastPrevTime) 
  If Err.Number <> 0 Then
    'no more values for this tag exit gracefully
    Exit Do
End If
prevQuality = PreviousQuality(pulseTag, lastPrevTime)
'if we have two consecutive bad data points, add to the downtime
If prevQuality = 0 And lastPrevQuality = 0 Then
  If prevTime > startTime Then
    totalDownTime = totalDownTime + DateDiff("s", prevTime, lastPrevTime) 
Else
    totalDownTime = totalDownTime + DateDiff("s", startTime, lastPrevTime) 
End If
End If
  'store the timestamp and quality for comparison with the next values 
lastPrevQuality = prevQuality
  lastPrevTime = prevTime
Loop While lastPrevTime > startTime
Result = totalDownTime