-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Define TrackedCommand for use as base class for tracking management commands #2079
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
29 changes: 29 additions & 0 deletions
29
common/djangoapps/track/management/tests/test_tracked_command.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import json | ||
| from StringIO import StringIO | ||
| from django.test import TestCase | ||
|
|
||
| from eventtracking import tracker as eventtracker | ||
|
|
||
| from track.management.tracked_command import TrackedCommand | ||
|
|
||
|
|
||
| class DummyCommand(TrackedCommand): | ||
| """A locally-defined command, for testing, that returns the current context as a JSON string.""" | ||
| def handle(self, *args, **options): | ||
| return json.dumps(eventtracker.get_tracker().resolve_context()) | ||
|
|
||
|
|
||
| class CommandsTestBase(TestCase): | ||
|
|
||
| def _run_dummy_command(self, *args, **kwargs): | ||
| """Runs the test command's execute method directly, and outputs a dict of the current context.""" | ||
| out = StringIO() | ||
| DummyCommand().execute(*args, stdout=out, **kwargs) | ||
| out.seek(0) | ||
| return json.loads(out.read()) | ||
|
|
||
| def test_command(self): | ||
| args = ['whee'] | ||
| kwargs = {'key1': 'default', 'key2': True} | ||
| json_out = self._run_dummy_command(*args, **kwargs) | ||
| self.assertEquals(json_out['command'], 'unknown') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| """Provides management command calling info to tracking context.""" | ||
|
|
||
| from django.core.management.base import BaseCommand | ||
|
|
||
| from eventtracking import tracker | ||
|
|
||
|
|
||
| class TrackedCommand(BaseCommand): | ||
| """ | ||
| Provides management command calling info to tracking context. | ||
|
|
||
| Information provided to context includes the following value: | ||
|
|
||
| 'command': the program name and the subcommand used to run a management command. | ||
|
|
||
| In future, other values (such as args and options) could be added as needed. | ||
|
|
||
| An example tracking log entry resulting from running the 'create_user' management command: | ||
|
|
||
| { | ||
| "username": "anonymous", | ||
| "host": "", | ||
| "event_source": "server", | ||
| "event_type": "edx.course.enrollment.activated", | ||
| "context": { | ||
| "course_id": "edX/Open_DemoX/edx_demo_course", | ||
| "org_id": "edX", | ||
| "command": "./manage.py create_user", | ||
| }, | ||
| "time": "2014-01-06T15:59:49.599522+00:00", | ||
| "ip": "", | ||
| "event": { | ||
| "course_id": "edX/Open_DemoX/edx_demo_course", | ||
| "user_id": 29, | ||
| "mode": "verified" | ||
| }, | ||
| "agent": "", | ||
| "page": null | ||
| } | ||
|
|
||
| The name of the context used to add (and remove) these values is "edx.mgmt.command". | ||
| The context name is used to allow the context additions to be scoped, but doesn't | ||
| appear in the context itself. | ||
| """ | ||
| prog_name = 'unknown' | ||
|
|
||
| def create_parser(self, prog_name, subcommand): | ||
| """Wraps create_parser to snag command line info.""" | ||
| self.prog_name = "{} {}".format(prog_name, subcommand) | ||
| return super(TrackedCommand, self).create_parser(prog_name, subcommand) | ||
|
|
||
| def execute(self, *args, **options): | ||
| """Wraps base execute() to add command line to tracking context.""" | ||
| context = { | ||
| 'command': self.prog_name, | ||
| } | ||
| COMMAND_CONTEXT_NAME = 'edx.mgmt.command' | ||
| with tracker.get_tracker().context(COMMAND_CONTEXT_NAME, context): | ||
| super(TrackedCommand, self).execute(*args, **options) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would leave these two entries (course_id, org_id) out until they are actually being added by the create_user command
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are added by the enrollment call itself. Only "command" is now being added by the TrackedCommand class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, I suppose they would be. Awesome.