github.com/xmidt-org/webpa-common@v1.11.9/device/devicehealth/health.go (about)

     1  package devicehealth
     2  
     3  import (
     4  	"github.com/xmidt-org/webpa-common/device"
     5  	"github.com/xmidt-org/webpa-common/health"
     6  )
     7  
     8  const (
     9  	DeviceCount                      health.Stat = "DeviceCount"
    10  	TotalWRPRequestResponseProcessed health.Stat = "TotalWRPRequestResponseProcessed"
    11  	TotalPingMessagesReceived        health.Stat = "TotalPingMessagesReceived"
    12  	TotalPongMessagesReceived        health.Stat = "TotalPongMessagesReceived"
    13  	TotalConnectionEvents            health.Stat = "TotalConnectionEvents"
    14  	TotalDisconnectionEvents         health.Stat = "TotalDisconnectionEvents"
    15  )
    16  
    17  // Options is an array of all the health Options exposed via this package
    18  var Options = []health.Option{
    19  	DeviceCount,
    20  	TotalWRPRequestResponseProcessed,
    21  	TotalPingMessagesReceived,
    22  	TotalPongMessagesReceived,
    23  	TotalConnectionEvents,
    24  	TotalDisconnectionEvents,
    25  }
    26  
    27  // Listener provides a device.Listener that dispatches health statistics
    28  type Listener struct {
    29  	Dispatcher health.Dispatcher
    30  }
    31  
    32  // OnDeviceEvent is a device.Listener that will dispatched health events to the configured
    33  // health Dispatcher.
    34  func (l *Listener) OnDeviceEvent(e *device.Event) {
    35  	switch e.Type {
    36  	case device.Connect:
    37  		l.Dispatcher.SendEvent(func(s health.Stats) {
    38  			s[DeviceCount] += 1
    39  			s[TotalConnectionEvents] += 1
    40  		})
    41  
    42  	case device.Disconnect:
    43  		l.Dispatcher.SendEvent(func(s health.Stats) {
    44  			s[DeviceCount] -= 1
    45  			s[TotalDisconnectionEvents] += 1
    46  		})
    47  
    48  	case device.TransactionComplete:
    49  		l.Dispatcher.SendEvent(func(s health.Stats) {
    50  			s[TotalWRPRequestResponseProcessed] += 1
    51  		})
    52  	}
    53  }