File: get_networks_data.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 (105 lines) | stat: -rw-r--r-- 2,485 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
--
-- (C) 2013-22 - ntop.org
--

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

local json = require("dkjson")

sendHTTPContentTypeHeader('text/html')

-- Table parameters
local currentPage  = _GET["currentPage"]
local perPage      = _GET["perPage"]
local sortColumn   = _GET["sortColumn"]
local sortOrder    = _GET["sortOrder"]

local sortPrefs = "local_network"

if((sortColumn == nil) or (sortColumn == "column_"))then
   sortColumn = getDefaultTableSort(sortPrefs)
else
   if((sortColumn ~= "column_")
    and (sortColumn ~= "")) then
      tablePreferences("sort_"..sortPrefs,sortColumn)
   end
end

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
   currentPage = 1
else
   currentPage = tonumber(currentPage)
end

if(perPage == nil) then
   perPage = getDefaultTableSize()
else
   perPage = tonumber(perPage)
   tablePreferences("rows_number", perPage)
end

local to_skip = (currentPage-1) * perPage

if(sortOrder == "desc") then sOrder = false else sOrder = true end

local networks_stats = interface.getNetworksStats()
local total_rows = 0

local sort_helper = {}
for n, ns in pairs(networks_stats) do
   total_rows = total_rows + 1

   if sortColumn == "column_hosts" then
      sort_helper[n] = ns["num_hosts"]
   elseif sortColumn == "column_thpt" then
      sort_helper[n] = ns["throughput_bps"]
   elseif sortColumn == "column_traffic" then
      sort_helper[n] = ns["bytes.sent"] + ns["bytes.rcvd"]
   else
      sort_helper[n] = getLocalNetworkAlias(ns["network_key"])
   end
end

local res_formatted = {}
local cur_row = 0
local tot_row_in_page = 0

for n, _ in pairsByValues(sort_helper, ternary(sOrder, asc, rev)) do
   cur_row = cur_row + 1

   if cur_row <= to_skip then
      goto continue
   end

   local record = network2record(interface.getId(), networks_stats[n])
   res_formatted[#res_formatted + 1] = record

   tot_row_in_page = tot_row_in_page + 1

   if tot_row_in_page >= perPage then
      break
   end

   ::continue::
end

local result = {}
result["perPage"] = perPage
result["currentPage"] = currentPage
result["totalRows"] = total_rows
result["data"] = res_formatted
result["sort"] = {{sortColumn, sortOrder}}

print(json.encode(result, nil))