From ba65b89bbb62ff85a6e6327936ca07f55efeb52a Mon Sep 17 00:00:00 2001 From: bergquist Date: Thu, 21 Jan 2016 14:21:38 +0100 Subject: [PATCH] feat(csv export): extract csv export into a new file --- public/app/core/utils/file_export.ts | 74 ++++++++++++++++++++ public/app/core/utils/kbn.js | 32 --------- public/app/plugins/panel/graph/module.js | 5 +- public/app/plugins/panel/table/controller.ts | 4 +- 4 files changed, 79 insertions(+), 36 deletions(-) create mode 100644 public/app/core/utils/file_export.ts diff --git a/public/app/core/utils/file_export.ts b/public/app/core/utils/file_export.ts new file mode 100644 index 00000000000..c3959aec12f --- /dev/null +++ b/public/app/core/utils/file_export.ts @@ -0,0 +1,74 @@ +/// + +import _ from 'lodash'; + +declare var window: any; + +export function exportSeriesListToCsv(seriesList) { + var text = 'Series;Time;Value\n'; + _.each(seriesList, function(series) { + _.each(series.datapoints, function(dp) { + text += series.alias + ';' + new Date(dp[1]).toISOString() + ';' + dp[0] + '\n'; + }); + }); + saveSaveBlob(text, 'grafana_data_export.csv'); +}; + +export function exportTableDataToCsv(table) { + var text = ''; + // add header + _.each(table.columns, function(column) { + text += column.text + ';'; + }); + text += '\n'; + // process data + _.each(table.rows, function(row) { + _.each(row, function(value) { + text += value + ';'; + }); + text += '\n'; + }); + saveSaveBlob(text, 'grafana_data_export.csv'); +}; + +export function saveSaveBlob(payload, fname) { + var blob = new Blob([payload], { type: "text/csv;charset=utf-8" }); + window.saveAs(blob, fname); +}; + +/* +export default function flatten(target, opts): any { + opts = opts || {}; + + var delimiter = opts.delimiter || '.'; + var maxDepth = opts.maxDepth || 3; + var currentDepth = 1; + var output = {}; + + function step(object, prev) { + Object.keys(object).forEach(function(key) { + var value = object[key]; + var isarray = opts.safe && Array.isArray(value); + var type = Object.prototype.toString.call(value); + var isobject = type === "[object Object]"; + + var newKey = prev ? prev + delimiter + key : key; + + if (!opts.maxDepth) { + maxDepth = currentDepth + 1; + } + + if (!isarray && isobject && Object.keys(value).length && currentDepth < maxDepth) { + ++currentDepth; + return step(value, newKey); + } + + output[newKey] = value; + }); + } + + step(target, null); + + return output; +} +*/ \ No newline at end of file diff --git a/public/app/core/utils/kbn.js b/public/app/core/utils/kbn.js index e3a9fbaa39e..959de5fa42a 100644 --- a/public/app/core/utils/kbn.js +++ b/public/app/core/utils/kbn.js @@ -179,38 +179,6 @@ function($, _) { .replace(/ +/g,'-'); }; - kbn.exportSeriesListToCsv = function(seriesList) { - var text = 'Series;Time;Value\n'; - _.each(seriesList, function(series) { - _.each(series.datapoints, function(dp) { - text += series.alias + ';' + new Date(dp[1]).toISOString() + ';' + dp[0] + '\n'; - }); - }); - kbn.saveSaveBlob(text, 'grafana_data_export.csv'); - }; - - kbn.exportTableDataToCsv = function(table) { - var text = ''; - // add header - _.each(table.columns, function(column) { - text += column.text + ';'; - }); - text += '\n'; - // process data - _.each(table.rows, function(row) { - _.each(row, function(value) { - text += value + ';'; - }); - text += '\n'; - }); - kbn.saveSaveBlob(text, 'grafana_data_export.csv'); - }; - - kbn.saveSaveBlob = function(payload, fname) { - var blob = new Blob([payload], { type: "text/csv;charset=utf-8" }); - window.saveAs(blob, fname); - }; - kbn.stringToJsRegex = function(str) { if (str[0] !== '/') { return new RegExp('^' + str + '$'); diff --git a/public/app/plugins/panel/graph/module.js b/public/app/plugins/panel/graph/module.js index 47d0e4f83a3..c1039e394a3 100644 --- a/public/app/plugins/panel/graph/module.js +++ b/public/app/plugins/panel/graph/module.js @@ -3,13 +3,14 @@ define([ 'lodash', 'moment', 'app/core/utils/kbn', + 'app/core/utils/file_export', 'app/core/time_series', 'app/features/panel/panel_meta', './seriesOverridesCtrl', './graph', './legend', ], -function (angular, _, moment, kbn, TimeSeries, PanelMeta) { +function (angular, _, moment, kbn, fileExport, TimeSeries, PanelMeta) { 'use strict'; /** @ngInject */ @@ -282,7 +283,7 @@ function (angular, _, moment, kbn, TimeSeries, PanelMeta) { }; $scope.exportCsv = function() { - kbn.exportSeriesListToCsv($scope.seriesList); + fileExport.exportSeriesListToCsv($scope.seriesList); }; panelSrv.init($scope); diff --git a/public/app/plugins/panel/table/controller.ts b/public/app/plugins/panel/table/controller.ts index 971f37de849..97c8f7a728a 100644 --- a/public/app/plugins/panel/table/controller.ts +++ b/public/app/plugins/panel/table/controller.ts @@ -3,7 +3,7 @@ import angular from 'angular'; import _ from 'lodash'; import moment from 'moment'; -import kbn from 'app/core/utils/kbn'; +import * as FileExport from 'app/core/utils/file_export'; import PanelMeta from 'app/features/panel/panel_meta2'; import {transformDataToTable} from './transformers'; @@ -128,7 +128,7 @@ export class TablePanelCtrl { }; $scope.exportCsv = function() { - kbn.exportTableDataToCsv($scope.table); + FileExport.exportTableDataToCsv($scope.table); }; $scope.init();