Skip to content

Commit

Permalink
rename consumer folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Kylie Pace committed Jan 18, 2021
1 parent 5b62043 commit 565bb48
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 10 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ Therefore, in order to save the data in its transformed state, I replaced Redis
### What I'd add
- a proper logging client so that logs are searchable by session_id and request_id header
- security & rate limiting
- database access: the server application only needs read permission
- CI/CD
- architecture: no need for this to be a monolithic application
- architecture: no need for this to be a monolithic application. deploying separate resources would improve resilience, scalability, and security.
- replace services from docker-compose with actual deployed instances
- validate event model
- create index on mongodb on session_id
- currently, there is a potential problem if the websocket client tries to send the same data again. There is no uniqueness constraint on each item in the `children` array so events would be duplicated. Maybe any existing database record should be deleted or archived when a SESSION_START event arrives, or the SaveDataService should use mongo's `$unset` operator to clear the `children` array.



Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"scripts": {
"compile": "rm -Rf ./lib && tsc",
"consumer:dev": "ts-node-dev src/consumers/app.ts",
"consumer:dev": "ts-node-dev src/consumer/app.ts",
"lint": "eslint --ext .js,.ts src/",
"mocha": "ts-mocha \"test/**/*.ts\" --recursive --exit",
"server:dev": "ts-node-dev src/server/index.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/constants.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"PORT": 8080,
"topics": {
"EVENTS": "events"
"EVENTS": "websocket.events"
},
"urlPaths": {
"websocket": "websocket"
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export default class SaveDataService {
$push: {
children: {
$each: data.children,
$sort: { 'data.timestamp': 1 }
// sort asc on timestamp so oldest events will be at beginning of array
$sort: { 'timestamp': 1 }
}
}
};
Expand Down
File renamed without changes.
38 changes: 32 additions & 6 deletions test/websocket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ describe('websocket http request', () => {
});


describe('sending multiple messages', () => {
beforeEach(done => setTimeout(done, 500));
describe('sending multiple messages with child events not in order', () => {
// give consumer app some time to receive kafka message
beforeEach(done => setTimeout(done, 1800));
before(() => {

// send session_start and the last event
clients[0].send(
JSON.stringify([
{
Expand All @@ -37,17 +40,23 @@ describe('websocket http request', () => {
{
timestamp: 1569972085,
type: "EVENT",
name: "basket_removed"
name: "last_event"
}
])
);

// send the middle events and then session_end
clients[0].send(
JSON.stringify([
{
timestamp: 1569972083,
type: "EVENT",
name: "purchase_completed"
name: "middle_event"
},
{
timestamp: 1569972083,
type: "EVENT",
name: "first_event"
},
{
timestamp: 1569972086,
Expand All @@ -58,14 +67,31 @@ describe('websocket http request', () => {
);
});

it('saves data', async () => {
it('saves data with child events in order by timestamp and with start and end timestamps', async () => {
const data = await database.findOne({session_id});
console.log(data)
chai.expect(data).to.have.property('session_id').which.equals(session_id);
chai.expect(data).to.have.property('end').which.equals(1569972086);
chai.expect(data).to.have.property('start').which.equals(1569972082);
chai.expect(data).to.have.property('children').which.is.an('array');
chai.expect(data.children.length).to.equal(2);
chai.expect(data.children.length).to.equal(3);
chai.expect(data.children).to.deep.equal([
{
type: 'EVENT',
timestamp: 1569972083,
name: 'middle_event'
},
{
type: 'EVENT',
timestamp: 1569972083,
name: 'first_event'
},
{
type: 'EVENT',
timestamp: 1569972085,
name: 'last_event'
}
])
});
});

Expand Down

0 comments on commit 565bb48

Please sign in to comment.