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

analytics: Make stats of all realms accessible to server admins. #9099

Closed

Conversation

shubhamdhama
Copy link
Member

@shubhamdhama shubhamdhama commented Apr 15, 2018

  • Currently, I've made things working.
    @timabbott @rishig FYI.
    peek 2018-04-15 23-13
  • Tests aren't updated/added yet. Added
  • Sorry for the delay actually in this comment activity: First steps to make the data clearer #7568 (comment) I've misunderstood that we want the data of /stat to be served in the tables of /activity page, indeed I'm not sure whether we want to do this also or not?
  • Currently we can access chart stats of any realm at URL analytics/chart_data/realm/(?P<realm_str>[\S]+). Definitely, we need a page to direct to these links or we can have these links at /activity page(I think this clears my above confusion) Added link in /activity table

realm=realm.string_id,
is_staff=request.user.is_staff))

@require_server_admin_access
Copy link
Member Author

Choose a reason for hiding this comment

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

Any better name suggestion for this decorator :)

Copy link
Sponsor Member

Choose a reason for hiding this comment

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

see above :)

@rishig
Copy link
Member

rishig commented Apr 16, 2018

cool! I would put the links on /activity next to the email icon. Maybe a chart icon?

@shubhamdhama
Copy link
Member Author

Sounds good, added the link:
link

@shubhamdhama shubhamdhama changed the title [WIP]analytics: Make stats of all realms accessible to server admins. analytics: Make stats of all realms accessible to server admins. Apr 16, 2018
if not user_profile.is_staff:
raise JsonableError(_("Must be an server administrator"))
return view_func(request, user_profile, *args, **kwargs)
return _wrapped_view_func # type: ignore # https://github.com/python/mypy/issues/1927
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

This should come with some tests in test_decorator.py.

Also, I'd call it require_server_admin_api -- since the point is it's for API/JSON type views.

'analytics/stats.html',
context=dict(given_realm_name=realm.name,
realm=realm.string_id,
is_staff=request.user.is_staff))
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

I think you want to deduplication this render block in a function. E.g. return render_stats(request, realm).

realm_str: str, **kwargs: Any) -> HttpResponse:
realm = get_realm(realm_str)
if realm is None:
raise JsonableError(_(("Realm %s does not exist") % (realm_str)))
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

This is the wrong order-of-operations of i8n and string interpolation and doesn't work.

Also, we generally don't use realm in user-facing strings. I'd probably make this error just be "Invalid organization" -- no need to include the organization ID in the error (it'll be right there in the URL).

Copy link
Member Author

Choose a reason for hiding this comment

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

Ohh yeah, I missed this, it should be _("Realm %s does not exist") % (realm_str), BTW removing realm_str.

function get_chart_data(data, callback) {
var url;
if (page_params.is_staff === "True") {
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

This is kinda hacky; ideally, we'd do the same JSON encoding thing that we do in home() to end up with a proper JS "true".

@@ -14,7 +24,7 @@
<div class="page-content">
<div id="id_stats_errors" class="alert alert-error"></div>
<div class="center-charts">
<h1 class="analytics-page-header">{% trans %}Zulip analytics for {{ realm_name }}{% endtrans %}</h1>
<h1 class="analytics-page-header">{% trans %}Zulip analytics for {{ given_realm_name }}{% endtrans %}</h1>
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

I think a better name would be target_realm_name, perhaps?

@timabbott
Copy link
Sponsor Member

I posted a batch of comments. Thanks for doing this @shubhamdhama, I'm excited about this :)

@rishig
Copy link
Member

rishig commented Apr 16, 2018

small comment about the UI: we don't need another column for this. We can just have an empty column label, and then put both the mail icon and the pie chart icon together in each box.

@shubhamdhama
Copy link
Member Author

@rishig @timabbott Updated the PR.

demo

@rishig
Copy link
Member

rishig commented Apr 17, 2018

Tim: do we care for this page to be translated at all?

@timabbott
Copy link
Sponsor Member

No, we don't want translations on this page; it'd waste translator time more than anything else.

if not user_profile.is_staff:
raise JsonableError(_("Must be an server administrator"))
return view_func(request, user_profile, *args, **kwargs)
return _wrapped_view_func # type: ignore # https://github.com/python/mypy/issues/1927
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

Can you add a test in the commit that adds this?

@rishig
Copy link
Member

rishig commented Apr 18, 2018

ah, I was confused by something in the diff. Agreed about not translating.

This is just variabnt of `require_server_admin` for JSON/api views.
In this commit:
Two new URLs are added, to make all realms accessible for server
admins. One is for the stats page itself and another for getting
chart data i.e. chart data API requests.
For the above two new URLs corresponding two view functions are
added.

result = self.client_get('/json/analytics/chart_data/realm/zulip/',
{'chart_name': 'number_of_humans'})
self.assert_json_error(result, "Must be an server administrator", 400)
Copy link
Member Author

Choose a reason for hiding this comment

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

@timabbott This already tests the new decorator function( just like require_realm_admin, which is also tested by similar requests in other tests).
Actually I give this a shot, and it is really a pain to add a test for this, the problem here is that if we want to test the decorator (in its own commit) we have to mock zulip_login_required decorator itself and from my search mocking a decorator is a complex task which become more worse here as we are further testing another decorator (because decorators act at the load time rather than run time which makes it difficult). Another alternative here is that to mock whole request object, but this approach doesn't look right to me because of redefining many functions and other object members.
If we still want to add test, can you please help me here?

Copy link
Sponsor Member

Choose a reason for hiding this comment

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

OK, I guess the current tests are fine.

@timabbott
Copy link
Sponsor Member

I fixed a couple things:

  • The target_realm_name variable fix was in the wrong commit
  • There was a typo in the last commit's spelling of "activity".

Merged, thanks @shubhamdhama!

I think one follow-up that we should do after this is take advantage of it to improve the analytics development process. @rishig may have some thoughts, but ideally, we'd have a default user have is_staff enabled.
(1) populate_analytics_db we should probably run directly from the main populate_db project, and maybe hide the shylock user.
(2) We should make update the docs on analytics development to recommend using this feature

(The overall goal is to make "testing analytics" not require a bunch of manual setup)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants