diff options
Diffstat (limited to 'provisioning-model.lua')
-rw-r--r-- | provisioning-model.lua | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/provisioning-model.lua b/provisioning-model.lua index 47f9017..c46e058 100644 --- a/provisioning-model.lua +++ b/provisioning-model.lua @@ -35,6 +35,26 @@ local functions -- ################################################################################ -- LOCAL FUNCTIONS +local logme = function(message) + local userid = "-" + if mymodule.sessiondata and mymodule.sessiondata.userinfo and mymodule.sessiondata.userinfo.userid then + userid = tostring(mymodule.sessiondata.userinfo.userid) + end + if provdb.isconnected() then + local sql = string.format("INSERT INTO dbhistlog VALUES ('now', '%s', '%s')", provdb.escape(message), provdb.escape(userid)) + provdb.runsqlcommand(sql, true) + end +end + +-- Delete history log information from more than a month ago +local groomdbhistlog = function() + if provdb.isconnected() then + local sql = "DELETE FROM dbhistlog WHERE logdatetime < (now() - INTERVAL '1 month')" + provdb.runsqlcommand(sql, true) + logme("removed " .. res .. " old dbhistlog lines") + end +end + local function logcall(fn) if mymodule.sessiondata and mymodule.sessiondata.userinfo and mymodule.sessiondata.userinfo.userid then mymodule.logevent("acf-provisioning: "..tostring(fn).." by "..tostring(mymodule.sessiondata.userinfo.userid)) @@ -2686,4 +2706,73 @@ mymodule.bulk_dump_raw_devices = function(self, dumprequest) return dump_devices(self, dumprequest, false) end +mymodule.getactivitylog = function(self, clientdata) + local connected + local retval = cfe({ type="group", value={}, label="Provisioning Activity Log" }) + retval.value.page = cfe({ value=0, label="Page Number", descr="0 indicates ALL", key=true }) + retval.value.pagesize = cfe({ value=10, label="Page Size", key=true }) + retval.value.rowcount = cfe({ value=0, label="Row Count" }) + -- orderby must be an array of tables with column name and direction + retval.value.orderby = cfe({ type="structure", value={{column="logdatetime", direction="desc"}}, label="Order By", key=true }) + -- filter is a table with a string filter for each column + retval.value.filter = cfe({ type="structure", value={}, label="Filter", key=true }) + self.handle_clientdata(retval, clientdata) + retval.value.result = cfe({ type="structure", value={}, label="Log Entries" }) + + -- Process the incoming page data + local page = tonumber(retval.value.page.value) or 0 + retval.value.page.value = page + local pagesize = tonumber(retval.value.pagesize.value) or 10 + retval.value.pagesize.value = pagesize + + local orderby = {} + local columns = {logdatetime="logdatetime", msgtext="msgtext", userid="userid"} + local directions = {asc="ASC", desc="DESC", ASC="ASC", DESC="DESC"} + for i,o in ipairs(retval.value.orderby.value) do + if columns[o.column] and directions[o.direction] then + orderby[#orderby+1] = columns[o.column].." "..directions[o.direction] + end + end + if #orderby == 0 then + orderby[#orderby+1] = "logdatetime DESC" + end + + local res, err = pcall(function() + connected = databaseconnect(true) + + local filter = {} + columns.logdatetime = nil -- Cannot regex filter based on logdatetime because of the timestamp type + for c,f in pairs(retval.value.filter.value) do + if columns[c] and f ~= "" then + filter[#filter+1] = columns[c].."~'"..provdb.escape(f).."'" + end + end + + local sql = " FROM dbhistlog" + if #filter>0 then + sql = sql.." WHERE "..table.concat(filter, " AND ") + end + if page > 0 then + local count = getselectresponse("SELECT count(*)"..sql) + retval.value.rowcount.value = count[1].count + end + sql = sql.." ORDER BY "..table.concat(orderby, ", ") + if page > 0 then + sql = sql.." LIMIT "..pagesize.." OFFSET "..(page - 1)*pagesize + end + retval.value.result.value = getselectresponse("SELECT *"..sql) or {} + if page <= 0 then + retval.value.rowcount.value = #retval.value.result.value + end + + if connected then databasedisconnect() end + end) + if not res and err then + handlesqlexception(connected, err) + retval.errtxt = err + end + + return retval +end + return mymodule |