Merge pull request #1563 from getredash/json_webhook

Change: send events to webhook as JSON with a schema.
This commit is contained in:
Arik Fraimovich 2017-02-05 10:36:55 +02:00 committed by GitHub
commit 01a8075a67
4 changed files with 28 additions and 8 deletions

View File

@ -1,5 +1,11 @@
# Change Log # 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 ## v1.0.0-rc.1 - 2017-01-31
This version has two big changes behind the scenes: This version has two big changes behind the scenes:

View File

@ -1316,7 +1316,7 @@ class Event(db.Model):
action = Column(db.String(255)) action = Column(db.String(255))
object_type = Column(db.String(255)) object_type = Column(db.String(255))
object_id = Column(db.String(255), nullable=True) 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()) created_at = Column(db.DateTime(True), default=db.func.now())
__tablename__ = 'events' __tablename__ = 'events'
@ -1324,6 +1324,17 @@ class Event(db.Model):
def __unicode__(self): def __unicode__(self):
return u"%s,%s,%s,%s" % (self.user_id, self.action, self.object_type, self.object_id) 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 @classmethod
def record(cls, event): def record(cls, event):
org_id = event.pop('org_id') org_id = event.pop('org_id')
@ -1333,11 +1344,10 @@ class Event(db.Model):
object_id = event.pop('object_id', None) object_id = event.pop('object_id', None)
created_at = datetime.datetime.utcfromtimestamp(event.pop('timestamp')) 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, event = cls(org_id=org_id, user_id=user_id, action=action,
object_type=object_type, object_id=object_id, object_type=object_type, object_id=object_id,
additional_properties=additional_properties, additional_properties=event,
created_at=created_at) created_at=created_at)
db.session.add(event) db.session.add(event)
return event return event

View File

@ -10,14 +10,18 @@ logger = get_task_logger(__name__)
@celery.task(name="redash.tasks.record_event", base=BaseTask) @celery.task(name="redash.tasks.record_event", base=BaseTask)
def record_event(event): def record_event(raw_event):
original_event = event.copy() event = models.Event.record(raw_event)
models.Event.record(event)
models.db.session.commit() models.db.session.commit()
for hook in settings.EVENT_REPORTING_WEBHOOKS: for hook in settings.EVENT_REPORTING_WEBHOOKS:
logger.debug("Forwarding event to: %s", hook) logger.debug("Forwarding event to: %s", hook)
try: 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: if response.status_code != 200:
logger.error("Failed posting to %s: %s", hook, response.content) logger.error("Failed posting to %s: %s", hook, response.content)
except Exception: except Exception:

View File

@ -409,7 +409,7 @@ class TestEvents(BaseTestCase):
event = models.Event.record(raw_event) 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): class TestWidgetDeleteInstance(BaseTestCase):