Skip to content

Commit

Permalink
[ML] fix token replacement regexp (#49086)
Browse files Browse the repository at this point in the history
  • Loading branch information
darnautov authored Oct 24, 2019
1 parent ed9aaf1 commit a279a0b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
37 changes: 37 additions & 0 deletions x-pack/legacy/plugins/ml/public/util/custom_url_utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,43 @@ describe('ML - custom URL utils', () => {
); // eslint-disable-line max-len
});

test('replaces tokens with nesting', () => {
const testUrlApache: KibanaUrlConfig = {
url_name: 'Raw data',
time_range: 'auto',
url_value:
'kibana#/dashboard/ml_http_access_explorer_ecs?_g=(time:(from:\u0027$earliest$\u0027,mode:absolute,to:\u0027$latest$\u0027))&_a=(description:\u0027\u0027,filters:!((\u0027$state\u0027:(store:appState),meta:(alias:!n,disabled:!f,index:\u0027INDEX_PATTERN_ID\u0027,key:event.dataset,negate:!f,params:(query:\u0027apache.access\u0027),type:phrase,value:\u0027apache.access\u0027),query:(match:(event.dataset:(query:\u0027apache.access\u0027,type:phrase)))),(\u0027$state\u0027:(store:appState),meta:(alias:!n,disabled:!f,index:\u0027INDEX_PATTERN_ID\u0027,key:http.response.status_code,negate:!f,params:(query:\u0027$http.response.status_code$\u0027),type:phrase,value:\u0027$http.response.status_code$\u0027),query:(match:(http.response.status_code:(query:\u0027$http.response.status_code$\u0027,type:phrase))))),query:(language:kuery,query:\u0027\u0027))', // eslint-disable-line max-len
};

const testRecord = {
job_id: 'farequote',
result_type: 'record',
probability: 6.533287347648861e-45,
record_score: 93.84475,
initial_record_score: 94.867922946384,
bucket_span: 300,
detector_index: 0,
is_interim: false,
timestamp: 1486656600000,
function: 'mean',
function_description: 'mean',
typical: [99.2329899996025],
actual: [274.7279901504516],
field_name: 'responsetime',
earliest: '2017-02-09T15:10:00.000Z',
latest: '2017-02-09T17:15:00.000Z',
http: {
response: {
status_code: 403,
},
},
};

expect(getUrlForRecord(testUrlApache, testRecord)).toBe(
"kibana#/dashboard/ml_http_access_explorer_ecs?_g=(time:(from:'2017-02-09T15:10:00.000Z',mode:absolute,to:'2017-02-09T17:15:00.000Z'))&_a=(description:\u0027\u0027,filters:!((\u0027$state\u0027:(store:appState),meta:(alias:!n,disabled:!f,index:\u0027INDEX_PATTERN_ID\u0027,key:event.dataset,negate:!f,params:(query:\u0027apache.access\u0027),type:phrase,value:\u0027apache.access\u0027),query:(match:(event.dataset:(query:\u0027apache.access\u0027,type:phrase)))),(\u0027$state\u0027:(store:appState),meta:(alias:!n,disabled:!f,index:\u0027INDEX_PATTERN_ID\u0027,key:http.response.status_code,negate:!f,params:(query:\u0027403\u0027),type:phrase,value:\u0027403\u0027),query:(match:(http.response.status_code:(query:\u0027403\u0027,type:phrase))))),query:(language:kuery,query:\u0027\u0027))"
); // eslint-disable-line max-len
});

test('returns expected URL for other type URL', () => {
expect(getUrlForRecord(TEST_OTHER_URL, TEST_RECORD)).toBe(
'http://airlinecodes.info/airline-code-AAL'
Expand Down
6 changes: 3 additions & 3 deletions x-pack/legacy/plugins/ml/public/util/custom_url_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ function isKibanaUrl(urlConfig: UrlConfig) {
/**
* Escape any double quotes in the value for correct use in KQL.
*/
function escapeForKQL(value: string): string {
return value.replace(/\"/g, '\\"');
function escapeForKQL(value: string | number): string {
return String(value).replace(/\"/g, '\\"');
}

// Builds a Kibana dashboard or Discover URL from the supplied config, with any
Expand All @@ -127,7 +127,7 @@ function buildKibanaUrl(urlConfig: UrlConfig, record: CustomUrlAnomalyRecordDoc)
);

const replaceSingleTokenValues = (str: string) =>
str.replace(/\$(\w+)\$/g, (match, name: string) => {
str.replace(/\$([^?&$\'"]+)\$/g, (match, name: string) => {
// Use lodash get to allow nested JSON fields to be retrieved.
let tokenValue: string | string[] | undefined = get(record, name);
tokenValue = Array.isArray(tokenValue) ? tokenValue[0] : tokenValue;
Expand Down
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/ml/public/util/string_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import d3 from 'd3';
// 'http://www.google.co.uk/#q=airline+code+AAL'.
// If a corresponding key is not found in valuesByTokenName, then the String is not replaced.
export function replaceStringTokens(str, valuesByTokenName, encodeForURI) {
return String(str).replace((/\$([^?&$\'"]{1,40})\$/g), (match, name) => {
return String(str).replace((/\$([^?&$\'"]+)\$/g), (match, name) => {
// Use lodash get to allow nested JSON fields to be retrieved.
let tokenValue = _.get(valuesByTokenName, name, null);
if (encodeForURI === true && tokenValue !== null) {
Expand Down

0 comments on commit a279a0b

Please sign in to comment.