def test_basic_computation(self):
        recommendations_services.update_topic_similarities(
            'Art,Biology,Chemistry\n'
            '1.0,0.2,0.1\n'
            '0.2,1.0,0.8\n'
            '0.1,0.8,1.0')

        with self.swap(
                jobs_registry, 'ALL_CONTINUOUS_COMPUTATION_MANAGERS',
                self.ALL_CONTINUOUS_COMPUTATION_MANAGERS_FOR_TESTS):
            ModifiedExplorationRecommendationsAggregator.start_computation()
            self.assertEqual(
                self.count_jobs_in_taskqueue(
                    queue_name=taskqueue_services.QUEUE_NAME_DEFAULT),
                1)
            self.process_and_flush_pending_tasks()

            recommendations = (
                recommendations_services.get_exploration_recommendations(
                    'exp_id_1'))
            self.assertEqual(
                recommendations, ['exp_id_4', 'exp_id_2', 'exp_id_3'])
            recommendations = (
                recommendations_services.get_exploration_recommendations(
                    'exp_id_4'))
            self.assertEqual(
                recommendations, ['exp_id_1', 'exp_id_2', 'exp_id_3'])
예제 #2
0
    def test_basic_computation(self):
        recommendations_services.update_topic_similarities(
            'Art,Biology,Chemistry\n'
            '1.0,0.2,0.1\n'
            '0.2,1.0,0.8\n'
            '0.1,0.8,1.0')

        with self.swap(
            jobs_registry, 'ALL_CONTINUOUS_COMPUTATION_MANAGERS',
            self.ALL_CC_MANAGERS_FOR_TESTS
            ):
            ModifiedExplorationRecommendationsAggregator.start_computation()
            self.assertEqual(
                self.count_jobs_in_taskqueue(
                    queue_name=taskqueue_services.QUEUE_NAME_DEFAULT),
                1)
            self.process_and_flush_pending_tasks()

            recommendations = (
                recommendations_services.get_exploration_recommendations(
                    'exp_id_1'))
            self.assertEqual(
                recommendations, ['exp_id_4', 'exp_id_2', 'exp_id_3'])
            recommendations = (
                recommendations_services.get_exploration_recommendations(
                    'exp_id_4'))
            self.assertEqual(
                recommendations, ['exp_id_1', 'exp_id_2', 'exp_id_3'])
    def test_basic_computation(self):
        recommendations_services.update_topic_similarities(
            'Art,Biology,Chemistry\n'
            '1.0,0.2,0.1\n'
            '0.2,1.0,0.8\n'
            '0.1,0.8,1.0')

        with self.swap(
            jobs_registry, 'ONE_OFF_JOB_MANAGERS',
            self.ONE_OFF_JOB_MANAGERS_FOR_TESTS
            ):
            self.job_class.enqueue(self.job_class.create_new())
            self.assertEqual(
                self.count_jobs_in_taskqueue(
                    queue_name=taskqueue_services.QUEUE_NAME_DEFAULT),
                1)
            self.process_and_flush_pending_tasks()

            recommendations = (
                recommendations_services.get_exploration_recommendations(
                    'exp_id_1'))
            self.assertEqual(
                recommendations, ['exp_id_4', 'exp_id_2', 'exp_id_3'])
            recommendations = (
                recommendations_services.get_exploration_recommendations(
                    'exp_id_4'))
            self.assertEqual(
                recommendations, ['exp_id_1', 'exp_id_2', 'exp_id_3'])
예제 #4
0
    def test_basic_computation(self):
        recommendations_services.update_topic_similarities(
            'Art,Biology,Chemistry\n'
            '1.0,0.2,0.1\n'
            '0.2,1.0,0.8\n'
            '0.1,0.8,1.0')

        with self.swap(
            jobs_registry, 'ONE_OFF_JOB_MANAGERS',
            self.ONE_OFF_JOB_MANAGERS_FOR_TESTS
            ):
            self.job_class.enqueue(self.job_class.create_new())
            self.assertEqual(
                self.count_jobs_in_mapreduce_taskqueue(
                    taskqueue_services.QUEUE_NAME_ONE_OFF_JOBS), 1)
            self.process_and_flush_pending_mapreduce_tasks()

            recommendations = (
                recommendations_services.get_exploration_recommendations(
                    'exp_id_1'))
            self.assertEqual(
                recommendations, ['exp_id_4', 'exp_id_2', 'exp_id_3'])
            recommendations = (
                recommendations_services.get_exploration_recommendations(
                    'exp_id_4'))
            self.assertEqual(
                recommendations, ['exp_id_1', 'exp_id_2', 'exp_id_3'])
예제 #5
0
    def post(self):
        """Handles POST requests."""
        try:
            if self.payload.get('action') == 'reload_exploration':
                exploration_id = self.payload.get('exploration_id')
                self._reload_exploration(exploration_id)
            elif self.payload.get('action') == 'reload_collection':
                collection_id = self.payload.get('collection_id')
                self._reload_collection(collection_id)
            elif self.payload.get('action') == 'clear_search_index':
                exp_services.clear_search_index()
            elif self.payload.get('action') == 'save_config_properties':
                new_config_property_values = self.payload.get(
                    'new_config_property_values')
                logging.info('[ADMIN] %s saved config property values: %s' %
                             (self.user_id, new_config_property_values))
                for (name, value) in new_config_property_values.iteritems():
                    config_services.set_property(self.user_id, name, value)
            elif self.payload.get('action') == 'revert_config_property':
                config_property_id = self.payload.get('config_property_id')
                logging.info('[ADMIN] %s reverted config property: %s' %
                             (self.user_id, config_property_id))
                config_services.revert_property(self.user_id,
                                                config_property_id)
            elif self.payload.get('action') == 'start_new_job':
                for klass in jobs_registry.ONE_OFF_JOB_MANAGERS:
                    if klass.__name__ == self.payload.get('job_type'):
                        klass.enqueue(klass.create_new())
                        break
            elif self.payload.get('action') == 'cancel_job':
                job_id = self.payload.get('job_id')
                job_type = self.payload.get('job_type')
                for klass in jobs_registry.ONE_OFF_JOB_MANAGERS:
                    if klass.__name__ == job_type:
                        klass.cancel(job_id, self.user_id)
                        break
            elif self.payload.get('action') == 'start_computation':
                computation_type = self.payload.get('computation_type')
                for klass in jobs_registry.ALL_CONTINUOUS_COMPUTATION_MANAGERS:
                    if klass.__name__ == computation_type:
                        klass.start_computation()
                        break
            elif self.payload.get('action') == 'stop_computation':
                computation_type = self.payload.get('computation_type')
                for klass in jobs_registry.ALL_CONTINUOUS_COMPUTATION_MANAGERS:
                    if klass.__name__ == computation_type:
                        klass.stop_computation(self.user_id)
                        break
            elif self.payload.get('action') == 'upload_topic_similarities':
                data = self.payload.get('data')
                recommendations_services.update_topic_similarities(data)

            self.render_json({})
        except Exception as e:
            self.render_json({'error': unicode(e)})
            raise
예제 #6
0
파일: admin.py 프로젝트: DSeanLaw/oppia
    def post(self):
        """Handles POST requests."""
        try:
            if self.payload.get('action') == 'reload_exploration':
                exploration_id = self.payload.get('exploration_id')
                self._reload_exploration(exploration_id)
            elif self.payload.get('action') == 'reload_collection':
                collection_id = self.payload.get('collection_id')
                self._reload_collection(collection_id)
            elif self.payload.get('action') == 'clear_search_index':
                exp_services.clear_search_index()
            elif self.payload.get('action') == 'save_config_properties':
                new_config_property_values = self.payload.get(
                    'new_config_property_values')
                logging.info('[ADMIN] %s saved config property values: %s' %
                             (self.user_id, new_config_property_values))
                for (name, value) in new_config_property_values.iteritems():
                    config_services.set_property(self.user_id, name, value)
            elif self.payload.get('action') == 'revert_config_property':
                config_property_id = self.payload.get('config_property_id')
                logging.info('[ADMIN] %s reverted config property: %s' %
                             (self.user_id, config_property_id))
                config_services.revert_property(
                    self.user_id, config_property_id)
            elif self.payload.get('action') == 'start_new_job':
                for klass in jobs_registry.ONE_OFF_JOB_MANAGERS:
                    if klass.__name__ == self.payload.get('job_type'):
                        klass.enqueue(klass.create_new())
                        break
            elif self.payload.get('action') == 'cancel_job':
                job_id = self.payload.get('job_id')
                job_type = self.payload.get('job_type')
                for klass in jobs_registry.ONE_OFF_JOB_MANAGERS:
                    if klass.__name__ == job_type:
                        klass.cancel(job_id, self.user_id)
                        break
            elif self.payload.get('action') == 'start_computation':
                computation_type = self.payload.get('computation_type')
                for klass in jobs_registry.ALL_CONTINUOUS_COMPUTATION_MANAGERS:
                    if klass.__name__ == computation_type:
                        klass.start_computation()
                        break
            elif self.payload.get('action') == 'stop_computation':
                computation_type = self.payload.get('computation_type')
                for klass in jobs_registry.ALL_CONTINUOUS_COMPUTATION_MANAGERS:
                    if klass.__name__ == computation_type:
                        klass.stop_computation(self.user_id)
                        break
            elif self.payload.get('action') == 'upload_topic_similarities':
                data = self.payload.get('data')
                recommendations_services.update_topic_similarities(data)

            self.render_json({})
        except Exception as e:
            self.render_json({'error': unicode(e)})
            raise
    def test_get_topic_similarity(self):
        self.assertEqual(recommendations_services.get_topic_similarity("Art", "Biology"), 0.1)
        self.assertEqual(recommendations_services.get_topic_similarity("Art", "Art"), feconf.SAME_TOPIC_SIMILARITY)
        self.assertEqual(
            recommendations_services.get_topic_similarity("Topic 1", "Topic 2"), feconf.DEFAULT_TOPIC_SIMILARITY
        )
        self.assertEqual(recommendations_services.get_topic_similarity("Topic", "Topic"), feconf.SAME_TOPIC_SIMILARITY)

        recommendations_services.update_topic_similarities(
            "Art,Biology,Chemistry\n" "1.0,0.2,0.1\n" "0.2,1.0,0.8\n" "0.1,0.8,1.0"
        )
        self.assertEqual(recommendations_services.get_topic_similarity("Art", "Biology"), 0.2)
    def test_get_topic_similarities_as_csv(self):
        # The splitlines() is needed because a carriage return is added in
        # the returned string
        self.assertEqual(
            recommendations_services.get_topic_similarities_as_csv().splitlines(),
            self.TOPIC_SIMILARITIES_DEFAULT.splitlines())

        recommendations_services.update_topic_similarities(
            'Art,Biology,Chemistry\n'
            '1.0,0.2,0.1\n'
            '0.2,1.0,0.8\n'
            '0.1,0.8,1.0')
        self.assertEqual(
            recommendations_services.get_topic_similarities_as_csv().splitlines(),
            self.TOPIC_SIMILARITIES_UPDATED.splitlines())
예제 #9
0
    def test_get_topic_similarities_as_csv(self):
        # The splitlines() is needed because a carriage return is added in
        # the returned string
        self.assertEqual(
            recommendations_services.get_topic_similarities_as_csv().
            splitlines(), self.TOPIC_SIMILARITIES_DEFAULT.splitlines())

        recommendations_services.update_topic_similarities(
            'Art,Biology,Chemistry\n'
            '1.0,0.2,0.1\n'
            '0.2,1.0,0.8\n'
            '0.1,0.8,1.0')
        self.assertEqual(
            recommendations_services.get_topic_similarities_as_csv().
            splitlines(), self.TOPIC_SIMILARITIES_UPDATED.splitlines())
    def test_get_topic_similarity(self):
        self.assertEqual(recommendations_services.get_topic_similarity(
            'Art', 'Biology'), 0.1)
        self.assertEqual(recommendations_services.get_topic_similarity(
            'Art', 'Art'), feconf.SAME_TOPIC_SIMILARITY)
        self.assertEqual(recommendations_services.get_topic_similarity(
            'Topic 1', 'Topic 2'), feconf.DEFAULT_TOPIC_SIMILARITY)
        self.assertEqual(recommendations_services.get_topic_similarity(
            'Topic', 'Topic'), feconf.SAME_TOPIC_SIMILARITY)

        recommendations_services.update_topic_similarities(
            'Art,Biology,Chemistry\n'
            '1.0,0.2,0.1\n'
            '0.2,1.0,0.8\n'
            '0.1,0.8,1.0')
        self.assertEqual(recommendations_services.get_topic_similarity(
            'Art', 'Biology'), 0.2)
    def test_get_topic_similarity(self):
        self.assertEqual(recommendations_services.get_topic_similarity(
            'Art', 'Biology'), 0.1)
        self.assertEqual(recommendations_services.get_topic_similarity(
            'Art', 'Art'), feconf.SAME_TOPIC_SIMILARITY)
        self.assertEqual(recommendations_services.get_topic_similarity(
            'Topic 1', 'Topic 2'), feconf.DEFAULT_TOPIC_SIMILARITY)
        self.assertEqual(recommendations_services.get_topic_similarity(
            'Topic', 'Topic'), feconf.SAME_TOPIC_SIMILARITY)

        recommendations_services.update_topic_similarities(
            'Art,Biology,Chemistry\n'
            '1.0,0.2,0.1\n'
            '0.2,1.0,0.8\n'
            '0.1,0.8,1.0')
        self.assertEqual(recommendations_services.get_topic_similarity(
            'Art', 'Biology'), 0.2)
예제 #12
0
파일: admin.py 프로젝트: tyleri/oppia
 def post(self):
     """Handles POST requests."""
     try:
         result = {}
         if self.payload.get('action') == 'reload_exploration':
             exploration_id = self.payload.get('exploration_id')
             self._reload_exploration(exploration_id)
         elif self.payload.get('action') == 'reload_collection':
             collection_id = self.payload.get('collection_id')
             self._reload_collection(collection_id)
         elif self.payload.get('action') == 'generate_dummy_explorations':
             num_dummy_exps_to_generate = self.payload.get(
                 'num_dummy_exps_to_generate')
             num_dummy_exps_to_publish = self.payload.get(
                 'num_dummy_exps_to_publish')
             if not isinstance(num_dummy_exps_to_generate, int):
                 raise self.InvalidInputException(
                     '%s is not a number' % num_dummy_exps_to_generate)
             elif not isinstance(num_dummy_exps_to_publish, int):
                 raise self.InvalidInputException('%s is not a number' %
                                                  num_dummy_exps_to_publish)
             elif num_dummy_exps_to_generate < num_dummy_exps_to_publish:
                 raise self.InvalidInputException(
                     'Generate count cannot be less than publish count')
             else:
                 self._generate_dummy_explorations(
                     num_dummy_exps_to_generate, num_dummy_exps_to_publish)
         elif self.payload.get('action') == 'clear_search_index':
             search_services.clear_collection_search_index()
             search_services.clear_exploration_search_index()
         elif (self.payload.get('action') ==
               'generate_dummy_new_structures_data'):
             self._load_dummy_new_structures_data()
         elif self.payload.get('action') == (
                 'flush_migration_bot_contribution_data'):
             user_services.flush_migration_bot_contributions_model()
         elif self.payload.get('action') == 'save_config_properties':
             new_config_property_values = self.payload.get(
                 'new_config_property_values')
             logging.info('[ADMIN] %s saved config property values: %s' %
                          (self.user_id, new_config_property_values))
             for (name, value) in new_config_property_values.items():
                 config_services.set_property(self.user_id, name, value)
         elif self.payload.get('action') == 'revert_config_property':
             config_property_id = self.payload.get('config_property_id')
             logging.info('[ADMIN] %s reverted config property: %s' %
                          (self.user_id, config_property_id))
             config_services.revert_property(self.user_id,
                                             config_property_id)
         elif self.payload.get('action') == 'start_new_job':
             for klass in (jobs_registry.ONE_OFF_JOB_MANAGERS +
                           (jobs_registry.AUDIT_JOB_MANAGERS)):
                 if klass.__name__ == self.payload.get('job_type'):
                     klass.enqueue(klass.create_new())
                     break
         elif self.payload.get('action') == 'cancel_job':
             job_id = self.payload.get('job_id')
             job_type = self.payload.get('job_type')
             for klass in (jobs_registry.ONE_OFF_JOB_MANAGERS +
                           (jobs_registry.AUDIT_JOB_MANAGERS)):
                 if klass.__name__ == job_type:
                     klass.cancel(job_id, self.user_id)
                     break
         elif self.payload.get('action') == 'start_computation':
             computation_type = self.payload.get('computation_type')
             for klass in jobs_registry.ALL_CONTINUOUS_COMPUTATION_MANAGERS:
                 if klass.__name__ == computation_type:
                     klass.start_computation()
                     break
         elif self.payload.get('action') == 'stop_computation':
             computation_type = self.payload.get('computation_type')
             for klass in jobs_registry.ALL_CONTINUOUS_COMPUTATION_MANAGERS:
                 if klass.__name__ == computation_type:
                     klass.stop_computation(self.user_id)
                     break
         elif self.payload.get('action') == 'upload_topic_similarities':
             data = self.payload.get('data')
             recommendations_services.update_topic_similarities(data)
         elif self.payload.get('action') == (
                 'regenerate_topic_related_opportunities'):
             topic_id = self.payload.get('topic_id')
             opportunities_count = (
                 opportunity_services.
                 regenerate_opportunities_related_to_topic(
                     topic_id, delete_existing_opportunities=True))
             result = {'opportunities_count': opportunities_count}
         self.render_json(result)
     except Exception as e:
         self.render_json({'error': python_utils.UNICODE(e)})
         raise
예제 #13
0
파일: admin.py 프로젝트: atpug22/oppia
    def post(self):
        """Handles POST requests."""
        action = self.normalized_payload.get('action')
        try:
            result = {}
            if action == 'reload_exploration':
                exploration_id = self.normalized_payload.get('exploration_id')
                self._reload_exploration(exploration_id)
            elif action == 'reload_collection':
                collection_id = self.normalized_payload.get('collection_id')
                self._reload_collection(collection_id)
            elif action == 'generate_dummy_explorations':
                num_dummy_exps_to_generate = self.normalized_payload.get(
                    'num_dummy_exps_to_generate')
                num_dummy_exps_to_publish = self.normalized_payload.get(
                    'num_dummy_exps_to_publish')

                if num_dummy_exps_to_generate < num_dummy_exps_to_publish:
                    raise self.InvalidInputException(
                        'Generate count cannot be less than publish count')
                else:
                    self._generate_dummy_explorations(
                        num_dummy_exps_to_generate, num_dummy_exps_to_publish)
            elif action == 'clear_search_index':
                search_services.clear_collection_search_index()
                search_services.clear_exploration_search_index()
            elif action == 'generate_dummy_new_structures_data':
                self._load_dummy_new_structures_data()
            elif action == 'generate_dummy_new_skill_data':
                self._generate_dummy_skill_and_questions()
            elif action == 'save_config_properties':
                new_config_property_values = self.normalized_payload.get(
                    'new_config_property_values')
                logging.info('[ADMIN] %s saved config property values: %s' %
                             (self.user_id, new_config_property_values))
                for (name, value) in new_config_property_values.items():
                    config_services.set_property(self.user_id, name, value)
            elif action == 'revert_config_property':
                config_property_id = self.normalized_payload.get(
                    'config_property_id')
                logging.info('[ADMIN] %s reverted config property: %s' %
                             (self.user_id, config_property_id))
                config_services.revert_property(self.user_id,
                                                config_property_id)
            elif action == 'upload_topic_similarities':
                data = self.normalized_payload.get('data')
                recommendations_services.update_topic_similarities(data)
            elif action == 'regenerate_topic_related_opportunities':
                topic_id = self.normalized_payload.get('topic_id')
                opportunities_count = (
                    opportunity_services.
                    regenerate_opportunities_related_to_topic(
                        topic_id, delete_existing_opportunities=True))
                result = {'opportunities_count': opportunities_count}
            elif action == 'update_feature_flag_rules':
                feature_name = self.normalized_payload.get('feature_name')
                new_rule_dicts = self.normalized_payload.get('new_rules')
                commit_message = self.normalized_payload.get('commit_message')

                try:
                    feature_services.update_feature_flag_rules(
                        feature_name, self.user_id, commit_message,
                        new_rule_dicts)
                except (utils.ValidationError,
                        feature_services.FeatureFlagNotFoundException) as e:
                    raise self.InvalidInputException(e)
                logging.info('[ADMIN] %s updated feature %s with new rules: '
                             '%s.' %
                             (self.user_id, feature_name, new_rule_dicts))
            self.render_json(result)
        except Exception as e:
            logging.exception('[ADMIN] %s', e)
            self.render_json({'error': python_utils.UNICODE(e)})
            python_utils.reraise_exception()
    def test_update_topic_similarities(self):
        recommendations_services.update_topic_similarities(
            'Art,Biology,Chemistry\n'
            '1.0,0.2,0.1\n'
            '0.2,1.0,0.8\n'
            '0.1,0.8,1.0')

        with self.assertRaisesRegexp(
                Exception,
            ('Length of topic similarities columns: 2 does not match '
             'length of topic list: 3.')):
            recommendations_services.update_topic_similarities(
                'Art,Biology,Chemistry\n'
                '1.0,0.2,0.1\n'
                '0.2,1.0,0.8')

        with self.assertRaisesRegexp(
                Exception,
            ('Length of topic similarities rows: 2 does not match '
             'length of topic list: 3.')):
            recommendations_services.update_topic_similarities(
                'Art,Biology,Chemistry\n'
                '1.0,0.2,0.1\n'
                '0.2,1.0\n'
                '0.1,0.8,1.0')

        with self.assertRaisesRegexp(
                ValueError,
                'Expected similarity to be between 0.0 and 1.0, received 800'):
            recommendations_services.update_topic_similarities(
                'Art,Biology,Chemistry\n'
                '1.0,0.2,0.1\n'
                '0.2,1.0,800\n'
                '0.1,0.8,1.0')

        with self.assertRaisesRegexp(
                ValueError,
                'Expected similarity to be a float, received string'):
            recommendations_services.update_topic_similarities(
                'Art,Biology,Chemistry\n'
                'string,0.2,0.1\n'
                '0.2,1.0,0.8\n'
                '0.1,0.8,1.0')

        with self.assertRaisesRegexp(
                Exception, 'Topic Fake Topic not in list of known topics.'):
            recommendations_services.update_topic_similarities(
                'Fake Topic,Biology,Chemistry\n'
                'string,0.2,0.1\n'
                '0.2,1.0,0.8\n'
                '0.1,0.8,1.0')

        with self.assertRaisesRegexp(
                Exception, 'Expected topic similarities to be symmetric.'):
            recommendations_services.update_topic_similarities(
                'Art,Biology,Chemistry\n'
                '1.0,0.2,0.1\n'
                '0.3,1.0,0.8\n'
                '0.8,0.1,1.0')
    def test_update_topic_similarities(self):
        recommendations_services.update_topic_similarities(
            'Art,Biology,Chemistry\n'
            '1.0,0.2,0.1\n'
            '0.2,1.0,0.8\n'
            '0.1,0.8,1.0')

        with self.assertRaisesRegexp(
            Exception,
            'Length of topic similarities columns does not match topic list.'
            ):
            recommendations_services.update_topic_similarities(
                'Art,Biology,Chemistry\n'
                '1.0,0.2,0.1\n'
                '0.2,1.0,0.8')

        with self.assertRaisesRegexp(
            Exception,
            'Length of topic similarities rows does not match topic list.'
            ):
            recommendations_services.update_topic_similarities(
                'Art,Biology,Chemistry\n'
                '1.0,0.2,0.1\n'
                '0.2,1.0\n'
                '0.1,0.8,1.0')

        with self.assertRaisesRegexp(
            ValueError,
            'Expected similarity to be between 0.0 and 1.0, received 800'
            ):
            recommendations_services.update_topic_similarities(
                'Art,Biology,Chemistry\n'
                '1.0,0.2,0.1\n'
                '0.2,1.0,800\n'
                '0.1,0.8,1.0')

        with self.assertRaisesRegexp(
            ValueError,
            'Expected similarity to be a float, received string'
            ):
            recommendations_services.update_topic_similarities(
                'Art,Biology,Chemistry\n'
                'string,0.2,0.1\n'
                '0.2,1.0,0.8\n'
                '0.1,0.8,1.0')

        with self.assertRaisesRegexp(
            Exception, 'Topic Fake Topic not in list of known topics.'
            ):
            recommendations_services.update_topic_similarities(
                'Fake Topic,Biology,Chemistry\n'
                'string,0.2,0.1\n'
                '0.2,1.0,0.8\n'
                '0.1,0.8,1.0')

        with self.assertRaisesRegexp(
            Exception, 'Expected topic similarities to be symmetric.'
            ):
            recommendations_services.update_topic_similarities(
                'Art,Biology,Chemistry\n'
                '1.0,0.2,0.1\n'
                '0.3,1.0,0.8\n'
                '0.8,0.1,1.0')
예제 #16
0
파일: admin.py 프로젝트: testMrinal/oppia
 def post(self):
     """Handles POST requests."""
     try:
         result = {}
         if self.payload.get('action') == 'reload_exploration':
             exploration_id = self.payload.get('exploration_id')
             self._reload_exploration(exploration_id)
         elif self.payload.get('action') == 'reload_collection':
             collection_id = self.payload.get('collection_id')
             self._reload_collection(collection_id)
         elif self.payload.get('action') == 'generate_dummy_explorations':
             num_dummy_exps_to_generate = self.payload.get(
                 'num_dummy_exps_to_generate')
             num_dummy_exps_to_publish = self.payload.get(
                 'num_dummy_exps_to_publish')
             if not isinstance(num_dummy_exps_to_generate, int):
                 raise self.InvalidInputException(
                     '%s is not a number' % num_dummy_exps_to_generate)
             elif not isinstance(num_dummy_exps_to_publish, int):
                 raise self.InvalidInputException('%s is not a number' %
                                                  num_dummy_exps_to_publish)
             elif num_dummy_exps_to_generate < num_dummy_exps_to_publish:
                 raise self.InvalidInputException(
                     'Generate count cannot be less than publish count')
             else:
                 self._generate_dummy_explorations(
                     num_dummy_exps_to_generate, num_dummy_exps_to_publish)
         elif self.payload.get('action') == 'clear_search_index':
             search_services.clear_collection_search_index()
             search_services.clear_exploration_search_index()
         elif (self.payload.get('action') ==
               'generate_dummy_new_structures_data'):
             self._load_dummy_new_structures_data()
         elif (self.payload.get('action') == 'generate_dummy_new_skill_data'
               ):
             self._generate_dummy_skill_and_questions()
         elif self.payload.get('action') == 'save_config_properties':
             new_config_property_values = self.payload.get(
                 'new_config_property_values')
             logging.info('[ADMIN] %s saved config property values: %s' %
                          (self.user_id, new_config_property_values))
             for (name, value) in new_config_property_values.items():
                 config_services.set_property(self.user_id, name, value)
         elif self.payload.get('action') == 'revert_config_property':
             config_property_id = self.payload.get('config_property_id')
             logging.info('[ADMIN] %s reverted config property: %s' %
                          (self.user_id, config_property_id))
             config_services.revert_property(self.user_id,
                                             config_property_id)
         elif self.payload.get('action') == 'start_new_job':
             for klass in (jobs_registry.ONE_OFF_JOB_MANAGERS +
                           (jobs_registry.AUDIT_JOB_MANAGERS)):
                 if klass.__name__ == self.payload.get('job_type'):
                     klass.enqueue(klass.create_new())
                     break
         elif self.payload.get('action') == 'cancel_job':
             job_id = self.payload.get('job_id')
             job_type = self.payload.get('job_type')
             for klass in (jobs_registry.ONE_OFF_JOB_MANAGERS +
                           (jobs_registry.AUDIT_JOB_MANAGERS)):
                 if klass.__name__ == job_type:
                     klass.cancel(job_id, self.user_id)
                     break
         elif self.payload.get('action') == 'start_computation':
             computation_type = self.payload.get('computation_type')
             for klass in jobs_registry.ALL_CONTINUOUS_COMPUTATION_MANAGERS:
                 if klass.__name__ == computation_type:
                     klass.start_computation()
                     break
         elif self.payload.get('action') == 'stop_computation':
             computation_type = self.payload.get('computation_type')
             for klass in jobs_registry.ALL_CONTINUOUS_COMPUTATION_MANAGERS:
                 if klass.__name__ == computation_type:
                     klass.stop_computation(self.user_id)
                     break
         elif self.payload.get('action') == 'upload_topic_similarities':
             data = self.payload.get('data')
             recommendations_services.update_topic_similarities(data)
         elif self.payload.get('action') == (
                 'regenerate_topic_related_opportunities'):
             topic_id = self.payload.get('topic_id')
             opportunities_count = (
                 opportunity_services.
                 regenerate_opportunities_related_to_topic(
                     topic_id, delete_existing_opportunities=True))
             result = {'opportunities_count': opportunities_count}
         elif self.payload.get('action') == 'update_feature_flag_rules':
             feature_name = self.payload.get('feature_name')
             new_rule_dicts = self.payload.get('new_rules')
             commit_message = self.payload.get('commit_message')
             if not isinstance(feature_name, python_utils.BASESTRING):
                 raise self.InvalidInputException(
                     'feature_name should be string, received \'%s\'.' %
                     (feature_name))
             elif not isinstance(commit_message, python_utils.BASESTRING):
                 raise self.InvalidInputException(
                     'commit_message should be string, received \'%s\'.' %
                     (commit_message))
             elif (not isinstance(new_rule_dicts, list) or not all([
                     isinstance(rule_dict, dict)
                     for rule_dict in new_rule_dicts
             ])):
                 raise self.InvalidInputException(
                     'new_rules should be a list of dicts, received'
                     ' \'%s\'.' % new_rule_dicts)
             try:
                 feature_services.update_feature_flag_rules(
                     feature_name, self.user_id, commit_message,
                     new_rule_dicts)
             except (utils.ValidationError,
                     feature_services.FeatureFlagNotFoundException) as e:
                 raise self.InvalidInputException(e)
             logging.info('[ADMIN] %s updated feature %s with new rules: '
                          '%s.' %
                          (self.user_id, feature_name, new_rule_dicts))
         self.render_json(result)
     except Exception as e:
         logging.error('[ADMIN] %s', e)
         self.render_json({'error': python_utils.UNICODE(e)})
         python_utils.reraise_exception()
예제 #17
0
    def test_update_topic_similarities(self):
        recommendations_services.update_topic_similarities(
            "Art,Biology,Chemistry\n" "1.0,0.2,0.1\n" "0.2,1.0,0.8\n" "0.1,0.8,1.0"
        )

        with self.assertRaisesRegexp(Exception, "Length of topic similarities columns does not" " match topic list."):
            recommendations_services.update_topic_similarities("Art,Biology,Chemistry\n" "1.0,0.2,0.1\n" "0.2,1.0,0.8")

        with self.assertRaisesRegexp(Exception, "Length of topic similarities rows does not match" " topic list."):
            recommendations_services.update_topic_similarities(
                "Art,Biology,Chemistry\n" "1.0,0.2,0.1\n" "0.2,1.0\n" "0.1,0.8,1.0"
            )

        with self.assertRaisesRegexp(ValueError, "Expected similarity to be between 0.0 and 1.0," " received 800"):
            recommendations_services.update_topic_similarities(
                "Art,Biology,Chemistry\n" "1.0,0.2,0.1\n" "0.2,1.0,800\n" "0.1,0.8,1.0"
            )

        with self.assertRaisesRegexp(ValueError, "Expected similarity to be a float, received" " string"):
            recommendations_services.update_topic_similarities(
                "Art,Biology,Chemistry\n" "string,0.2,0.1\n" "0.2,1.0,0.8\n" "0.1,0.8,1.0"
            )

        with self.assertRaisesRegexp(Exception, "Topic Fake Topic not in list of known topics."):
            recommendations_services.update_topic_similarities(
                "Fake Topic,Biology,Chemistry\n" "string,0.2,0.1\n" "0.2,1.0,0.8\n" "0.1,0.8,1.0"
            )

        with self.assertRaisesRegexp(Exception, "Expected topic similarities to be symmetric."):
            recommendations_services.update_topic_similarities(
                "Art,Biology,Chemistry\n" "1.0,0.2,0.1\n" "0.3,1.0,0.8\n" "0.8,0.1,1.0"
            )