Пример #1
0
    def _update_user(self, cursor, userid, **kwargs):
        if "status" in kwargs:
            kwargs["status_timestamp"] = current_timestamp_in_seconds()
        if "connected_status" in kwargs and "connected_timestamp" not in kwargs:
            kwargs["connected_timestamp"] = current_timestamp_in_seconds()
        keys = sorted(kwargs.keys())
        values = [kwargs[k] for k in keys]
        set_string = ", ".join(["{}=?".format(k) for k in keys])

        cursor.execute(
            "UPDATE active_user SET {} WHERE name=?".format(set_string),
            tuple(values + [userid]))
Пример #2
0
    def get_chat_info(self, userid, peek=False):
        try:
            with self.conn:
                cursor = self.conn.cursor()
                u = self._get_user_info_unchecked(cursor, userid)
                num_seconds_remaining = (
                    self.config["status_params"]["chat"]["num_seconds"] +
                    u.status_timestamp) - current_timestamp_in_seconds()
                scenario = self.scenario_db.get(u.scenario_id)
                if peek:
                    return UserChatState(u.agent_index, scenario.uuid,
                                         u.chat_id,
                                         scenario.get_kb(u.agent_index),
                                         scenario.attributes,
                                         num_seconds_remaining,
                                         scenario.get_kb(1 - u.agent_index))
                else:
                    return UserChatState(u.agent_index, scenario.uuid,
                                         u.chat_id,
                                         scenario.get_kb(u.agent_index),
                                         scenario.attributes,
                                         num_seconds_remaining)

        except sqlite3.IntegrityError:
            print("WARNING: Rolled back transaction")
Пример #3
0
 def create_user_if_not_exists(self, username):
     with self.conn:
         cursor = self.conn.cursor()
         now = current_timestamp_in_seconds()
         cursor.execute(
             '''INSERT OR IGNORE INTO active_user VALUES (?,?,?,?,?,?,?,?,?,?,?,?)''',
             (username, Status.Waiting, now, 0, now, "", "", "", "", -1, -1,
              ""))
Пример #4
0
    def get_waiting_info(self, userid):
        try:
            with self.conn:
                cursor = self.conn.cursor()
                u = self._get_user_info(cursor,
                                        userid,
                                        assumed_status=Status.Waiting)
                num_seconds = (
                    self.config["status_params"]["waiting"]["num_seconds"] +
                    u.status_timestamp) - current_timestamp_in_seconds()
                return WaitingState(u.message, num_seconds)

        except sqlite3.IntegrityError:
            print("WARNING: Rolled back transaction")
Пример #5
0
    def get_finished_info(self,
                          userid,
                          from_mturk=False,
                          current_status=Status.Finished,
                          hit_id=None,
                          assignment_id=None):
        def _generate_mturk_code(completed=True):
            if completed:
                return "MTURK_TASK_C{}".format(str(uuid4().hex))
            return "MTURK_TASK_I{}".format(str(uuid4().hex))

        def _add_finished_task_row(cursor, userid, mturk_code, chat_id, hit_id,
                                   assignment_id):
            cursor.execute(
                'INSERT INTO mturk_task VALUES (?,?,?,?,?)',
                (userid, mturk_code, chat_id, hit_id, assignment_id))

        def _is_chat_complete(cursor, chat_id):
            cursor.execute('''SELECT outcome FROM chat WHERE chat_id=?''',
                           (chat_id, ))
            try:
                result = cursor.fetchone()
                if result is None or len(result) == 0:
                    return False

                try:
                    outcome = json.loads(result[0])
                except ValueError:
                    return False

                if outcome['reward'] is None:  # or outcome['reward'] == 0:
                    return False
                else:
                    # check whether chat includes both selection events
                    select_id = defaultdict(lambda: None)
                    chat_info = DatabaseReader.get_chat_example(
                        cursor, chat_id, self.scenario_db,
                        include_meta=True).to_dict()
                    for chat_event in chat_info['events']:
                        if chat_event['action'] == 'select':
                            select_id[chat_event['agent']] = chat_event['data']
                    if (select_id[0] is None) or (select_id[1] is None):
                        return False
                    return True
            except ValueError:
                return False

        try:
            with self.conn:
                cursor = self.conn.cursor()
                u = self._get_user_info(cursor,
                                        userid,
                                        assumed_status=current_status)
                num_seconds = (
                    self.config["status_params"]["finished"]["num_seconds"] +
                    u.status_timestamp) - current_timestamp_in_seconds()
                completed = _is_chat_complete(cursor, u.chat_id)
                if from_mturk:
                    mturk_code = _generate_mturk_code(completed)
                    self.logger.debug(
                        "User {:s} got completion code {:s}".format(
                            userid, mturk_code))
                else:
                    mturk_code = None
                _add_finished_task_row(cursor, userid, mturk_code, u.chat_id,
                                       hit_id, assignment_id)
                return FinishedState(Markup(u.message), num_seconds,
                                     mturk_code), u.chat_id, completed

        except sqlite3.IntegrityError:
            print("WARNING: Rolled back transaction")
Пример #6
0
 def _is_timeout(timeout_limit, timestamp):
     if timeout_limit < 0:
         return False
     num_seconds_remaining = (timeout_limit +
                              timestamp) - current_timestamp_in_seconds()
     return num_seconds_remaining <= 0