-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
1,146 additions
and
306 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
packages/elastic-analytics/src/shippers/fullstory/format_payload.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import moment from 'moment'; | ||
import { formatPayload } from './format_payload'; | ||
|
||
describe('formatPayload', () => { | ||
test('appends `_str` to string values', () => { | ||
const payload = { | ||
foo: 'bar', | ||
baz: ['qux'], | ||
}; | ||
|
||
expect(formatPayload(payload)).toEqual({ | ||
foo_str: payload.foo, | ||
baz_strs: payload.baz, | ||
}); | ||
}); | ||
|
||
test('appends `_int` to integer values', () => { | ||
const payload = { | ||
foo: 1, | ||
baz: [100000], | ||
}; | ||
|
||
expect(formatPayload(payload)).toEqual({ | ||
foo_int: payload.foo, | ||
baz_ints: payload.baz, | ||
}); | ||
}); | ||
|
||
test('appends `_real` to integer values', () => { | ||
const payload = { | ||
foo: 1.5, | ||
baz: [100000.5], | ||
}; | ||
|
||
expect(formatPayload(payload)).toEqual({ | ||
foo_real: payload.foo, | ||
baz_reals: payload.baz, | ||
}); | ||
}); | ||
|
||
test('appends `_bool` to booleans values', () => { | ||
const payload = { | ||
foo: true, | ||
baz: [false], | ||
}; | ||
|
||
expect(formatPayload(payload)).toEqual({ | ||
foo_bool: payload.foo, | ||
baz_bools: payload.baz, | ||
}); | ||
}); | ||
|
||
test('appends `_date` to Date values', () => { | ||
const payload = { | ||
foo: new Date(), | ||
baz: [new Date()], | ||
}; | ||
|
||
expect(formatPayload(payload)).toEqual({ | ||
foo_date: payload.foo, | ||
baz_dates: payload.baz, | ||
}); | ||
}); | ||
|
||
test('appends `_date` to moment values', () => { | ||
const payload = { | ||
foo: moment(), | ||
baz: [moment()], | ||
}; | ||
|
||
expect(formatPayload(payload)).toEqual({ | ||
foo_date: payload.foo, | ||
baz_dates: payload.baz, | ||
}); | ||
}); | ||
|
||
test('supports nested values', () => { | ||
const payload = { | ||
nested: { | ||
foo: 'bar', | ||
baz: ['qux'], | ||
}, | ||
}; | ||
|
||
expect(formatPayload(payload)).toEqual({ | ||
nested: { | ||
foo_str: payload.nested.foo, | ||
baz_strs: payload.nested.baz, | ||
}, | ||
}); | ||
}); | ||
|
||
test('does not mutate reserved keys', () => { | ||
const payload = { | ||
uid: 'uid', | ||
displayName: 'displayName', | ||
email: 'email', | ||
acctId: 'acctId', | ||
website: 'website', | ||
pageName: 'pageName', | ||
}; | ||
|
||
expect(formatPayload(payload)).toEqual(payload); | ||
}); | ||
|
||
test('removes undefined values', () => { | ||
const payload = { | ||
foo: undefined, | ||
baz: [undefined], | ||
}; | ||
|
||
expect(formatPayload(payload)).toEqual({}); | ||
}); | ||
|
||
describe('String to Date identification', () => { | ||
test('appends `_date` to ISO string values', () => { | ||
const payload = { | ||
foo: new Date().toISOString(), | ||
baz: [new Date().toISOString()], | ||
}; | ||
|
||
expect(formatPayload(payload)).toEqual({ | ||
foo_date: payload.foo, | ||
baz_dates: payload.baz, | ||
}); | ||
}); | ||
|
||
test('appends `_str` to random string values', () => { | ||
const payload = { | ||
foo: 'test-1', | ||
baz: ['test-1'], | ||
}; | ||
|
||
expect(formatPayload(payload)).toEqual({ | ||
foo_str: payload.foo, | ||
baz_strs: payload.baz, | ||
}); | ||
}); | ||
}); | ||
}); |
88 changes: 88 additions & 0 deletions
88
packages/elastic-analytics/src/shippers/fullstory/format_payload.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import moment from 'moment'; | ||
|
||
// https://help.fullstory.com/hc/en-us/articles/360020623234#reserved-properties | ||
const FULLSTORY_RESERVED_PROPERTIES = [ | ||
'uid', | ||
'displayName', | ||
'email', | ||
'acctId', | ||
'website', | ||
// https://developer.fullstory.com/page-variables | ||
'pageName', | ||
]; | ||
|
||
export function formatPayload(context: Record<string, unknown>): Record<string, unknown> { | ||
// format context keys as required for env vars, see docs: https://help.fullstory.com/hc/en-us/articles/360020623234 | ||
return Object.fromEntries( | ||
Object.entries(context) | ||
// Discard any undefined values | ||
.map<[string, unknown]>(([key, value]) => { | ||
return Array.isArray(value) | ||
? [key, value.filter((v) => typeof v !== 'undefined')] | ||
: [key, value]; | ||
}) | ||
.filter( | ||
([, value]) => typeof value !== 'undefined' && (!Array.isArray(value) || value.length > 0) | ||
) | ||
// Transform key names according to the FullStory needs | ||
.map(([key, value]) => { | ||
if (FULLSTORY_RESERVED_PROPERTIES.includes(key)) { | ||
return [key, value]; | ||
} | ||
if (isRecord(value)) { | ||
return [key, formatPayload(value)]; | ||
} | ||
const valueType = getFullStoryType(value); | ||
const formattedKey = valueType ? `${key}_${valueType}` : key; | ||
return [formattedKey, value]; | ||
}) | ||
); | ||
} | ||
|
||
function getFullStoryType(value: unknown) { | ||
// For arrays, make the decision based on the first element | ||
const isArray = Array.isArray(value); | ||
const v = isArray ? value[0] : value; | ||
let type = ''; | ||
switch (typeof v) { | ||
case 'string': | ||
if (moment(v, moment.ISO_8601, true).isValid()) { | ||
type = 'date'; | ||
break; | ||
} | ||
type = 'str'; | ||
break; | ||
case 'number': | ||
type = Number.isInteger(v) ? 'int' : 'real'; | ||
break; | ||
case 'boolean': | ||
type = 'bool'; | ||
break; | ||
case 'object': | ||
if (isDate(v)) { | ||
type = 'date'; | ||
break; | ||
} | ||
default: | ||
throw new Error(`Unsupported type: ${typeof v}`); | ||
} | ||
|
||
// convert to plural form for arrays | ||
return isArray ? `${type}s` : type; | ||
} | ||
|
||
function isRecord(value: unknown): value is Record<string, unknown> { | ||
return typeof value === 'object' && value !== null && !Array.isArray(value) && !isDate(value); | ||
} | ||
|
||
function isDate(value: unknown): value is Date { | ||
return value instanceof Date || moment.isMoment(value); | ||
} |
Oops, something went wrong.