Skip to content

Commit

Permalink
Fix quoting and url-encoding (opensearch-project#153)
Browse files Browse the repository at this point in the history
* Replace '%2F' with '/' in relative dates

Signed-off-by: Konstantin Roussou <[email protected]>

* Ensure proper quoting of converted dates in URL

Signed-off-by: Konstantin Roussou <[email protected]>

* Use generic URL decoding

Co-authored-by: Zhongnan Su <[email protected]>

* Use roundUp for toDate

Co-authored-by: Zhongnan Su <[email protected]>

* Make date transformation same as in context_menu_helpers.js

Co-authored-by: Zhongnan Su <[email protected]>
  • Loading branch information
kroussou and zhongnansu committed Oct 28, 2021
1 parent 076e353 commit 5a4673d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,13 @@ export const getTimeFieldsFromUrl = () => {
const url = unhashUrl(window.location.href);

let [, fromDateString, toDateString] = url.match(timeRangeMatcher);
fromDateString = fromDateString.replace(/[']+/g, '');
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, '');
const toDateFormat = dateMath.parse(toDateString);
toDateString = decodeURIComponent(toDateString.replace(/[']+/g, ''));
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,
Expand Down Expand Up @@ -141,22 +139,23 @@ 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, '');
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 = toDateString.replace(/[']+/g, '');
const toDateFormat = dateMath.parse(toDateString);
toDateString = decodeURIComponent(toDateStringMatch.replace(/[']+/g, ''));
const toDateFormat = dateMath.parse(toDateString, { roundUp: true });

// 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;
};
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ export function ReportDetails(props) {
timeRangeMatcher
);

fromDateString = fromDateString.replace(/[']+/g, '');
toDateString = toDateString.replace(/[']+/g, '');
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();
Expand Down

0 comments on commit 5a4673d

Please sign in to comment.