Merge "Add Health Analyzers"
diff --git a/tools/lab/health/__init__.py b/tools/lab/health/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tools/lab/health/__init__.py
diff --git a/tools/lab/health/constant_health_analyzer.py b/tools/lab/health/constant_health_analyzer.py
new file mode 100644
index 0000000..5f513ee
--- /dev/null
+++ b/tools/lab/health/constant_health_analyzer.py
@@ -0,0 +1,68 @@
+#!/usr/bin/env python
+#
+#   Copyright 2017 - The Android Open Source Project
+#
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+
+from health import health_analyzer
+
+
+class ConstantHealthAnalyzer(health_analyzer.HealthAnalyzer):
+    """Extends HealthAnalyzer for all HealthAnalyzers that compare to a constant
+
+    Attributes:
+        _key: a string representing a key to a response dictionary
+        _constant: a constant value to compare metric_results[key] to
+    """
+
+    def __init__(self, key, constant):
+        self._key = key
+        self._constant = constant
+
+
+class HealthyIfGreaterThanConstantNumber(ConstantHealthAnalyzer):
+    def is_healthy(self, metric_results):
+        """Returns whether numeric result is greater than numeric constant
+        Args:
+          metric_results: a dictionary of metric results.
+
+        Returns:
+          True if numeric result is greater than numeric constant
+        """
+
+        return metric_results[self._key] > self._constant
+
+
+class HealthyIfLessThanConstantNumber(ConstantHealthAnalyzer):
+    def is_healthy(self, metric_results):
+        """Returns whether numeric result is less than numeric constant
+        Args:
+          metric_results: a dictionary of metric results.
+
+        Returns:
+          True if numeric result is less than numeric constant
+        """
+
+        return metric_results[self._key] < self._constant
+
+
+class HealthyIfEquals(ConstantHealthAnalyzer):
+    def is_healthy(self, metric_results):
+        """Returns whether result is equal to constant
+        Args:
+          metric_results: a dictionary of metric results.
+
+        Returns:
+          True if result is equal to constant
+        """
+        return metric_results[self._key] == self._constant
diff --git a/tools/lab/health/health_analyzer.py b/tools/lab/health/health_analyzer.py
new file mode 100644
index 0000000..d02b7a1
--- /dev/null
+++ b/tools/lab/health/health_analyzer.py
@@ -0,0 +1,34 @@
+#!/usr/bin/env python
+#
+#   Copyright 2017 - The Android Open Source Project
+#
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+
+
+class HealthAnalyzer(object):
+    """Interface class for determining whether metrics are healthy or unhealthy.
+
+    """
+
+    def is_healthy(self, metric_results):
+        """Returns whether a metric is considered healthy
+
+        Function compares the metric mapped to by key it was initialized with
+
+        Args:
+          metric_results: a dictionary of metric results with
+            key = metric field to compare
+        Returns:
+          True if metric is healthy, false otherwise
+        """
+        raise NotImplementedError()
diff --git a/tools/lab/tests/constant_health_analyzer_test.py b/tools/lab/tests/constant_health_analyzer_test.py
new file mode 100644
index 0000000..ebf38b2
--- /dev/null
+++ b/tools/lab/tests/constant_health_analyzer_test.py
@@ -0,0 +1,65 @@
+#!/usr/bin/env python
+#
+#   Copyright 2017 - The Android Open Source Project
+#
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+
+from health import constant_health_analyzer as ha
+import unittest
+
+
+class HealthyIfGreaterThanConstantNumberTest(unittest.TestCase):
+    def test_is_healthy_correct_inputs_return_true(self):
+        sample_metric = {'a_key': 3}
+        analyzer = ha.HealthyIfGreaterThanConstantNumber(
+            key='a_key', constant=2)
+        self.assertTrue(analyzer.is_healthy(sample_metric))
+
+    def test_is_healthy_correct_inputs_return_false(self):
+        sample_metric = {'a_key': 3}
+        analyzer = ha.HealthyIfGreaterThanConstantNumber(
+            key='a_key', constant=4)
+        self.assertFalse(analyzer.is_healthy(sample_metric))
+
+
+class HealthyIfLessThanConstantNumberTest(unittest.TestCase):
+    def test_is_healthy_correct_inputs_return_true(self):
+        sample_metric = {'a_key': 1}
+        analyzer = ha.HealthyIfLessThanConstantNumber(key='a_key', constant=2)
+        self.assertTrue(analyzer.is_healthy(sample_metric))
+
+    def test_is_healthy_correct_inputs_return_false(self):
+        sample_metric = {'a_key': 3}
+        analyzer = ha.HealthyIfLessThanConstantNumber(key='a_key', constant=2)
+        self.assertFalse(analyzer.is_healthy(sample_metric))
+
+
+class HealthyIfEqualsTest(unittest.TestCase):
+    def test_is_healthy_correct_string_inputs_return_true(self):
+        sample_metric = {'a_key': "hi"}
+        analyzer = ha.HealthyIfEquals(key='a_key', constant="hi")
+        self.assertTrue(analyzer.is_healthy(sample_metric))
+
+    def test_is_healthy_correct_num_inputs_return_true(self):
+        sample_metric = {'a_key': 1}
+        analyzer = ha.HealthyIfEquals(key='a_key', constant=1)
+        self.assertTrue(analyzer.is_healthy(sample_metric))
+
+    def test_is_healthy_correct_inputs_return_false(self):
+        sample_metric = {'a_key': 3}
+        analyzer = ha.HealthyIfEquals(key='a_key', constant=2)
+        self.assertFalse(analyzer.is_healthy(sample_metric))
+
+
+if __name__ == '__main__':
+    unittest.main()