Skip to content

Commit 6c53354

Browse files
Timeline: Add a date range and keep zooms in permalinks
1 parent 54ea327 commit 6c53354

7 files changed

Lines changed: 335 additions & 18 deletions

File tree

‎codespeed/static/css/main.css‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,10 @@ a#permalink, a#exportcsv {
587587
/* The download glyph's viewBox carries a margin, so both look the same size */
588588
a#permalink svg, a#exportcsv svg { width: 1.1em; height: 1.1em; }
589589
a#exportcsv + a#permalink { margin-left: 1em; }
590+
a#permalink.copied::after { content: "copied"; color: var(--text-muted); }
591+
a#resetzoom { margin-left: auto; font-size: 0.9em; }
592+
a#resetzoom:not([hidden]) + a#permalink { margin-left: 1em; }
593+
div#configbar input[type="date"] { margin: 0 0.3em; font: inherit; }
590594

591595
/* Plot styles */
592596
div#plot { text-align: left; height: 500px; width: 100%; }

‎codespeed/static/js/timeline.js‎

Lines changed: 181 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@ var baselineColor = "#d8b83f",
99
"#ff5800", "#958c12", "#4b5de4", "#0085cc"],
1010
defaults,
1111
plotInstance = null,
12-
miniplotInstances = [];
12+
miniplotInstances = [],
13+
// Zoom of the main plot, mirrored in the URL as zx/zy so permalinks keep
14+
// it: {x: [minMs, maxMs], y: [min, max]}, either key optional.
15+
zoom = null,
16+
// Reads the zoom off the main plot; runs shortly after its last redraw
17+
zoomSync = null,
18+
zoomSyncTimer = null,
19+
// First and last day (YYYY-MM-DD) of the results on screen
20+
dataRange = null;
1321

1422
function setExeColors() {
1523
$("#executable > div.boxbody > ul > ul > li > input").each(function(index) {
@@ -45,6 +53,9 @@ function getConfiguration() {
4553
};
4654
var branch = readCheckbox("input[name='branch']:checked");
4755
if (branch) { config.bran = branch; }
56+
var from = $("#datefrom").val(), to = $("#dateto").val();
57+
if (from) { config.from = from; }
58+
if (to) { config.to = to; }
4859
return config;
4960
}
5061

@@ -61,16 +72,91 @@ function getUrlParams() {
6172
return params;
6273
}
6374

75+
function parseRange(str) {
76+
if (!str) { return null; }
77+
var parts = str.split(',').map(Number);
78+
return (parts.length === 2 && isFinite(parts[0]) && isFinite(parts[1]) &&
79+
parts[0] < parts[1]) ? parts : null;
80+
}
81+
82+
function parseZoom(params) {
83+
var x = parseRange(params.zx), y = parseRange(params.zy);
84+
if (!x && !y) { return null; }
85+
var parsed = {};
86+
if (x) {
87+
parsed.x = x.map(function(v) { return v * 1000; });
88+
}
89+
if (y) {
90+
parsed.y = y;
91+
}
92+
return parsed;
93+
}
94+
95+
function cancelZoomSync() {
96+
clearTimeout(zoomSyncTimer);
97+
zoomSyncTimer = null;
98+
}
99+
100+
// Run a pending sync now, for a caller that needs the zoom as it is on screen
101+
function flushZoomSync() {
102+
if (zoomSyncTimer !== null) {
103+
zoomSync();
104+
}
105+
}
106+
107+
function clearZoom() {
108+
cancelZoomSync();
109+
zoom = null;
110+
$("#resetzoom").prop("hidden", true);
111+
}
112+
113+
// The query string for the selection plus the current zoom, which is not sent
114+
// to the server.
115+
function getQuery() {
116+
var config = getConfiguration();
117+
// A start date replaces the "last N results" limit
118+
if (config.from) { delete config.revs; }
119+
if (zoom && zoom.x) {
120+
config.zx = zoom.x.map(function(v) { return Math.round(v / 1000); }).join(',');
121+
}
122+
if (zoom && zoom.y) {
123+
config.zy = zoom.y.map(function(v) { return Number(v.toPrecision(4)); }).join(',');
124+
}
125+
return '?' + $.param(config).replace(/%2C/g, ',');
126+
}
127+
64128
function updateUrl() {
65-
history.replaceState(null, '', '?' + $.param(getConfiguration()));
129+
history.replaceState(null, '', getQuery());
130+
}
131+
132+
// Set the dates left open to the days of the results on screen, so that a
133+
// link to this view keeps showing them as newer ones arrive. The plot is left
134+
// alone: it already shows these results.
135+
function pinDates() {
136+
if (!dataRange) { return; }
137+
if (!$("#datefrom").val()) { $("#datefrom").val(dataRange.from); }
138+
if (!$("#dateto").val()) { $("#dateto").val(dataRange.to); }
139+
$("#revisions").attr("disabled", true);
140+
}
141+
142+
function noteDataRange(sortedDateKeys) {
143+
if (!sortedDateKeys.length) { return; }
144+
var day = function(dk) { return dk.trim().slice(0, 10).replace(/\//g, '-'); };
145+
var from = day(sortedDateKeys[0]), to = day(sortedDateKeys[sortedDateKeys.length - 1]);
146+
dataRange = {
147+
from: (dataRange && dataRange.from < from) ? dataRange.from : from,
148+
to: (dataRange && dataRange.to > to) ? dataRange.to : to
149+
};
66150
}
67151

68152
function destroyPlots() {
153+
cancelZoomSync();
69154
if (plotInstance) { plotInstance.destroy(); plotInstance = null; }
70155
$("#plot").off("mousemove.tooltip");
71156
$("#plot-legend").remove();
72157
miniplotInstances.forEach(function(g) { g.destroy(); });
73158
miniplotInstances = [];
159+
dataRange = null;
74160
}
75161

76162
// Build dygraphs-ready data from the branches dict.
@@ -192,6 +278,13 @@ function renderPlot(data) {
192278
'<p class="note"><i>' + data.benchmark + '</i>: ' + data.benchmark_description + '</p>');
193279
}
194280

281+
if (data.truncated) {
282+
$("#plotdescription").append(
283+
'<p class="note">This date range holds more results than can be shown ' +
284+
'at once, so only the most recent ones are plotted. Set an earlier ' +
285+
'"to" date to see the older ones.</p>');
286+
}
287+
195288
if (median) {
196289
$("span.options.median").css("display", "inline");
197290
}
@@ -216,6 +309,7 @@ function renderPlot(data) {
216309

217310
var commitMap = built.commitMap;
218311
var sortedDateKeys = built.sortedDateKeys;
312+
noteDataRange(sortedDateKeys);
219313
var seriesIds = built.seriesIds;
220314
var tsMap = built.tsMap;
221315
var dateIndex = built.dateIndex;
@@ -333,6 +427,37 @@ function renderPlot(data) {
333427

334428
var lastHighlightPoints = null, lastHighlightX = null;
335429

430+
// The zoom is kept in timestamps so that a permalink still frames the
431+
// same commits once newer results shift the equidistant indexes.
432+
var tsList = sortedDateKeys.map(function(dk) { return dateIndex[dk].getTime(); });
433+
var lastSeg = tsList.length - 2;
434+
function xToTs(x) {
435+
if (!equidistant) { return x; }
436+
var i = Math.max(0, Math.min(lastSeg, Math.floor(x)));
437+
return tsList[i] + (x - i) * (tsList[i + 1] - tsList[i]);
438+
}
439+
function tsToX(ts) {
440+
if (!equidistant) { return ts; }
441+
var i = 0;
442+
while (i < lastSeg && tsList[i + 1] <= ts) { i++; }
443+
var span = tsList[i + 1] - tsList[i];
444+
return span ? i + (ts - tsList[i]) / span : i;
445+
}
446+
// A single point gets a padded window below, which is not a user zoom
447+
var zoomable = built.data.length > 1;
448+
if (!zoomable && zoom) { clearZoom(); updateUrl(); }
449+
var initialZoom = zoom;
450+
$("#resetzoom").prop("hidden", !initialZoom);
451+
zoomSync = function() {
452+
cancelZoomSync();
453+
var g = plotInstance;
454+
zoom = null;
455+
if (g.isZoomed('x')) { zoom = {x: g.xAxisRange().map(xToTs)}; }
456+
if (g.isZoomed('y')) { (zoom = zoom || {}).y = g.yAxisRange(0); }
457+
$("#resetzoom").prop("hidden", !zoom);
458+
updateUrl();
459+
};
460+
336461
// When there's a single data point, xMin===xMax and Dygraph clips the dot
337462
// at the canvas edge. Compute an explicit dateWindow with padding.
338463
var singlePointWindow = null;
@@ -365,11 +490,22 @@ function renderPlot(data) {
365490
ylabel: data.units + data.lessisbetter,
366491
axes: {
367492
x: xAxisOpts,
368-
y: { valueRange: [0, null] }
493+
// includeZero rather than valueRange: [0, null], which
494+
// Dygraphs would report as a permanent y zoom
495+
y: { includeZero: true,
496+
valueRange: (initialZoom && initialZoom.y) || null }
369497
},
370498
xRangePad: 20,
371-
dateWindow: singlePointWindow,
499+
dateWindow: (initialZoom && initialZoom.x) ? initialZoom.x.map(tsToX)
500+
: singlePointWindow,
372501
connectSeparatedPoints: true,
502+
// Fires on zoom, pan and reset alike; debounced as panning redraws
503+
// on every mouse move.
504+
drawCallback: function(g, isInitial) {
505+
if (isInitial || !zoomable) { return; }
506+
clearTimeout(zoomSyncTimer);
507+
zoomSyncTimer = setTimeout(zoomSync, 200);
508+
},
373509
underlayCallback: function(canvas, area, g) {
374510
// Read the colour at draw time: a theme switch redraws the plot
375511
var textColor = Theme.chartColors().text;
@@ -428,6 +564,12 @@ function renderPlot(data) {
428564
}
429565
);
430566

567+
if (initialZoom && initialZoom.y) {
568+
// The plot now holds the range as its zoom; drop the option (without
569+
// redrawing) so that resetting the zoom does not bring it back.
570+
plotInstance.updateOptions({axes: {y: {valueRange: null}}}, true);
571+
}
572+
431573
// Persistent legend below the plot
432574
(function() {
433575
var $legend = $('<div id="plot-legend" style="margin:6px 0 4px 40px;font-size:0.9em;"></div>');
@@ -484,6 +626,7 @@ function renderMiniplot(plotid, data) {
484626
var median = data['data_type'] === 'M';
485627
var built = buildGraphData(data.branches, data.environments || [], median, false);
486628
if (built.data.length === 0) { return; }
629+
noteDataRange(built.sortedDateKeys);
487630

488631
if (data.baseline !== "None") {
489632
built.labels.push('Baseline');
@@ -526,7 +669,7 @@ function renderMiniplot(plotid, data) {
526669
legend: 'never',
527670
axes: {
528671
x: { drawAxis: false, drawGrid: false },
529-
y: { drawAxis: false, drawGrid: false, valueRange: [0, null] }
672+
y: { drawAxis: false, drawGrid: false, includeZero: true }
530673
},
531674
xRangePad: 20,
532675
dateWindow: miniWindow,
@@ -538,7 +681,8 @@ function renderMiniplot(plotid, data) {
538681
}
539682

540683
function render(data) {
541-
$("#revisions").attr("disabled", false);
684+
// A start date replaces the "last N results" limit
685+
$("#revisions").attr("disabled", !!$("#datefrom").val());
542686
$("#equidistant").attr("disabled", false);
543687
$("span.options.median").css("display", "none");
544688

@@ -576,6 +720,7 @@ function render(data) {
576720
$div.click(function() {
577721
var benchid = $(this).attr("id").slice(5);
578722
$("#benchmark_" + benchid).prop('checked', true);
723+
clearZoom();
579724
updateUrl();
580725
refreshContent();
581726
});
@@ -603,6 +748,8 @@ function setValuesOfInputFields(params) {
603748

604749
$("#revisions").val(valueOrDefault(params.revs, defaults.revisions));
605750
$("#baseline").val(valueOrDefault(params.base, defaults.baseline));
751+
$("#datefrom").val(params.from || '');
752+
$("#dateto").val(params.to || '');
606753

607754
var executables = params.exe ? params.exe.split(',') : defaults.executables;
608755
var sel = $("input[name='executable']");
@@ -697,9 +844,12 @@ function initializeSite() {
697844
setValuesOfInputFields(params);
698845
initBenchmarkSearch();
699846

700-
var onChange = function() { updateUrl(); refreshContent(); };
847+
zoom = parseZoom(params);
701848

702-
$("#revisions, #baseline").change(onChange);
849+
// A zoom belongs to the plot it was made on: any new selection drops it
850+
var onChange = function() { clearZoom(); updateUrl(); refreshContent(); };
851+
852+
$("#revisions, #baseline, #datefrom, #dateto").change(onChange);
703853
$("input[name='branch'], input[name='benchmark']," +
704854
"#equidistant, #show_quartile_bands, #show_extrema_bands"
705855
).change(onChange);
@@ -714,12 +864,32 @@ function initializeSite() {
714864
});
715865
$('.checkall, .uncheckall').click(function() { updateExeColors(); onChange(); });
716866

717-
$("#permalink").click(function() {
718-
window.location = "?" + $.param(getConfiguration());
867+
$("#resetzoom").click(function(e) {
868+
e.preventDefault();
869+
clearZoom();
870+
if (plotInstance) { plotInstance.resetZoom(); }
871+
updateUrl();
872+
});
873+
874+
$("#permalink").click(function(e) {
875+
e.preventDefault();
876+
flushZoomSync();
877+
pinDates();
878+
updateUrl();
879+
var $link = $(this);
880+
if (navigator.clipboard) {
881+
navigator.clipboard.writeText(window.location.href).then(function() {
882+
$link.addClass("copied");
883+
setTimeout(function() { $link.removeClass("copied"); }, 1500);
884+
}, function() {});
885+
}
719886
});
720887

721888
window.addEventListener('popstate', function() {
722-
setValuesOfInputFields(getUrlParams());
889+
var params = getUrlParams();
890+
clearZoom();
891+
zoom = parseZoom(params);
892+
setValuesOfInputFields(params);
723893
refreshContent();
724894
});
725895

‎codespeed/templates/codespeed/timeline.html‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@
101101
<option value="{{ rev }}">{{ rev }}</option>{% endfor %}
102102
</select> results
103103
</span>
104+
<span class="options" title="Only show results of revisions between these dates. A start date shows every result since then instead of the last few.">
105+
<label for="datefrom">From</label>
106+
<input id="datefrom" type="date" />
107+
<label for="dateto">to</label>
108+
<input id="dateto" type="date" />
109+
</span>
104110
<span class="options" title="Equal distance between results">
105111
<input id="equidistant" name="equidistant" type="checkbox" />
106112
<label for="equidistant">Equidistant</label>
@@ -115,6 +121,7 @@
115121
<label for="show_extrema_bands">Show extrema bands</label>
116122
</span>
117123
{% endif %}
124+
<a id="resetzoom" href="#" hidden>Reset zoom</a>
118125
{% include "codespeed/permalink.html" %}
119126
</div>
120127
<div id="content" class="clearfix">

0 commit comments

Comments
 (0)