diff --git a/include/Grouper.h b/include/Grouper.h index 77a0b82220..e1d609e451 100644 --- a/include/Grouper.h +++ b/include/Grouper.h @@ -43,6 +43,7 @@ class Grouper { private: sortField sorter; + int table_index; int64_t group_id_i; bool group_id_set; char *group_id_s; diff --git a/scripts/lua/get_grouped_hosts_data.lua b/scripts/lua/get_grouped_hosts_data.lua index 726d4852d4..38c72ab070 100644 --- a/scripts/lua/get_grouped_hosts_data.lua +++ b/scripts/lua/get_grouped_hosts_data.lua @@ -277,7 +277,7 @@ for key,value in pairs(stats_by_group_col) do v = stats_by_group_col[key] if((key ~= nil) and (v ~= nil)) then if(sortColumn == "column_id") then - vals[key] = key + vals[key] = v["id"] elseif(sortColumn == "column_name") then vals[key] = v["name"] elseif(sortColumn == "column_hosts") then @@ -309,11 +309,21 @@ else funct = rev end -num = 0 -for _key, _value in pairsByValues(vals, funct) do - if((_key ~= nil) and (not(_key == ""))) then - value = stats_by_group_col[_key] +local iterator +if sortColumn == "column_id" then + -- Sort for this column is already provided by C, only the sort order can be reversed here + iterator = pairsByKeys +else + -- We provide our own sort + iterator = pairsByValues +end +num = 0 +for _key, _val in iterator(vals, funct) do + value = stats_by_group_col[_key] + + -- e.g. this is empty for hosts without a country + if not isEmptyString(value["id"]) then if(to_skip > 0) then to_skip = to_skip-1 else diff --git a/scripts/lua/network_stats.lua b/scripts/lua/network_stats.lua index ea89996789..a119abec4d 100644 --- a/scripts/lua/network_stats.lua +++ b/scripts/lua/network_stats.lua @@ -39,7 +39,7 @@ 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("network") ..'","' .. getDefaultTableSortOrder("network").. '"] ],') +print ('sort: [ ["' .. getDefaultTableSort("local_network_id") ..'","' .. getDefaultTableSortOrder("local_network_id").. '"] ],') print [[ diff --git a/src/Grouper.cpp b/src/Grouper.cpp index ffc669cb26..0c6eb3ccf4 100644 --- a/src/Grouper.cpp +++ b/src/Grouper.cpp @@ -28,6 +28,7 @@ Grouper::Grouper(sortField sf){ group_id_set = false; group_id_s = NULL; group_label = NULL; + table_index = 1; memset(&stats, 0, sizeof(stats)); } @@ -188,17 +189,13 @@ void Grouper::lua(lua_State* vm) { lua_push_float_table_entry(vm, "throughput_trend_bps_diff", max_val(stats.throughput_trend_bps_diff, 0)); lua_push_str_table_entry(vm, "country", strlen(stats.country) ? stats.country : (char*)""); - if(sorter == column_mac){ // special case for mac + if(sorter == column_mac) // special case for mac lua_push_str_table_entry(vm, "id", group_label); - lua_pushstring(vm, group_label); - } else if(!group_id_s){ // integer group id + else if(!group_id_s){ // integer group id lua_push_int32_table_entry(vm, "id", group_id_i); - lua_pushinteger(vm, group_id_i); } else { // string group id lua_push_str_table_entry(vm, "id", group_id_s); - lua_pushstring(vm, group_id_s); } - lua_insert(vm, -2); - lua_settable(vm, -3); + lua_rawseti(vm, -2, table_index++); /* Use indexes to preserve order */ }