* Merged Luci to use native UCI-library
[project/luci.git] / applications / luci-statistics / luasrc / controller / luci_statistics / luci_statistics.lua
index 7fdedfb..2f68071 100644 (file)
@@ -1,12 +1,23 @@
-module("luci.controller.luci_statistics.luci_statistics", package.seeall)
+--[[
+
+Luci statistics - statistics controller module
+(c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
+
+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
 
-require("luci.fs")
-require("luci.i18n")
-require("luci.template")
+        http://www.apache.org/licenses/LICENSE-2.0
 
+$Id$
+
+]]--
+
+module("luci.controller.luci_statistics.luci_statistics", package.seeall)
 
 function index()
 
+       require("luci.fs")
        require("luci.i18n")
        require("luci.statistics.datatree")
 
@@ -24,11 +35,6 @@ function index()
                end
        end
 
-       -- override call(): call requested action function with supplied parameters
-       function _call( func, tree, plugin )
-               return function() getfenv()[func]( tree, plugin ) end
-       end
-
        -- override i18n(): try to translate stat_<str> or fall back to <str>
        function _i18n( str )
                return luci.i18n.translate( "stat_" .. str, str )
@@ -62,9 +68,15 @@ function index()
        _entry({"admin", "statistics", "network", "dns"},       cbi("luci_statistics/dns"),             _i18n("dns"),           60)
        _entry({"admin", "statistics", "network", "wireless"},  cbi("luci_statistics/wireless"),        _i18n("wireless"),      70)
 
-       
-       -- public views
-       entry({"freifunk", "statistics"}, call("statistics_index"), "Statistiken", 80).i18n = "statistics"
+
+       -- output views
+       local page = entry( { "admin", "statistics", "graph" }, call("statistics_index"), _i18n("graphs"), 80)
+             page.i18n     = "statistics"
+             page.setuser  = "nobody"
+             page.setgroup = "nogroup"
+
+       local vars = luci.http.formvalues()
+       local span = vars.timespan or nil
 
        for i, plugin in ipairs( tree:plugins() ) do
 
@@ -72,22 +84,24 @@ function index()
                local instances = tree:plugin_instances( plugin )
 
                -- plugin menu entry
-               _entry( { "freifunk", "statistics", plugin }, call("statistics_render"), _i18n( plugin ), i )
+               entry(
+                       { "admin", "statistics", "graph", plugin },
+                       call("statistics_render"), _i18n( plugin ), i
+               ).query = { timespan = span }
 
                -- if more then one instance is found then generate submenu
                if #instances > 1 then
                        for j, inst in ipairs(instances) do
                                -- instance menu entry
                                entry(
-                                       { "freifunk", "statistics", plugin, inst },
+                                       { "admin", "statistics", "graph", plugin, inst },
                                        call("statistics_render"), inst, j
-                               )
+                               ).query = { timespan = span }
                        end
-               end                     
+               end
        end
 end
 
-
 function statistics_index()
        luci.template.render("admin_statistics/index")
 end
@@ -123,37 +137,34 @@ function statistics_networkplugins()
 end
 
 
-function statistics_render( tree )
+function statistics_render()
 
        require("luci.statistics.rrdtool")
        require("luci.template")
-
-       local req   = luci.dispatcher.request 
-       local graph = luci.statistics.rrdtool.Graph()
-
-       local plugin    = req[3]
-       local instances = { req[4] }
-       local images    = { }
+       require("luci.model.uci")
+
+       local vars  = luci.http.formvalues()
+       local req   = luci.dispatcher.request
+       local path  = luci.dispatcher.dispatched.path
+       local uci   = luci.model.uci
+       local spans = luci.util.split( uci.get( "luci_statistics", "collectd_rrdtool", "RRATimespans" ), "%s+", nil, true )
+       local span  = vars.timespan or uci.get( "luci_statistics", "rrdtool", "default_timespan" ) or spans[1]
+       local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ) )
+
+       local plugin, instances
+       local images = { }
+
+       -- find requested plugin and instance
+        for i, p in ipairs( luci.dispatcher.dispatched.path ) do
+                if luci.dispatcher.dispatched.path[i] == "graph" then
+                        plugin    = luci.dispatcher.dispatched.path[i+1]
+                        instances = { luci.dispatcher.dispatched.path[i+2] }
+                end
+        end
 
        -- no instance requested, find all instances
        if #instances == 0 then
-
-               instances = graph.tree:plugin_instances( plugin )
-
-               -- more than one available instance
-               if #instances > 1 then
-
-                       -- redirect to first instance and return
-                       local r = luci.dispatcher.request
-                       local i = instances[1]
-                       if i:len() == 0 then i = "-" end
-
-                       luci.http.redirect( luci.dispatcher.build_url(
-                               req[1], req[2], req[3], i
-                       ) )
-
-                       return
-               end
+               instances = { graph.tree:plugin_instances( plugin )[1] }
 
        -- index instance requested
        elseif instances[1] == "-" then
@@ -168,5 +179,10 @@ function statistics_render( tree )
                end
        end
 
-       luci.template.render("public_statistics/graph", { images=images, plugin=plugin } )
+       luci.template.render( "public_statistics/graph", {
+               images           = images,
+               plugin           = plugin,
+               timespans        = spans,
+               current_timespan = span
+       } )
 end