From 93f2f2d8f3b0e78343f87d9936e0cdb893eaa2b4 Mon Sep 17 00:00:00 2001 From: Konstantin Roussou Date: Wed, 8 Sep 2021 16:49:55 +0300 Subject: [PATCH 1/5] Replace '%2F' with '/' in relative dates Signed-off-by: Konstantin Roussou --- .../components/context_menu/context_menu_helpers.js | 8 ++++---- .../components/main/report_details/report_details.tsx | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/dashboards-reports/public/components/context_menu/context_menu_helpers.js b/dashboards-reports/public/components/context_menu/context_menu_helpers.js index 1c89007b..222be353 100644 --- a/dashboards-reports/public/components/context_menu/context_menu_helpers.js +++ b/dashboards-reports/public/components/context_menu/context_menu_helpers.js @@ -48,10 +48,10 @@ export const getTimeFieldsFromUrl = () => { const url = unhashUrl(window.location.href); let [, fromDateString, toDateString] = url.match(timeRangeMatcher); - fromDateString = fromDateString.replace(/[']+/g, ''); + fromDateString = fromDateString.replace(/[']+/g, '').replace('%2F','/'); // convert time range to from date format in case time range is relative const fromDateFormat = dateMath.parse(fromDateString); - toDateString = toDateString.replace(/[']+/g, ''); + toDateString = toDateString.replace(/[']+/g, '').replace('%2F','/'); const toDateFormat = dateMath.parse(toDateString); const timeDuration = moment.duration( @@ -142,11 +142,11 @@ export const replaceQueryURL = (pageUrl) => { const unhashedUrl = new URL(unhashUrl(pageUrl)); let queryUrl = unhashedUrl.pathname + unhashedUrl.hash; let [, fromDateString, toDateString] = queryUrl.match(timeRangeMatcher); - fromDateString = fromDateString.replace(/[']+/g, ''); + fromDateString = fromDateString.replace(/[']+/g, '').replace('%2F','/'); // convert time range to from date format in case time range is relative const fromDateFormat = dateMath.parse(fromDateString); - toDateString = toDateString.replace(/[']+/g, ''); + toDateString = toDateString.replace(/[']+/g, '').replace('%2F','/'); const toDateFormat = dateMath.parse(toDateString); // replace to and from dates with absolute date diff --git a/dashboards-reports/public/components/main/report_details/report_details.tsx b/dashboards-reports/public/components/main/report_details/report_details.tsx index 280db93f..281617f0 100644 --- a/dashboards-reports/public/components/main/report_details/report_details.tsx +++ b/dashboards-reports/public/components/main/report_details/report_details.tsx @@ -215,8 +215,8 @@ export function ReportDetails(props: { match?: any; setBreadcrumbs?: any; httpCl timeRangeMatcher ); - fromDateString = fromDateString.replace(/[']+/g, ''); - toDateString = toDateString.replace(/[']+/g, ''); + fromDateString = fromDateString.replace(/[']+/g, '').replace('%2F','/'); + toDateString = toDateString.replace(/[']+/g, '').replace('%2F','/'); let fromDateParsed = dateMath.parse(fromDateString); let toDateParsed = dateMath.parse(toDateString); From 27de66257c713adf94332b71385f9893f435b2af Mon Sep 17 00:00:00 2001 From: Konstantin Roussou Date: Wed, 8 Sep 2021 16:56:42 +0300 Subject: [PATCH 2/5] Ensure proper quoting of converted dates in URL Signed-off-by: Konstantin Roussou --- .../components/context_menu/context_menu_helpers.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/dashboards-reports/public/components/context_menu/context_menu_helpers.js b/dashboards-reports/public/components/context_menu/context_menu_helpers.js index 222be353..afabf03f 100644 --- a/dashboards-reports/public/components/context_menu/context_menu_helpers.js +++ b/dashboards-reports/public/components/context_menu/context_menu_helpers.js @@ -141,22 +141,22 @@ export const replaceQueryURL = (pageUrl) => { // we unhash the url in case OpenSearch Dashboards advanced UI setting 'state:storeInSessionStorage' is turned on const unhashedUrl = new URL(unhashUrl(pageUrl)); let queryUrl = unhashedUrl.pathname + unhashedUrl.hash; - let [, fromDateString, toDateString] = queryUrl.match(timeRangeMatcher); - fromDateString = fromDateString.replace(/[']+/g, '').replace('%2F','/'); + let [, fromDateStringMatch, toDateStringMatch] = queryUrl.match(timeRangeMatcher); + fromDateString = fromDateStringMatch.replace(/[']+/g, '').replace('%2F','/'); // convert time range to from date format in case time range is relative const fromDateFormat = dateMath.parse(fromDateString); - toDateString = toDateString.replace(/[']+/g, '').replace('%2F','/'); + toDateString = toDateStringMatch.replace(/[']+/g, '').replace('%2F','/'); const toDateFormat = dateMath.parse(toDateString); // replace to and from dates with absolute date queryUrl = queryUrl.replace( - fromDateString, + fromDateStringMatch, "'" + fromDateFormat.toISOString() + "'" ); queryUrl = queryUrl.replace( - toDateString + '))', - "'" + toDateFormat.toISOString() + "'))" + toDateStringMatch, + "'" + toDateFormat.toISOString() + "'" ); return queryUrl; }; From 30b1407f4f86d0bf16c0255b78e990f10daa6166 Mon Sep 17 00:00:00 2001 From: Konstantin Roussou Date: Mon, 4 Oct 2021 10:31:37 +0300 Subject: [PATCH 3/5] Use generic URL decoding Co-authored-by: Zhongnan Su --- .../components/context_menu/context_menu_helpers.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dashboards-reports/public/components/context_menu/context_menu_helpers.js b/dashboards-reports/public/components/context_menu/context_menu_helpers.js index afabf03f..79220e32 100644 --- a/dashboards-reports/public/components/context_menu/context_menu_helpers.js +++ b/dashboards-reports/public/components/context_menu/context_menu_helpers.js @@ -48,10 +48,10 @@ export const getTimeFieldsFromUrl = () => { const url = unhashUrl(window.location.href); let [, fromDateString, toDateString] = url.match(timeRangeMatcher); - fromDateString = fromDateString.replace(/[']+/g, '').replace('%2F','/'); + fromDateString = decodeURIComponent(fromDateString.replace(/[']+/g, '')); // convert time range to from date format in case time range is relative const fromDateFormat = dateMath.parse(fromDateString); - toDateString = toDateString.replace(/[']+/g, '').replace('%2F','/'); + toDateString = decodeURIComponent(toDateString.replace(/[']+/g, '')); const toDateFormat = dateMath.parse(toDateString); const timeDuration = moment.duration( @@ -142,11 +142,11 @@ export const replaceQueryURL = (pageUrl) => { const unhashedUrl = new URL(unhashUrl(pageUrl)); let queryUrl = unhashedUrl.pathname + unhashedUrl.hash; let [, fromDateStringMatch, toDateStringMatch] = queryUrl.match(timeRangeMatcher); - fromDateString = fromDateStringMatch.replace(/[']+/g, '').replace('%2F','/'); + fromDateString = decodeURIComponent(fromDateStringMatch.replace(/[']+/g, '')); // convert time range to from date format in case time range is relative const fromDateFormat = dateMath.parse(fromDateString); - toDateString = toDateStringMatch.replace(/[']+/g, '').replace('%2F','/'); + toDateString = decodeURIComponent(toDateStringMatch.replace(/[']+/g, '')); const toDateFormat = dateMath.parse(toDateString); // replace to and from dates with absolute date From 5da7d0ca4fce93cb53d4ba73bd25882217367f79 Mon Sep 17 00:00:00 2001 From: Konstantin Roussou Date: Tue, 5 Oct 2021 02:28:21 +0300 Subject: [PATCH 4/5] Use roundUp for toDate Co-authored-by: Zhongnan Su --- .../components/context_menu/context_menu_helpers.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/dashboards-reports/public/components/context_menu/context_menu_helpers.js b/dashboards-reports/public/components/context_menu/context_menu_helpers.js index 79220e32..b9f615f4 100644 --- a/dashboards-reports/public/components/context_menu/context_menu_helpers.js +++ b/dashboards-reports/public/components/context_menu/context_menu_helpers.js @@ -52,11 +52,9 @@ export const getTimeFieldsFromUrl = () => { // convert time range to from date format in case time range is relative const fromDateFormat = dateMath.parse(fromDateString); toDateString = decodeURIComponent(toDateString.replace(/[']+/g, '')); - const toDateFormat = dateMath.parse(toDateString); + const toDateFormat = dateMath.parse(toDateString, { roundUp: true }); - const timeDuration = moment.duration( - dateMath.parse(toDateString).diff(dateMath.parse(fromDateString)) - ); + const timeDuration = moment.duration(toDateFormat.diff(fromDateFormat)); return { time_from: fromDateFormat, @@ -141,13 +139,14 @@ export const replaceQueryURL = (pageUrl) => { // we unhash the url in case OpenSearch Dashboards advanced UI setting 'state:storeInSessionStorage' is turned on const unhashedUrl = new URL(unhashUrl(pageUrl)); let queryUrl = unhashedUrl.pathname + unhashedUrl.hash; - let [, fromDateStringMatch, toDateStringMatch] = queryUrl.match(timeRangeMatcher); + let [, fromDateStringMatch, toDateStringMatch] = + queryUrl.match(timeRangeMatcher); fromDateString = decodeURIComponent(fromDateStringMatch.replace(/[']+/g, '')); // convert time range to from date format in case time range is relative const fromDateFormat = dateMath.parse(fromDateString); toDateString = decodeURIComponent(toDateStringMatch.replace(/[']+/g, '')); - const toDateFormat = dateMath.parse(toDateString); + const toDateFormat = dateMath.parse(toDateString, { roundUp: true }); // replace to and from dates with absolute date queryUrl = queryUrl.replace( From 7939f00346a2cc9282e78df90512e1c1e8bccc88 Mon Sep 17 00:00:00 2001 From: Konstantin Roussou Date: Wed, 6 Oct 2021 21:21:04 +0300 Subject: [PATCH 5/5] Make date transformation same as in context_menu_helpers.js --- .../components/main/report_details/report_details.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dashboards-reports/public/components/main/report_details/report_details.tsx b/dashboards-reports/public/components/main/report_details/report_details.tsx index 281617f0..3fc01585 100644 --- a/dashboards-reports/public/components/main/report_details/report_details.tsx +++ b/dashboards-reports/public/components/main/report_details/report_details.tsx @@ -215,11 +215,11 @@ export function ReportDetails(props: { match?: any; setBreadcrumbs?: any; httpCl timeRangeMatcher ); - fromDateString = fromDateString.replace(/[']+/g, '').replace('%2F','/'); - toDateString = toDateString.replace(/[']+/g, '').replace('%2F','/'); + fromDateString = decodeURIComponent(fromDateString.replace(/[']+/g, '')); + toDateString = decodeURIComponent(toDateString.replace(/[']+/g, '')); let fromDateParsed = dateMath.parse(fromDateString); - let toDateParsed = dateMath.parse(toDateString); + let toDateParsed = dateMath.parse(toDateString, { roundUp: true }); const fromTimePeriod = fromDateParsed?.toDate(); const toTimePeriod = toDateParsed?.toDate();