def convert_events_to_json(chat_id, cursor, scenario_db): try: cursor.execute( 'SELECT agent, action, time, data, start_time FROM event WHERE chat_id=? ORDER BY time ASC', (chat_id, )) logged_events = cursor.fetchall() except sqlite3.OperationalError: cursor.execute( 'SELECT agent, action, time, data FROM event WHERE chat_id=? ORDER BY time ASC', (chat_id, )) logged_events = cursor.fetchall() events = [] for i, (agent, action, time, data) in enumerate(logged_events): events.append((agent, action, time, data, time)) logged_events = events cursor.execute('SELECT scenario_id, outcome FROM chat WHERE chat_id=?', (chat_id, )) (uuid, outcome) = cursor.fetchone() try: outcome = json.loads(outcome) except ValueError: outcome = {'reward': 0} try: cursor.execute('SELECT agent_types FROM chat WHERE chat_id=?', (chat_id, )) agent_types = cursor.fetchone()[0] agent_types = json.loads(agent_types) except sqlite3.OperationalError: agent_types = {0: HumanSystem.name(), 1: HumanSystem.name()} chat_events = [] for (agent, action, time, data, start_time) in logged_events: if action == 'join' or action == 'leave': continue if action == 'select': data = json.loads(data) time = convert_time_format(time) start_time = convert_time_format(start_time) event = Event(agent, time, action, data, start_time) chat_events.append(event) return Example(scenario_db.get(uuid), uuid, chat_events, outcome, chat_id, agent_types)
def get_chat_events(cls, cursor, chat_id): """Read all events in the chat specified by chat_id. Returns: [Event] """ cursor.execute('SELECT * FROM event WHERE chat_id=? ORDER BY time ASC', (chat_id, )) logged_events = cursor.fetchall() chat_events = [] agent_chat = {0: False, 1: False} for row in logged_events: # Compatible with older event structure agent, action, time, data = [ row[k] for k in ('agent', 'action', 'time', 'data') ] try: start_time = row['start_time'] except IndexError: start_time = time try: metadata = json.loads(row['metadata']) except IndexError: metadata = None if action == 'join' or action == 'leave' or action == 'typing': continue if action == 'message' and len(data.strip()) == 0: continue data = cls.process_event_data(action, data) agent_chat[agent] = True time = cls.convert_time_format(time) start_time = cls.convert_time_format(start_time) # event = Event(agent, time, action, data, start_time, metadata=metadata) event = Event(agent, time, action, data, start_time) chat_events.append(event) return chat_events
def select(self, item): return Event(agent=self.agent, time=None, action='select', data=item)
def message(self, text): return Event(agent=self.agent, time=None, action='message', data=text)
def message(self, text, state=None): event = Event(agent=self.agent, time=None, action='message', data=text) event.state = state return event