Skip to content

Commit

Permalink
[explore] Fix and test slice id logging issue (#3339)
Browse files Browse the repository at this point in the history
* [explore] fixed padding bug on filter section

* fix slice_id logging issue

* [superset-sqllab] fix slice_id population in appropriate column

* [explore-logging] test the slice_id logging fix

* fix travis errors

* fix nits pointed out in PR comments

* cleanup tests

* made python more beautiful

* made python even more beautiful

* made python even more more beautiful

* made python even more more more beautiful

* fix lint error

* make exception handling more specific

* fixed silly error

* fixed argument indentation
  • Loading branch information
timifasubaa authored and mistercrunch committed Aug 24, 2017
1 parent c5b1eb7 commit 8d877e8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
17 changes: 9 additions & 8 deletions superset/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ class CssTemplate(Model, AuditMixinNullable):
slice_user = Table('slice_user', metadata,
Column('id', Integer, primary_key=True),
Column('user_id', Integer, ForeignKey('ab_user.id')),
Column('slice_id', Integer, ForeignKey('slices.id'))
)
Column('slice_id', Integer, ForeignKey('slices.id')))


class Slice(Model, AuditMixinNullable, ImportMixin):
Expand Down Expand Up @@ -435,7 +434,7 @@ def alter_positions(dashboard, old_to_new_slc_id_dict):
new_filter_immune_slices.append(new_slc_id_str)
if ('timed_refresh_immune_slices' in i_params_dict and
old_slc_id_str in
i_params_dict['timed_refresh_immune_slices']):
i_params_dict['timed_refresh_immune_slices']):
new_timed_refresh_immune_slices.append(new_slc_id_str)
if ('expanded_slices' in i_params_dict and
old_slc_id_str in i_params_dict['expanded_slices']):
Expand Down Expand Up @@ -761,24 +760,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')

try:
slice_id = int(slice_id) if slice_id else 0
except ValueError:
slice_id = int(
slice_id or json.loads(d.get('form_data')).get('slice_id'))
except (ValueError, 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
Original file line number Diff line number Diff line change
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()

0 comments on commit 8d877e8

Please sign in to comment.