Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit e147b58

Browse files
authored
Merge pull request #154 from edx/thallada/fake-data-no-videos-option
Add --no-videos option to generate_fake_course_data command
2 parents e6235e5 + bbb6734 commit e147b58

4 files changed

Lines changed: 40 additions & 10 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,4 @@ demo: clean requirements loaddata
7070
travis: clean test.requirements migrate
7171
python manage.py set_api_key edx edx
7272
python manage.py loaddata problem_response_answer_distribution --database=analytics
73-
python manage.py generate_fake_course_data --num-weeks=1
73+
python manage.py generate_fake_course_data --num-weeks=1 --no-videos

analytics_data_api/management/commands/generate_fake_course_data.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ def add_arguments(self, parser):
5858
default='ed_xavier',
5959
help='Username for which to generate fake data',
6060
)
61+
parser.add_argument(
62+
'--no-videos',
63+
action='store_false',
64+
dest='videos',
65+
default=True,
66+
help='Disables pulling video ids from the LMS server to generate fake video data and instead uses fake ids.'
67+
)
6168

6269
def generate_daily_data(self, course_id, start_date, end_date):
6370
# Use the preset ratios below to generate data in the specified demographics
@@ -308,18 +315,26 @@ def generate_all_video_data(self, course_id, videos):
308315

309316
logger.info("Done!")
310317

318+
def fake_video_ids_fallback(self):
319+
return [
320+
{
321+
'video_id': '0fac49ba',
322+
'video_module_id': 'i4x-edX-DemoX-video-5c90cffecd9b48b188cbfea176bf7fe9'
323+
}
324+
]
325+
311326
def handle(self, *args, **options):
312327
course_id = options['course_id']
313328
username = options['username']
314-
video_ids = self.fetch_videos_from_course_blocks(course_id)
315-
if not video_ids:
316-
logger.warning("Falling back to fake video id due to Course Blocks API failure...")
317-
video_ids = [
318-
{
319-
'video_id': '0fac49ba',
320-
'video_module_id': 'i4x-edX-DemoX-video-5c90cffecd9b48b188cbfea176bf7fe9'
321-
}
322-
]
329+
videos = options['videos']
330+
if videos:
331+
video_ids = self.fetch_videos_from_course_blocks(course_id)
332+
if not video_ids:
333+
logger.warning("Falling back to fake video id due to Course Blocks API failure...")
334+
video_ids = self.fake_video_ids_fallback()
335+
else:
336+
logger.info("Option to generate videos with ids pulled from the LMS is disabled, using fake video ids...")
337+
video_ids = self.fake_video_ids_fallback()
323338
start_date = timezone.now() - datetime.timedelta(weeks=10)
324339

325340
num_weeks = options['num_weeks']

analyticsdataserver/clients.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import logging
22

3+
from requests.exceptions import RequestException
4+
35
from edx_rest_api_client.client import EdxRestApiClient
46
from edx_rest_api_client.exceptions import HttpClientError
57
from opaque_keys.edx.keys import UsageKey
@@ -40,6 +42,9 @@ def all_videos(self, course_id):
4042
else:
4143
logger.warning("Course Blocks API failed to return video ids (%s).", e.response.status_code)
4244
return None
45+
except RequestException as e:
46+
logger.warning("Course Blocks API request failed. Is the LMS running?: " + str(e))
47+
return None
4348

4449
# Setup a terrible hack to silence mysterious flood of ImportErrors from stevedore inside edx-opaque-keys.
4550
# (The UsageKey utility still works despite the import errors, so I think the errors are not important).

analyticsdataserver/tests.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from django.test import TestCase
1212
from django.test.utils import override_settings
1313
from rest_framework.authtoken.models import Token
14+
from requests.exceptions import ConnectionError
1415

1516
from analytics_data_api.v0.models import CourseEnrollmentDaily, CourseEnrollmentByBirthYear
1617
from analyticsdataserver.clients import CourseBlocksApiClient
@@ -176,6 +177,15 @@ def test_all_videos_500(self, logger):
176177
logger.warning.assert_called_with('Course Blocks API failed to return video ids (%s).', 418)
177178
self.assertEqual(videos, None)
178179

180+
@responses.activate
181+
@mock.patch('analyticsdataserver.clients.logger')
182+
def test_all_videos_connection_error(self, logger):
183+
exception = ConnectionError('LMS is dead')
184+
responses.add(responses.GET, 'http://example.com/blocks/', body=exception)
185+
videos = self.client.all_videos('course_id')
186+
logger.warning.assert_called_with('Course Blocks API request failed. Is the LMS running?: ' + str(exception))
187+
self.assertEqual(videos, None)
188+
179189
@responses.activate
180190
def test_all_videos_pass_through_bad_id(self):
181191
responses.add(responses.GET, 'http://example.com/blocks/', body=json.dumps({'blocks': {

0 commit comments

Comments
 (0)