Skip to content

Instantly share code, notes, and snippets.

@anderson-marques
Last active April 30, 2019 21:34
Show Gist options
  • Save anderson-marques/96ec2c3180cf7f0c68ce6637dfe6989c to your computer and use it in GitHub Desktop.
Save anderson-marques/96ec2c3180cf7f0c68ce6637dfe6989c to your computer and use it in GitHub Desktop.
NodeJS example - RxJS - Processing S3 Event keys and group them by account id
/**
* https://repl.it/@marquesanderson/pongo-aggregation-events-by-account-id
*/
const RxJS = require('rxjs')
const filter = require('rxjs/operators').filter
const map = require('rxjs/operators').map
const groupBy = require('rxjs/operators').groupBy
const flatMap = require('rxjs/operators').flatMap
const toArray = require('rxjs/operators').toArray
let eventKeys$ = RxJS.from([
'2019/05/30/16/23/api-call/2/xpto-xpto-1',
'2019/05/30/16/24/api-call/2/xpto-xpto-2',
'2019/05/30/16/25/api-call/2/xpto-xpto-3',
'2019/05/30/16/21/api-call/1/xpto-xpto-4',
'2019/05/30/16/21/api-call/1/xpto-xpto-5'
])
eventKeys$.pipe(
map((eventKey) => {
const eventKeyParts = eventKey.split('/')
return {
accountId: eventKeyParts[6],
eventKey: eventKey,
eventName: eventKeyParts[5]
}
}),
filter(e => e.eventName === 'api-call'),
groupBy(e => e.accountId),
flatMap(group => {
return group.pipe(toArray())
}),
map((group) => {
return {
accountId: group[0].accountId,
events: group.map(event => {
return event.eventKey
})
}
})
).subscribe((data) => console.log(data))
/**
Result:
{ accountId: '2',
events:
[ '2019/05/30/16/23/api-call/2/xpto-xpto-1',
'2019/05/30/16/24/api-call/2/xpto-xpto-2',
'2019/05/30/16/25/api-call/2/xpto-xpto-3' ] }
{ accountId: '1',
events:
[ '2019/05/30/16/21/api-call/1/xpto-xpto-4',
'2019/05/30/16/21/api-call/1/xpto-xpto-5' ] }
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment