Skip to content
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

Performance fixes #5134

Merged
merged 4 commits into from
Oct 22, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make the renderer reuse Date objects instead of instantiating a huge …
…amount of dates all the time
  • Loading branch information
sulkaharo committed Oct 21, 2019
commit dc484faefb8d8c8241cc3f6776695fabc573dc1f
46 changes: 27 additions & 19 deletions lib/client/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,21 @@ var DEFAULT_FOCUS = times.hours(3).msecs
, TOOLTIP_WIDTH = 150 //min-width + padding
;

const zeroDate = new Date(0);

function init (client, d3) {

var renderer = {};

var utils = client.utils;
var translate = client.translate;

function getOrAddDate(entry) {
if (entry.date) return entry.date;
entry.date = new Date(entry.mills);
return entry.date;
}

//chart isn't created till the client gets data, so can grab the var at init
function chart () {
return client.chart;
Expand Down Expand Up @@ -97,12 +105,12 @@ function init (client, d3) {
sel.attr('cx', function(d) {
if (!d) {
console.error('Bad data', d);
return chart().xScale(new Date(0));
return chart().xScale(zeroDate);
} else if (!d.mills) {
console.error('Bad data, no mills', d);
return chart().xScale(new Date(0));
return chart().xScale(zeroDate);
} else {
return chart().xScale(new Date(d.mills));
return chart().xScale(getOrAddDate(d));
}
})
.attr('cy', function(d) {
Expand Down Expand Up @@ -172,7 +180,7 @@ function init (client, d3) {
(d.type === 'forecast' && d.forecastType ? '<br/><strong>' + translate('Forecast Type') + ': </strong>' + d.forecastType : '') +
(rawbgInfo.value ? '<br/><strong>' + translate('Raw BG') + ':</strong> ' + rawbgInfo.value : '') +
(rawbgInfo.noise ? '<br/><strong>' + translate('Noise') + ':</strong> ' + rawbgInfo.noise : '') +
'<br/><strong>' + translate('Time') + ':</strong> ' + client.formatTime(new Date(d.mills)))
'<br/><strong>' + translate('Time') + ':</strong> ' + client.formatTime(getOrAddDate(d)))
.style('left', tooltipLeft())
.style('top', (d3.event.pageY + 15) + 'px');
}
Expand Down Expand Up @@ -232,7 +240,7 @@ function init (client, d3) {
}
}

return '<strong>' + translate('Time') + ':</strong> ' + client.formatTime(new Date(d.mills)) + '<br/>' +
return '<strong>' + translate('Time') + ':</strong> ' + client.formatTime(getOrAddDate(d)) + '<br/>' +
(d.eventType ? '<strong>' + translate('Treatment type') + ':</strong> ' + translate(client.careportal.resolveEventName(d.eventType)) + '<br/>' : '') +
(d.reason ? '<strong>' + translate('Reason') + ':</strong> ' + translate(d.reason) + '<br/>' : '') +
(d.glucose ? '<strong>' + translate('BG') + ':</strong> ' + d.glucose + (d.glucoseType ? ' (' + translate(d.glucoseType) + ')' : '') + '<br/>' : '') +
Expand All @@ -246,7 +254,7 @@ function init (client, d3) {
}

function announcementTooltip (d) {
return '<strong>' + translate('Time') + ':</strong> ' + client.formatTime(new Date(d.mills)) + '<br/>' +
return '<strong>' + translate('Time') + ':</strong> ' + client.formatTime(getOrAddDate(d)) + '<br/>' +
(d.eventType ? '<strong>' + translate('Announcement') + '</strong><br/>' : '') +
(d.notes && d.notes.length > 1 ? '<strong>' + translate('Message') + ':</strong> ' + d.notes + '<br/>' : '') +
(d.enteredBy ? '<strong>' + translate('Entered By') + ':</strong> ' + d.enteredBy + '<br/>' : '');
Expand Down Expand Up @@ -275,7 +283,7 @@ function init (client, d3) {
function updateTreatCircles (sel) {

sel.attr('cx', function(d) {
return chart().xScale(new Date(d.mills));
return chart().xScale(getOrAddDate(d));
})
.attr('cy', function(d) {
return chart().yScale(client.sbx.scaleEntry(d));
Expand Down Expand Up @@ -369,26 +377,26 @@ function init (client, d3) {
if (d.eventType === 'Temporary Target') {
top = d.targetTop === d.targetBottom ? d.targetTop + rectHeight(d) : d.targetTop;
}
return 'translate(' + chart().xScale(new Date(d.mills)) + ',' + chart().yScale(utils.scaleMgdl(top)) + ')';
return 'translate(' + chart().xScale(getOrAddDate(d)) + ',' + chart().yScale(utils.scaleMgdl(top)) + ')';
}

function treatmentRectWidth (d) {
if (d.durationType === "indefinite") {
return chart().xScale(chart().xScale.domain()[1].getTime()) - chart().xScale(new Date(d.mills));
return chart().xScale(chart().xScale.domain()[1].getTime()) - chart().xScale(getOrAddDate(d));
} else {
return chart().xScale(new Date(d.mills + times.mins(d.duration).msecs)) - chart().xScale(new Date(d.mills));
return chart().xScale(new Date(d.mills + times.mins(d.duration).msecs)) - chart().xScale(getOrAddDate(d));
}
}

function treatmentTextTransform (d) {
if (d.durationType === "indefinite") {
var offset = 0;
if (chart().xScale(new Date(d.mills)) < chart().xScale(chart().xScale.domain()[0].getTime())) {
offset = chart().xScale(nowDate) - chart().xScale(new Date(d.mills));
if (chart().xScale(getOrAddDate(d)) < chart().xScale(chart().xScale.domain()[0].getTime())) {
offset = chart().xScale(nowDate) - chart().xScale(getOrAddDate(d));
}
return 'translate(' + offset + ',' + 10 + ')';
} else {
return 'translate(' + (chart().xScale(new Date(d.mills + times.mins(d.duration).msecs)) - chart().xScale(new Date(d.mills))) / 2 + ',' + 10 + ')';
return 'translate(' + (chart().xScale(new Date(d.mills + times.mins(d.duration).msecs)) - chart().xScale(getOrAddDate(d))) / 2 + ',' + 10 + ')';
}
}

Expand Down Expand Up @@ -457,7 +465,7 @@ function init (client, d3) {

function prepareContextCircles (sel) {
var badData = [];
sel.attr('cx', function(d) { return chart().xScale2(new Date(d.mills)); })
sel.attr('cx', function(d) { return chart().xScale2(getOrAddDate(d)); })
.attr('cy', function(d) {
var scaled = client.sbx.scaleEntry(d);
if (isNaN(scaled)) {
Expand Down Expand Up @@ -623,7 +631,7 @@ function init (client, d3) {
}

client.tooltip.transition().duration(TOOLTIP_TRANS_MS).style('opacity', .9);
client.tooltip.html('<strong>' + translate('Time') + ':</strong> ' + client.formatTime(new Date(treatment.mills)) + '<br/>' + '<strong>' + translate('Treatment type') + ':</strong> ' + translate(client.careportal.resolveEventName(treatment.eventType)) + '<br/>' +
client.tooltip.html('<strong>' + translate('Time') + ':</strong> ' + client.formatTime(getOrAddDate(treatment)) + '<br/>' + '<strong>' + translate('Treatment type') + ':</strong> ' + translate(client.careportal.resolveEventName(treatment.eventType)) + '<br/>' +
(treatment.carbs ? '<strong>' + translate('Carbs') + ':</strong> ' + treatment.carbs + '<br/>' : '') +
(treatment.protein ? '<strong>' + translate('Protein') + ':</strong> ' + treatment.protein + '<br/>' : '') +
(treatment.fat ? '<strong>' + translate('Fat') + ':</strong> ' + treatment.fat + '<br/>' : '') +
Expand Down Expand Up @@ -771,7 +779,7 @@ function init (client, d3) {
chart().drag.append('line')
.attr('class', 'arrow')
.attr('marker-end', 'url(#arrow)')
.attr('x1', chart().xScale(new Date(treatment.mills)))
.attr('x1', chart().xScale(getOrAddDate(treatment)))
.attr('y1', chart().yScale(client.sbx.scaleEntry(treatment)))
.attr('x2', x)
.attr('y2', y)
Expand Down Expand Up @@ -915,7 +923,7 @@ function init (client, d3) {
.enter()
.append('g')
.attr('class', 'draggable-treatment')
.attr('transform', 'translate(' + chart().xScale(new Date(treatment.mills)) + ', ' + chart().yScale(client.sbx.scaleEntry(treatment)) + ')')
.attr('transform', 'translate(' + chart().xScale(getOrAddDate(treatment)) + ', ' + chart().yScale(client.sbx.scaleEntry(treatment)) + ')')
.on('mouseover', treatmentTooltip)
.on('mouseout', hideTooltip);
if (client.editMode) {
Expand Down Expand Up @@ -997,7 +1005,7 @@ function init (client, d3) {
//when the tests are run window isn't available
var innerWidth = window && window.innerWidth || -1;
// don't render the treatment if it's not visible
if (Math.abs(chart().xScale(new Date(treatment.mills))) > innerWidth) {
if (Math.abs(chart().xScale(getOrAddDate(treatment))) > innerWidth) {
return;
}

Expand Down Expand Up @@ -1162,7 +1170,7 @@ function init (client, d3) {
}

function profileTooltip (d) {
return '<strong>' + translate('Time') + ':</strong> ' + client.formatTime(new Date(d.mills)) + '<br/>' +
return '<strong>' + translate('Time') + ':</strong> ' + client.formatTime(getOrAddDate(d)) + '<br/>' +
(d.eventType ? '<strong>' + translate('Treatment type') + ':</strong> ' + translate(client.careportal.resolveEventName(d.eventType)) + '<br/>' : '') +
(d.endprofile ? '<strong>' + translate('End of profile') + ':</strong> ' + d.endprofile + '<br/>' : '') +
(d.profile ? '<strong>' + translate('Profile') + ':</strong> ' + d.profile + '<br/>' : '') +
Expand Down