Skip to content

Commit

Permalink
feat: add deviceInfo to flpPipe, extraction httpBatch
Browse files Browse the repository at this point in the history
  • Loading branch information
pismenskiy authored Jul 21, 2020
1 parent d1e3adc commit f63a664
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 72 deletions.
1 change: 1 addition & 0 deletions src/main/ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { createHttpPipe, IHttpPipeOpts, IHttpHeaders } from './pipes/http'
export { createHttpPipeFallback } from './pipes/httpFallback'
export { createMaskerPipe, panMaskerPipe } from './pipes/masker'
export { createHttpBatchPipe } from './pipes/httpBatch'
export { createFlpPipeline, eventifyPipe } from './pipes/flp'
export { createDeviceInfoPipe, getDeviceInfo } from './pipes/deviceInfo'
export { createTransmitter, createTransmittable } from './transmitter'
Expand Down
1 change: 0 additions & 1 deletion src/main/ts/pipes/deviceInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export const createDeviceInfoPipe = (): IPipe => ({
type,
execute ({ data }: ITransmittable): IPromise<IPipeOutput> {
const output = set(clone(data), 'meta.deviceInfo', getDeviceInfo())

return Promise.resolve([null, output])
},
})
45 changes: 10 additions & 35 deletions src/main/ts/pipes/flp.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import StackTrace from 'stacktrace-js'
import { HttpMethod, IClientEventDto, LogLevel } from '@qiwi/substrate'
import { IClientEventDto, LogLevel } from '@qiwi/substrate'
import { IPipe, ITransmittable, TPipeline } from '../interfaces'
import { panMaskerPipe } from './masker'
import { IHttpHeaders } from './http'
import { createHttpPipeFallback } from './httpFallback'
import { createHttpBatchPipe, IHttpBatchPipeOpts } from './httpBatch'
import { createDeviceInfoPipe } from './deviceInfo'
import { identity } from '../utils'

export type IFlpOptions = {
url: string | string[],
method: HttpMethod,
batchUrl?: string | string [],
headers?: IHttpHeaders,
}

const DEFAULT_LEVEL = LogLevel.INFO

export const type = 'flp-eventify'
Expand Down Expand Up @@ -74,28 +67,10 @@ export const eventifyPipe: IPipe = {
},
}

export const createFlpPipeline = ({ url, batchUrl, headers, method }: IFlpOptions): TPipeline => {
const opts = ([] as string[])
.concat(url)
.map(url => ({ url, headers, method }))

const batchOpts = batchUrl
? ([] as string[])
.concat(batchUrl)
.map(url => ({ url, headers, method }))
: opts

const httpPipe = createHttpPipeFallback(opts)
const httpPipeBatch = createHttpPipeFallback(batchOpts)

const httpPipeResolver: IPipe = ({
type: httpPipe.type,
execute (...args) {
return Array.isArray(args[0].data.events)
? httpPipeBatch.execute(...args)
: httpPipe.execute(...args)
},
})

return [panMaskerPipe, eventifyPipe, httpPipeResolver]
}
export const createFlpPipeline =
({ url, batchUrl, headers, method }: IHttpBatchPipeOpts): TPipeline => [
panMaskerPipe,
eventifyPipe,
createDeviceInfoPipe(),
createHttpBatchPipe({ url, batchUrl, headers, method }),
]
35 changes: 35 additions & 0 deletions src/main/ts/pipes/httpBatch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { IPipe } from '../interfaces'
import { createHttpPipeFallback } from './httpFallback'
import { HttpMethod } from '@qiwi/substrate'
import { IHttpHeaders } from './http'

export type IHttpBatchPipeOpts = {
url: string | string[],
method: HttpMethod,
batchUrl?: string | string [],
headers?: IHttpHeaders,
}

export const createHttpBatchPipe = ({ url, batchUrl, headers, method }: IHttpBatchPipeOpts): IPipe => {
const opts = ([] as string[])
.concat(url)
.map(url => ({ url, headers, method }))

const batchOpts = batchUrl
? ([] as string[])
.concat(batchUrl)
.map(url => ({ url, headers, method }))
: opts

const httpPipe = createHttpPipeFallback(opts)
const httpPipeBatch = createHttpPipeFallback(batchOpts)

return ({
type: httpPipe.type,
execute (...args) {
return Array.isArray(args[0].data.events)
? httpPipeBatch.execute(...args)
: httpPipe.execute(...args)
},
})
}
78 changes: 42 additions & 36 deletions src/test/ts/pipes/flp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,20 @@ describe('flpPipeline', () => {
const res = await transmitter.push(['4539246180805047', '5101754226671617'])

expect(res).toEqual([null, ['4539 **** **** 5047', '5101 **** **** 1617']])
expect(spy).toHaveBeenCalledWith(batchUrl, {
method: HttpMethod.POST,
body: JSON.stringify({
events: [{
message: '4539 **** **** 5047',
meta: {},
level: 'info',
}, {
message: '5101 **** **** 1617',
meta: {},
level: 'info',
}],
}),
headers: {
'Content-Type': 'application/json',
},
expect(spy.mock.calls[0][0]).toBe(batchUrl)
expect(spy.mock.calls[0][1]?.method).toBe(HttpMethod.POST)
expect(spy.mock.calls[0][1]?.headers).toMatchObject({ 'Content-Type': 'application/json' })
// @ts-ignore
expect(JSON.parse(spy.mock.calls[0][1].body)).toMatchObject({
events: [{
message: '4539 **** **** 5047',
meta: {},
level: 'info',
}, {
message: '5101 **** **** 1617',
meta: {},
level: 'info',
}],
})

spy.mockClear()
Expand All @@ -93,16 +91,20 @@ describe('flpPipeline', () => {
const res = await transmitter.push('0000000000000000')

expect(res).toEqual([null, '0000 **** **** 0000'])
expect(spy).toHaveBeenCalledWith(url, {
method: HttpMethod.POST,
body: JSON.stringify({
message: '0000 **** **** 0000',
meta: {},
level: 'info',
}),
headers: {
'Content-Type': 'application/json',
expect(spy.mock.calls[0][0]).toBe(url)
expect(spy.mock.calls[0][1]?.method).toBe(HttpMethod.POST)
expect(spy.mock.calls[0][1]?.headers).toMatchObject({ 'Content-Type': 'application/json' })
// @ts-ignore
expect(JSON.parse(spy.mock.calls[0][1].body)).toMatchObject({
message: '0000 **** **** 0000',
meta: {
deviceInfo: {
browser: expect.any(Object),
model: expect.any(Object),
os: expect.any(Object),
},
},
level: 'info',
})

spy.mockClear()
Expand All @@ -117,19 +119,23 @@ describe('flpPipeline', () => {
const res = await transmitter.push({ data: 'data', message: 'message' })

expect(res).toEqual([null, { data: 'data', message: 'message' }])
expect(spy).toHaveBeenCalledWith(url, {
method: HttpMethod.POST,
body: JSON.stringify({
message: 'message',
meta: {},
level: 'info',
data: 'data',
}),
headers: {
'Content-Type': 'application/json',

expect(spy.mock.calls[0][0]).toBe(url)
expect(spy.mock.calls[0][1]?.method).toBe(HttpMethod.POST)
expect(spy.mock.calls[0][1]?.headers).toMatchObject({ 'Content-Type': 'application/json' })
// @ts-ignore
expect(JSON.parse(spy.mock.calls[0][1].body)).toMatchObject({
message: 'message',
meta: {
deviceInfo: {
browser: expect.any(Object),
model: expect.any(Object),
os: expect.any(Object),
},
},
level: 'info',
data: 'data',
})

spy.mockClear()
})

Expand Down

0 comments on commit f63a664

Please sign in to comment.