health metric: check hysteresis for value notify

Add hysteresis for metric value and check it before notifying the metric
value update.

Change-Id: I8dace4b9436dd9b16f1a075505079d5001928f52
Signed-off-by: Jagpal Singh Gill <paligill@gmail.com>
diff --git a/health_metric.cpp b/health_metric.cpp
index 31feb55..2f23394 100644
--- a/health_metric.cpp
+++ b/health_metric.cpp
@@ -2,6 +2,7 @@
 
 #include <phosphor-logging/lg2.hpp>
 
+#include <cmath>
 #include <numeric>
 #include <unordered_map>
 
@@ -12,6 +13,8 @@
 
 using association_t = std::tuple<std::string, std::string, std::string>;
 
+static constexpr double hysteresis = 1.0;
+
 auto HealthMetric::getPath(MType type, std::string name, SubType subType)
     -> std::string
 {
@@ -205,9 +208,25 @@
     }
 }
 
+auto HealthMetric::shouldNotify(MValue value) -> bool
+{
+    if (std::isnan(value.current))
+    {
+        return true;
+    }
+    auto changed = std::abs((value.current - lastNotifiedValue) /
+                            lastNotifiedValue * 100.0);
+    if (changed >= hysteresis)
+    {
+        lastNotifiedValue = value.current;
+        return true;
+    }
+    return false;
+}
+
 void HealthMetric::update(MValue value)
 {
-    ValueIntf::value(value.current);
+    ValueIntf::value(value.current, !shouldNotify(value));
 
     // Maintain window size for threshold calculation
     if (history.size() >= config.windowSize)