Skip to content

Commit

Permalink
[fix] Prevent chart loading failure if timezone JS fails #550
Browse files Browse the repository at this point in the history
Fixes #550
  • Loading branch information
nemesifier committed Oct 13, 2023
1 parent 3795e61 commit e92f1db
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions openwisp_monitoring/monitoring/static/monitoring/js/chart-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,11 @@ django.jQuery(function ($) {
const now = moment().format('YYYY-MM-DD HH:mm:ss');
const endDateTime = moment(endDate).format('YYYY-MM-DD HH:mm:ss');
endDate = endDateTime > now ? now : endDateTime;
url = `${apiUrl}?start=${startDate}&end=${endDate}`;
var timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
url = `${apiUrl}?timezone=${timezone}&start=${startDate}&end=${endDate}`;
if (timezone) {
url = `${url}&timezone=${timezone}`;
}
}
return url;
},
Expand Down Expand Up @@ -255,7 +258,7 @@ django.jQuery(function ($) {
var range = localStorage.getItem(timeRangeKey) || defaultTimeRange;
var startLabel = localStorage.getItem(startDayKey) || moment().format('MMMM D, YYYY');
var endLabel = localStorage.getItem(endDayKey) || moment().format('MMMM D, YYYY');

// Disable the zoom chart and scrolling when we refresh the page
localStorage.setItem(isChartZoomScroll, false);
localStorage.setItem(isChartZoomed, false);
Expand All @@ -271,7 +274,6 @@ django.jQuery(function ($) {
// Then loads charts with custom ranges selected
loadCharts(range, true);
}

else {
endLabel = moment().format('MMMM D, YYYY');
startLabel = moment().subtract(range.split('d')[0], 'days').format('MMMM D, YYYY');
Expand All @@ -284,7 +286,9 @@ django.jQuery(function ($) {
// try adding the browser timezone to the querystring
try {
var timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
baseUrl = baseUrl.replace('time=', 'timezone=' + timezone + '&time=');
if (timezone) {
baseUrl = baseUrl.replace('time=', 'timezone=' + timezone + '&time=');
}
// ignore failures (older browsers do not support this)
} catch (e) {}

Expand Down Expand Up @@ -318,15 +322,19 @@ django.jQuery(function ($) {
location.href = baseUrl + time + '&csv=1';
// If custom or pickerChosenLabelKey is 'Custom Range', pass pickerEndDate and pickerStartDate to csv url
if (localStorage.getItem(isCustomDateRange) === 'true' || localStorage.getItem(pickerChosenLabelKey) === customDateRangeLabel) {
var startDate = localStorage.getItem(startDateTimeKey);
var endDate = localStorage.getItem(endDateTimeKey);
if (localStorage.getItem(isChartZoomed) === 'true') {
time = localStorage.getItem(zoomtimeRangeKey);
endDate = localStorage.getItem(zoomEndDateTimeKey);
startDate = localStorage.getItem(zoomStartDateTimeKey);
}
var timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
location.href = `${apiUrl}?timezone=${timezone}&start=${startDate}&end=${endDate}&csv=1`;
var startDate = localStorage.getItem(startDateTimeKey);
var endDate = localStorage.getItem(endDateTimeKey);
if (localStorage.getItem(isChartZoomed) === 'true') {
time = localStorage.getItem(zoomtimeRangeKey);
endDate = localStorage.getItem(zoomEndDateTimeKey);
startDate = localStorage.getItem(zoomStartDateTimeKey);
}
var url = `${apiUrl}?start=${startDate}&end=${endDate}&csv=1`;
var timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
if (timezone) {
url = `${url}&timezone=${timezone}`;
}
location.href = url;
}
});
// fetch chart data and replace the old charts with the new ones
Expand Down

0 comments on commit e92f1db

Please sign in to comment.