Beispiel #1
0
    def get_exploration_stats(cls, event_name, exploration_id):
        """Retrieves statistics for the given event name and exploration id."""

        if event_name == STATS_ENUMS.exploration_visited:
            event_key = 'e.%s' % exploration_id
            counter = Counter.get_by_id(event_key)
            if not counter:
                return 0
            return counter.value

        if event_name == STATS_ENUMS.exploration_completed:
            event_key = 'c.%s' % exploration_id
            counter = Counter.get_by_id(event_key)
            if not counter:
                return 0
            return counter.value

        if event_name == STATS_ENUMS.rule_hit:
            result = {}

            exploration = Exploration.get(exploration_id)
            for state_key in exploration.states:
                state = state_key.get()
                result[state.id] = {
                    'name': state.name,
                    'rules': {}
                }
                for handler in state.widget.handlers:
                    for rule in handler.rules:
                        rule_name = create_rule_name(rule)
                        event_key = 'default.%s.%s.%s' % (exploration_id, state.id, rule_name)

                        journal = Journal.get_by_id(event_key)

                        if journal:
                            top_ten = collections.Counter(
                                journal.values).most_common(10)
                        else:
                            top_ten = []

                        result[state.id]['rules'][rule_name] = {
                            'answers': top_ten,
                        }

            return result

        if event_name == STATS_ENUMS.state_hit:
            result = {}

            exploration = Exploration.get(exploration_id)
            for state_key in exploration.states:
                state = state_key.get()
                event_key = 's.%s.%s' % (exploration_id, state.id)

                counter = Counter.get_by_id(event_key)
                if not counter:
                    count = 0
                else:
                    count = counter.value

                result[state.id] = {
                    'name': state.name,
                    'count': count,
                }
            return result
Beispiel #2
0
    def test_exploration_class(self):
        """Test the Exploration class."""
        exploration = Exploration(id='The exploration hash id')

        # A new exploration should have a default title property.
        self.assertEqual(exploration.title, 'New exploration')

        # A new exploration should have a default is_public property.
        self.assertEqual(exploration.is_public, False)

        # An Exploration must have properties 'category' and 'init_state' set.
        with self.assertRaises(BadValueError):
            exploration.put()
        exploration.category = 'The category'
        with self.assertRaises(BadValueError):
            exploration.put()

        # The 'init_state' property must be a valid State.
        with self.assertRaises(BadValueError):
            exploration.init_state = 'The State'
        state = State(id='The state hash id')
        state.put()
        exploration.init_state = state.key

        # The 'states' property must be a list.
        with self.assertRaises(BadValueError):
            exploration.states = 'A string'
        # TODO(emersoj): We should put the initial state in the states list it should not be empty
        exploration.states = []

        # The 'states property must be a list of State keys.
        with self.assertRaises(BadValueError):
            exploration.states = ['A string']
        with self.assertRaises(BadValueError):
            exploration.states = [state]
        exploration.states = [state.key]

        # The 'parameters' property must be a list of Parameter objects.
        with self.assertRaises(BadValueError):
            exploration.parameters = 'A string'
        exploration.parameters = []
        parameter = Parameter(name='theParameter', obj_type='Int')
        with self.assertRaises(BadValueError):
            exploration.parameters = [parameter.key]
        exploration.parameters = [parameter]

        # The 'is_public' property must be a boolean.
        with self.assertRaises(BadValueError):
            exploration.is_public = 'true'
        exploration.is_public = True

        # The 'image_id' property must be a string.
        image = Image(id='The image')
        with self.assertRaises(BadValueError):
            exploration.image_id = image
        with self.assertRaises(BadValueError):
            exploration.image_id = image.key
        exploration.image_id = 'A string'

        # The 'editors' property must be a list of User objects.
        with self.assertRaises(BadValueError):
            exploration.editors = 'A string'
        exploration.editors = []
        with self.assertRaises(BadValueError):
            exploration.editors = ['A string']
        user = User(email='*****@*****.**')
        exploration.editors = [user]

        # Put and Retrieve the exploration.
        exploration.put()
        retrieved_exploration = Exploration.get_by_id('The exploration hash id')
        self.assertEqual(retrieved_exploration.category, 'The category')
        self.assertEqual(retrieved_exploration.init_state, state.key)
        self.assertEqual(retrieved_exploration.title, 'New exploration')
        self.assertEqual(retrieved_exploration.states, [state.key])
        self.assertEqual(retrieved_exploration.parameters, [parameter])
        self.assertEqual(retrieved_exploration.is_public, True)
        self.assertEqual(retrieved_exploration.image_id, 'A string')
        self.assertEqual(retrieved_exploration.editors, [user])

        # The Exploration class has a 'create' class method.
        exploration2 = Exploration.create(user, 'A title', 'A category', 'A exploration_id')
        exploration2.put()

        # The Exploration class has a 'get' class method.
        retrieved_exploration2 = Exploration.get('A exploration_id')
        self.assertEqual(exploration2, retrieved_exploration2)

        # An Exploration has a 'delete' method.
        exploration2.delete()
        with self.assertRaises(Exception):
            retrieved_exploration2 = Exploration.get('A exploration_id')
        # The get() should fail silently when strict == False.
        retrieved_exploration2 = Exploration.get(
            'A exploration_id', strict=False)
        self.assertIsNone(retrieved_exploration2)

        # An Exploration has a 'is_demo_exploration' method.
        demo = Exploration(id='0')
        self.assertEqual(demo.is_demo_exploration(), True)
        notdemo1 = Exploration(id='a')
        self.assertEqual(notdemo1.is_demo_exploration(), False)
        notdemo2 = Exploration(id='abcd')
        self.assertEqual(notdemo2.is_demo_exploration(), False)

        # Ad Exploration has a 'as_yaml' method.
        exploration3 = Exploration.create(user, 'A title', 'A category', 'A different exploration_id')
        self.assertEqual(exploration3.as_yaml(), """Activity 1:
  content: []
  param_changes: []
  widget:
    handlers:
    - name: submit
      rules:
      - dest: Activity 1
        feedback: []
        inputs: {}
        name: Default
        param_changes: []
    params: {}
    sticky: false
    widget_id: Continue
""")