-
Notifications
You must be signed in to change notification settings - Fork 252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Export group #83
Export group #83
Changes from 27 commits
aef9792
d3bb015
e381bd2
5f9996a
5e811fa
c3f6752
7783382
950b045
1d56556
539401e
d610906
eda9804
ff4f005
199d31d
5082823
b659757
c3fc89f
3ae6e54
40dea5e
9db2746
3c22a47
6c0937f
dc03958
538b85c
c91b9a6
16f4e3a
4c6b4ec
a8ca745
394c155
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import React from 'react'; | ||
import { Button } from 'react-bootstrap'; | ||
import moment from 'moment'; | ||
import graphExport from '../services/exportData'; | ||
|
||
export default class ExportComponent extends React.Component { | ||
/** | ||
* Initializes the component's state, binds all functions to 'this' ExportComponent | ||
* @param props The props passed down through the ExportContainer | ||
*/ | ||
constructor(props) { | ||
super(props); | ||
this.exportReading = this.exportReading.bind(this); | ||
} | ||
|
||
/** | ||
* Called when Export button is clicked. | ||
* Passes an object containing the selected meter data to a function for export. | ||
*/ | ||
exportReading() { | ||
const compressedData = this.props.exportVals.datasets; | ||
let time = compressedData[0].exportVals[0].x; | ||
const chart = compressedData[0].currentChart; | ||
time = moment(time).format('ddddMMMDDYYYY'); | ||
const name = `oedExport${time}${chart}.csv`; | ||
graphExport(compressedData, name); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<Button bsStyle="default" onClick={this.exportReading}>Export!</Button> | ||
</div> | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ | |
import React from 'react'; | ||
import HeaderComponent from './HeaderComponent'; | ||
import DashboardContainer from '../containers/DashboardContainer'; | ||
import ExportContainer from '../containers/ExportContainer'; | ||
|
||
|
||
/** | ||
* Top-level React component that controls the home page | ||
|
@@ -15,6 +17,7 @@ export default function HomeComponent() { | |
<div> | ||
<HeaderComponent renderLoginButton /> | ||
<DashboardContainer /> | ||
<ExportContainer /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you move this to be in UIOptionsComponent (at the bottom) instead of here? |
||
</div> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import { connect } from 'react-redux'; | ||
import ExportComponent from '../components/ExportComponent'; | ||
import { chartTypes } from '../reducers/graph'; | ||
|
||
/** | ||
* @param {State} state | ||
* @return {{meterInfo: *, selectedMeters: Array}} | ||
*/ | ||
function mapStateToProps(state) { | ||
const timeInterval = state.graph.timeInterval; | ||
const data = { datasets: [] }; | ||
let readingsData; | ||
const chart = state.graph.chartToRender; | ||
const barDuration = state.graph.barDuration; | ||
|
||
for (const meterID of state.graph.selectedMeters) { | ||
if (chart === chartTypes.line) { | ||
readingsData = state.readings.line.byMeterID[meterID][timeInterval]; | ||
} else if (chart === chartTypes.bar) { readingsData = state.readings.bar.byMeterID[meterID][timeInterval][barDuration]; } | ||
if (readingsData !== undefined && !readingsData.isFetching && chart === chartTypes.line) { | ||
data.datasets.push({ | ||
label: state.meters.byMeterID[meterID].name, | ||
id: state.meters.byMeterID[meterID].id, | ||
timestamp: state.readings.line.byMeterID[meterID][timeInterval].start_timestamp, | ||
currentChart: chart, | ||
exportVals: state.readings.line.byMeterID[meterID][timeInterval].readings.map(arr => ({ x: arr[0], y: arr[1] })) | ||
}); | ||
} else if (readingsData !== undefined && !readingsData.isFetching && chart === chartTypes.bar) { | ||
data.datasets.push({ | ||
label: state.meters.byMeterID[meterID].name, | ||
id: state.meters.byMeterID[meterID].id, | ||
timestamp: state.readings.bar.byMeterID[meterID][timeInterval][barDuration].timestamp, | ||
currentChart: chart, | ||
exportVals: state.readings.bar.byMeterID[meterID][timeInterval][barDuration].readings.map(arr => ({ x: arr[0], y: arr[1] })) | ||
}); | ||
} | ||
} | ||
return { | ||
selectedMeters: state.graph.selectedMeters, | ||
exportVals: data | ||
}; | ||
} | ||
|
||
/** | ||
* Connects changes to the Redux store to UIOptionsComponent via mapStateToProps | ||
*/ | ||
export default connect(mapStateToProps)(ExportComponent); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import moment from 'moment'; | ||
|
||
|
||
/** | ||
* Function to converts the compressed meter data into a CSV formatted string. | ||
* @param items The compressed meter data. | ||
* @returns output A string containing the CSV formatted compressed meter data. | ||
*/ | ||
|
||
function convertToCSV(items) { | ||
let csvOutput = 'Label,Readings,Start Timestamp\n'; | ||
items.forEach(set => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You use a |
||
const data = set.exportVals; | ||
const label = set.label; | ||
data.forEach(reading => { | ||
const info = reading.y; | ||
const startTimeStamp = moment(reading.x).format('dddd MMM DD YYYY hh:mm a'); | ||
csvOutput += `${label},${info} kwh, ${startTimeStamp}\n`; // this assumes that meter readings are in kwh | ||
}); | ||
}); | ||
return csvOutput; | ||
} | ||
/** | ||
* Function to download the formatted CSV file to the users computer. | ||
* @param inputCSV A String containing the formatted CSV data. | ||
* @param fileName A string representing the name of the file. | ||
*/ | ||
function downloadCSV(inputCSV, fileName) { | ||
const element = document.createElement('a'); | ||
element.setAttribute('href', `data:text/csv;charset=utf-8,${encodeURIComponent(inputCSV)}`); | ||
element.setAttribute('download', fileName); | ||
|
||
element.style.display = 'none'; | ||
document.body.appendChild(element); | ||
|
||
element.click(); | ||
|
||
document.body.removeChild(element); | ||
} | ||
|
||
/** | ||
* Function to export compressed data from the graph currently displaying. May be used for routing if more export options are added | ||
* @param dataSets An Object. The compressed data from each meter currently selected in the graph. | ||
* @param name the name of the file. | ||
*/ | ||
export default function graphExport(dataSets, name) { | ||
const dataToExport = convertToCSV(dataSets); | ||
downloadCSV(dataToExport, name); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could easily be a functional component. No need for a class.