diff --git a/resources/src/redborder/postgresql.py b/resources/src/redborder/postgresql.py index 7e72a1c..4c1be77 100644 --- a/resources/src/redborder/postgresql.py +++ b/resources/src/redborder/postgresql.py @@ -1,26 +1,68 @@ -import asyncpg, asyncio -from resources.src.server.rest import config +# Copyright (C) 2023 Eneo Tecnologia S.L. +# +# Authors: +# Miguel Álvarez Adsuara +# Pablo Rodriguez Flores +# +# This program is free software: you can redistribute it and/or modify it under the terms of the +# GNU Affero General Public License as published by the Free Software Foundation, either version 3 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License along with this program. +# If not, see . +import psycopg2 +from resources.src.server.rest import config +from resources.src.logger.logger import logger class RbOutliersPSQL: RB_AIOUTLIERS_FILTERS_QUERY = "SELECT filter FROM saved_filters" - def __init__(self) -> None: - self.filters = [] - asyncio.run(self.get_filtered_data()) - - async def get_filtered_data(self): - connection = await asyncpg.connect( - host=config.get("rbpsql", "host"), - database=config.get("rbpsql", "database"), - user=config.get("rbpsql", "user"), - password=config.get("rbpsql", "password") - ) - + @staticmethod + def get_filtered_data(self): + """ + Retrieves filters from the database. + + Establishes a connection to the PostgreSQL database using credentials from the config file, + executes a query to fetch filters from the saved_filters table, and returns the list of filters. + + Args: + self: Reference to the current instance of the class. + + Returns: + List of filters fetched from the database. + + Raises: + Logs any exception that occurs during database operations. + + Ensures: + The database connection is closed after the operation. + """ + connection = None try: - filtered_data = await connection.fetch(self.RB_AIOUTLIERS_FILTERS_QUERY) - self.filters = [record['filter'] for record in filtered_data] - print(self.filters) + connection = psycopg2.connect( + host=config.get("rbpsql", "host"), + database=config.get("rbpsql", "database"), + user=config.get("rbpsql", "user"), + password=config.get("rbpsql", "password") + ) + + cursor = connection.cursor() + cursor.execute(self.RB_AIOUTLIERS_FILTERS_QUERY) + filtered_data = cursor.fetchall() + filters = [record[0] for record in filtered_data] + cursor.close() + + return filters + + except (Exception, psycopg2.DatabaseError) as error: + logger.error(error) + finally: - await connection.close() \ No newline at end of file + if connection is not None: + connection.close() diff --git a/resources/src/requirements.txt b/resources/src/requirements.txt index 3928811..a60be4f 100644 --- a/resources/src/requirements.txt +++ b/resources/src/requirements.txt @@ -7,9 +7,10 @@ numpy~=1.26.4 pandas~=1.3.5 pylogrus~=0.4.0 pytz~=2023.4 -redis~=5.0.3 Requests~=2.32.1 scikit_learn~=1.4.1.post1 tensorflow[and-cuda]~=2.15.0 ntplib~=0.4.0 rq~=1.16.2 +kazoo~=2.10.0 +psycopg2-binary~=2.9.9 \ No newline at end of file