foobar11 comes after foobar2
Message-ID: <53ad223f.caGFJ4barIIvbnKb%mk(a)mathias-kettner.de>
User-Agent: Heirloom mailx 12.4 7/29/08
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Module: check_mk
Branch: master
Commit: 22a6a06f2dbd88d73c3bc994ba6a4e96073fe6c6
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=22a6a06f2dbd88…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Fri Jun 27 09:49:38 2014 +0200
Sort host names naturally, e.g. foobar11 comes after foobar2
Hostnames are now sorted naturally. That means that sequences of numbers
are being interpreted as numbers. This also works for host names that
have the form of an IPv4 address, e.g. <tt>10.1.1.5</tt> is now correctly
sorted before <tt>10.1.1.11</tt> and <tt>srv17_3</tt> comes before
<tt>src17_108</tt>.
This new sorting is implemented in the status GUI and also in WATO.
---
.werks/1013 | 12 ++++++++++++
ChangeLog | 1 +
web/htdocs/lib.py | 13 +++++++++++++
web/htdocs/views.py | 3 +++
web/htdocs/wato.py | 4 ++--
web/plugins/views/sorters.py | 9 +++------
6 files changed, 34 insertions(+), 8 deletions(-)
diff --git a/.werks/1013 b/.werks/1013
new file mode 100644
index 0000000..e33313e
--- /dev/null
+++ b/.werks/1013
@@ -0,0 +1,12 @@
+Title: Sort host names naturally, e.g. foobar11 comes after foobar2
+Level: 2
+Component: multisite
+Version: 1.2.5i5
+Date: 1403855030
+Class: feature
+
+Hostnames are now sorted naturally. That means that sequences of numbers
+are being interpreted as numbers. This also works for host names that
+have the form of an IPv4 address, e.g. <tt>10.1.1.5</tt> is now correctly
+sorted before <tt>10.1.1.11</tt> and <tt>srv17_3</tt> comes
before <tt>src17_108</tt>.
+This new sorting is implemented in the status GUI and also in WATO.
diff --git a/ChangeLog b/ChangeLog
index 1692787..7e3ba3a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -12,6 +12,7 @@
* 0944 FIX: oracle_tablespaces: fixed calculation of space left and number of
remaining increments...
Multisite:
+ * 1013 Sort host names naturally, e.g. foobar11 comes after foobar2...
* 0945 FIX: Sidebar snapin "Problem hosts": Now excludes hosts and services
in downtime
WATO:
diff --git a/web/htdocs/lib.py b/web/htdocs/lib.py
index 645aae9..a724079 100644
--- a/web/htdocs/lib.py
+++ b/web/htdocs/lib.py
@@ -365,6 +365,19 @@ def regex(r):
return rx
+# Splits a word into sequences of numbers and non-numbers.
+# Creates a tuple from these where the number are converted
+# into int datatype. That way a naturual sort can be
+# implemented.
+def num_split(s):
+ if not s:
+ return ()
+ elif s[0].isdigit():
+ first_num = regex("[^0-9]").split(s)[0]
+ return ( int(first_num), ) + num_split(s[len(first_num):])
+ else:
+ first_word = regex("[0-9]").split(s)[0]
+ return ( first_word, ) + num_split(s[len(first_word):])
__builtin__.default_user_localizations = {
diff --git a/web/htdocs/views.py b/web/htdocs/views.py
index dd155df..985ad18 100644
--- a/web/htdocs/views.py
+++ b/web/htdocs/views.py
@@ -2780,6 +2780,9 @@ def cmp_simple_string(column, r1, r2):
v1, v2 = r1.get(column, ''), r2.get(column, '')
return cmp_insensitive_string(v1, v2)
+def cmp_num_split(column, r1, r2):
+ return cmp(num_split(r1[column]), num_split(r2[column]))
+
def cmp_string_list(column, r1, r2):
v1 = ''.join(r1.get(column, []))
v2 = ''.join(r2.get(column, []))
diff --git a/web/htdocs/wato.py b/web/htdocs/wato.py
index 98735a5..9acf07f 100644
--- a/web/htdocs/wato.py
+++ b/web/htdocs/wato.py
@@ -1310,7 +1310,7 @@ def show_hosts(folder):
html.write("<h3>" + _("Hosts") + "</h3>")
hostnames = folder[".hosts"].keys()
- hostnames.sort()
+ hostnames.sort(cmp = lambda a, b: cmp(num_split(a), num_split(b)))
search_text = html.var("search")
# Helper function for showing bulk actions. This is needed at the bottom
@@ -3493,7 +3493,7 @@ def search_hosts_in_folder(folder, crit):
if found:
render_folder_path(folder, True)
- found.sort()
+ found.sort(cmp = lambda a,b: cmp(num_split(a[0]), num_split(b[0])))
table.begin("search_hosts", "");
for hostname, host, effective in found:
diff --git a/web/plugins/views/sorters.py b/web/plugins/views/sorters.py
index a429c82..767dafa 100644
--- a/web/plugins/views/sorters.py
+++ b/web/plugins/views/sorters.py
@@ -96,11 +96,8 @@ multisite_sorters["hoststate"] = {
}
def cmp_site_host(r1, r2):
- c = cmp(r1["site"], r2["site"])
- if c != 0:
- return c
- else:
- return cmp_simple_string("host_name", r1, r2)
+ return cmp(r1["site"], r2["site"]) or \
+ cmp_num_split("host_name", r1, r2)
multisite_sorters["site_host"] = {
"title" : _("Host"),
@@ -221,7 +218,7 @@ multisite_sorters['svc_perf_val10'] = {
# Host
-declare_1to1_sorter("alias", cmp_simple_string)
+declare_1to1_sorter("alias", cmp_num_split)
declare_1to1_sorter("host_address", cmp_ip_address)
declare_1to1_sorter("host_plugin_output", cmp_simple_string)
declare_1to1_sorter("host_perf_data", cmp_simple_string)