def test_incomplete_and_default_flags(self): InteractiveWidget.load_default_widgets() exp = Exploration.create( User(email='*****@*****.**'), 'exploration', 'category', 'eid') state_id = exp.init_state.get().id # Hit the default once, and do an incomplete twice. The result should # be classified as incomplete. for i in range(3): EventHandler.record_state_hit('eid', state_id) EventHandler.record_rule_hit( 'eid', state_id, Rule(name='Default', dest=state_id), extra_info='1') states = Statistics.get_top_ten_improvable_states(['eid']) 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): EventHandler.record_state_hit('eid', state_id) EventHandler.record_rule_hit( 'eid', state_id, Rule(name='Default', dest=state_id), extra_info='1') states = Statistics.get_top_ten_improvable_states(['eid']) self.assertEquals(len(states), 1) self.assertEquals(states[0]['rank'], 3) self.assertEquals(states[0]['type'], 'default')
def get_exploration_stats(exploration): """Returns a dict with stats for the given exploration.""" num_visits = Statistics.get_exploration_stats( STATS_ENUMS.exploration_visited, exploration.id) num_completions = Statistics.get_exploration_stats( STATS_ENUMS.exploration_completed, exploration.id) answers = Statistics.get_exploration_stats( STATS_ENUMS.rule_hit, exploration.id) state_counts = Statistics.get_exploration_stats( STATS_ENUMS.state_hit, exploration.id) state_stats = {} for state_id in answers.keys(): state_stats[state_id] = { 'name': answers[state_id]['name'], 'count': state_counts[state_id]['count'], 'rule_stats': {}, } for rule in answers[state_id]['rules'].keys(): state_stats[state_id]['rule_stats'][rule] = answers[state_id]['rules'][rule] state_stats[state_id]['rule_stats'][rule]['count'] = 0 for _, count in answers[state_id]['rules'][rule]['answers']: state_stats[state_id]['rule_stats'][rule]['count'] += count return { 'num_visits': num_visits, 'num_completions': num_completions, 'state_stats': state_stats, }
def test_two_state_default_hit(self): SECOND_STATE = 'State 2' InteractiveWidget.load_default_widgets() exp = Exploration.create( User(email='*****@*****.**'), 'exploration', 'category', 'eid') second_state = exp.add_state(SECOND_STATE) state_1_id = exp.init_state.get().id state_2_id = second_state.id # Hit the default rule of state 1 once, and the default rule of state 2 # twice. EventHandler.record_state_hit('eid', state_1_id) EventHandler.record_rule_hit( 'eid', state_1_id, Rule(name='Default', dest=state_1_id), extra_info='1') for i in range(2): EventHandler.record_state_hit('eid', state_2_id) EventHandler.record_rule_hit( 'eid', state_2_id, Rule(name='Default', dest=state_2_id), extra_info='1') states = Statistics.get_top_ten_improvable_states(['eid']) self.assertEquals(len(states), 2) self.assertEquals(states[0]['rank'], 2) self.assertEquals(states[0]['type'], 'default') self.assertEquals(states[0]['state_id'], state_2_id) self.assertEquals(states[1]['rank'], 1) self.assertEquals(states[1]['type'], 'default') self.assertEquals(states[1]['state_id'], state_1_id) # Hit the default rule of state 1 two more times. for i in range(2): EventHandler.record_state_hit('eid', state_1_id) EventHandler.record_rule_hit( 'eid', state_1_id, Rule(name='Default', dest=state_1_id), extra_info='1') states = Statistics.get_top_ten_improvable_states(['eid']) self.assertEquals(len(states), 2) self.assertEquals(states[0]['rank'], 3) self.assertEquals(states[0]['type'], 'default') self.assertEquals(states[0]['state_id'], state_1_id) self.assertEquals(states[1]['rank'], 2) self.assertEquals(states[1]['type'], 'default') self.assertEquals(states[1]['state_id'], state_2_id)
def test_get_top_ten_improvable_states(self): InteractiveWidget.load_default_widgets() exp = Exploration.create( User(email='*****@*****.**'), 'exploration', 'category', 'eid') state_id = exp.init_state.get().id EventHandler.record_rule_hit( 'eid', state_id, Rule(name='Default', dest=state_id), extra_info='1') EventHandler.record_rule_hit( 'eid', state_id, Rule(name='Default', dest=state_id), extra_info='2') EventHandler.record_rule_hit( 'eid', state_id, Rule(name='Default', dest=state_id), extra_info='1') EventHandler.record_state_hit('eid', state_id) EventHandler.record_state_hit('eid', state_id) EventHandler.record_state_hit('eid', state_id) EventHandler.record_state_hit('eid', state_id) EventHandler.record_state_hit('eid', state_id) states = Statistics.get_top_ten_improvable_states(['eid']) self.assertEquals(len(states), 1) self.assertEquals(states[0]['exp_id'], 'eid') self.assertEquals(states[0]['type'], 'default') self.assertEquals(states[0]['rank'], 3) self.assertEquals(states[0]['state_id'], exp.init_state.get().id)
def get_exploration_stats(exploration): """Returns a dict with stats for the given exploration.""" num_visits = Statistics.get_exploration_stats( STATS_ENUMS.exploration_visited, exploration.id) num_completions = Statistics.get_exploration_stats( STATS_ENUMS.exploration_completed, exploration.id) answers = Statistics.get_exploration_stats( STATS_ENUMS.rule_hit, exploration.id) state_counts = Statistics.get_exploration_stats( STATS_ENUMS.state_hit, exploration.id) state_stats = {} for state_id in answers.keys(): state_stats[state_id] = { 'name': answers[state_id]['name'], 'count': state_counts[state_id]['count'], 'rule_stats': {}, } all_rule_count = 0 state_count = state_counts[state_id]['count'] for rule in answers[state_id]['rules'].keys(): state_stats[state_id]['rule_stats'][rule] = answers[state_id]['rules'][rule] rule_count = 0 for _, count in answers[state_id]['rules'][rule]['answers']: rule_count += count all_rule_count += count state_stats[state_id]['rule_stats'][rule]['chartData'] = [ ['', 'This rule', 'Other answers'], ['', rule_count, state_count - rule_count]] state_stats[state_id]['no_answer_chartdata'] = [ ['', 'No answer', 'Answer given'], ['', state_count - all_rule_count, all_rule_count]] return { 'num_visits': num_visits, 'num_completions': num_completions, 'state_stats': state_stats, }
def get(self): """Handles GET requests.""" exp_ids = Exploration.get_explorations_user_can_edit(self.user) improvable = Statistics.get_top_ten_improvable_states(exp_ids) explorations = [] for exp_id in exp_ids: exp = Exploration.get(exp_id) explorations.append({"name": exp.title, "id": exp.id, "image_id": exp.image_id}) self.values.update({"improvable": improvable, "explorations": explorations}) self.render_json(self.values)
def clean_statistics(*args): logger = Logger("clean_statistics") logger.write("Start") try: num = Statistics.cronjob() except: logger.error(traceback.format_exc()) else: logger.write("Done. Delete {0}".format(num)) finally: logger.close()
def run_it(self): ds_name = os.path.splitext(os.path.basename(self.ds.ffile.name))[0] self.ds = self.grab_item(models.DataSet, self.ds.pk,'parsing imgs from %s' % ds_name) if not self.ds: return True # all got taken by somebody else if self.ds.status == models.DS_RECHECK: self.recheck_dataset(ds_name) elif self.ds.tar_dir: # already in progress, continue to work on it self.process_tar_snippets() else: self.log('Reading dataset %s' % ds_name, 3) stats, created = Statistics.objects.get_or_create(ds=self.ds) if not created: stats.delete() stats = Statistics(ds=self.ds) stats.set_name = ds_name stats.save() self.process_dataset() self.release_item(models.DataSet, self.ds.pk) return True
def test_no_improvement_flag_hit(self): InteractiveWidget.load_default_widgets() exp = Exploration.create( User(email='*****@*****.**'), 'exploration', 'category', 'eid') init_state = exp.init_state.get() state_id = init_state.id init_state.widget.handlers[0].rules = [ Rule(name='NotDefault', dest=state_id), Rule(name='Default', dest=state_id), ] init_state.put() EventHandler.record_rule_hit( 'eid', state_id, Rule(name='NotDefault', dest=state_id), extra_info='1') EventHandler.record_state_hit('eid', state_id) states = Statistics.get_top_ten_improvable_states(['eid']) self.assertEquals(len(states), 0)
def get(self, exploration): """Gets the question name and state list for a question page.""" state_list = {} for state_key in exploration.states: state = state_key.get() state_list[state.id] = get_state_for_frontend(state, exploration) parameters = [] for param in exploration.parameters: parameters.append({ 'name': param.name, 'obj_type': param.obj_type, 'description': param.description, 'values': param.values }) self.values.update({ 'exploration_id': exploration.id, 'init_state_id': exploration.init_state.get().id, 'is_public': exploration.is_public, 'image_id': exploration.image_id, 'category': exploration.category, 'title': exploration.title, 'editors': [editor.nickname() for editor in exploration.editors], 'states': state_list, 'parameters': parameters, }) statistics = get_exploration_stats(exploration) self.values.update({ 'num_visits': statistics['num_visits'], 'num_completions': statistics['num_completions'], 'state_stats': statistics['state_stats'], }) improvements = Statistics.get_top_ten_improvable_states([exploration.id]) self.values.update({ 'imp': improvements, }) self.render_json(self.values)