File: find_prefs.lua

package info (click to toggle)
ntopng 5.2.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 121,832 kB
  • sloc: javascript: 143,431; cpp: 71,175; ansic: 11,108; sh: 4,687; makefile: 911; python: 587; sql: 512; pascal: 234; perl: 118; ruby: 52; exp: 4
file content (64 lines) | stat: -rw-r--r-- 1,911 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
--
-- (C) 2017-22 - ntop.org
--

dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path

require "lua_utils"
require "prefs_utils"
local json = require "dkjson"

sendHTTPHeader('application/json')

local max_num_to_find = 5
local res = {results={}}
local results = res.results
local menu_subpages = require "prefs_menu"

local query = _GET["query"] or ""

local function matchesQuery(value, query)
  return string.find(string.lower(noHtml(value)), string.lower(query))
end

local function queryResultShorten(result, query, context)
  local maxlen = 35
  local idx = matchesQuery(result, query)
  local left_slice, right_slice = shortenInTheMiddle(result, idx, idx + string.len(query), maxlen)

  return context..(
    ternary(left_slice ~= 1, "...", "")..
    string.sub(result, left_slice, right_slice)..
    ternary(right_slice ~= string.len(result), "...", "")
  )
end

local function addResult(result, tab, context)
  results[#results + 1] = {name=queryResultShorten(noHtml(result), query, context), tab=tab.id}
end

for _, tab in pairs(menu_subpages) do
  if isSubpageAvailable(tab) and not (tab.disabled) then
    -- Menu match, do not proceed with children
    if matchesQuery(tab.label, query) then
      addResult(tab.label, tab, "")
    else
      for _, entry in pairs(tab.entries) do
        if entry.hidden ~= true then
          -- Entry title match, do not proceed with description
          if matchesQuery(entry.title, query) then
            -- Decorate with tab label
            addResult(entry.title, tab, tab.label .. ": ")
            break
          --[[elseif matchesQuery(entry.description, query) then
            -- Decorate with entry title and tab label
            addResult(entry.description, tab, tab.label .. " [" .. entry.title .. "]: ")]]--
          end
        end
      end
    end
  end
end

print(json.encode(res, nil, 1))