Skip to content

Commit

Permalink
feat: fix table group report
Browse files Browse the repository at this point in the history
feat: use psycopg instead of django connection to connect into chats db
  • Loading branch information
helllllllder committed Jun 14, 2024
1 parent 50f07b2 commit 7a259ad
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 30 deletions.
4 changes: 2 additions & 2 deletions insights/dashboards/usecases/dashboard_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def create_reports(
},
{
"name": "Horário do início",
"value": "start_time",
"value": "created_on",
"display": True,
"hidden_name": False,
},
Expand Down Expand Up @@ -238,7 +238,7 @@ def create_reports(
},
{
"name": "Horário do início",
"value": "start_time",
"value": "created_on",
"display": True,
"hidden_name": False,
},
Expand Down
45 changes: 25 additions & 20 deletions insights/db/postgres/connection.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
from contextlib import contextmanager

from django.db import connections
import settings
from psycopg import connect
from psycopg.rows import dict_row
from psycopg_pool import ConnectionPool, NullConnectionPool


def dictfetchall(cursor):
"""
Return all rows from a cursor as a dict.
Assume the column names are unique.
"""
columns = [col[0] for col in cursor.description]
return [dict(zip(columns, row)) for row in cursor.fetchall()]
return cursor.fetchall()


def dictfetchone(cursor):
"""
Return all rows from a cursor as a dict.
Assume the column names are unique.
"""
columns = [col[0] for col in cursor.description]
row = cursor.fetchone()
return dict(zip(columns, row))
return cursor.fetchone()


def pg_execute_query(db_name: str, query: str, *args, **kwargs):
with connections[db_name].cursor() as cursor:
return cursor.execute(query, args).fetchall()
chats_pool = NullConnectionPool(
max_size=5,
conninfo=settings.CHATS_PG,
check=ConnectionPool.check_connection,
)


@contextmanager
def get_cursor(db_name: str):
with connections[db_name].cursor() as cur:
yield cur
def get_connection():
# if settings.CONNECTION_TYPE == "pool":
# with chats_pool.connection() as conn:
# yield conn
# else:
with connect(settings.CHATS_PG) as conn:
yield conn


@contextmanager
def get_cursor(*args, **kwargs):
with get_connection() as conn:
with conn.cursor(row_factory=dict_row) as cur:
yield cur
28 changes: 28 additions & 0 deletions insights/db/postgres/connection_old.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from contextlib import contextmanager

from django.db import connections


def dictfetchall(cursor):
"""
Return all rows from a cursor as a dict.
Assume the column names are unique.
"""
columns = [col[0] for col in cursor.description]
return [dict(zip(columns, row)) for row in cursor.fetchall()]


def dictfetchone(cursor):
"""
Return all rows from a cursor as a dict.
Assume the column names are unique.
"""
columns = [col[0] for col in cursor.description]
row = cursor.fetchone()
return dict(zip(columns, row))


@contextmanager
def get_cursor(db_name: str):
with connections[db_name].cursor() as cur:
yield cur
1 change: 1 addition & 0 deletions insights/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@

# Database
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases
CHATS_PG = env.str(var="CHATS_PG_DATABASE")

DATABASES = {
"default": env.db(var="DEFAULT_DATABASE", default="sqlite:///insights_db.sqlite3"),
Expand Down
29 changes: 22 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ python = "^3.10"
django = "^5.0.3"
djangorestframework = "^3.15.1"
pre-commit = "^3.7.0"
psycopg = "^3.1.18"
psycopg = {extras = ["pool"], version = "^3.1.19"}
factory-boy = "^3.3.0"
gevent = "^24.2.1"
django-environ = "^0.11.2"
Expand Down

0 comments on commit 7a259ad

Please sign in to comment.