RaghavJuneja
RaghavJuneja

Reputation: 11

How to manage multiple connections in production when using dialogflow and FastAPI?

I need to know how I can manage multiple connections. So I was making a hackathon chatbot . Initially dialogflow detect the intents and then request sent to fastapi backend which returns a fulfillmentText to me. (the fastapi backend send request to mysql to return some information). Here is a part of my code elif intent == 'isHackathonPaid': # drill_name='WUEBOTDEMODRILL' try: conn = connect_to_sql() cursor = conn.cursor()

            query_paid_status = "SELECT is_drill_paid FROM view_drills WHERE drill_cust_url = %s"
            cursor.execute(query_paid_status, (drill_cust_url,))
            paid_status = cursor.fetchone()

            if not paid_status:
                return JSONResponse(content={'fulfillmentText': "Sorry, this information is not mentioned by the organizers"})

            paid_status = paid_status[0]

    
            if paid_status == 1:
                return JSONResponse(content={'fulfillmentText': f"Yes,{drill_cust_url} is paid."})
            elif paid_status == 0:
                return JSONResponse(content={'fulfillmentText': f"No, {drill_cust_url} is not paid."})
            else:
                return JSONResponse(content={'fulfillmentText': "Sorry, this information is not present in db."})

        except Exception as e:
            return JSONResponse(content={'fulfillmentText': f"Error: {e}"})
        finally:
            cursor.close()
            conn.close()
    elif intent == 'phase_info':
        try:
            phases, error = get_phases(drill_cust_url)

            if error:
                return JSONResponse(content={'fulfillmentText': error})
            
            if not phases:
                return JSONResponse(content={'fulfillmentText': f"No phases found for hackathon {drill_cust_url}"})
            
            phase_info = ""
            for phase in phases:
                phase_name, phase_desc, phase_mode, phase_type, phase_start_dt, phase_end_dt, phase_judgement_start_dt, phase_judgement_end_dt = phase
                
                phase_info += f"Phase Name: {phase_name}\n"
                if phase_desc is not None:
                    phase_info += f"Description: {phase_desc}\n"
                if phase_mode is not None:
                    phase_info += f"Mode: {phase_mode}\n"
                if phase_type is not None:
                    phase_info += f"Type: {phase_type}\n"
                if phase_start_dt is not None:
                    phase_info += f"Start Date: {phase_start_dt}\n"
                if phase_end_dt is not None:
                    phase_info += f"End Date: {phase_end_dt}\n"
                if phase_judgement_start_dt is not None:
                    phase_info += f"Judgement Start Date: {phase_judgement_start_dt}\n"
                if phase_judgement_end_dt is not None:
                    phase_info += f"Judgement End Date: {phase_judgement_end_dt}\n\n"
            
            # Remove the last two newline characters from the response if they exist
            phase_info = phase_info.rstrip('\n')
            phase_info = phase_info[:-2]

            return JSONResponse(content={'fulfillmentText': f"The phases for {drill_cust_url} hackathon are: \n{phase_info}"})

        except Exception as e:
            return JSONResponse(content={'fulfillmentText': f"An error occurred while fetching phase information: {str(e)}"})

    elif intent == 'team_Size':
        try:
            # drill_name = payload['queryResult']['parameters']['hackathon_name']
            # drill_name='WUEBOTDEMODRILL'
            team_size, error = get_team_size(drill_cust_url)

            if error:
                return JSONResponse(content={'fulfillmentText': error})
            
            if not team_size:
                return JSONResponse(content={'fulfillmentText': f"Team size information not available for hackathon {drill_cust_url}"})
            
            min_team_size = team_size.get('min')
            max_team_size = team_size.get('max')

            return JSONResponse(content={'fulfillmentText': f"The team size for {drill_cust_url} hackathon is from {min_team_size} to {max_team_size} members."})

        except Exception as e:
            return JSONResponse(content={'fulfillmentText': f"An error occurred while fetching team size information: {str(e)}"})
    elif intent == 'collaboratorsInfo':
        from bs4 import BeautifulSoup
        try:
            collaborators, error = get_collaborators(drill_cust_url)

            if error:
                return JSONResponse(content={'fulfillmentText': error})
            
            if not collaborators:
                return JSONResponse(content={'fulfillmentText': f"No collaborators found for hackathon {drill_cust_url}"})
            
            collaborator_info = ""
            for collaborator in collaborators:
                collaborator_name, collaborator_type, collaborator_desc, collaborator_email, collaborator_social_links = collaborator
                
                # Removing HTML tags from collaborator description
                collaborator_desc_text = BeautifulSoup(collaborator_desc, "html.parser").get_text()
                
                collaborator_info += f"Name: {collaborator_name}\n"
                collaborator_info += f"Type: {collaborator_type}\n"
                collaborator_info += f"Description: {collaborator_desc_text}\n"
                collaborator_info += f"Email: {collaborator_email}\n"
                collaborator_info += f"Social Links: {collaborator_social_links}\n\n"
            
            # Remove the last two newline characters and add proper spacing
            collaborator_info = collaborator_info.rstrip('\n\n')
            collaborator_info = collaborator_info.replace('\n\n', '\n\n\n')

            return JSONResponse(content={'fulfillmentText': f"The collaborators for {drill_cust_url} hackathon are: \n\n{collaborator_info}"})

        except Exception as e:
            return JSONResponse(content={'fulfillmentText': f"An error occurred while fetching collaborator information: {str(e)}"})

I WAS WONDERING IS THERE A BETTER WAY INSTEAD OF OPENING AND CLOSING A NEW SQL EACH TIME

I was expecting a better way than opening and closing each connection. I meant I read about pooling but idk i read pooling size u need to specify, But idk much about it. can someone just me pooling , pooling size and stuff. Also which library to use sqlconnector, sqlalchemy or aiomysql . In reference to production ready code. which I need to deploy .

Upvotes: 0

Views: 40

Answers (0)

Related Questions