Skip to content

Commit

Permalink
Group events by parent id
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Jul 20, 2016
1 parent fbd196a commit 2e159ec
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
6 changes: 4 additions & 2 deletions kinto/core/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ def get_resource_events(request, after_commit=False):
return events


def notify_resource_event(request, timestamp, data, action, old=None):
def notify_resource_event(request, parent_id, timestamp, data, action,
old=None):
"""
Request helper to stack a resource event.
Expand All @@ -146,10 +147,11 @@ def notify_resource_event(request, timestamp, data, action, old=None):

# Get previously triggered events.
events = request.bound_data.setdefault("resource_events", OrderedDict())

resource_name = request.current_resource_name

# Group events by resource and action.
group_by = resource_name + action.value
group_by = resource_name + parent_id + action.value

if group_by in events:
# Add to impacted records of existing event.
Expand Down
4 changes: 3 additions & 1 deletion kinto/core/resource/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,9 @@ def postprocess(self, result, action=ACTIONS.READ, old=None):
'data': result
}

self.request.notify_resource_event(timestamp=self.timestamp,
parent_id = self.get_parent_id(self.request)
self.request.notify_resource_event(parent_id=parent_id,
timestamp=self.timestamp,
data=result,
action=action,
old=old)
Expand Down
16 changes: 16 additions & 0 deletions kinto/tests/core/resource/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,22 @@ def test_one_event_is_sent_per_resource(self):
self.app.post_json("/batch", body, headers=self.headers)
self.assertEqual(len(self.events), 2)

def test_one_event_is_sent_per_parent_id(self):
body = {
"defaults": {
"path": '/mushrooms',
"method": "POST",
"body": self.body
},
"requests": [
{"headers": {"Authorization": "Basic bWF0OjE="}},
{"headers": {"Authorization": "Basic dG90bzp0dXR1"}},
{"headers": {"Authorization": "Basic bWF0OjE="}},
]
}
self.app.post_json("/batch", body, headers=self.headers)
self.assertEqual(len(self.events), 2)

def test_one_event_is_sent_per_action(self):
body = {
"defaults": {
Expand Down

0 comments on commit 2e159ec

Please sign in to comment.