dut_status: Add new metric to record status of dut.

BUG=chromium:637442
TEST=None

Change-Id: I9bdd9c237dde0bc3e7656544e72902d57a7d01c3
Reviewed-on: https://chromium-review.googlesource.com/386324
Commit-Ready: David Riley <[email protected]>
Tested-by: David Riley <[email protected]>
Reviewed-by: Aviv Keshet <[email protected]>
Reviewed-by: Dan Shi <[email protected]>
diff --git a/scheduler/rdb_hosts.py b/scheduler/rdb_hosts.py
index 0011ebc..b02cab1 100644
--- a/scheduler/rdb_hosts.py
+++ b/scheduler/rdb_hosts.py
@@ -179,6 +179,8 @@
 
     _HOST_WORKING_METRIC = metrics.Boolean('chromeos/autotest/dut_working',
                                            reset_after=True)
+    _HOST_STATUS_METRIC = metrics.Boolean('chromeos/autotest/dut_status',
+                                          reset_after=True)
     _HOST_POOL_METRIC = metrics.String('chromeos/autotest/dut_pool',
                                        reset_after=True)
 
@@ -240,14 +242,52 @@
         metadata_reporter.queue(metadata)
 
 
+    def get_metric_fields(self):
+        """Generate default set of fields to include for Monarch.
+
+        @return: Dictionary of default fields.
+        """
+        fields = {
+            'dut_host_name': self.hostname,
+            'board': self.board or '',
+        }
+
+        return fields
+
+
+    def record_pool(self, fields):
+        """Report to Monarch current pool of dut.
+
+        @param fields   Dictionary of fields to include.
+        """
+        pool = ''
+        if len(self.pools) == 1:
+            pool = self.pools[0]
+        if pool in lab_inventory.MANAGED_POOLS:
+            pool = 'managed:' + pool
+
+        self._HOST_POOL_METRIC.set(pool, fields=fields)
+
+
     def set_status(self, status):
         """Proxy for setting the status of a host via the rdb.
 
         @param status: The new status.
         """
+        # Update elasticsearch db.
         self._update({'status': status})
         self.record_state('host_history', 'status', status)
 
+        # Update Monarch.
+        fields = self.get_metric_fields()
+        self.record_pool(fields)
+        # As each device switches state, indicate that it is not in any
+        # other state.  This allows Monarch queries to avoid double counting
+        # when additional points are added by the Window Align operation.
+        for s in rdb_models.AbstractHostModel.Status.names:
+            fields['status'] = s
+            self._HOST_STATUS_METRIC.set(s == status, fields=fields)
+
 
     def record_working_state(self, working, timestamp):
         """Report to Monarch whether we are working or broken.
@@ -258,19 +298,9 @@
                           manual intervention.
         @param timestamp  Time that the status was recorded.
         """
-        fields = {
-            'dut_host_name': self.hostname,
-            'board': self.board or '',
-        }
-
-        pool = ''
-        if len(self.pools) == 1:
-            pool = self.pools[0]
-        if pool in lab_inventory.MANAGED_POOLS:
-            pool = 'managed:' + pool
-
+        fields = self.get_metric_fields()
         self._HOST_WORKING_METRIC.set(working, fields=fields)
-        self._HOST_POOL_METRIC.set(pool, fields=fields)
+        self.record_pool(fields)
 
 
     def update_field(self, fieldname, value):