Skip to content

Commit

Permalink
Add score store check & log. openedx#2422 (openedx#2424)
Browse files Browse the repository at this point in the history
  • Loading branch information
kawaguchi-ks authored Feb 13, 2018
1 parent 068f339 commit 36fea93
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,78 @@ def test_force_if_batch_status_exist_today(self):
self.assert_finished(1, self.contract, self.course1)
self.assert_finished(1, self.contract, self.course2)

@patch('biz.djangoapps.ga_achievement.management.commands.update_biz_score_status.log.info')
def test_success_log(self, mock_log_info):
for var in range(0, 50):
user = UserFactory.create()
self._input_contract(self.contract, user)
for var in range(0, 50):
user = UserFactory.create()
self._register_contract(self.contract, user, additional_value=ADDITIONAL_SETTINGS_VALUE)

call_command('update_biz_score_status', self.contract.id)

mock_log_info.assert_any_call(
u'Removed ScoreStore records. contract_id={} course_id={}'.format(
self.contract.id, self.course1.id))
mock_log_info.assert_any_call(
u'Removed ScoreStore records. contract_id={} course_id={}'.format(
self.contract.id, self.course2.id))

mock_log_info.assert_any_call(
u'Stored ScoreStore records(100). contract_id={} course_id={}'.format(
self.contract.id, self.course1.id))
mock_log_info.assert_any_call(
u'Stored ScoreStore records(100). contract_id={} course_id={}'.format(
self.contract.id, self.course2.id))

ScoreBatchStatus.objects.get(contract=self.contract, course_id=self.course1.id, status=BATCH_STATUS_FINISHED, student_count=100)
ScoreBatchStatus.objects.get(contract=self.contract, course_id=self.course2.id, status=BATCH_STATUS_FINISHED, student_count=100)

@patch('biz.djangoapps.ga_achievement.management.commands.update_biz_score_status.log.error')
def test_error_log_can_not_remove_documents(self, mock_log_error):
for var in range(0, 50):
user = UserFactory.create()
self._input_contract(self.contract, user)
for var in range(0, 50):
user = UserFactory.create()
self._register_contract(self.contract, user, additional_value=ADDITIONAL_SETTINGS_VALUE)

with patch('biz.djangoapps.ga_achievement.management.commands.update_biz_score_status.ScoreStore.get_record_count', return_value=100):
call_command('update_biz_score_status', self.contract.id)

mock_log_error.assert_any_call(
u'Unexpected error occurred: Can not remove ScoreStore records(100). contract_id={} course_id={}'.format(
self.contract.id, self.course1.id))
mock_log_error.assert_any_call(
u'Unexpected error occurred: Can not remove ScoreStore records(100). contract_id={} course_id={}'.format(
self.contract.id, self.course2.id))

ScoreBatchStatus.objects.get(contract=self.contract, course_id=self.course1.id, status=BATCH_STATUS_ERROR, student_count=None)
ScoreBatchStatus.objects.get(contract=self.contract, course_id=self.course2.id, status=BATCH_STATUS_ERROR, student_count=None)

@patch('biz.djangoapps.ga_achievement.management.commands.update_biz_score_status.log.error')
def test_error_log_can_not_set_documents(self, mock_log_error):
for var in range(0, 50):
user = UserFactory.create()
self._input_contract(self.contract, user)
for var in range(0, 50):
user = UserFactory.create()
self._register_contract(self.contract, user, additional_value=ADDITIONAL_SETTINGS_VALUE)

with patch('biz.djangoapps.ga_achievement.management.commands.update_biz_score_status.ScoreStore.get_record_count', return_value=0):
call_command('update_biz_score_status', self.contract.id)

mock_log_error.assert_any_call(
u'Unexpected error occurred: ScoreStore record count(0) does not mutch Contract Register record count(100) or records count(100). contract_id={} course_id={}'.format(
self.contract.id, self.course1.id))
mock_log_error.assert_any_call(
u'Unexpected error occurred: ScoreStore record count(0) does not mutch Contract Register record count(100) or records count(100). contract_id={} course_id={}'.format(
self.contract.id, self.course2.id))

ScoreBatchStatus.objects.get(contract=self.contract, course_id=self.course1.id, status=BATCH_STATUS_ERROR, student_count=None)
ScoreBatchStatus.objects.get(contract=self.contract, course_id=self.course2.id, status=BATCH_STATUS_ERROR, student_count=None)


class TestGroupedTargetSections(ModuleStoreTestCase):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,10 @@ def handle(self, *args, **options):
for contract_detail in contract.details.all():
try:
course_key = contract_detail.course_id
unicode_course_key = unicode(course_key)
log.info(
u"Command update_biz_score_status for contract({}) and course({}) is now processing...".format(
contract.id, unicode(course_key)))
contract.id, unicode_course_key))
ScoreBatchStatus.save_for_started(contract.id, course_key)

# Check if course exists in modulestore
Expand All @@ -214,7 +215,7 @@ def handle(self, *args, **options):
# Column
column = OrderedDict()
column[ScoreStore.FIELD_CONTRACT_ID] = contract.id
column[ScoreStore.FIELD_COURSE_ID] = unicode(course_key)
column[ScoreStore.FIELD_COURSE_ID] = unicode_course_key
column[ScoreStore.FIELD_DOCUMENT_TYPE] = ScoreStore.FIELD_DOCUMENT_TYPE__COLUMN
if ContractAuth.objects.filter(contract_id=contract.id).exists():
column[_(ScoreStore.FIELD_LOGIN_CODE)] = ScoreStore.COLUMN_TYPE__TEXT
Expand All @@ -240,8 +241,9 @@ def handle(self, *args, **options):

# Records
records = []
for contract_register in ContractRegister.find_input_and_register_by_contract(
contract.id).select_related('user__standing', 'user__profile'):
q_contract_register = ContractRegister.find_input_and_register_by_contract(contract.id)
count_contract_register = q_contract_register.count()
for contract_register in q_contract_register.select_related('user__standing', 'user__profile'):
user = contract_register.user
# Student Status
course_enrollment = CourseEnrollment.get_enrollment(user, course_key)
Expand All @@ -267,7 +269,7 @@ def handle(self, *args, **options):
# Records
record = OrderedDict()
record[ScoreStore.FIELD_CONTRACT_ID] = contract.id
record[ScoreStore.FIELD_COURSE_ID] = unicode(course_key)
record[ScoreStore.FIELD_COURSE_ID] = unicode_course_key
record[ScoreStore.FIELD_DOCUMENT_TYPE] = ScoreStore.FIELD_DOCUMENT_TYPE__RECORD
# Only in the case of a contract using login code, setting processing of data is performed.
if ContractAuth.objects.filter(contract_id=contract.id).exists():
Expand Down Expand Up @@ -310,16 +312,37 @@ def handle(self, *args, **options):
record[_(ScoreStore.FIELD_TOTAL_SCORE)] = score_calculator.get_total_score()
records.append(record)

score_store = ScoreStore(contract.id, unicode(course_key))
score_store = ScoreStore(contract.id, unicode_course_key)
score_store.remove_documents()

# Confirm remove_documents.
count_score_store = score_store.get_record_count()
if count_score_store == 0:
log.info(u"Removed ScoreStore records. contract_id={} course_id={}".format(
contract.id, unicode_course_key))
else:
raise Exception(u"Can not remove ScoreStore records({}). contract_id={} course_id={}".format(
count_score_store, contract.id, unicode_course_key))

len_records = len(records)

if records:
score_store.set_documents([column])
score_store.set_documents(records)
score_store.drop_indexes()
score_store.ensure_indexes()

# Confirm set_documents.
count_score_store = score_store.get_record_count()
if count_score_store == count_contract_register == len_records:
log.info(u"Stored ScoreStore records({}). contract_id={} course_id={}".format(
count_score_store, contract.id, unicode_course_key))
else:
raise Exception(u"ScoreStore record count({}) does not mutch Contract Register record count({}) or records count({}). contract_id={} course_id={}".format(
count_score_store, count_contract_register, len_records, contract.id, unicode_course_key))

except CourseDoesNotExist:
log.warning(u"This course does not exist in modulestore. course_id={}".format(unicode(course_key)))
log.warning(u"This course does not exist in modulestore. course_id={}".format(unicode_course_key))
ScoreBatchStatus.save_for_error(contract.id, course_key)
except Exception as ex:
error_flag = True
Expand Down

0 comments on commit 36fea93

Please sign in to comment.