def update_db_order_completed(env, order_id): """Updates the database when the order is completed""" SQL = f""" UPDATE orders SET completed_at = {env.now} , status = 'Completed' WHERE id = {order_id}; """ with css_cursor() as cur: execute_sql(SQL, cur)
def update_db_order_started(env, order_id): """Updates the database when the order starts being prepared""" SQL = f""" UPDATE orders SET started_at = {env.now} , status = 'In Progress' WHERE id = {order_id}; """ with css_cursor() as cur: execute_sql(SQL, cur)
def update_db_order_received(env, order): """Inserts an order when it is first received""" SQL = f""" INSERT INTO orders (id, status, received_at, customer_name, service, total_price, items) VALUES ( {order['id']} , 'Queued' , {env.now} , '{order['name']}' , '{order['service']}' , {sum([i['price_per_unit'] * i['quantity'] for i in order['items']])} , '{json.dumps({i['name'].replace("'", "''"):i['quantity'] for i in order['items']})}' ); """ with css_cursor() as cur: execute_sql(SQL, cur)
Handles all chatbot related stuff, including initial training data. """ from chatterbot import ChatBot from db import connection chatbot = ChatBot( 'Glory Wallters', storage_adapter='chatterbot.storage.SQLStorageAdapter', trainer='chatterbot.trainers.ChatterBotCorpusTrainer', database_uri="postgresql+psycopg2://camelot:camelot@db:5432/camelot") MAX_MESSAGE_SIZE = 400 conversation_id = connection.execute_sql( 'select id from conversation order by id DESC limit 1;').fetchone() def get_response(message): """Get a response from the chat bot. Returns a default string if the bot returned nothing.""" if len(message) > MAX_MESSAGE_SIZE: message = "I sent you a really long message." response = chatbot.get_response(message, conversation_id) if not response or len(str(response)) == 0: response = "I don't know what you want from me" return response # chatbot.train("chatterbot.corpus.english.greetings")
def recreate_orders_table(): """Drop and recreate orders table""" with css_cursor() as cur: execute_sql(DOWN_SQL, cur, verbose=True) execute_sql(UP_SQL, cur, verbose=True)