Skip to content

Commit

Permalink
Use psycopg2 and add requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
malvads committed Jun 3, 2024
1 parent c4eee33 commit 3fb6134
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 19 deletions.
78 changes: 60 additions & 18 deletions resources/src/redborder/postgresql.py
Original file line number Diff line number Diff line change
@@ -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 <malvarez@redborder.com>
# Pablo Rodriguez Flores <prodriguez@redborder.com>
#
# 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 <https://www.gnu.org/licenses/>.

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()
if connection is not None:
connection.close()
3 changes: 2 additions & 1 deletion resources/src/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3fb6134

Please sign in to comment.