Commit ba65b89b by bergquist

feat(csv export): extract csv export into a new file

parent 3d353c7d
///<reference path="../../headers/common.d.ts" />
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
...@@ -179,38 +179,6 @@ function($, _) { ...@@ -179,38 +179,6 @@ function($, _) {
.replace(/ +/g,'-'); .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) { kbn.stringToJsRegex = function(str) {
if (str[0] !== '/') { if (str[0] !== '/') {
return new RegExp('^' + str + '$'); return new RegExp('^' + str + '$');
......
...@@ -3,13 +3,14 @@ define([ ...@@ -3,13 +3,14 @@ define([
'lodash', 'lodash',
'moment', 'moment',
'app/core/utils/kbn', 'app/core/utils/kbn',
'app/core/utils/file_export',
'app/core/time_series', 'app/core/time_series',
'app/features/panel/panel_meta', 'app/features/panel/panel_meta',
'./seriesOverridesCtrl', './seriesOverridesCtrl',
'./graph', './graph',
'./legend', './legend',
], ],
function (angular, _, moment, kbn, TimeSeries, PanelMeta) { function (angular, _, moment, kbn, fileExport, TimeSeries, PanelMeta) {
'use strict'; 'use strict';
/** @ngInject */ /** @ngInject */
...@@ -282,7 +283,7 @@ function (angular, _, moment, kbn, TimeSeries, PanelMeta) { ...@@ -282,7 +283,7 @@ function (angular, _, moment, kbn, TimeSeries, PanelMeta) {
}; };
$scope.exportCsv = function() { $scope.exportCsv = function() {
kbn.exportSeriesListToCsv($scope.seriesList); fileExport.exportSeriesListToCsv($scope.seriesList);
}; };
panelSrv.init($scope); panelSrv.init($scope);
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
import angular from 'angular'; import angular from 'angular';
import _ from 'lodash'; import _ from 'lodash';
import moment from 'moment'; 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 PanelMeta from 'app/features/panel/panel_meta2';
import {transformDataToTable} from './transformers'; import {transformDataToTable} from './transformers';
...@@ -128,7 +128,7 @@ export class TablePanelCtrl { ...@@ -128,7 +128,7 @@ export class TablePanelCtrl {
}; };
$scope.exportCsv = function() { $scope.exportCsv = function() {
kbn.exportTableDataToCsv($scope.table); FileExport.exportTableDataToCsv($scope.table);
}; };
$scope.init(); $scope.init();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment