function after_save_audit(method, uciname, secname, sectype, para, secdata, ucidata, filter_para) local uci_r = uci.cursor() if para.log_send ~= nil then uci_r:delete("behave_audit", "setting", "log_send") uci_r:commit("behave_audit") end if para.log_send_enable == nil and para.send_server == nil then return err.ENONE end local status = ubus.connect():call("logger","config_reload",{}) return err.ENONE end function before_save_audit(method, uciname, secname, sectype, para, secdata, ucidata, filter_para) local uci_r = uci.cursor() if para.log_send ~= nil then uci_r:set("behave_audit", "setting", "log_send_enable", para.log_send) uci_r:commit("behave_audit") end return err.ENONE end function get_behave_audit_info(filter, uciname, secname, para) local uci_r = uci.cursor() local result = {} uci_r:foreach("behave_audit", "behave_audit", function(section) section["log_send"] = section["log_send_enable"] result = section end) return err.ENONE, result end 有啥可优化的吗
时间: 2024-03-07 11:48:15 浏览: 134
这段代码可以进行一些优化,如下所示:
1. 在函数 `after_save_audit` 中,可以将 `if para.log_send_enable == nil and para.send_server == nil then` 的判断条件改为 `if not para.log_send_enable and not para.send_server then`,使代码更为简洁明了。
2. 在函数 `before_save_audit` 中,可以将 `if para.log_send ~= nil then` 的判断条件改为 `if para.log_send then`,同样可以使代码更为简洁。
3. 在函数 `get_behave_audit_info` 中,可以将 `result = section` 改为 `table.insert(result, section)`,以便将所有查询到的结果都存储到 `result` 表中。
4. 可以将重复的代码提取出来,定义为公共函数,以便在多个函数中复用,提高代码的复用性和可维护性。
综上所述,可以对这段代码进行如下优化:
```
local function update_audit_log_send(uci_r, enable)
if enable then
uci_r:set("behave_audit", "setting", "log_send_enable", "1")
else
uci_r:delete("behave_audit", "setting", "log_send_enable")
end
uci_r:commit("behave_audit")
end
function after_save_audit(method, uciname, secname, sectype, para, secdata, ucidata, filter_para)
local uci_r = uci.cursor()
if para.log_send ~= nil then
update_audit_log_send(uci_r, false)
end
if not para.log_send_enable and not para.send_server then
return err.ENONE
end
ubus.connect():call("logger","config_reload",{})
return err.ENONE
end
function before_save_audit(method, uciname, secname, sectype, para, secdata, ucidata, filter_para)
local uci_r = uci.cursor()
if para.log_send then
update_audit_log_send(uci_r, true)
end
return err.ENONE
end
function get_behave_audit_info(filter, uciname, secname, para)
local uci_r = uci.cursor()
local result = {}
uci_r:foreach("behave_audit", "behave_audit", function(section)
section["log_send"] = section["log_send_enable"]
table.insert(result, section)
end)
return err.ENONE, result
end
```
阅读全文
相关推荐

















