luci_statistics: Add y_min, y_max, units_exponent options and clean up some graphs
[project/luci.git] / applications / luci-statistics / luasrc / statistics / rrdtool.lua
index cf420fe..dbcae9d 100644 (file)
@@ -17,12 +17,12 @@ module("luci.statistics.rrdtool", package.seeall)
 
 require("luci.statistics.datatree")
 require("luci.statistics.rrdtool.colors")
-require("luci.statistics.rrdtool.definitions")
 require("luci.statistics.i18n")
 require("luci.model.uci")
 require("luci.util")
 require("luci.sys")
-require("luci.fs")
+
+local fs = require "nixio.fs"
 
 
 Graph = luci.util.class()
@@ -34,12 +34,6 @@ function Graph.__init__( self, timespan, opts )
        local uci = luci.model.uci.cursor()
        local sections = uci:get_all( "luci_statistics" )
 
-       -- helper classes
-       self.colors = luci.statistics.rrdtool.colors.Instance()
-       self.defs   = luci.statistics.rrdtool.definitions.Instance()
-       self.tree   = luci.statistics.datatree.Instance()
-       self.i18n   = luci.statistics.i18n.Instance( self )
-
        -- options
        opts.timespan  = timespan       or sections.rrdtool.default_timespan or 900
        opts.rrasingle = opts.rrasingle or ( sections.collectd_rrdtool.RRASingle == "1" )
@@ -50,6 +44,11 @@ function Graph.__init__( self, timespan, opts )
        opts.rrdpath   = opts.rrdpath:gsub("/$","")
        opts.imgpath   = opts.imgpath:gsub("/$","")
 
+       -- helper classes
+       self.colors = luci.statistics.rrdtool.colors.Instance()
+       self.tree   = luci.statistics.datatree.Instance(opts.host)
+       self.i18n   = luci.statistics.i18n.Instance( self )
+
        -- rrdtool default args
        self.args = {
                "-a", "PNG",
@@ -96,7 +95,7 @@ function Graph._rrdtool( self, def, rrd )
 
        -- prepare directory
        local dir = def[1]:gsub("/[^/]+$","")
-       luci.fs.mkdir( dir, true )
+       fs.mkdirr( dir )
 
        -- construct commandline
        local cmdline = "rrdtool graph"
@@ -157,11 +156,14 @@ function Graph._generic( self, opts, plugin, plugin_instance, dtype, index )
 
                if not ds or ds:len() == 0 then ds = "value" end
 
-               _tif( _args, "DEF:%s_avg=%s:%s:AVERAGE", inst, rrd, ds )
+               _tif( _args, "DEF:%s_avg_raw=%s:%s:AVERAGE", inst, rrd, ds )
+               _tif( _args, "CDEF:%s_avg=%s_avg_raw,%s", inst, inst, source.transform_rpn )
 
                if not self.opts.rrasingle then
-                       _tif( _args, "DEF:%s_min=%s:%s:MIN", inst, rrd, ds )
-                       _tif( _args, "DEF:%s_max=%s:%s:MAX", inst, rrd, ds )
+                       _tif( _args, "DEF:%s_min_raw=%s:%s:MIN", inst, rrd, ds )
+                       _tif( _args, "CDEF:%s_min=%s_min_raw,%s", inst, inst, source.transform_rpn )
+                       _tif( _args, "DEF:%s_max_raw=%s:%s:MAX", inst, rrd, ds )
+                       _tif( _args, "CDEF:%s_max=%s_max_raw,%s", inst, inst, source.transform_rpn )
                end
 
                _tif( _args, "CDEF:%s_nnl=%s_avg,UN,0,%s_avg,IF", inst, inst, inst )
@@ -181,20 +183,23 @@ function Graph._generic( self, opts, plugin, plugin_instance, dtype, index )
 
                -- is first source in stack or overlay source: source_stk = source_nnl
                if not prev or source.overlay then
-                       -- create cdef statement
+                       -- create cdef statement for cumulative stack (no NaNs) and also
+                        -- for display (preserving NaN where no points should be displayed)
                        _tif( _args, "CDEF:%s_stk=%s_nnl", source.sname, source.sname )
+                       _tif( _args, "CDEF:%s_plot=%s_avg", source.sname, source.sname )
 
                -- is subsequent source without overlay: source_stk = source_nnl + previous_stk
                else
                        -- create cdef statement
                        _tif( _args, "CDEF:%s_stk=%s_nnl,%s_stk,+", source.sname, source.sname, prev )
+                       _tif( _args, "CDEF:%s_plot=%s_avg,%s_stk,+", source.sname, source.sname, prev )
                end
 
                -- create multiply by minus one cdef if flip is enabled
                if source.flip then
 
                        -- create cdef statement: source_stk = source_stk * -1
-                       _tif( _args, "CDEF:%s_neg=%s_stk,-1,*", source.sname, source.sname )
+                       _tif( _args, "CDEF:%s_neg=%s_plot,-1,*", source.sname, source.sname )
 
                        -- push to negative stack if overlay is disabled
                        if not source.overlay then
@@ -254,11 +259,11 @@ function Graph._generic( self, opts, plugin, plugin_instance, dtype, index )
                -- derive area background color from line color
                area_color = self.colors:to_string( self.colors:faded( area_color ) )
 
-               -- choose source_stk or source_neg variable depending on flip state
+               -- choose source_plot or source_neg variable depending on flip state
                if source.flip then
                        var = "neg"
                else
-                       var = "stk"
+                       var = "plot"
                end
 
                -- create legend
@@ -270,7 +275,9 @@ function Graph._generic( self, opts, plugin, plugin_instance, dtype, index )
                end
 
                -- create line1 statement
-               _tif( _args, "LINE1:%s_%s#%s:%s", source.sname, var, line_color, legend )
+               _tif( _args, "LINE%d:%s_%s#%s:%s",
+                       source.noarea and 2 or 1,
+                       source.sname, var, line_color, legend )
        end
 
        -- local helper: create gprint statements
@@ -281,22 +288,22 @@ function Graph._generic( self, opts, plugin, plugin_instance, dtype, index )
 
                -- don't include MIN if rrasingle is enabled
                if not self.opts.rrasingle then
-                       _tif( _args, "GPRINT:%s_min:MIN:%s Min", source.sname, numfmt )
+                       _tif( _args, "GPRINT:%s_min:MIN:\tMin\\: %s", source.sname, numfmt )
                end
 
                -- always include AVERAGE
-               _tif( _args, "GPRINT:%s_avg:AVERAGE:%s Avg", source.sname, numfmt )
+               _tif( _args, "GPRINT:%s_avg:AVERAGE:\tAvg\\: %s", source.sname, numfmt )
 
                -- don't include MAX if rrasingle is enabled
                if not self.opts.rrasingle then
-                       _tif( _args, "GPRINT:%s_max:MAX:%s Max", source.sname, numfmt )
+                       _tif( _args, "GPRINT:%s_max:MAX:\tMax\\: %s", source.sname, numfmt )
                end
 
                -- include total count if requested else include LAST
                if source.total then
                        _tif( _args, "GPRINT:%s_avg_sum:LAST:(ca. %s Total)\\l", source.sname, totfmt )
                else
-                       _tif( _args, "GPRINT:%s_avg:LAST:%s Last\\l", source.sname, numfmt )
+                       _tif( _args, "GPRINT:%s_avg:LAST:\tLast\\: %s\\l", source.sname, numfmt )
                end
        end
 
@@ -374,6 +381,8 @@ function Graph._generic( self, opts, plugin, plugin_instance, dtype, index )
                                local dsname  = dtype .. "_" .. dinst:gsub("[^%w]","_") .. "_" .. dsource
                                local altname = dtype .. "__" .. dsource
 
+                               --assert(dtype ~= "ping", dsname .. " or " .. altname)
+
                                -- find datasource options
                                local dopts = { }
 
@@ -397,7 +406,9 @@ function Graph._generic( self, opts, plugin, plugin_instance, dtype, index )
                                        flip     = dopts.flip    or false,
                                        total    = dopts.total   or false,
                                        overlay  = dopts.overlay or false,
+                                       transform_rpn = dopts.transform_rpn or "0,+",
                                        noarea   = dopts.noarea  or false,
+                                       title    = dopts.title   or nil,
                                        ds       = dsource,
                                        type     = dtype,
                                        instance = dinst,
@@ -443,9 +454,21 @@ function Graph._generic( self, opts, plugin, plugin_instance, dtype, index )
 
                -- store title and vlabel
                _ti( _args, "-t" )
-               _ti( _args, opts.title  or self.i18n:title( plugin, plugin_instance, _sources[1].type, instance ) )
+               _ti( _args, self.i18n:title( plugin, plugin_instance, _sources[1].type, instance, opts.title ) )
                _ti( _args, "-v" )
-               _ti( _args, opts.vlabel or self.i18n:label( plugin, plugin_instance, _sources[1].type, instance ) )
+               _ti( _args, self.i18n:label( plugin, plugin_instance, _sources[1].type, instance, opts.vlabel ) )
+               if opts.y_max then
+                       _ti ( _args, "-u" )
+                       _ti ( _args, opts.y_max )
+               end
+               if opts.y_min then
+                       _ti ( _args, "-l" )
+                       _ti ( _args, opts.y_min )
+               end
+               if opts.units_exponent then
+                       _ti ( _args, "-X" )
+                       _ti ( _args, opts.units_exponent )
+               end
 
                -- store additional rrd options
                if opts.rrdopts then
@@ -493,14 +516,14 @@ function Graph._generic( self, opts, plugin, plugin_instance, dtype, index )
        return defs
 end
 
-function Graph.render( self, plugin, plugin_instance )
+function Graph.render( self, plugin, plugin_instance, is_index )
 
        dtype_instances = dtype_instances or { "" }
        local pngs = { }
 
        -- check for a whole graph handler
        local plugin_def = "luci.statistics.rrdtool.definitions." .. plugin
-       local stat, def = luci.util.copcall( require, plugin_def )
+       local stat, def = pcall( require, plugin_def )
 
        if stat and def and type(def.rrdargs) == "function" then
 
@@ -508,21 +531,21 @@ function Graph.render( self, plugin, plugin_instance )
                local _images = { }
 
                -- get diagram definitions
-               for i, opts in ipairs( self:_forcelol( def.rrdargs( self, plugin, plugin_instance ) ) ) do
+               for i, opts in ipairs( self:_forcelol( def.rrdargs( self, plugin, plugin_instance, nil, is_index ) ) ) do
+                       if not is_index or not opts.detail then
+                               _images[i] = { }
 
-                       _images[i] = { }
+                               -- get diagram definition instances
+                               local diagrams = self:_generic( opts, plugin, plugin_instance, nil, i )
 
-                       -- get diagram definition instances
-                       local diagrams = self:_generic( opts, plugin, plugin_instance, nil, i )
+                               -- render all diagrams
+                               for j, def in ipairs( diagrams ) do
+                                       -- remember image
+                                       _images[i][j] = def[1]
 
-                       -- render all diagrams
-                       for j, def in ipairs( diagrams ) do
-
-                               -- remember image
-                               _images[i][j] = def[1]
-
-                               -- exec
-                               self:_rrdtool( def )
+                                       -- exec
+                                       self:_rrdtool( def )
+                               end
                        end
                end
 
@@ -532,75 +555,6 @@ function Graph.render( self, plugin, plugin_instance )
                                table.insert( pngs, _images[x][y] )
                        end
                end
-       else
-
-               -- no graph handler, iterate over data types
-               for i, dtype in ipairs( self.tree:data_types( plugin, plugin_instance ) ) do
-
-                       -- check for data type handler
-                       local dtype_def = plugin_def .. "." .. dtype
-                       local stat, def = luci.util.copcall( require, dtype_def )
-
-                       if stat and def and type(def.rrdargs) == "function" then
-
-                               -- temporary image matrix
-                               local _images = { }
-
-                               -- get diagram definitions
-                               for i, opts in ipairs( self:_forcelol( def.rrdargs( self, plugin, plugin_instance, dtype ) ) ) do
-
-                                       _images[i] = { }
-
-                                       -- get diagram definition instances
-                                       local diagrams = self:_generic( opts, plugin, plugin_instance, dtype, i )
-
-                                       -- render all diagrams
-                                       for j, def in ipairs( diagrams ) do
-
-                                               -- remember image
-                                               _images[i][j] = def[1]
-
-                                               -- exec
-                                               self:_rrdtool( def )
-                                       end
-                               end
-
-                               -- remember images - XXX: fixme (will cause probs with asymmetric data)
-                               for y = 1, #_images[1] do
-                                       for x = 1, #_images do
-                                               table.insert( pngs, _images[x][y] )
-                                       end
-                               end
-                       else
-
-                               -- no data type handler, fall back to builtin definition
-                               if type(self.defs.definitions[dtype]) == "table" then
-
-                                       -- iterate over data type instances
-                                       for i, inst in ipairs( self.tree:data_instances( plugin, plugin_instance, dtype ) ) do
-
-                                               local title = self.i18n:title( plugin, plugin_instance, dtype, inst )
-                                               local label = self.i18n:label( plugin, plugin_instance, dtype, inst )
-                                               local png   = self:mkpngpath( plugin, plugin_instance, dtype, inst )
-                                               local rrd   = self:mkrrdpath( plugin, plugin_instance, dtype, inst )
-                                               local args  = { png, "-t", title, "-v", label }
-
-                                               for i, o in ipairs(self.defs.definitions[dtype]) do
-                                                       -- XXX: this is a somewhat ugly hack to exclude min/max RRAs when rrasingle is on
-                                                       if not ( self.opts.rrasingle and ( o:match("_min") or o:match("_max") ) ) then
-                                                               table.insert( args, o )
-                                                       end
-                                               end
-
-                                               -- remember image
-                                               table.insert( pngs, png )
-
-                                               -- exec
-                                               self:_rrdtool( args, rrd )
-                                       end
-                               end
-                       end
-               end
        end
 
        return pngs