-
Notifications
You must be signed in to change notification settings - Fork 1
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
Fix stats summary table time of year bugs #247
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
60e04ab
Create StatisticalSummaryTable component
rod-glover 5c75f7f
Use StatisticalSummaryTable in SingleDataController
rod-glover 4148b91
Conver SingleDataController to native class extension style
rod-glover 0246820
Add css for StatisticalSummaryTable
rod-glover 3cd27b2
Remove unnecessary dependency on DataControllerMixin from DualDataCon…
rod-glover 7e41fb8
Convert DualDataController to native class extension style
rod-glover 79c2151
Add deprecation notes to DataControllerMixin and MotiDataController
rod-glover 769f286
Clean up StatsTableSummary code
rod-glover 2e70721
Comment src/core/util
rod-glover b2f8a35
Make ce-backend function signatures uniform
rod-glover f27b052
Refactor StatisticalSummaryTable to React 16+ standards; fix ToY error
rod-glover b39166e
Modify DataControllerMixin; not sure why, but things work
rod-glover 73b1a07
Code cleanup
rod-glover d6722e9
Add TODOs to data controllers
rod-glover 6cb9bc8
Fix StatisticalSummaryTable smoke test
rod-glover a434d4e
Clean up DataTable code
rod-glover d1f12a1
Remove erroneous promise "cancel" when SST component unmounts
rod-glover 91a21cb
Fix stupid comment placement
rod-glover c690e8b
Use real errorMessage function
rod-glover File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/components/StatisticalSummaryTable/StatisticalSummaryTable.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.mevSummary { | ||
text-align: right; | ||
} |
204 changes: 204 additions & 0 deletions
204
src/components/StatisticalSummaryTable/StatisticalSummaryTable.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
// Statistical Summary Table: Panel containing a Data Table viewer component | ||
// showing statistical information for each climatology period or timeseries. | ||
|
||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
import { Row, Col, Panel } from 'react-bootstrap'; | ||
|
||
import _ from 'underscore'; | ||
|
||
import DataTable from '../DataTable/DataTable'; | ||
import TimeOfYearSelector from '../Selector/TimeOfYearSelector'; | ||
import ExportButtons from '../graphs/ExportButtons'; | ||
import { statsTableLabel } from '../guidance-content/info/InformationItems'; | ||
import { MEVSummary } from '../data-presentation/MEVSummary'; | ||
|
||
import styles from './StatisticalSummaryTable.css'; | ||
import { getStats } from '../../data-services/ce-backend'; | ||
import { | ||
defaultTimeOfYear, | ||
parseBootstrapTableData, | ||
timeKeyToResolutionIndex, | ||
timeResolutions, | ||
validateStatsData, | ||
} from '../../core/util'; | ||
import { errorMessage } from '../graphs/graph-helpers'; | ||
import { exportDataToWorksheet } from '../../core/export'; | ||
|
||
|
||
export default class StatisticalSummaryTable extends React.Component { | ||
static propTypes = { | ||
model_id: PropTypes.string, | ||
variable_id: PropTypes.string, | ||
experiment: PropTypes.string, | ||
area: PropTypes.string, | ||
meta: PropTypes.array, | ||
contextMeta: PropTypes.array, | ||
}; | ||
|
||
// Lifecycle hooks | ||
// Follows React 16+ lifecycle API and recommendations. | ||
// See https://reactjs.org/blog/2018/03/29/react-v-16-3.html | ||
// See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html | ||
// See https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html | ||
|
||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
prevMeta: null, | ||
prevArea: null, | ||
timeOfYear: defaultTimeOfYear(timeResolutions(this.props.meta)), | ||
data: null, | ||
dataError: null, | ||
}; | ||
} | ||
|
||
static getDerivedStateFromProps(props, state) { | ||
if ( | ||
props.meta !== state.prevMeta || | ||
props.area !== state.prevArea | ||
) { | ||
return { | ||
prevMeta: props.meta, | ||
prevArea: props.area, | ||
timeOfYear: defaultTimeOfYear(timeResolutions(props.meta)), | ||
data: null, // Signal that data fetch is required | ||
dataError: null, | ||
}; | ||
} | ||
|
||
// No state update necessary. | ||
return null; | ||
} | ||
|
||
componentDidMount() { | ||
this.fetchData(); | ||
} | ||
|
||
componentDidUpdate(prevProps, prevState) { | ||
if ( | ||
// props changed => data invalid | ||
this.state.data === null || | ||
// user selected new time of year | ||
this.state.timeOfYear !== prevState.timeOfYear | ||
) { | ||
this.fetchData(); | ||
} | ||
} | ||
|
||
// Data fetching | ||
|
||
getAndValidateData(metadata) { | ||
return ( | ||
getStats(metadata) | ||
.then(validateStatsData) | ||
.then(response => response.data) | ||
); | ||
} | ||
|
||
injectRunIntoStats(data) { | ||
// TODO: Make this into a pure function | ||
// Injects model run information into object returned by stats call | ||
_.map(data, function (val, key) { | ||
const selected = this.props.meta.filter(el => el.unique_id === key); | ||
_.extend(val, { run: selected[0].ensemble_member }); | ||
}.bind(this)); | ||
return data; | ||
} | ||
|
||
fetchData() { | ||
const metadata = { | ||
..._.pick(this.props, | ||
'ensemble_name', 'model_id', 'variable_id', 'experiment', 'area'), | ||
...timeKeyToResolutionIndex(this.state.timeOfYear), | ||
}; | ||
this.getAndValidateData(metadata) | ||
.then(data => { | ||
this.setState({ | ||
data: parseBootstrapTableData( | ||
this.injectRunIntoStats(data), this.props.meta), | ||
dataError: null, | ||
}); | ||
}).catch(dataError => { | ||
this.setState({ | ||
// Do we have to set data non-null here to prevent infinite update loop? | ||
dataError, | ||
}); | ||
}); | ||
} | ||
|
||
// User event handlers | ||
|
||
handleChangeTimeOfYear = (timeOfYear) => { | ||
this.setState({ timeOfYear }); | ||
}; | ||
|
||
exportDataTable(format) { | ||
exportDataToWorksheet( | ||
'stats', this.props, this.state.data, format, | ||
{ timeidx: this.state.timeOfYear, | ||
timeres: this.state.dataTableTimeScale } | ||
); | ||
} | ||
|
||
// render helpers | ||
|
||
dataTableOptions() { | ||
// Return a data table options object appropriate to the current state. | ||
|
||
// An error occurred | ||
if (this.state.dataError) { | ||
return { noDataText: errorMessage(this.state.dataError) }; | ||
} | ||
|
||
// Waiting for data | ||
if (this.state.data === null) { | ||
return { noDataText: 'Loading data...' }; | ||
} | ||
|
||
// We can haz data | ||
return { noDataText: 'We have data and this message should not show' }; | ||
} | ||
|
||
render() { | ||
return ( | ||
<Panel> | ||
<Panel.Heading> | ||
<Panel.Title> | ||
<Row> | ||
<Col lg={4}> | ||
{statsTableLabel} | ||
</Col> | ||
<Col lg={8}> | ||
<MEVSummary className={styles.mevSummary} {...this.props} /> | ||
</Col> | ||
</Row> | ||
</Panel.Title> | ||
</Panel.Heading> | ||
<Panel.Body className={styles.data_panel}> | ||
<Row> | ||
<Col lg={6} md={6} sm={6}> | ||
<TimeOfYearSelector | ||
value={this.state.timeOfYear} | ||
onChange={this.handleChangeTimeOfYear} | ||
{...timeResolutions(this.props.meta)} | ||
inlineLabel | ||
/> | ||
</Col> | ||
<Col lg={6} md={6} sm={6}> | ||
<ExportButtons | ||
onExportXlsx={this.exportDataTable.bind(this, 'xlsx')} | ||
onExportCsv={this.exportDataTable.bind(this, 'csv')} | ||
/> | ||
</Col> | ||
</Row> | ||
<DataTable | ||
data={this.state.data || []} | ||
options={this.dataTableOptions()} | ||
/> | ||
</Panel.Body> | ||
</Panel> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import StatisticalSummaryTable from '../StatisticalSummaryTable'; | ||
import { noop } from 'underscore'; | ||
import { meta } from '../../../test_support/data'; | ||
|
||
it('renders without crashing', () => { | ||
const div = document.createElement('div'); | ||
ReactDOM.render( | ||
<StatisticalSummaryTable | ||
model_id={'GFDL-ESM2G'} | ||
variable_id={'tasmax'} | ||
experiment={'historical,rcp26'} | ||
meta={meta} | ||
/>, | ||
div | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "StatisticalSummaryTable", | ||
"version": "0.0.0", | ||
"private": true, | ||
"main": "./StatisticalSummaryTable.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/components/data-controllers/MotiDataController/MotiDataController.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Yay! Finally escaped the clutches of the mixin!