[Xfce4-commits] [panel-plugins/xfce4-hardware-monitor-plugin] 50/96: Addition of fixed_max() to monitors, CurveView now scales directly with plotted data

noreply at xfce.org noreply at xfce.org
Thu Nov 27 22:20:55 CET 2014


This is an automated email from the git hooks/post-receive script.

nick pushed a commit to branch master
in repository panel-plugins/xfce4-hardware-monitor-plugin.

commit 7e6df30967cd7d3d24b760e607ff24a02e29b143
Author: Omega Weapon <OmegaPhil at gmail.com>
Date:   Fri Nov 8 20:57:06 2013 +0000

    Addition of fixed_max() to monitors, CurveView now scales directly with plotted data
    
    fixed_max allows the CurveView to keep a fixed scale for monitors
    such as the CPU usage monitor.
    
    CurveView no longer gradually decreases scale of graph over a long
    period of time - the scale is tied to the largest value across the
    curves. This keeps the scale representative at all times, and prevents
    clipping of the curve maxima. This allows for a proper network
    bandwidth graph to be implemented
    
    Fixes #1 / https://github.com/OmegaPhil/hardware-monitor-applet/issues/1
---
 src/curve-view.cpp    |   33 ++++++++++++++++++++++-----
 src/monitor-impls.cpp |   59 ++++++++++++++++++++++++++++++++++++++++++++-----
 src/monitor-impls.hpp |    8 +++++++
 src/monitor.hpp       |   36 ++++++++++++++++--------------
 src/value-history.cpp |   57 ++++++++++++++++++++++++++++++++++++++++++++---
 src/value-history.hpp |    5 ++++-
 6 files changed, 167 insertions(+), 31 deletions(-)

diff --git a/src/curve-view.cpp b/src/curve-view.cpp
index bee5e38..0a15b01 100644
--- a/src/curve-view.cpp
+++ b/src/curve-view.cpp
@@ -39,9 +39,10 @@ class Curve
 public:
   Curve(Monitor *monitor, unsigned int color);
 
-  void update(unsigned int max_samples); // gather info from monitor
-  void draw(Gnome::Canvas::Canvas &canvas, // redraw curve on canvas
-      int width, int height);
+  void update(unsigned int max_samples);  // Gather info from monitor
+  void draw(Gnome::Canvas::Canvas &canvas,  // Redraw curve on canvas
+      int width, int height, double max);
+  double get_max_value();  // Used to get overall max across curves
 
   Monitor *monitor;
   
@@ -66,7 +67,8 @@ void Curve::update(unsigned int max_samples)
     remaining_draws = CanvasView::draw_iterations;
 }
 
-void Curve::draw(Gnome::Canvas::Canvas &canvas, int width, int height)
+void Curve::draw(Gnome::Canvas::Canvas &canvas, int width, int height,
+  double max)
 {
   if (remaining_draws <= 0)
     return;
@@ -94,8 +96,13 @@ void Curve::draw(Gnome::Canvas::Canvas &canvas, int width, int height)
 
   line->property_fill_color_rgba() = color;
   line->property_width_units() = line_width;
+
+  /* Use the actual maxima associated with all curves in the view, unless
+   * the monitor has a fixed max (variable maxes should not be used with
+   * monitors like the CPU usage monitor) */
+  if (monitor->fixed_max())
+      max = monitor->max();
   
-  double max = monitor->max();
   if (max <= 0)
     max = 0.0000001;
 
@@ -120,6 +127,12 @@ void Curve::draw(Gnome::Canvas::Canvas &canvas, int width, int height)
   //std::cout << "In CurveView::draw!\n" << color << "\n";
 }
 
+double Curve::get_max_value()
+{
+  /* Used as part of determination of the max value for all curves in
+   * the view */
+  return value_history.get_max_value();
+}
 
 //
 // class CurveView
@@ -228,6 +241,14 @@ void CurveView::do_detach(Monitor *monitor)
 
 void CurveView::do_draw_loop()
 {
+  double max = 0;
+
+  // Obtain maximum value of all curves in the view
+  for (curve_iterator i = curves.begin(), end = curves.end(); i != end; ++i)
+    if ((*i)->get_max_value() > max)
+      max = (*i)->get_max_value();
+
+  // Draw the curves with the unified max value
   for (curve_iterator i = curves.begin(), end = curves.end(); i != end; ++i)
-    (*i)->draw(*canvas, width(), height());
+    (*i)->draw(*canvas, width(), height(), max);
 }
diff --git a/src/monitor-impls.cpp b/src/monitor-impls.cpp
index 967c165..64d338e 100644
--- a/src/monitor-impls.cpp
+++ b/src/monitor-impls.cpp
@@ -20,6 +20,7 @@
  */
 
 #include <string>
+#include <iostream>
 #include <iomanip>
 #include <ostream>
 #include <sys/time.h>       // for high-precision timing for the network load
@@ -43,8 +44,9 @@
 #include "i18n.hpp"
 
 
-// decay factor for maximum values (log_0.999(0.9) = 105 iterations before
-// reduced 10%)
+/* Decay factor for maximum values (log_0.999(0.9) = 105 iterations
+ * before reduced 10%). This is now no longer used for CurveView - the
+ * actual max value across the ValueHistories is used */
 double const max_decay = 0.999;
 
 
@@ -281,6 +283,11 @@ double CpuUsageMonitor::max()
   return 1;
 }
 
+bool CpuUsageMonitor::fixed_max()
+{
+  return true;
+}
+
 Glib::ustring CpuUsageMonitor::format_value(double val)
 {
   return String::ucompose(_("%1%%"), precision(1), 100 * val);
@@ -349,6 +356,11 @@ double SwapUsageMonitor::max()
   return max_value;
 }
 
+bool SwapUsageMonitor::fixed_max()
+{
+  return false;
+}
+
 Glib::ustring SwapUsageMonitor::format_value(double val)
 {
   val /= 1000000;
@@ -419,6 +431,11 @@ double LoadAverageMonitor::max()
   return max_value;
 }
 
+bool LoadAverageMonitor::fixed_max()
+{
+  return false;
+}
+
 Glib::ustring LoadAverageMonitor::format_value(double val)
 {
   return String::ucompose("%1", precision(1), val);
@@ -496,6 +513,11 @@ double MemoryUsageMonitor::max()
   return max_value;
 }
 
+bool MemoryUsageMonitor::fixed_max()
+{
+  return false;
+}
+
 Glib::ustring MemoryUsageMonitor::format_value(double val)
 {
   val /= 1000000;
@@ -566,6 +588,11 @@ double DiskUsageMonitor::max()
   return max_value;
 }
 
+bool DiskUsageMonitor::fixed_max()
+{
+  return false;
+}
+
 Glib::ustring DiskUsageMonitor::format_value(double val)
 {
   if (val >= 1000 * 1000 * 1000) {
@@ -639,16 +666,18 @@ double NetworkLoadMonitor::do_measure()
   else
     measured_bytes = netload.bytes_out;
 
-  if (byte_count == 0) // no estimate initially
+  if (byte_count == 0) // No estimate initially
     val = 0;
-  else if (measured_bytes < byte_count) // interface was reset
+  else if (measured_bytes < byte_count) // Interface was reset
     val = 0;
   else
     val = measured_bytes - byte_count;
 
   byte_count = measured_bytes;
 
-  if (val != 0)     // reduce scale gradually
+  /* Note - max_value is no longer used to determine the graph max for
+   * Curves - the actual maxima stored in the ValueHistories are used */
+  if (val != 0)     // Reduce scale gradually
     max_value = guint64(max_value * max_decay);
 
   if (val > max_value)
@@ -673,6 +702,10 @@ double NetworkLoadMonitor::do_measure()
     time_stamp_usecs = tv.tv_usec;
   }
 
+  // Debug code
+  /*std::cout << "NetworkLoadMonitor::do_measure: val: " << val <<
+    ", max_value: " << max_value << "\n";*/
+
   return val;
 }
 
@@ -681,6 +714,11 @@ double NetworkLoadMonitor::max()
   return max_value;
 }
 
+bool NetworkLoadMonitor::fixed_max()
+{
+  return false;
+}
+
 Glib::ustring NetworkLoadMonitor::format_value(double val)
 {
   // 1000 ms = 1 s
@@ -972,6 +1010,12 @@ double TemperatureMonitor::max()
   return max_value;
 }
 
+bool TemperatureMonitor::fixed_max()
+{
+  return false;
+}
+
+
 Glib::ustring TemperatureMonitor::format_value(double val)
 {
   // %2 contains the degree sign (the following 'C' stands for Celsius)
@@ -1064,6 +1108,11 @@ double FanSpeedMonitor::max()
   return max_value;
 }
 
+bool FanSpeedMonitor::fixed_max()
+{
+  return false;
+}
+
 Glib::ustring FanSpeedMonitor::format_value(double val)
 {
   // rpm is rotations per minute
diff --git a/src/monitor-impls.hpp b/src/monitor-impls.hpp
index f1a159a..b69d0a3 100644
--- a/src/monitor-impls.hpp
+++ b/src/monitor-impls.hpp
@@ -44,6 +44,7 @@ public:
   CpuUsageMonitor(int cpu_no);  // monitor only cpu no.
 
   virtual double max();
+  virtual bool fixed_max();
   virtual Glib::ustring format_value(double val);
   virtual Glib::ustring get_name();
   virtual Glib::ustring get_short_name();
@@ -70,6 +71,7 @@ public:
   SwapUsageMonitor();
 
   virtual double max();
+  virtual bool fixed_max();
   virtual Glib::ustring format_value(double val);
   virtual Glib::ustring get_name();
   virtual Glib::ustring get_short_name();
@@ -89,6 +91,7 @@ public:
   LoadAverageMonitor();
 
   virtual double max();
+  virtual bool fixed_max();
   virtual Glib::ustring format_value(double val);
   virtual Glib::ustring get_name();
   virtual Glib::ustring get_short_name();
@@ -109,6 +112,7 @@ public:
   MemoryUsageMonitor();
 
   virtual double max();
+  virtual bool fixed_max();
   virtual Glib::ustring format_value(double val);
   virtual Glib::ustring get_name();
   virtual Glib::ustring get_short_name();
@@ -128,6 +132,7 @@ public:
   DiskUsageMonitor(const std::string &mount_dir, bool show_free);
 
   virtual double max();
+  virtual bool fixed_max();
   virtual Glib::ustring format_value(double val);
   virtual Glib::ustring get_name();
   virtual Glib::ustring get_short_name();
@@ -154,6 +159,7 @@ public:
          Direction direction);
 
   virtual double max();
+  virtual bool fixed_max();
   virtual Glib::ustring format_value(double val);
   virtual Glib::ustring get_name();
   virtual Glib::ustring get_short_name();
@@ -187,6 +193,7 @@ public:
   TemperatureMonitor(int no); // no. in the temperature features
 
   virtual double max();
+  virtual bool fixed_max();
   virtual Glib::ustring format_value(double val);
   virtual Glib::ustring get_name();
   virtual Glib::ustring get_short_name();
@@ -209,6 +216,7 @@ public:
   FanSpeedMonitor(int no);  // no. in the fan features
 
   virtual double max();
+  virtual bool fixed_max();
   virtual Glib::ustring format_value(double val);
   virtual Glib::ustring get_name();
   virtual Glib::ustring get_short_name();
diff --git a/src/monitor.hpp b/src/monitor.hpp
index f4809dc..757c50c 100644
--- a/src/monitor.hpp
+++ b/src/monitor.hpp
@@ -44,15 +44,15 @@ public:
   virtual ~Monitor()
   {}
 
-  // update the measured value from device
+  // Update the measured value from device
   void measure()
   {
     measured_value = do_measure();
-    if (measured_value < 0) // safety check
+    if (measured_value < 0)  // Safety check
       measured_value = 0;
   }
   
-  // fetch the currently measured value
+  // Fetch the currently measured value
   double value()
   {
     return measured_value;
@@ -68,37 +68,40 @@ public:
     return settings_dir;
   }
   
-  // the max value that the monitor may attain
+  // The max value that the monitor may attain
   virtual double max() = 0;
 
-  // convert float to string which represents an actual number with the
-  // appropriate unit
+  // Indicate whether the monitor's max is fixed or not
+  virtual bool fixed_max() = 0;
+
+  /* Convert float to string which represents an actual number with the
+   * appropriate unit */
   virtual Glib::ustring format_value(double val) = 0;
 
-  // return a descriptive name
+  // Return a descriptive name
   virtual Glib::ustring get_name() = 0;
 
-  // return a short name
+  // Return a short name
   virtual Glib::ustring get_short_name() = 0;
 
-  // the interval between updates in milliseconds
+  // The interval between updates in milliseconds
   virtual int update_interval() = 0;
 
-  // save information about the monitor
+  // Save information about the monitor
   virtual void save(XfceRc *settings) = 0;
 
-  // load any internal monitor state
+  // Load any internal monitor state
   virtual void load(XfceRc *settings)
   {
   }
 
-  // if other is watching the same thing as this monitor, it might be
-  // a good idea to sync maxima with it
+  /* If other is watching the same thing as this monitor, it might be
+   * a good idea to sync maxima with it */
   virtual void possibly_add_sync_with(Monitor *other)
   {
   }
 
-  // remove a synchronisation
+  // Remove a synchronisation
   virtual void remove_sync_with(Monitor *other)
   {
   }
@@ -107,7 +110,8 @@ protected:
   double measured_value;
   
 private:
-  // perform actual measurement, for derived classes
+
+  // Perform actual measurement, for derived classes
   virtual double do_measure() = 0;
 
   Glib::ustring settings_dir;
@@ -115,7 +119,7 @@ private:
 
 
 //
-// helpers implemented in monitor-impls.cpp
+// Helpers implemented in monitor-impls.cpp
 //
 
 typedef std::list<Monitor *> monitor_seq;
diff --git a/src/value-history.cpp b/src/value-history.cpp
index 0a24e55..8161ee7 100644
--- a/src/value-history.cpp
+++ b/src/value-history.cpp
@@ -19,6 +19,9 @@
  * USA.
  */
 
+#include <algorithm>
+#include <iostream>
+
 #include "value-history.hpp"
 #include "monitor.hpp"
 #include "applet.hpp"
@@ -31,6 +34,13 @@ ValueHistory::ValueHistory(Monitor *mon)
   waits_remaining = 0;
 }
 
+double ValueHistory::get_max_value()
+{
+  /* This is used so that the maximum displayed point on the graph is
+   * always known */
+  return max_value;
+}
+
 void ValueHistory::update(unsigned int max_samples, bool &new_value)
 {
   --waits_remaining;
@@ -38,14 +48,55 @@ void ValueHistory::update(unsigned int max_samples, bool &new_value)
   if (waits_remaining <= 0) {
     new_value = true;
     monitor->measure();
-    values.push_front(monitor->value());
+
+    // Fetching new measurement
+    double measurement = monitor->value();
+
+    // Dealing with new max measurements
+    if (measurement > max_value)
+    {
+      max_value = measurement;
+      max_count = 1;
+    }
+    else if (measurement == max_value)
+      ++max_count;
+
+    // Saving data and resetting waits
+    values.push_front(measurement);
     waits_remaining = wait_iterations;
   }
   else
     new_value = false;
   
-  // get rid of extra samples (there may be more than one if user changes
-  // configuration)
+  /* Get rid of extra samples (there may be more than one if user changes
+   * configuration */
   while (values.size() > max_samples)
+  {
+    // Removing last value - saving to allow one pop_back at the top
+    double last_value = values.back();
     values.pop_back();
+
+    // Detecting dropping max values
+    if (last_value == max_value)
+    {
+      --max_count;
+
+      /* Determining the new maximum value and count if all of the
+       * previous maxes have been dropped */
+      if (max_count < 1)
+      {
+
+        // Debug code
+        /*std::cout << "ValueHistory::update: Dropping samples, dropping "
+        "max detected: " << max_value << ", count: " << max_count << "\n";*/
+
+        max_value = *std::max_element(values.begin(), values.end());
+        max_count = std::count(values.begin(), values.end(), max_value);
+
+        // Debug code
+        /*std::cout << "New max: " << max_value << ", new count: " <<
+          max_count << "\n";*/
+      }
+    }
+  }
 }
diff --git a/src/value-history.hpp b/src/value-history.hpp
index acfb8b7..41ebaa3 100644
--- a/src/value-history.hpp
+++ b/src/value-history.hpp
@@ -33,6 +33,8 @@ public:
 
   // perform a measurement if needed, new_value is set to true if it was
   void update(unsigned int max_samples, bool &new_value);
+
+  double get_max_value();
   
   // the past values
   typedef std::deque<double> sequence;
@@ -41,7 +43,8 @@ public:
 
 private:
   Monitor *monitor;
-  int wait_iterations, waits_remaining;
+  int wait_iterations, waits_remaining, max_count;
+  double max_value;
 };
 
 

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list