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

Record event when accessing query result from API #387

Merged
merged 1 commit into from
Mar 12, 2015
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
12 changes: 12 additions & 0 deletions migrations/0004_allow_null_in_event_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from playhouse.migrate import PostgresqlMigrator, migrate

from redash.models import db

if __name__ == '__main__':
db.connect_db()
migrator = PostgresqlMigrator(db.database)

with db.database.transaction():
migrate(
migrator.drop_not_null('events', 'user_id')
)
21 changes: 19 additions & 2 deletions redash/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
import csv
import hashlib
import json
import numbers
import cStringIO
import datetime
import time
import logging

from flask import render_template, send_from_directory, make_response, request, jsonify, redirect, \
Expand Down Expand Up @@ -490,6 +489,24 @@ def get(self, query_id=None, query_result_id=None, filetype='json'):
query_result = models.QueryResult.get_by_id(query_result_id)

if query_result:
if isinstance(self.current_user, models.ApiUser):
event = {
'user_id': None,
'action': 'api_get',
'timestamp': int(time.time()),
'api_key': self.current_user.id,
'file_type': filetype
}

if query_id:
event['object_type'] = 'query'
event['object_id'] = query_id
else:
event['object_type'] = 'query_result'
event['object_id'] = query_result_id

record_event.delay(event)

if filetype == 'json':
data = json.dumps({'query_result': query_result.to_dict()}, cls=utils.JSONEncoder)
return make_response(data, 200, cache_headers)
Expand Down
2 changes: 1 addition & 1 deletion redash/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ def delete_instance(self, *args, **kwargs):


class Event(BaseModel):
user = peewee.ForeignKeyField(User, related_name="events")
user = peewee.ForeignKeyField(User, related_name="events", null=True)
action = peewee.CharField()
object_type = peewee.CharField()
object_id = peewee.CharField(null=True)
Expand Down