Commit e147b58b by Tyler Hallada Committed by GitHub

Merge pull request #154 from edx/thallada/fake-data-no-videos-option

Add --no-videos option to generate_fake_course_data command
parents e6235e51 bbb67343
...@@ -70,4 +70,4 @@ demo: clean requirements loaddata ...@@ -70,4 +70,4 @@ demo: clean requirements loaddata
travis: clean test.requirements migrate travis: clean test.requirements migrate
python manage.py set_api_key edx edx python manage.py set_api_key edx edx
python manage.py loaddata problem_response_answer_distribution --database=analytics python manage.py loaddata problem_response_answer_distribution --database=analytics
python manage.py generate_fake_course_data --num-weeks=1 python manage.py generate_fake_course_data --num-weeks=1 --no-videos
...@@ -58,6 +58,13 @@ class Command(BaseCommand): ...@@ -58,6 +58,13 @@ class Command(BaseCommand):
default='ed_xavier', default='ed_xavier',
help='Username for which to generate fake data', help='Username for which to generate fake data',
) )
parser.add_argument(
'--no-videos',
action='store_false',
dest='videos',
default=True,
help='Disables pulling video ids from the LMS server to generate fake video data and instead uses fake ids.'
)
def generate_daily_data(self, course_id, start_date, end_date): def generate_daily_data(self, course_id, start_date, end_date):
# Use the preset ratios below to generate data in the specified demographics # Use the preset ratios below to generate data in the specified demographics
...@@ -308,18 +315,26 @@ class Command(BaseCommand): ...@@ -308,18 +315,26 @@ class Command(BaseCommand):
logger.info("Done!") logger.info("Done!")
def fake_video_ids_fallback(self):
return [
{
'video_id': '0fac49ba',
'video_module_id': 'i4x-edX-DemoX-video-5c90cffecd9b48b188cbfea176bf7fe9'
}
]
def handle(self, *args, **options): def handle(self, *args, **options):
course_id = options['course_id'] course_id = options['course_id']
username = options['username'] username = options['username']
videos = options['videos']
if videos:
video_ids = self.fetch_videos_from_course_blocks(course_id) video_ids = self.fetch_videos_from_course_blocks(course_id)
if not video_ids: if not video_ids:
logger.warning("Falling back to fake video id due to Course Blocks API failure...") logger.warning("Falling back to fake video id due to Course Blocks API failure...")
video_ids = [ video_ids = self.fake_video_ids_fallback()
{ else:
'video_id': '0fac49ba', logger.info("Option to generate videos with ids pulled from the LMS is disabled, using fake video ids...")
'video_module_id': 'i4x-edX-DemoX-video-5c90cffecd9b48b188cbfea176bf7fe9' video_ids = self.fake_video_ids_fallback()
}
]
start_date = timezone.now() - datetime.timedelta(weeks=10) start_date = timezone.now() - datetime.timedelta(weeks=10)
num_weeks = options['num_weeks'] num_weeks = options['num_weeks']
......
import logging import logging
from requests.exceptions import RequestException
from edx_rest_api_client.client import EdxRestApiClient from edx_rest_api_client.client import EdxRestApiClient
from edx_rest_api_client.exceptions import HttpClientError from edx_rest_api_client.exceptions import HttpClientError
from opaque_keys.edx.keys import UsageKey from opaque_keys.edx.keys import UsageKey
...@@ -40,6 +42,9 @@ class CourseBlocksApiClient(EdxRestApiClient): ...@@ -40,6 +42,9 @@ class CourseBlocksApiClient(EdxRestApiClient):
else: else:
logger.warning("Course Blocks API failed to return video ids (%s).", e.response.status_code) logger.warning("Course Blocks API failed to return video ids (%s).", e.response.status_code)
return None return None
except RequestException as e:
logger.warning("Course Blocks API request failed. Is the LMS running?: " + str(e))
return None
# Setup a terrible hack to silence mysterious flood of ImportErrors from stevedore inside edx-opaque-keys. # Setup a terrible hack to silence mysterious flood of ImportErrors from stevedore inside edx-opaque-keys.
# (The UsageKey utility still works despite the import errors, so I think the errors are not important). # (The UsageKey utility still works despite the import errors, so I think the errors are not important).
......
...@@ -11,6 +11,7 @@ from django.db.utils import ConnectionHandler, DatabaseError ...@@ -11,6 +11,7 @@ from django.db.utils import ConnectionHandler, DatabaseError
from django.test import TestCase from django.test import TestCase
from django.test.utils import override_settings from django.test.utils import override_settings
from rest_framework.authtoken.models import Token from rest_framework.authtoken.models import Token
from requests.exceptions import ConnectionError
from analytics_data_api.v0.models import CourseEnrollmentDaily, CourseEnrollmentByBirthYear from analytics_data_api.v0.models import CourseEnrollmentDaily, CourseEnrollmentByBirthYear
from analyticsdataserver.clients import CourseBlocksApiClient from analyticsdataserver.clients import CourseBlocksApiClient
...@@ -177,6 +178,15 @@ class ClientTests(TestCase): ...@@ -177,6 +178,15 @@ class ClientTests(TestCase):
self.assertEqual(videos, None) self.assertEqual(videos, None)
@responses.activate @responses.activate
@mock.patch('analyticsdataserver.clients.logger')
def test_all_videos_connection_error(self, logger):
exception = ConnectionError('LMS is dead')
responses.add(responses.GET, 'http://example.com/blocks/', body=exception)
videos = self.client.all_videos('course_id')
logger.warning.assert_called_with('Course Blocks API request failed. Is the LMS running?: ' + str(exception))
self.assertEqual(videos, None)
@responses.activate
def test_all_videos_pass_through_bad_id(self): def test_all_videos_pass_through_bad_id(self):
responses.add(responses.GET, 'http://example.com/blocks/', body=json.dumps({'blocks': { responses.add(responses.GET, 'http://example.com/blocks/', body=json.dumps({'blocks': {
'block-v1:edX+DemoX+Demo_Course+type@video+block@5c90cffecd9b48b188cbfea176bf7fe9': { 'block-v1:edX+DemoX+Demo_Course+type@video+block@5c90cffecd9b48b188cbfea176bf7fe9': {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment