def test_incomplete_and_default_flags(self): exp = exp_services.get_exploration_by_id(exp_services.create_new( '*****@*****.**', 'exploration', 'category', 'eid')) state_id = exp.init_state_id # Hit the default rule once, and fail to answer twice. The result # should be classified as incomplete. for _ in range(3): stats_services.EventHandler.record_state_hit('eid', state_id, True) stats_services.EventHandler.record_answer_submitted( 'eid', state_id, self.SUBMIT_HANDLER, self.DEFAULT_RULESPEC_STR, '1') states = stats_services.get_top_improvable_states(['eid'], 2) self.assertEquals(len(states), 1) self.assertEquals(states[0]['rank'], 2) self.assertEquals(states[0]['type'], 'incomplete') # Now hit the default two more times. The result should be classified # as default. for i in range(2): stats_services.EventHandler.record_state_hit('eid', state_id, True) stats_services.EventHandler.record_answer_submitted( 'eid', state_id, self.SUBMIT_HANDLER, self.DEFAULT_RULESPEC_STR, '1') states = stats_services.get_top_improvable_states(['eid'], 2) self.assertEquals(len(states), 1) self.assertEquals(states[0]['rank'], 3) self.assertEquals(states[0]['type'], 'default')
def test_two_state_default_hit(self): exp = exp_services.get_exploration_by_id(exp_services.create_new( '*****@*****.**', 'exploration', 'category', 'eid')) SECOND_STATE = 'State 2' exp_services.add_state('*****@*****.**', exp.id, SECOND_STATE) exp = exp_services.get_exploration_by_id('eid') state1_id = exp.init_state_id second_state = exp_services.get_state_by_name('eid', SECOND_STATE) state2_id = second_state.id # Hit the default rule of state 1 once, and the default rule of state 2 # twice. stats_services.EventHandler.record_state_hit('eid', state1_id, True) stats_services.EventHandler.record_answer_submitted( 'eid', state1_id, self.SUBMIT_HANDLER, self.DEFAULT_RULESPEC_STR, '1') for i in range(2): stats_services.EventHandler.record_state_hit( 'eid', state2_id, True) stats_services.EventHandler.record_answer_submitted( 'eid', state2_id, self.SUBMIT_HANDLER, self.DEFAULT_RULESPEC_STR, '1') states = stats_services.get_top_improvable_states(['eid'], 5) self.assertEquals(len(states), 2) self.assertEquals(states[0]['rank'], 2) self.assertEquals(states[0]['type'], 'default') self.assertEquals(states[0]['state_id'], state2_id) self.assertEquals(states[1]['rank'], 1) self.assertEquals(states[1]['type'], 'default') self.assertEquals(states[1]['state_id'], state1_id) # Hit the default rule of state 1 two more times. for i in range(2): stats_services.EventHandler.record_state_hit( 'eid', state1_id, True) stats_services.EventHandler.record_answer_submitted( 'eid', state1_id, self.SUBMIT_HANDLER, self.DEFAULT_RULESPEC_STR, '1') states = stats_services.get_top_improvable_states(['eid'], 5) self.assertEquals(len(states), 2) self.assertEquals(states[0]['rank'], 3) self.assertEquals(states[0]['type'], 'default') self.assertEquals(states[0]['state_id'], state1_id) self.assertEquals(states[1]['rank'], 2) self.assertEquals(states[1]['type'], 'default') self.assertEquals(states[1]['state_id'], state2_id) # Try getting just the top improvable state. states = stats_services.get_top_improvable_states(['eid'], 1) self.assertEquals(len(states), 1) self.assertEquals(states[0]['rank'], 3) self.assertEquals(states[0]['type'], 'default') self.assertEquals(states[0]['state_id'], state1_id)
def get(self, exploration_id): """Gets the data for the exploration overview page.""" exploration = exp_services.get_exploration_by_id(exploration_id) state_list = {} for state_id in exploration.state_ids: state_list[state_id] = exp_services.export_state_to_verbose_dict( exploration_id, state_id) self.values.update({ 'exploration_id': exploration_id, 'init_state_id': exploration.init_state_id, 'is_public': exploration.is_public, 'image_id': exploration.image_id, 'category': exploration.category, 'title': exploration.title, 'editors': exploration.editor_ids, 'states': state_list, 'param_changes': exploration.param_change_dicts, 'param_specs': exploration.param_specs_dict, 'version': exploration.version, # Add information about the most recent versions. 'snapshots': exp_services.get_exploration_snapshots_metadata( exploration_id, DEFAULT_NUM_SNAPSHOTS), # Add information for the exploration statistics page. 'num_visits': stats_services.get_exploration_visit_count( exploration_id), 'num_completions': stats_services.get_exploration_completed_count( exploration_id), 'state_stats': stats_services.get_state_stats_for_exploration( exploration_id), 'imp': stats_services.get_top_improvable_states( [exploration_id], 10), }) self.render_json(self.values)
def test_no_improvement_flag_hit(self): exp = exp_domain.Exploration.create_default_exploration( 'eid', 'A title', 'A category') exp_services.save_new_exploration('*****@*****.**', exp) not_default_rule_spec = exp_domain.RuleSpec( { 'rule_type': rule_domain.ATOMIC_RULE_TYPE, 'name': 'NotDefault', 'inputs': {}, 'subject': 'answer' }, exp.init_state_name, [], [], 'NormalizedString') default_rule_spec = exp_domain.RuleSpec.get_default_rule_spec( feconf.END_DEST, 'NormalizedString') exp.init_state.widget.handlers[0].rule_specs = [ not_default_rule_spec, default_rule_spec ] exp_services._save_exploration('*****@*****.**', exp, '', []) stats_services.EventHandler.record_state_hit('eid', exp.init_state_name, True) stats_services.EventHandler.record_answer_submitted( 'eid', 1, exp.init_state_name, self.SUBMIT_HANDLER, not_default_rule_spec, '1') states = stats_services.get_top_improvable_states(['eid'], 1) self.assertEquals(len(states), 0)
def test_get_top_improvable_states(self): exp = exp_domain.Exploration.create_default_exploration( 'eid', 'A title', 'A category') exp_services.save_new_exploration('*****@*****.**', exp) state_name = exp.init_state_name for _ in range(5): stats_services.EventHandler.record_state_hit( 'eid', state_name, True) stats_services.EventHandler.record_answer_submitted( 'eid', state_name, self.SUBMIT_HANDLER, self.DEFAULT_RULESPEC_STR, '1') for _ in range(2): stats_services.EventHandler.record_answer_submitted( 'eid', state_name, self.SUBMIT_HANDLER, self.DEFAULT_RULESPEC_STR, '2') expected_top_state = { 'exp_id': 'eid', 'type': 'default', 'rank': 3, 'state_name': exp.init_state_name } states = stats_services.get_top_improvable_states(['eid'], 10) self.assertEquals(len(states), 1) self.assertDictContainsSubset(expected_top_state, states[0])
def test_get_top_improvable_states(self): exp = exp_domain.Exploration.create_default_exploration( 'eid', 'A title', 'A category') exp_services.save_new_exploration('*****@*****.**', exp) state_name = exp.init_state_name for _ in range(5): stats_services.EventHandler.record_state_hit( 'eid', state_name, True) stats_services.EventHandler.record_answer_submitted( 'eid', 1, state_name, self.SUBMIT_HANDLER, self.DEFAULT_RULESPEC, '1') for _ in range(2): stats_services.EventHandler.record_answer_submitted( 'eid', 1, state_name, self.SUBMIT_HANDLER, self.DEFAULT_RULESPEC, '2') expected_top_state = { 'exp_id': 'eid', 'type': 'default', 'rank': 3, 'state_name': exp.init_state_name } states = stats_services.get_top_improvable_states(['eid'], 10) self.assertEquals(len(states), 1) self.assertDictContainsSubset(expected_top_state, states[0])
def test_no_improvement_flag_hit(self): exp = exp_domain.Exploration.create_default_exploration( 'eid', 'A title', 'A category') exp_services.save_new_exploration('*****@*****.**', exp) not_default_rule_spec = exp_domain.RuleSpec({ 'rule_type': rule_domain.ATOMIC_RULE_TYPE, 'name': 'NotDefault', 'inputs': {}, 'subject': 'answer' }, exp.init_state_name, [], [], 'NormalizedString') default_rule_spec = exp_domain.RuleSpec.get_default_rule_spec(feconf.END_DEST, 'NormalizedString') exp.init_state.widget.handlers[0].rule_specs = [ not_default_rule_spec, default_rule_spec ] exp_services._save_exploration('*****@*****.**', exp, '', []) stats_services.EventHandler.record_state_hit( 'eid', exp.init_state_name, True) stats_services.EventHandler.record_answer_submitted( 'eid', 1, exp.init_state_name, self.SUBMIT_HANDLER, not_default_rule_spec, '1') states = stats_services.get_top_improvable_states(['eid'], 1) self.assertEquals(len(states), 0)
def get(self, exploration_id): """Handles GET requests.""" self.render_json({ 'num_visits': stats_services.get_exploration_visit_count( exploration_id), 'num_completions': stats_services.get_exploration_completed_count( exploration_id), 'state_stats': stats_services.get_state_stats_for_exploration( exploration_id), 'imp': stats_services.get_top_improvable_states( [exploration_id], 10), })
def get(self, exploration_id): """Handles GET requests.""" self.render_json({ 'num_visits': stats_services.get_exploration_visit_count(exploration_id), 'num_completions': stats_services.get_exploration_completed_count(exploration_id), 'state_stats': stats_services.get_state_stats_for_exploration(exploration_id), 'imp': stats_services.get_top_improvable_states([exploration_id], 10), })
def test_incomplete_and_default_flags(self): exp = exp_domain.Exploration.create_default_exploration( 'eid', 'A title', 'A category') exp_services.save_new_exploration('*****@*****.**', exp) state_name = exp.init_state_name # Hit the default rule once, and fail to answer twice. The result # should be classified as incomplete. for _ in range(3): stats_services.EventHandler.record_state_hit( 'eid', state_name, True) stats_services.EventHandler.record_answer_submitted( 'eid', 1, state_name, self.SUBMIT_HANDLER, self.DEFAULT_RULESPEC, '1') states = stats_services.get_top_improvable_states(['eid'], 2) self.assertEquals(len(states), 1) self.assertEquals(states[0]['rank'], 2) self.assertEquals(states[0]['type'], 'incomplete') # Now hit the default two more times. The result should be classified # as default. for i in range(2): stats_services.EventHandler.record_state_hit( 'eid', state_name, True) stats_services.EventHandler.record_answer_submitted( 'eid', 1, state_name, self.SUBMIT_HANDLER, self.DEFAULT_RULESPEC, '1') states = stats_services.get_top_improvable_states(['eid'], 2) self.assertEquals(len(states), 1) self.assertEquals(states[0]['rank'], 3) self.assertEquals(states[0]['type'], 'default')
def get(self): """Handles GET requests.""" if user_services.is_current_user_admin(self.request): exps = exp_services.get_all_explorations() else: exps = exp_services.get_editable_explorations(self.user_id) self.values.update({ 'explorations': [{ 'id': exp.id, 'image_id': exp.image_id, 'name': exp.title, } for exp in exps], 'improvable': stats_services.get_top_improvable_states( [exp.id for exp in exps], 10), }) self.render_json(self.values)
def test_single_default_rule_hit(self): exp = exp_services.get_exploration_by_id(exp_services.create_new( '*****@*****.**', 'exploration', 'category', 'eid')) state_id = exp.init_state_id stats_services.EventHandler.record_state_hit('eid', state_id, True) stats_services.EventHandler.record_answer_submitted( 'eid', state_id, self.SUBMIT_HANDLER, self.DEFAULT_RULESPEC_STR, '1') expected_top_state = { 'exp_id': 'eid', 'type': 'default', 'rank': 1, 'state_id': exp.init_state_id } states = stats_services.get_top_improvable_states(['eid'], 2) self.assertEquals(len(states), 1) self.assertDictContainsSubset(expected_top_state, states[0])
def test_no_improvement_flag_hit(self): exp = exp_services.get_exploration_by_id(exp_services.create_new( '*****@*****.**', 'exploration', 'category', 'eid')) not_default_rule_spec = exp_domain.RuleSpec({ 'rule_type': rule_domain.ATOMIC_RULE_TYPE, 'name': 'NotDefault', 'inputs': {}, 'subject': 'answer' }, exp.init_state.id, [], []) exp.init_state.widget.handlers[0].rule_specs = [ not_default_rule_spec, exp_domain.DEFAULT_RULESPEC ] exp_services.save_state('*****@*****.**', exp.id, exp.init_state) stats_services.EventHandler.record_state_hit( 'eid', exp.init_state.id, True) stats_services.EventHandler.record_answer_submitted( 'eid', exp.init_state.id, self.SUBMIT_HANDLER, str(not_default_rule_spec), '1') states = stats_services.get_top_improvable_states(['eid'], 1) self.assertEquals(len(states), 0)
def test_two_state_default_hit(self): exp = exp_domain.Exploration.create_default_exploration( 'eid', 'A title', 'A category') exp_services.save_new_exploration('*****@*****.**', exp) FIRST_STATE_NAME = exp.init_state_name SECOND_STATE_NAME = 'State 2' exp.add_states([SECOND_STATE_NAME]) exp_services._save_exploration('*****@*****.**', exp, '', []) # Hit the default rule of state 1 once, and the default rule of state 2 # twice. stats_services.EventHandler.record_state_hit('eid', FIRST_STATE_NAME, True) stats_services.EventHandler.record_answer_submitted( 'eid', 1, FIRST_STATE_NAME, self.SUBMIT_HANDLER, self.DEFAULT_RULESPEC, '1') for i in range(2): stats_services.EventHandler.record_state_hit( 'eid', SECOND_STATE_NAME, True) stats_services.EventHandler.record_answer_submitted( 'eid', 1, SECOND_STATE_NAME, self.SUBMIT_HANDLER, self.DEFAULT_RULESPEC, '1') states = stats_services.get_top_improvable_states(['eid'], 5) self.assertEquals(len(states), 2) self.assertDictContainsSubset( { 'rank': 2, 'type': 'default', 'state_name': SECOND_STATE_NAME }, states[0]) self.assertDictContainsSubset( { 'rank': 1, 'type': 'default', 'state_name': FIRST_STATE_NAME }, states[1]) # Hit the default rule of state 1 two more times. for i in range(2): stats_services.EventHandler.record_state_hit( 'eid', FIRST_STATE_NAME, True) stats_services.EventHandler.record_answer_submitted( 'eid', 1, FIRST_STATE_NAME, self.SUBMIT_HANDLER, self.DEFAULT_RULESPEC, '1') states = stats_services.get_top_improvable_states(['eid'], 5) self.assertEquals(len(states), 2) self.assertDictContainsSubset( { 'rank': 3, 'type': 'default', 'state_name': FIRST_STATE_NAME }, states[0]) self.assertDictContainsSubset( { 'rank': 2, 'type': 'default', 'state_name': SECOND_STATE_NAME }, states[1]) # Try getting just the top improvable state. states = stats_services.get_top_improvable_states(['eid'], 1) self.assertEquals(len(states), 1) self.assertDictContainsSubset( { 'rank': 3, 'type': 'default', 'state_name': FIRST_STATE_NAME }, states[0])
def test_two_state_default_hit(self): exp = exp_domain.Exploration.create_default_exploration( 'eid', 'A title', 'A category') exp_services.save_new_exploration('*****@*****.**', exp) FIRST_STATE_NAME = exp.init_state_name SECOND_STATE_NAME = 'State 2' exp.add_states([SECOND_STATE_NAME]) exp_services._save_exploration('*****@*****.**', exp, '', []) # Hit the default rule of state 1 once, and the default rule of state 2 # twice. stats_services.EventHandler.record_state_hit( 'eid', FIRST_STATE_NAME, True) stats_services.EventHandler.record_answer_submitted( 'eid', FIRST_STATE_NAME, self.SUBMIT_HANDLER, self.DEFAULT_RULESPEC_STR, '1') for i in range(2): stats_services.EventHandler.record_state_hit( 'eid', SECOND_STATE_NAME, True) stats_services.EventHandler.record_answer_submitted( 'eid', SECOND_STATE_NAME, self.SUBMIT_HANDLER, self.DEFAULT_RULESPEC_STR, '1') states = stats_services.get_top_improvable_states(['eid'], 5) self.assertEquals(len(states), 2) self.assertDictContainsSubset({ 'rank': 2, 'type': 'default', 'state_name': SECOND_STATE_NAME }, states[0]) self.assertDictContainsSubset({ 'rank': 1, 'type': 'default', 'state_name': FIRST_STATE_NAME }, states[1]) # Hit the default rule of state 1 two more times. for i in range(2): stats_services.EventHandler.record_state_hit( 'eid', FIRST_STATE_NAME, True) stats_services.EventHandler.record_answer_submitted( 'eid', FIRST_STATE_NAME, self.SUBMIT_HANDLER, self.DEFAULT_RULESPEC_STR, '1') states = stats_services.get_top_improvable_states(['eid'], 5) self.assertEquals(len(states), 2) self.assertDictContainsSubset({ 'rank': 3, 'type': 'default', 'state_name': FIRST_STATE_NAME }, states[0]) self.assertDictContainsSubset({ 'rank': 2, 'type': 'default', 'state_name': SECOND_STATE_NAME }, states[1]) # Try getting just the top improvable state. states = stats_services.get_top_improvable_states(['eid'], 1) self.assertEquals(len(states), 1) self.assertDictContainsSubset({ 'rank': 3, 'type': 'default', 'state_name': FIRST_STATE_NAME }, states[0])