diff --git a/ui/spec/shared/parsing/resultsToCSVSpec.js b/ui/spec/shared/parsing/resultsToCSVSpec.js index e2e2bae2b..18c0b8843 100644 --- a/ui/spec/shared/parsing/resultsToCSVSpec.js +++ b/ui/spec/shared/parsing/resultsToCSVSpec.js @@ -1,4 +1,4 @@ -import resultsToCSV, {formatDate} from 'shared/parsing/resultsToCSV' +import {resultsToCSV, formatDate} from 'shared/parsing/resultsToCSV' describe('formatDate', () => { it('converts timestamp to an excel compatible date string', () => { diff --git a/ui/src/data_explorer/components/VisHeader.js b/ui/src/data_explorer/components/VisHeader.js index 6e2c85cb0..2d3325456 100644 --- a/ui/src/data_explorer/components/VisHeader.js +++ b/ui/src/data_explorer/components/VisHeader.js @@ -3,7 +3,7 @@ import classnames from 'classnames' import _ from 'lodash' import {fetchTimeSeriesAsync} from 'shared/actions/timeSeries' -import resultsToCSV from 'src/shared/parsing/resultsToCSV.js' +import {resultsToCSV} from 'src/shared/parsing/resultsToCSV.js' import download from 'src/external/download.js' const getCSV = (query, errorThrown) => async () => { diff --git a/ui/src/shared/components/LayoutCell.js b/ui/src/shared/components/LayoutCell.js index 9ab96c617..18e4c99dd 100644 --- a/ui/src/shared/components/LayoutCell.js +++ b/ui/src/shared/components/LayoutCell.js @@ -5,6 +5,9 @@ import LayoutCellMenu from 'shared/components/LayoutCellMenu' import LayoutCellHeader from 'shared/components/LayoutCellHeader' import {fetchTimeSeriesAsync} from 'shared/actions/timeSeries' import {removeUnselectedTemplateValues} from 'src/dashboards/constants' +import {errorThrown} from 'shared/actions/errors' +import {dashboardtoCSV} from 'shared/parsing/resultsToCSV' +import download from 'src/external/download.js' class LayoutCell extends Component { constructor(props) { @@ -32,17 +35,12 @@ class LayoutCell extends Component { this.props.onSummonOverlayTechnologies(cell) } - handleDataDownload = cell => async () => { - console.log(cell.name) - const qs = this.props.queries - const templates = this.props.templates - const resolution = 740 + handleCSVDownload = cell => () => { + const {queries, templates} = this.props + const joined_name = cell.name.split(' ').join('_') + const resolution = undefined // TODO - if (!qs.length) { - console.log('empty!') // TODO - } - - const timeSeriesPromises = qs.map(query => { + const timeSeriesPromises = queries.map(query => { const {host, database, rp} = query const templatesWithResolution = templates.map(temp => { if (temp.tempVar === ':interval:') { @@ -62,10 +60,16 @@ class LayoutCell extends Component { resolution, }) }) - Promise.all(timeSeriesPromises).then(timeSeries => { - console.log('zomG successfull') - const newSeries = timeSeries.map(response => ({response})) - console.log(newSeries) + + Promise.all(timeSeriesPromises).then(results => { + console.log(results) + const CSVString = dashboardtoCSV(results) + try { + download(CSVString, `${joined_name}.csv`, 'text/plain') + } catch (error) { + errorThrown(error, 'Unable to download .csv file') + console.error(error) + } }) } @@ -79,13 +83,14 @@ class LayoutCell extends Component {
+ ({ + isDeleting, + onEdit, + onDeleteClick, + onDelete, + onCSVDownload, + queriesExist, + cell, + }) =>
-
- -
+ {queriesExist + ?
+ +
+ : null} {isDeleting ?
diff --git a/ui/src/shared/parsing/resultsToCSV.js b/ui/src/shared/parsing/resultsToCSV.js index 18db204c8..ff7a2191d 100644 --- a/ui/src/shared/parsing/resultsToCSV.js +++ b/ui/src/shared/parsing/resultsToCSV.js @@ -4,7 +4,7 @@ import moment from 'moment' export const formatDate = timestamp => moment(timestamp).format('M/D/YYYY h:mm:ss A') -const resultsToCSV = results => { +export const resultsToCSV = results => { if (!_.get(results, ['0', 'series', '0'])) { return {flag: 'no_data', name: '', CSVString: ''} } @@ -30,4 +30,24 @@ const resultsToCSV = results => { return {flag: 'ok', name, CSVString} } -export default resultsToCSV +export const dashboardtoCSV = data => { + const columnNames = _.flatten( + data.map(r => _.get(r, 'results[0].series[0].columns')) // TODO default? + ) + const timeIndices = columnNames + .map((e, i) => (e === 'time' ? i : -1)) + .filter(e => e >= 0) + + let values = data.map(r => _.get(r, 'results[0].series[0].values')) // TODO default? + values = _.unzip(values).map(v => _.flatten(v)) + if (timeIndices) { + values.map(v => { + timeIndices.forEach(i => (v[i] = formatDate(v[i]))) + return v + }) + } + const CSVString = [columnNames.join(',')] + .concat(values.map(v => v.join(','))) + .join('\n') + return CSVString +}