diff --git a/src/app/directives/kibanaPanel.js b/src/app/directives/kibanaPanel.js index 5948912e1caad..6f6888d807242 100644 --- a/src/app/directives/kibanaPanel.js +++ b/src/app/directives/kibanaPanel.js @@ -54,7 +54,7 @@ function (angular,$) { '' + ''+ + 'bs-tooltip="task.description" ng-class="task.icon" class="pointer" ng-click="task.click()">'+ '' + '' + diff --git a/src/app/panels/histogram/module.js b/src/app/panels/histogram/module.js index 8438b7f748367..4c61886889342 100644 --- a/src/app/panels/histogram/module.js +++ b/src/app/panels/histogram/module.js @@ -45,6 +45,13 @@ function (angular, app, $, _, kbn, moment, timeSeries, numeral) { icon: "icon-info-sign", partial: "app/partials/inspector.html", show: $scope.panel.spyable + }, + { + description: "Csv", + icon: "icon-table", + partial: "app/partials/csv.html", + show: true, + click: function() { $scope.csv_data = $scope.to_csv(); } } ], editorTabs : [ @@ -511,6 +518,13 @@ function (angular, app, $, _, kbn, moment, timeSeries, numeral) { }); }; + $scope.download_csv = function() { + var blob = new Blob([$scope.csv_data], { type: "text/csv" }); + // from filesaver.js + window.saveAs(blob, $scope.panel.title + ".csv"); + return true; + }; + // function $scope.zoom // factor :: Zoom factor, so 0.5 = cuts timespan in half, 2 doubles timespan $scope.zoom = function(factor) { @@ -579,6 +593,57 @@ function (angular, app, $, _, kbn, moment, timeSeries, numeral) { render_panel(data); }); + scope.to_csv = function() { + var headers, rows, csv; + + headers = []; + rows = {}; + csv = []; + + headers.push('"time"'); + + _.each(data, function(series) { + headers.push('"' + (series.info.alias || series.info.query) + '"'); + _.each(series.data, function(point, row) { + if (!rows[row]) { + rows[row] = { + time : point[0], + values : [] + }; + } + + rows[row].values.push(point[1] || 0); + }); + + rows = _.filter(rows, function(row) { + return row.values.length > 0; + }); + }); + + csv.push(headers); + _.each(rows, function(row) { + var values = []; + + values.push(moment(row.time).format('"YYYY-MM-DDTHH:mm:ss"')); + _.each(row.values, function(value) { + values.push(value); + }); + + csv.push(values.join(",")); + }); + + return csv.join("\n") + "\n"; + }; + + scope.$watch('panel.span', function(){ + render_panel(data); + }); + + // Re-render if the window is resized + angular.element(window).bind('resize', function(){ + render_panel(data); + }); + var scale = function(series,factor) { return _.map(series,function(p) { return [p[0],p[1]*factor]; diff --git a/src/app/panels/stats/module.js b/src/app/panels/stats/module.js index 94c905e5f9dcb..606a1d3fbe544 100644 --- a/src/app/panels/stats/module.js +++ b/src/app/panels/stats/module.js @@ -39,6 +39,13 @@ define([ icon: "icon-info-sign", partial: "app/partials/inspector.html", show: $scope.panel.spyable + }, + { + description: "Csv", + icon: "icon-table", + partial: "app/partials/csv.html", + show: true, + click: function() { $scope.csv_data = $scope.to_csv(); } } ], editorTabs : [ @@ -189,6 +196,23 @@ define([ $scope.$emit('render'); }; + $scope.to_csv = function() { + var rows = []; + + _.each($scope.data.rows, function(row) { + rows.push('"' + row.Label + '"' + "," + row.Value); + }); + + return rows.join("\n") + "\n"; + }; + + $scope.download_csv = function() { + var blob = new Blob([$scope.csv_data], { type: "text/csv" }); + // from filesaver.js + window.saveAs(blob, $scope.panel.title + ".csv"); + return true; + }; + }); module.filter('formatstats', function(){ diff --git a/src/app/panels/table/module.js b/src/app/panels/table/module.js index 7dbfb2d37cd8f..15451a4e4737d 100644 --- a/src/app/panels/table/module.js +++ b/src/app/panels/table/module.js @@ -35,6 +35,13 @@ function (angular, app, _, kbn, moment) { icon: "icon-info-sign", partial: "app/partials/inspector.html", show: $scope.panel.spyable + }, + { + description: "Csv", + icon: "icon-table", + partial: "app/partials/csv.html", + show: true, + click: function() { $scope.csv_data = $scope.to_csv(); } } ], editorTabs : [ @@ -466,6 +473,50 @@ function (angular, app, _, kbn, moment) { return obj; }; + $scope.to_csv = function() { + var headers, rows, csv, fields; + + headers = []; + rows = []; + csv = []; + + if ($scope.panel.fields.length === 0) { + fields = $scope.fields.list; + } else { + fields = $scope.panel.fields; + } + + _.each(fields, function(field) { + headers.push('"' + field + '"'); + }); + + rows.push(headers); + + _.each($scope.data, function(event) { + rows.push(_.map(fields, function(field) { + var value = event.kibana._source[field]; + + if (_.isUndefined(value)) { + return ""; + } else { + return '"' + value + '"'; + } + })); + }); + + _.each(rows, function(row) { + csv.push(row.join(",")); + }); + + return csv.join("\n") + "\n"; + }; + + $scope.download_csv = function() { + var blob = new Blob([$scope.csv_data], { type: "text/csv" }); + // from filesaver.js + window.saveAs(blob, $scope.panel.title + ".csv"); + return true; + }; }); diff --git a/src/app/panels/terms/module.js b/src/app/panels/terms/module.js index d7df9c1682b15..a63f2b6a8e73e 100644 --- a/src/app/panels/terms/module.js +++ b/src/app/panels/terms/module.js @@ -32,6 +32,13 @@ function (angular, app, _, $, kbn) { icon: "icon-info-sign", partial: "app/partials/inspector.html", show: $scope.panel.spyable + }, + { + description: "Csv", + icon: "icon-table", + partial: "app/partials/csv.html", + show: true, + click: function() { $scope.csv_data = $scope.to_csv(); } } ], editorTabs : [ @@ -249,6 +256,23 @@ function (angular, app, _, $, kbn) { return true; }; + $scope.to_csv = function() { + var csv = []; + + _.each($scope.data, function(series) { + csv.push(['"' + series.label + '"', series.data[0][1]].join(",")); + }); + + return csv.join("\n") + "\n"; + }; + + $scope.download_csv = function() { + var blob = new Blob([$scope.csv_data], { type: "text/csv" }); + // from filesaver.js + window.saveAs(blob, $scope.panel.title + "-" + $scope.panel.field + ".csv"); + return true; + }; + }); module.directive('termsChart', function(querySrv) { diff --git a/src/app/partials/csv.html b/src/app/partials/csv.html new file mode 100755 index 0000000000000..8dc1456e873b7 --- /dev/null +++ b/src/app/partials/csv.html @@ -0,0 +1,12 @@ + + +