Skip to content

Commit c7c1097

Browse files
authored
Fix distribution date (#1251)
1 parent 22de64d commit c7c1097

3 files changed

Lines changed: 47 additions & 6 deletions

File tree

src/js/techreport/cwvDistribution.js

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,33 @@ class CwvDistribution {
1616
this.date = this.pageFilters.end || this.root?.dataset?.latestDate || '';
1717
this.selectedMetric = this.resolveMetric(new URLSearchParams(window.location.search).get('good-cwv-over-time'));
1818

19-
// Populate "Latest data" timestamp immediately
20-
const tsSlot = this.root?.querySelector('[data-slot="cwv-distribution-timestamp"]');
21-
if (tsSlot && this.date) tsSlot.textContent = UIUtils.printMonthYear(this.date);
19+
const updateDateFromTimeseries = (newDate) => {
20+
this.date = newDate;
21+
const tsSlot = this.root?.querySelector('[data-slot="cwv-distribution-timestamp"]');
22+
if (tsSlot && this.date) tsSlot.textContent = UIUtils.printMonthYear(this.date);
23+
if (!this.distributionData) {
24+
this.fetchData();
25+
}
26+
};
27+
28+
if (!this.date) {
29+
// If we don't have a date, see if we can get it from the timeseries component
30+
// If not wait for the event to emit when the date is available
31+
const cwvChartDate = document.querySelector('#section-good_cwv_timeseries [data-slot=timestamp]');
32+
if (cwvChartDate && cwvChartDate.dataset.date) {
33+
updateDateFromTimeseries(cwvChartDate.dataset.date);
34+
} else {
35+
document.addEventListener('timeseries-date-updated', (event) => {
36+
if (event.detail.id === 'good_cwv_timeseries') {
37+
updateDateFromTimeseries(event.detail.date);
38+
}
39+
});
40+
}
41+
} else {
42+
// Populate "Latest data" timestamp immediately
43+
const tsSlot = this.root?.querySelector('[data-slot="cwv-distribution-timestamp"]');
44+
if (tsSlot && this.date) tsSlot.textContent = UIUtils.printMonthYear(this.date);
45+
}
2246

2347
this.updateTitle();
2448
this.bindEventListeners();
@@ -66,7 +90,11 @@ class CwvDistribution {
6690
if (show) {
6791
this.root.classList.remove('hidden');
6892
if (btn) btn.textContent = 'Hide histogram';
69-
if (!this.distributionData) {
93+
if (!this.date) {
94+
const cwvChartDate = document.querySelector('#section-good_cwv_timeseries [data-slot=timestamp]');
95+
this.date = cwvChartDate.dataset.date;
96+
}
97+
if (!this.distributionData && this.date) {
7098
this.fetchData();
7199
} else if (this.chart) {
72100
this.chart.reflow();

src/js/techreport/timeseries.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,14 @@ class Timeseries {
137137
container.innerHTML = '';
138138

139139
/* Update the date to the most recent timestamp in the dataset */
140-
viz.querySelector('[data-slot="timestamp"]').innerHTML = UIUtils.printMonthYear(sorted?.[0]?.date);
140+
const timestamp = viz.querySelector('[data-slot="timestamp"]');
141+
timestamp.textContent = UIUtils.printMonthYear(sorted?.[0]?.date);
142+
timestamp.setAttribute('data-date', sorted?.[0]?.date);
143+
144+
/* Broadcast that the date has been updated */
145+
document.dispatchEvent(new CustomEvent('timeseries-date-updated', {
146+
detail: { id: this.id, date: sorted?.[0]?.date }
147+
}));
141148

142149
/* For each of the breakdowns, add a component with the latest data */
143150
config.series.values.forEach(breakdown => {
@@ -246,6 +253,12 @@ class Timeseries {
246253
value.textContent = 'No data';
247254
}
248255
timestamp.textContent = UIUtils.printMonthYear(latest.date);
256+
timestamp.setAttribute('data-date', latest.date);
257+
258+
/* Broadcast that the date has been updated */
259+
document.dispatchEvent(new CustomEvent('timeseries-date-updated', {
260+
detail: { id: this.id, date: latest.date }
261+
}));
249262
const techColor = UIUtils.getAppColor(app, this.pageFilters.app, this.pageConfig.colors);
250263
const fallback = this.pageConfig.colors.app[index];
251264
card.style.setProperty('--breakdown-color', techColor || fallback);

templates/techreport/components/cwv_distribution.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
data-id="{{ cwv_distribution_config.id }}"
77
data-component="cwvDistribution"
88
data-client="{{ request.args.get('client', '') or 'mobile' }}"
9-
data-latest-date="{{ all_dates[0].value if all_dates else '' }}"
9+
data-latest-date=""
1010
>
1111
<div class="cwv-distribution-header">
1212
<h3><a href="#section-{{ cwv_distribution_config.id }}" class="anchor"><span data-slot="cwv-distribution-title-metric">Core Web Vitals</span> histogram</a></h3>

0 commit comments

Comments
 (0)