Skip to content

Commit

Permalink
Merge pull request #1465 from tidepool-org/WEB-3083-libre-2-plus-stats
Browse files Browse the repository at this point in the history
[WEB-3083] Ensure bgSource chartPrefs are set if missing on subsequent data fetches
  • Loading branch information
clintonium-119 authored Nov 18, 2024
2 parents e588ec0 + 2eb87fe commit ae2954e
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 17 deletions.
27 changes: 21 additions & 6 deletions app/pages/patientdata/patientdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -1420,9 +1420,10 @@ export const PatientDataClass = createReactClass({
return aggregations;
},

getStatsByChartType: function(chartType = this.state.chartType) {
const cbgSelected = _.get(this.state.chartPrefs, [chartType, 'bgSource']) === 'cbg';
const smbgSelected = _.get(this.state.chartPrefs, [chartType, 'bgSource']) === 'smbg';
getStatsByChartType: function(chartType = this.state.chartType, bgSource) {
const currentBgSource = bgSource || _.get(this.state.chartPrefs, [chartType, 'bgSource']);
const cbgSelected = currentBgSource === 'cbg';
const smbgSelected = currentBgSource === 'smbg';
const isAutomatedBasalDevice = _.get(this.props.data, 'metaData.latestPumpUpload.isAutomatedBasalDevice');
const isSettingsOverrideDevice = _.get(this.props.data, 'metaData.latestPumpUpload.isSettingsOverrideDevice');

Expand Down Expand Up @@ -1782,8 +1783,21 @@ export const PatientDataClass = createReactClass({

const newDataAdded = this.props.addingData.inProgress && nextProps.addingData.completed;
if (newDataAdded) {
const nextBgSource = _.get(nextProps, 'data.metaData.bgSources.current');
const queryOpts = {}

if (nextBgSource && this.state.chartType && _.isEmpty(_.get(this.state.chartPrefs, [this.state.chartType, 'bgSource']))) {
// If this is the first data fetch that contained bg data, we check for it and set the
// bgSource state accordingly to allow generating all appropriate stats
this.updateChartPrefs({
[this.state.chartType]: { ...this.state.chartPrefs[this.state.chartType], bgSource: nextBgSource },
}, false, false);

queryOpts.bgSource = nextBgSource;
}

// New data has been added. Let's query it to update the chart.
this.queryData();
this.queryData(null, queryOpts);

// If new data was fetched to support requested PDF dates, kick off pdf generation.
if (this.state.printDialogPDFOpts) {
Expand Down Expand Up @@ -1874,13 +1888,14 @@ export const PatientDataClass = createReactClass({
updateChartEndpoints: options.updateChartEndpoints || !this.state.chartEndpoints,
transitioningChartType: false,
metaData: 'bgSources,devices,matchedDevices,excludedDevices,queryDataCount',
bgSource: _.get(this.state, ['chartPrefs', this.state.chartType, 'bgSource']),
});

if (this.state.queryingData) return;
this.setState({ loading: options.showLoading, queryingData: true });

let chartQuery = {
bgSource: _.get(this.state, ['chartPrefs', this.state.chartType, 'bgSource']),
bgSource: options.bgSource,
chartType: this.state.chartType,
excludedDevices: _.get(this.state, 'chartPrefs.excludedDevices', []),
excludeDaysWithoutBolus: _.get(this.state, ['chartPrefs', this.state.chartType, 'stats', 'excludeDaysWithoutBolus']),
Expand Down Expand Up @@ -1956,7 +1971,7 @@ export const PatientDataClass = createReactClass({

const { next: nextDays, prev: prevDays } = this.getDaysByType();

chartQuery.stats = this.getStatsByChartType();
chartQuery.stats = this.getStatsByChartType(this.state.chartType, options.bgSource);
chartQuery.nextDays = nextDays;
chartQuery.prevDays = prevDays;

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"node": "20.8.0"
},
"packageManager": "yarn@3.6.4",
"version": "1.82.0-web-3178-tide-connection-status.1",
"version": "1.83.0-web-3083-libre-2-plus-stats.1",
"private": true,
"scripts": {
"test": "TZ=UTC NODE_ENV=test NODE_OPTIONS='--max-old-space-size=4096' yarn karma start",
Expand Down Expand Up @@ -64,7 +64,7 @@
"@storybook/react": "7.5.0",
"@storybook/react-webpack5": "7.5.0",
"@testing-library/react-hooks": "8.0.1",
"@tidepool/viz": "1.43.0-develop.2",
"@tidepool/viz": "1.44.0-web-3083-libre-2-plus-stats.1",
"async": "2.6.4",
"autoprefixer": "10.4.16",
"babel-core": "7.0.0-bridge.0",
Expand Down
96 changes: 92 additions & 4 deletions test/unit/pages/patientdata.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1928,6 +1928,34 @@ describe('PatientData', function () {
expect(instance.getStatsByChartType()).to.eql([]);
});
});

context('bgSource chartPref state missing', () => {
beforeEach(() => {
wrapper.setState({
chartType: 'daily',
chartPrefs: { daily: { bgSource: undefined } },
});
});

it('should add appropriate stats when no bgSource is available', () => {
expect(instance.getStatsByChartType('daily')).to.eql([
'averageGlucose',
'totalInsulin',
'carbs',
]);
});

it('should add appropriate stats when cbg is provided via arg', () => {
expect(instance.getStatsByChartType('daily', 'cbg')).to.eql([
'timeInRange',
'averageGlucose',
'totalInsulin',
'carbs',
'standardDev',
'coefficientOfVariation',
]);
});
});
});

describe('getDaysByType', () => {
Expand Down Expand Up @@ -2704,7 +2732,7 @@ describe('PatientData', function () {
});

context('new data added', () => {
it('should call queryData with no arguments', () => {
it('should call queryData with default arguments', () => {
const queryDataSpy = sinon.spy(instance, 'queryData');

// Adding Data
Expand All @@ -2717,9 +2745,51 @@ describe('PatientData', function () {
addingData: { inProgress: false, completed: true }
}));

sinon.assert.callCount(queryDataSpy,1);
// ensure queryData called with zero args
sinon.assert.calledWithExactly(queryDataSpy, ...[]);
sinon.assert.callCount(queryDataSpy, 1);

// ensure queryData called with default args
sinon.assert.calledWithExactly(queryDataSpy, null,{
showLoading: true,
updateChartEndpoints: true,
transitioningChartType: false,
metaData: 'bgSources,devices,matchedDevices,excludedDevices,queryDataCount',
bgSource: undefined,
});
});

context('bgSource metadata newly available', () => {
it('should call queryData with bgSource in the `options` argument and set as chartPref to state', () => {
const queryDataSpy = sinon.spy(instance, 'queryData');

wrapper.setState({
chartType: 'daily',
chartPrefs: {
daily: { bgSource: undefined },
},
});

// Adding Data
wrapper.setProps(_.assign({}, props, {
addingData: { inProgress: true, completed: false }
}));

// Completed adding data with
wrapper.setProps(_.assign({}, props, {
addingData: { inProgress: false, completed: true },
data: { ...props.data, metaData: { ...props.data.metaData, bgSources: { current: 'cbg' } } },
}));

sinon.assert.callCount(queryDataSpy, 1);

// ensure queryData called with bgSource in addition to the default args
sinon.assert.calledWithExactly(queryDataSpy, null,{
showLoading: true,
updateChartEndpoints: true,
transitioningChartType: false,
metaData: 'bgSources,devices,matchedDevices,excludedDevices,queryDataCount',
bgSource: 'cbg',
});
});
});

it('should not generate a pdf if `printDialogPDFOpts` state is not set', () => {
Expand Down Expand Up @@ -2898,6 +2968,24 @@ describe('PatientData', function () {
sinon.assert.calledWithMatch(setStateSpy, { loading: true });
});

it('should set the `bgSource` query to `options.bgSource` arg, if provided, otherwise use the bgSource from chartPrefs state', () => {
wrapper.setState({
chartType: 'daily',
chartPrefs: { daily: { bgSource: 'cbg' } }
});

instance.queryData(emptyQuery, {});
sinon.assert.calledWithMatch(defaultProps.dataWorkerQueryDataRequest, { bgSource: 'cbg' });

defaultProps.dataWorkerQueryDataRequest.resetHistory();
wrapper.setState({
queryingData: false,
});

instance.queryData(emptyQuery, { bgSource: 'smbg' });
sinon.assert.calledWithMatch(defaultProps.dataWorkerQueryDataRequest, { bgSource: 'smbg' });
});

it('should set the `metaData` query to `options.metaData` arg', () => {
instance.queryData(emptyQuery, { metaData: 'latestDatumByType, size' });
sinon.assert.calledWithMatch(defaultProps.dataWorkerQueryDataRequest, { metaData: 'latestDatumByType, size' });
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5278,9 +5278,9 @@ __metadata:
languageName: node
linkType: hard

"@tidepool/viz@npm:1.43.0-develop.2":
version: 1.43.0-develop.2
resolution: "@tidepool/viz@npm:1.43.0-develop.2"
"@tidepool/viz@npm:1.44.0-web-3083-libre-2-plus-stats.1":
version: 1.44.0-web-3083-libre-2-plus-stats.1
resolution: "@tidepool/viz@npm:1.44.0-web-3083-libre-2-plus-stats.1"
dependencies:
bluebird: 3.7.2
bows: 1.7.2
Expand Down Expand Up @@ -5340,7 +5340,7 @@ __metadata:
react-dom: 16.x
react-redux: 8.x
redux: 4.x
checksum: 15c7230fa2a7ca1ea07cf0249c768291356cc932a2fbe3f122e475a1af4ccf68a1cb5c0db4ee0160f5c6b69df579757592578911aadd23a28c12d3038932c5eb
checksum: 29350f2ca441b44ba4cb097202983e55498f8c7f17a3a9478b8716acc33e25b09641d456e89a4b376b2eeed509656cfe0701f80978551c5cdb1c5f2465ffa1fe
languageName: node
linkType: hard

Expand Down Expand Up @@ -7305,7 +7305,7 @@ __metadata:
"@storybook/react": 7.5.0
"@storybook/react-webpack5": 7.5.0
"@testing-library/react-hooks": 8.0.1
"@tidepool/viz": 1.43.0-develop.2
"@tidepool/viz": 1.44.0-web-3083-libre-2-plus-stats.1
async: 2.6.4
autoprefixer: 10.4.16
babel-core: 7.0.0-bridge.0
Expand Down

0 comments on commit ae2954e

Please sign in to comment.