Improve category editor labels and protocol navigation

This commit is contained in:
emanuele-f 2018-10-18 19:21:03 +02:00
parent 11e783bae1
commit 9f8a70e279
11 changed files with 215 additions and 101 deletions

View file

@ -36,10 +36,13 @@ if _POST["action"] == "edit" then
if (matched_category ~= nil) and (matched_category ~= category_id) then
-- NOTE: this check is not comprehensive
category_warnings[#category_warnings + 1] = i18n("custom_categories.similar_host_found", {
host = host,
category = interface.getnDPICategoryName(matched_category),
})
category_warnings[#category_warnings + 1] = {
type = "warning",
text = i18n("custom_categories.similar_host_found", {
host = host,
category = interface.getnDPICategoryName(matched_category),
})
}
end
hosts_ok[#hosts_ok + 1] = host
@ -48,21 +51,9 @@ if _POST["action"] == "edit" then
categories_utils.updateCustomCategoryHosts(category_id, hosts_ok)
lists_utils.reloadLists()
elseif _POST["action"] == "clear" then
local category_id = split(_POST["category"], "cat_")[2]
categories_utils.clearCustomCategoryHosts(category_id)
lists_utils.reloadLists()
end
for _, msg in ipairs(category_warnings) do
print([[
<div class="alert alert-warning alert-dismissible" style="margin-top:2em; margin-bottom:0em;">
<button type="button" class="close" data-dismiss="alert" aria-label="]]..i18n("close")..[[">
<span aria-hidden="true">&times;</span>
</button><b>]]..i18n("warning")..[[</b>: ]]..msg..[[
</div>]])
end
printMessageBanners(category_warnings)
print(
template.gen("modal_confirm_dialog.html", {
@ -80,20 +71,7 @@ print(
<li>]].. i18n("custom_categories.host_domain_or_cidr") .. [[</li>
<li>]].. i18n("custom_categories.domain_names_substrings", {s1="ntop.org", s2="mail.ntop.org", s3="ntop.org.example.com"}) ..[[</li>
</ul>]],
confirm = "Update",
}
})
)
print(
template.gen("modal_confirm_dialog.html", {
dialog={
id = "clear_category_rules",
action = "clearCategory()",
title = i18n("custom_categories.clear_category"),
message = i18n("custom_categories.clear_confirm_message.", {category="<span id='category_to_clean'></span>"}),
confirm = i18n("custom_categories.clear"),
confirm = i18n("users.edit"),
}
})
)
@ -101,7 +79,7 @@ print(
print [[<hr>
<table><tbody><tr>
<td style="white-space:nowrap; padding-right:1em;">
<h2 style="margin-top:0">]] print(i18n("custom_categories.customized_categories")) print[[</h2>
<h2 style="margin-top:0">]] print(i18n("users.categories")) print[[</h2>
</td>]]
if not isEmptyString(category_filter) then
@ -191,6 +169,17 @@ print[[
$("#table-custom-cat-form").datatable({
url: "]] print (ntop.getHttpPrefix()) print [[/lua/admin/get_custom_categories_hosts.lua?l7proto=]] print(category_filter or "") print[[",
class: "table table-striped table-bordered table-condensed",
]]
-- Set the preference table
local preference = tablePreferences("rows_number", _GET["perPage"])
if (preference ~= "") then print ('perPage: '..preference.. ",\n") end
-- Automatic default sorted. NB: the column must exist.
print ('sort: [ ["' .. getDefaultTableSort("custom_categories_hosts") ..'","' .. getDefaultTableSortOrder("custom_categories_hosts").. '"] ],')
print [[
title:"",
columns: [
{
@ -200,6 +189,10 @@ print[[
title: "]] print(i18n("category")) print[[",
field: "column_category_name",
sortable: true,
},{
title: "]] print(i18n("users.num_protocols")) print[[",
field: "column_num_protos",
sortable: true,
},{
title: "]] print(i18n("custom_categories.custom_hosts")) print[[",
field: "column_num_hosts",
@ -223,22 +216,20 @@ print[[
], rowCallback: function(row, data) {
var category_id = data.column_category_id;
var category_name = data.column_category_name;
var actions_td_idx = 4;
var actions_td_idx = 5;
if(data.column_num_protos != 0)
datatableAddLinkButtonCallback.bind(row)(actions_td_idx,
"]] print(ntop.getHttpPrefix()) print[[/lua/admin/edit_ndpi_applications.lua?category=cat_" + category_id, "]] print(i18n("host_pools.view")) print[[");
datatableAddActionButtonCallback.bind(row)(actions_td_idx,
"loadCategories("+ data.column_category_id +"); $('#selected_category_name').html('"+ category_name +"'); selected_category_id = "+ category_id +"; $('#edit_category_rules').modal('show')", "Edit");
datatableAddDeleteButtonCallback.bind(row)(actions_td_idx,
"$('#category_to_clean').html('"+ category_name +"'); selected_category_id = "+ category_id +"; $('#clear_category_rules').modal('show')", "Clear");
"loadCategories("+ data.column_category_id +"); $('#selected_category_name').html('"+ category_name +"'); selected_category_id = "+ category_id +"; $('#edit_category_rules').modal('show')", "]] print(i18n("custom_categories.edit_hosts")) print[[");
return row;
}
});
</script>
]] print(i18n("notes")) print[[
<ul>
<li>]] print(i18n("custom_categories.note_page_usage")) print[[</li>
</ul>
<br><br>
]]
dofile(dirs.installdir .. "/scripts/lua/inc/footer.lua")

View file

@ -6,10 +6,12 @@ local dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
require "lua_utils"
require "graph_utils"
local template = require "template_utils"
sendHTTPContentTypeHeader('text/html')
local proto_filter = _GET["l7proto"]
local category_filter = _GET["category"]
local ifId = getInterfaceId(ifname)
@ -20,6 +22,18 @@ end
ntop.dumpFile(dirs.installdir .. "/httpdocs/inc/header.inc")
dofile(dirs.installdir .. "/scripts/lua/inc/menu.lua")
local base_url = ntop.getHttpPrefix() .. "/lua/admin/edit_ndpi_applications.lua"
local page_params = {
l7proto = proto_filter,
category = category_filter,
}
local catid = nil
if not isEmptyString(category_filter) then
catid = split(category_filter, "cat_")[2]
end
if not table.empty(_POST) then
local custom_categories = getCustomnDPIProtoCategories(ifname)
@ -48,7 +62,15 @@ end
print [[<hr>
<table><tbody><tr>
<td style="white-space:nowrap; padding-right:1em;">
<h2 style="margin-top:0">]] print(i18n("protocols")) print[[</h2>
<h2 style="margin-top:0">]]
if catid == nil then
print(i18n("protocols"))
else
print(i18n("users.cat_protocols", {cat=interface.getnDPICategoryName(tonumber(catid))}))
end
print[[</h2>
</td>]]
if not isEmptyString(proto_filter) then
@ -72,9 +94,10 @@ print(
template.gen("typeahead_input.html", {
typeahead={
base_id = "t_app",
action = ntop.getHttpPrefix() .. "/lua/admin/edit_ndpi_applications.lua",
action = base_url,
parameters = {
ifid = tostring(ifId),
category = category_filter,
},
json_key = "key",
query_field = "l7proto",
@ -114,15 +137,15 @@ print[[
};
var url_update = "]] print (ntop.getHttpPrefix()) print [[/lua/admin/get_ndpi_applications.lua]]
if not isEmptyString(proto_filter) then
print("?l7proto=" .. proto_filter)
end
var url_update = "]] print (ntop.getHttpPrefix()) print [[/lua/admin/get_ndpi_applications.lua?l7proto=]]
print(proto_filter or "") print("&category=") print(category_filter or "")
print[[";
$("#table-edit-ndpi-applications").datatable({
url: url_update ,
class: "table table-striped table-bordered table-condensed",
buttons: []] printCategoryDropdownButton(true, catid, base_url, page_params) print[[],
]]
-- Set the preference table
@ -157,7 +180,7 @@ print [[
textAlign: 'left'
}
},{
title: "]] print(i18n("categories_page.traffic_category")) print[[",
title: "]] print(i18n("category")) print[[",
field: "column_ndpi_application_category",
sortable: true,
css: {

View file

@ -22,20 +22,24 @@ local category_filter = _GET["l7proto"]
-- ################################################
-- Sorting and Pagination
local sortPrefs = "custom_categories_lists"
local sortPrefs = "custom_categories_hosts"
if isEmptyString(sortColumn) or sortColumn == "column_" then
if((sortColumn == nil) or (sortColumn == "column_"))then
sortColumn = getDefaultTableSort(sortPrefs)
elseif sortColumn ~= "" then
tablePreferences("sort_"..sortPrefs, sortColumn)
else
if((sortColumn ~= "column_")
and (sortColumn ~= "")) then
tablePreferences("sort_"..sortPrefs,sortColumn)
end
end
if isEmptyString(_GET["sortColumn"]) then
sortOrder = getDefaultTableSortOrder(sortPrefs, true)
end
if _GET["sortColumn"] ~= "column_" and _GET["sortColumn"] ~= "" then
tablePreferences("sort_order_"..sortPrefs, sortOrder, true)
if(sortOrder == nil) then
sortOrder = getDefaultTableSortOrder(sortPrefs)
else
if((sortColumn ~= "column_")
and (sortColumn ~= "")) then
tablePreferences("sort_order_"..sortPrefs,sortOrder)
end
end
if currentPage == nil then
@ -45,10 +49,10 @@ else
end
if perPage == nil then
perPage = 10
perPage = getDefaultTableSize()
else
perPage = tonumber(perPage)
tablePreferences("rows_number_policies", perPage)
tablePreferences("rows_number", perPage)
end
-- ################################################
@ -73,14 +77,17 @@ for item_name, item_id in pairs(items) do
local hosts_list = categories_utils.getCustomCategoryHosts(item_id)
local num_hosts = #hosts_list
local num_protocols = table.len(interface.getnDPIProtocols(tonumber(item_id)))
items[item_name] = { name = item_name, id = item_id, num_hosts = num_hosts, hosts_list = hosts_list }
items[item_name] = { name = item_name, id = item_id, num_hosts = num_hosts, hosts_list = hosts_list , num_protocols = num_protocols }
num_items = num_items + 1
if sortColumn == "column_" or sortColumn == "column_category_name" then
sorter[item_name] = item_name
elseif sortColumn == "column_num_hosts" then
sorter[item_name] = num_hosts
elseif sortColumn == "column_num_protos" then
sorter[item_name] = num_protocols
end
::continue::
@ -101,6 +108,7 @@ for sorted_item, _ in pairsByValues(sorter, sOrder) do
record["column_category_id"] = tostring(items[sorted_item]["id"])
record["column_category_name"] = tostring(items[sorted_item]["name"])
record["column_num_hosts"] = tostring(items[sorted_item].num_hosts)
record["column_num_protos"] = tostring(items[sorted_item].num_protocols)
record["column_category_hosts"] = table.concat(items[sorted_item].hosts_list, ",")
res_formatted[#res_formatted + 1] = record

View file

@ -15,6 +15,7 @@ local perPage = _GET["perPage"]
local sortColumn = _GET["sortColumn"]
local sortOrder = _GET["sortOrder"]
local proto_filter = _GET["l7proto"]
local category_filter = _GET["category"]
local sortPrefs = "ndpi_application_category"
@ -51,6 +52,10 @@ end
interface.select(ifname)
if category_filter ~= nil and starts(category_filter, "cat_") then
category_filter = split(category_filter, "cat_")[2]
end
local to_skip = (currentPage-1) * perPage
if(sortOrder == "desc") then sOrder = rev_insensitive else sOrder = asc_insensitive end
@ -98,6 +103,12 @@ for app, _ in pairsByValues(sorter, sOrder) do
end
end
if not isEmptyString(category_filter) then
if tostring(app.cat.id) ~= category_filter then
goto continue
end
end
cur_num = cur_num + 1
if cur_num <= to_skip then
goto continue