Module: check_mk
Branch: master
Commit: 492ac9304c4ab54facdd1682ccd08ce144a18ce1
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=492ac9304c4ab5…
Author: Andreas Boesl <ab(a)mathias-kettner.de>
Date: Thu Jul 4 17:23:55 2013 +0200
Multisite: new status icons: export as csv, export as json
---
ChangeLog | 1 +
web/htdocs/htmllib.py | 5 ++++
web/plugins/views/webservice.py | 62 +++++++++++++++++++++++++++++++++++++--
3 files changed, 66 insertions(+), 2 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 5e29dea..48a2781 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -76,6 +76,7 @@
via "sidebar_show_scrollbar = True" in multisite.mk
* Added option to disable automatic userdb synchronizations in multisite
* Implemented search forms for most data tables
+ * New status icons: export as CSV, export as JSON
WATO:
* Allow to configure check-/retry_interval in second precision
diff --git a/web/htdocs/htmllib.py b/web/htdocs/htmllib.py
index 4f7cd04..5f16179 100644
--- a/web/htdocs/htmllib.py
+++ b/web/htdocs/htmllib.py
@@ -965,6 +965,11 @@ document.body.onfocus = keybindings_focus;
(self.makeuri([]), _("URL to this frame"))
h += '<a target="_top" href="%s"><img
class=statusicon src="images/status_pageurl.png"
title="%s"></a>\n' % \
("index.py?" + urlencode_vars([("start_url",
self.makeuri([]))]), _("URL to this page including sidebar"))
+
+ h += '<a target="_top" href="%s"><img
class=statusicon src="images/status_frameurl.png"
title="%s"></a>\n' % \
+ (self.makeuri([("output_format", "json_export")]),
_("Export as JSON"))
+ h += '<a target="_top" href="%s"><img
class=statusicon src="images/status_frameurl.png"
title="%s"></a>\n' % \
+ (self.makeuri([("output_format", "csv_export")]),
_("Export as CSV"))
for img, tooltip in self.status_icons.items():
h += '<img class=statusicon src="images/status_%s.png"
title="%s">\n' % (img, tooltip)
return h
diff --git a/web/plugins/views/webservice.py b/web/plugins/views/webservice.py
index ed31b35..1e8cfce 100644
--- a/web/plugins/views/webservice.py
+++ b/web/plugins/views/webservice.py
@@ -63,7 +63,12 @@ def encode_string_json(s):
return '"' + json_escape.sub(lambda m: json_encoding_table[m.group(0)],
s) + '"'
-def render_json(rows, view, group_painters, painters, num_columns, show_checkboxes):
+def render_json(rows, view, group_painters, painters, num_columns, show_checkboxes,
export = False):
+ if export:
+ html.req.content_type = "appliation/json; charset=UTF-8"
+ filename = '%s-%s.json' % (view['name'],
time.strftime('%Y-%m-%d_%H-%M-%S', time.localtime(time.time())))
+ html.req.headers_out['Content-Disposition'] = 'Attachment;
filename=%s' % filename
+
html.write("[\n")
first = True
@@ -97,9 +102,62 @@ def render_json(rows, view, group_painters, painters, num_columns,
show_checkbox
html.write("\n]\n")
+multisite_layouts["json_export"] = {
+ "title" : _("JSON data export"),
+ "render" : lambda a,b,c,d,e,f: render_json(a,b,c,d,e,f,True),
+ "group" : False,
+ "hide" : True,
+}
+
multisite_layouts["json"] = {
"title" : _("JSON data output"),
- "render" : render_json,
+ "render" : lambda a,b,c,d,e,f: render_json(a,b,c,d,e,f,False),
+ "group" : False,
+ "hide" : True,
+}
+
+
+def render_csv(rows, view, group_painters, painters, num_columns, show_checkboxes, export
= False):
+ if export:
+ html.req.content_type = "text/csv; charset=UTF-8"
+ filename = '%s-%s.csv' % (view['name'],
time.strftime('%Y-%m-%d_%H-%M-%S', time.localtime(time.time())))
+ html.req.headers_out['Content-Disposition'] = 'Attachment;
filename=%s' % filename
+
+ csv_separator = html.var("csv_separator", ",")
+ first = True
+ for p in painters:
+ if first:
+ first = False
+ else:
+ html.write(csv_separator)
+ content = p[0]["name"]
+ stripped = htmllib.strip_tags(content)
+ stripped = stripped.replace(csv_separator, "\%s" % csv_separator)
+ html.write(stripped.encode("utf-8"))
+
+ for row in rows:
+ html.write("\n")
+ first = True
+ for p in painters:
+ if first:
+ first = False
+ else:
+ html.write(csv_separator)
+ tdclass, content = p[0]["paint"](row)
+ stripped = htmllib.strip_tags(content)
+ stripped = stripped.replace(csv_separator, "\%s" % csv_separator)
+ html.write(stripped.encode("utf-8"))
+
+multisite_layouts["csv_export"] = {
+ "title" : _("CSV data export"),
+ "render" : lambda a,b,c,d,e,f: render_csv(a,b,c,d,e,f,True),
+ "group" : False,
+ "hide" : True,
+}
+
+multisite_layouts["csv"] = {
+ "title" : _("CSV data output"),
+ "render" : lambda a,b,c,d,e,f: render_csv(a,b,c,d,e,f,False),
"group" : False,
"hide" : True,
}