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

[explore] Fix and test slice id logging issue #3339

Merged
merged 17 commits into from Aug 24, 2017
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions superset/models/core.py
Expand Up @@ -761,24 +761,26 @@ def wrapper(*args, **kwargs):
post_data = request.form or {}
d.update(post_data)
d.update(kwargs)
slice_id = d.get('slice_id', 0)
slice_id = d.get('slice_id')
form_data = d.get('form_data')

try:
slice_id = int(slice_id) if slice_id else 0
except ValueError:
slice_id = int(d.get('slice_id') or json.loads(d.get('form_data')).get('slice_id'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this ugly? You have slice_id and form_data already unpacked before and you are not using them here. That would make the code a lot more readable.

Also blind exceptions are an antipattern. If you want to go this route just catch TypeError and ValueError

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would yo prefer that I break it out in the if else style I had before that doesn't use exception for flow control? I am trying out the ValueError and TypeError approach now.

except Exception:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove blind exceptions, You only need ValueError and TypeError

slice_id = 0

params = ""
try:
params = json.dumps(d)
except:
pass
stats_logger.incr(f.__name__)
value = f(*args, **kwargs)

sesh = db.session()
log = cls(
action=f.__name__,
json=params,
dashboard_id=d.get('dashboard_id') or None,
dashboard_id=d.get('dashboard_id'),
slice_id=slice_id,
duration_ms=(
datetime.now() - start_dttm).total_seconds() * 1000,
Expand Down
18 changes: 17 additions & 1 deletion tests/core_tests.py
Expand Up @@ -41,6 +41,7 @@ def setUpClass(cls):
def setUp(self):
db.session.query(Query).delete()
db.session.query(models.DatasourceAccessRequest).delete()
db.session.query(models.Log).delete()

def tearDown(self):
db.session.query(Query).delete()
Expand Down Expand Up @@ -726,7 +727,22 @@ def test_user_profile(self, username='admin'):
data = self.get_json_resp('/superset/fave_dashboards_by_username/{}/'.format(username))
self.assertNotIn('message', data)

def test_slice_id_is_always_logged_correctly_on_web_request(self):
# superset/explore case
slc = db.session.query(models.Slice).filter_by(slice_name='Girls').one()
qry = db.session.query(models.Log).filter_by(slice_id=slc.id)
self.get_resp(slc.slice_url)
self.assertEqual(1, qry.count())

def test_slice_id_is_always_logged_correctly_on_ajax_request(self):
# superset/explore_json case
self.login(username="admin")
slc = db.session.query(models.Slice).filter_by(slice_name='Girls').one()
qry = db.session.query(models.Log).filter_by(slice_id=slc.id)
slc_url = slc.slice_url.replace("explore", "explore_json")
self.get_json_resp(slc_url)
self.assertEqual(1, qry.count())


if __name__ == '__main__':
unittest.main()