def get_exploration_stats_from_model(exploration_stats_model): """Gets an ExplorationStats domain object from an ExplorationStatsModel instance. Args: exploration_stats_model: ExplorationStatsModel. Exploration statistics model in datastore. Returns: ExplorationStats. The domain object for exploration statistics. """ new_state_stats_mapping = { state_name: stats_domain.StateStats.from_dict( exploration_stats_model.state_stats_mapping[state_name]) for state_name in exploration_stats_model.state_stats_mapping } return stats_domain.ExplorationStats( exploration_stats_model.exp_id, exploration_stats_model.exp_version, exploration_stats_model.num_starts_v1, exploration_stats_model.num_starts_v2, exploration_stats_model.num_actual_starts_v1, exploration_stats_model.num_actual_starts_v2, exploration_stats_model.num_completions_v1, exploration_stats_model.num_completions_v2, new_state_stats_mapping)
def mock_get_statistics(self, exp_id, unused_version): current_completions = { self.EXP_ID_1: stats_domain.ExplorationStats( self.EXP_ID_1, self.EXP_DEFAULT_VERSION, 5, 2, 0, 0, 0, 0, { 'state1': stats_domain.StateStats( 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0), 'state2': stats_domain.StateStats( 0, 0, 0, 0, 0, 0, 7, 1, 0, 0, 0), } ), self.EXP_ID_2: stats_domain.ExplorationStats( self.EXP_ID_2, self.EXP_DEFAULT_VERSION, 5, 2, 0, 0, 0, 0, { 'state1': stats_domain.StateStats( 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0), 'state2': stats_domain.StateStats( 0, 0, 0, 0, 0, 0, 7, 1, 0, 0, 0), } ), self.EXP_ID_3: stats_domain.ExplorationStats( self.EXP_ID_3, self.EXP_DEFAULT_VERSION, 0, 0, 0, 0, 0, 0, {}) } return current_completions[exp_id]
def test_deferred_tasks_handler_handles_tasks_correctly(self): exp_id = '15' self.login(self.VIEWER_EMAIL) self.signup(self.VIEWER_EMAIL, self.VIEWER_USERNAME) exp_services.load_demo(exp_id) exploration = exp_fetchers.get_exploration_by_id(exp_id) exp_version = exploration.version state_name = 'Home' state_stats_mapping = { state_name: stats_domain.StateStats.create_default() } exploration_stats = stats_domain.ExplorationStats( exp_id, exp_version, 0, 0, 0, 0, 0, 0, state_stats_mapping) stats_services.create_stats_model(exploration_stats) aggregated_stats = { 'num_starts': 1, 'num_actual_starts': 1, 'num_completions': 1, 'state_stats_mapping': { 'Home': { 'total_hit_count': 1, 'first_hit_count': 1, 'total_answers_count': 1, 'useful_feedback_count': 1, 'num_times_solution_viewed': 1, 'num_completions': 1 } } } self.post_json('/explorehandler/stats_events/%s' % (exp_id), { 'aggregated_stats': aggregated_stats, 'exp_version': exp_version }) self.assertEqual( self.count_jobs_in_taskqueue(taskqueue_services.QUEUE_NAME_STATS), 1) self.process_and_flush_pending_tasks() # Check that the models are updated. exploration_stats = stats_services.get_exploration_stats_by_id( exp_id, exp_version) self.assertEqual(exploration_stats.num_starts_v2, 1) self.assertEqual(exploration_stats.num_actual_starts_v2, 1) self.assertEqual(exploration_stats.num_completions_v2, 1) self.assertEqual( exploration_stats.state_stats_mapping[state_name]. total_hit_count_v2, 1)
def _get_exploration_stats_from_dict(self, exploration_stats_dict): state_stats_mapping = {} for state_name in exploration_stats_dict['state_stats_mapping']: state_stats_mapping[ state_name] = stats_domain.StateStats.from_dict( exploration_stats_dict['state_stats_mapping'][state_name]) return stats_domain.ExplorationStats( exploration_stats_dict['exp_id'], exploration_stats_dict['exp_version'], exploration_stats_dict['num_starts_v1'], exploration_stats_dict['num_starts_v2'], exploration_stats_dict['num_actual_starts_v1'], exploration_stats_dict['num_actual_starts_v2'], exploration_stats_dict['num_completions_v1'], exploration_stats_dict['num_completions_v2'], state_stats_mapping)