Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change: send events to webhook as JSON with a schema. #1563

Merged
merged 3 commits into from
Feb 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## v1.0.0-rc.2 - Unreleased

### Changed

- [#1563](https://github.com/getredash/redash/pull/1563) Send events to webhook as JSON with a schema.

## v1.0.0-rc.1 - 2017-01-31

This version has two big changes behind the scenes:
Expand Down
16 changes: 13 additions & 3 deletions redash/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1316,14 +1316,25 @@ class Event(db.Model):
action = Column(db.String(255))
object_type = Column(db.String(255))
object_id = Column(db.String(255), nullable=True)
additional_properties = Column(db.Text, nullable=True)
additional_properties = Column(MutableDict.as_mutable(PseudoJSON), nullable=True, default={})
created_at = Column(db.DateTime(True), default=db.func.now())

__tablename__ = 'events'

def __unicode__(self):
return u"%s,%s,%s,%s" % (self.user_id, self.action, self.object_type, self.object_id)

def to_dict(self):
return {
'org_id': self.org_id,
'user_id': self.user_id,
'action': self.action,
'object_type': self.object_type,
'object_id': self.object_id,
'additional_properties': self.additional_properties,
'created_at': self.created_at.isoformat()
}

@classmethod
def record(cls, event):
org_id = event.pop('org_id')
Expand All @@ -1333,11 +1344,10 @@ def record(cls, event):
object_id = event.pop('object_id', None)

created_at = datetime.datetime.utcfromtimestamp(event.pop('timestamp'))
additional_properties = json.dumps(event)

event = cls(org_id=org_id, user_id=user_id, action=action,
object_type=object_type, object_id=object_id,
additional_properties=additional_properties,
additional_properties=event,
created_at=created_at)
db.session.add(event)
return event
Expand Down
12 changes: 8 additions & 4 deletions redash/tasks/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@


@celery.task(name="redash.tasks.record_event", base=BaseTask)
def record_event(event):
original_event = event.copy()
models.Event.record(event)
def record_event(raw_event):
event = models.Event.record(raw_event)
models.db.session.commit()

for hook in settings.EVENT_REPORTING_WEBHOOKS:
logger.debug("Forwarding event to: %s", hook)
try:
response = requests.post(hook, original_event)
data = {
"schema": "iglu:io.redash.webhooks/event/jsonschema/1-0-0",
"data": event.to_dict()
}
response = requests.post(hook, json=data)
if response.status_code != 200:
logger.error("Failed posting to %s: %s", hook, response.content)
except Exception:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ def test_records_additional_properties(self):

event = models.Event.record(raw_event)

self.assertDictEqual(json.loads(event.additional_properties), additional_properties)
self.assertDictEqual(event.additional_properties, additional_properties)


class TestWidgetDeleteInstance(BaseTestCase):
Expand Down