make use of the new features in the binding for uci v0.4.0 - fixes remaining dependen...
[project/luci.git] / applications / luci-statistics / luasrc / statistics / rrdtool.lua
index 17ac48a..5f07a41 100644 (file)
@@ -3,8 +3,10 @@ module("luci.statistics.rrdtool", package.seeall)
 require("luci.statistics.datatree")
 require("luci.statistics.rrdtool.colors")
 require("luci.statistics.rrdtool.definitions")
-require("luci.i18n")
+require("luci.statistics.i18n")
+require("luci.model.uci")
 require("luci.util")
+require("luci.sys")
 require("luci.fs")
 
 
@@ -14,16 +16,24 @@ function Graph.__init__( self, timespan, opts )
 
        opts = opts or { }
 
+       local uci = luci.model.uci.Session()
+       local sections = uci:sections( "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.i18n
+       self.i18n   = luci.statistics.i18n.Instance( self )
 
        -- options
-       opts.rrasingle = opts.rrasingle or true         -- XXX: fixme (uci)
-       opts.host      = opts.host      or "OpenWrt"    -- XXX: fixme (uci)
-       opts.timespan  = opts.timespan  or 900          -- XXX: fixme (uci)
-       opts.width     = opts.width     or 400          -- XXX: fixme (uci)
+       opts.timespan  = timespan       or sections.rrdtool.default_timespan or 900
+       opts.rrasingle = opts.rrasingle or ( sections.collectd_rrdtool.RRASingle ~= "0" )
+       opts.host      = opts.host      or sections.collectd.Hostname        or luci.sys.hostname()
+       opts.width     = opts.width     or sections.rrdtool.image_width      or 400
+       opts.rrdpath   = opts.rrdpath   or sections.collectd_rrdtool.DataDir or "/tmp/rrd"
+       opts.imgpath   = opts.imgpath   or sections.rrdtool.image_path       or "/tmp/rrdimg"
+       opts.rrdpath   = opts.rrdpath:gsub("/$","")
+       opts.imgpath   = opts.imgpath:gsub("/$","")
 
        -- rrdtool default args
        self.args = {
@@ -34,12 +44,9 @@ function Graph.__init__( self, timespan, opts )
 
        -- store options
        self.opts = opts
-
-       -- load language file
-       self.i18n.loadc("statistics")
 end
 
-function Graph.mktitle( self, plugin, plugin_instance, dtype, dtype_instance )
+function Graph._mkpath( self, plugin, plugin_instance, dtype, dtype_instance )
        local t = self.opts.host .. "/" .. plugin
        if type(plugin_instance) == "string" and plugin_instance:len() > 0 then
                t = t .. "-" .. plugin_instance
@@ -52,11 +59,21 @@ function Graph.mktitle( self, plugin, plugin_instance, dtype, dtype_instance )
 end
 
 function Graph.mkrrdpath( self, ... )
-       return string.format( "/tmp/%s.rrd", self:mktitle( ... ) )
+       return string.format( "%s/%s.rrd", self.opts.rrdpath, self:_mkpath( ... ) )
 end
 
 function Graph.mkpngpath( self, ... )
-       return string.format( "/tmp/rrdimg/%s.png", self:mktitle( ... ) )
+       return string.format( "%s/%s.%i.png", self.opts.imgpath, self:_mkpath( ... ), self.opts.timespan )
+end
+
+function Graph.strippngpath( self, path )
+       return path:sub( self.opts.imgpath:len() + 2 )
+end
+
+function Graph.mktitle( self, plugin, plugin_instance, dtype, dtype_instance )
+
+       -- try various strings to retrieve a diagram title from the langfile
+       return "XXX"
 end
 
 function Graph._forcelol( self, list )
@@ -83,7 +100,7 @@ function Graph._rrdtool( self, def, rrd )
        -- construct commandline from def stack
        for i, opt in ipairs(def) do
                opt = opt .. ""    -- force string
-               
+
                if rrd then
                        opt = opt:gsub( "{file}", rrd )
                end
@@ -160,7 +177,7 @@ function Graph._generic( self, opts, plugin, plugin_instance, dtype, index )
 
                -- is subsequent source without overlay: source_stk = source_nnl + previous_stk
                else
-                       -- create cdef statement                                
+                       -- create cdef statement
                        _tif( _args, "CDEF:%s_stk=%s_nnl,%s_stk,+", source.sname, source.sname, prev )
                end
 
@@ -366,7 +383,6 @@ function Graph._generic( self, opts, plugin, plugin_instance, dtype, index )
 
                                -- store values
                                _ti( _sources, {
-                                       title    = dsname,   -- XXX: fixme i18n (dopts.title || i18n || dname)
                                        rrd      = dopts.rrd     or self:mkrrdpath( plugin, plugin_instance, dtype, dinst ),
                                        color    = dopts.color   or self.colors:to_string( self.colors:random() ),
                                        flip     = dopts.flip    or false,
@@ -381,6 +397,10 @@ function Graph._generic( self, opts, plugin, plugin_instance, dtype, index )
                                } )
 
 
+                               -- generate datasource title
+                               _sources[#_sources].title = self.i18n:ds( _sources[#_sources] )
+
+
                                -- find longest name ...
                                if _sources[#_sources].title:len() > _longest_name then
                                        _longest_name = _sources[#_sources].title:len()
@@ -413,11 +433,10 @@ function Graph._generic( self, opts, plugin, plugin_instance, dtype, index )
        for i, instance in ipairs(instances) do
 
                -- store title and vlabel
-               -- XXX: i18n
                _ti( _args, "-t" )
-               _ti( _args, opts.title )
+               _ti( _args, opts.title  or self.i18n:title( plugin, plugin_instance, _sources[1].type, instance ) )
                _ti( _args, "-v" )
-               _ti( _args, opts.vlabel )
+               _ti( _args, opts.vlabel or self.i18n:label( plugin, plugin_instance, _sources[1].type, instance ) )
 
                -- store additional rrd options
                if opts.rrdopts then
@@ -555,7 +574,7 @@ function Graph.render( self, plugin, plugin_instance )
                                                local png   = self:mkpngpath( plugin, plugin_instance, dtype, inst )
                                                local rrd   = self:mkrrdpath( plugin, plugin_instance, dtype, inst )
                                                local args  = { png, "-t", title }
-                                               
+
                                                for i, o in ipairs(self.defs.definitions[dtype]) do
                                                        table.insert( args, o )
                                                end