Skip to content

Commit

Permalink
fix: more pg
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiionutdamian committed Feb 8, 2024
1 parent b032215 commit b5a678b
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 47 deletions.
2 changes: 1 addition & 1 deletion demo-basic-fastapi/src/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from mixins.postgres_mixin import _PostgresMixin
from mixins.redis_mixin import _RedisMixin

__VER__ = '0.4.4'
__VER__ = '0.4.7'


class AppPaths:
Expand Down
35 changes: 22 additions & 13 deletions demo-basic-fastapi/src/mixins/postgres_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def _maybe_setup_postgres(self):
self.P("Tables in the database: {}".format(rows))
self._has_postgres = True
except Exception as e:
self.P("Error in _maybe_setup_postgres: {}".format(e))
self.P("Error in _maybe_setup_postgres: {}".format(e))
return


Expand All @@ -74,7 +74,8 @@ def postgres_insert_data(self, table_name: str, **kwargs):
cur.execute(str_sql, values)
self.__pg.commit()
except Exception as e:
self.P("Error in postgres_insert_data: {}".format(e))
self.P("Error in postgres_insert_data: {}".format(e))
raise ValueError("Postgres issue")
return


Expand All @@ -97,29 +98,37 @@ def postgres_select_data(self, table_name: str, **kwargs):
result = rows
except Exception as e:
self.P("Error in postgres_select_data: {}".format(e))
result
raise ValueError("Postgres issue")
return result


def postgres_select_counts(self, table_name : str, group_by : str):
result = None
if self._has_postgres:
str_sql = f"SELECT {group_by}, COUNT(*) FROM {table_name} GROUP BY {group_by};"
with self.__pg.cursor() as cur:
cur.execute(str_sql)
rows = cur.fetchall()
result = rows
try:
str_sql = f"SELECT {group_by}, COUNT(*) FROM {table_name} GROUP BY {group_by};"
with self.__pg.cursor() as cur:
cur.execute(str_sql)
rows = cur.fetchall()
result = rows
except:
self.P("Error in postgres_select_counts: {}".format(e))
raise ValueError("Postgres issue")
return result


def postgres_get_count(self, table_name : str):
result = None
if self._has_postgres:
str_sql = f"SELECT COUNT(*) FROM {table_name};"
with self.__pg.cursor() as cur:
cur.execute(str_sql)
rows = cur.fetchall()
result = rows
try:
str_sql = f"SELECT COUNT(*) FROM {table_name};"
with self.__pg.cursor() as cur:
cur.execute(str_sql)
rows = cur.fetchall()
result = rows
except Exception as e:
self.P("Error in postgres_get_count: {}".format(e))
raise ValueError("Postgres issue")
return result


Expand Down
89 changes: 56 additions & 33 deletions demo-basic-fastapi/src/mixins/redis_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,61 +24,84 @@ def _maybe_setup_redis(self):
self._has_redis = False
dct_redis = self.__get_redis_config()
if len(dct_redis) > 0:
if "REDIS_MASTER_SERVICE_HOST" in dct_redis:
# this is a redis master/slave setup
self.P("Setting up Redis with master/slave configuration:\n{}".format(safe_jsonify(dct_redis)))
redis_host = dct_redis.get("REDIS_MASTER_SERVICE_HOST")
redis_port = dct_redis.get("REDIS_MASTER_SERVICE_PORT")
redis_password = dct_redis.get("REDIS_PASSWORD")
# redis_master_port = dct_redis.get("REDIS_MASTER_PORT")
else:
self.P("Setting up simple Redis with configuration:\n{}".format(safe_jsonify(dct_redis)))
redis_host = dct_redis.get("REDIS_SERVICE_HOST")
redis_port = dct_redis.get("REDIS_SERVICE_PORT", 6379)
redis_password = dct_redis.get("REDIS_PASSWORD", None)
hidden_password = redis_password[:2] + "*" * (len(redis_password) - 4) + redis_password[-2:] if redis_password is not None else None
self.P("Connecting to Redis at {}:{} with password: {}".format(
redis_host, redis_port, hidden_password
))
self.__redis = redis.Redis(
host=redis_host, port=redis_port,
password=redis_password,
decode_responses=True,
)
self._has_redis = True
self.P("Connected to Redis at {}:{}".format(redis_host, redis_port))
self.__redis_info = {
k : v for k, v in self.__redis.info().items()
if k in INFO_KEYS
}
self.P("Redis info:\n {}".format(safe_jsonify(self.__redis_info)))
try:
if "REDIS_MASTER_SERVICE_HOST" in dct_redis:
# this is a redis master/slave setup
self.P("Setting up Redis with master/slave configuration:\n{}".format(safe_jsonify(dct_redis)))
redis_host = dct_redis.get("REDIS_MASTER_SERVICE_HOST")
redis_port = dct_redis.get("REDIS_MASTER_SERVICE_PORT")
redis_password = dct_redis.get("REDIS_PASSWORD")
# redis_master_port = dct_redis.get("REDIS_MASTER_PORT")
else:
self.P("Setting up simple Redis with configuration:\n{}".format(safe_jsonify(dct_redis)))
redis_host = dct_redis.get("REDIS_SERVICE_HOST")
redis_port = dct_redis.get("REDIS_SERVICE_PORT", 6379)
redis_password = dct_redis.get("REDIS_PASSWORD", None)
hidden_password = redis_password[:2] + "*" * (len(redis_password) - 4) + redis_password[-2:] if redis_password is not None else None
self.P("Connecting to Redis at {}:{} with password: {}".format(
redis_host, redis_port, hidden_password
))
self.__redis = redis.Redis(
host=redis_host, port=redis_port,
password=redis_password,
decode_responses=True,
)
self.P("Connected to Redis at {}:{}".format(redis_host, redis_port))
self.__redis_info = {
k : v for k, v in self.__redis.info().items()
if k in INFO_KEYS
}
self.P("Redis info:\n {}".format(safe_jsonify(self.__redis_info)))
self._has_redis = True
except Exception as ex:
self.P("Failed to connect to Redis: {}".format(ex))
return


def redis_inc(self, key : str, amount : int = 1):
if self._has_redis:
self.__redis.incr(key, amount)
try:
self.__redis.incr(key, amount)
except Exception as ex:
self.P("Failed to increment key {} by {}: {}".format(key, amount, ex))
raise ValueError("Redis issue")
return


def redis_set(self, key : str, value):
if self._has_redis:
self.__redis.set(key, value)
try:
self.__redis.set(key, value)
except Exception as ex:
self.P("Failed to set key {} to {}: {}".format(key, value, ex))
raise ValueError("Redis issue")
return

def redis_sethash(self, hashname : str, key : str, value):
if self._has_redis:
self.__redis.hset(hashname, key, value)
try:
self.__redis.hset(hashname, key, value)
except Exception as ex:
self.P("Failed to set hash {} key {} to {}: {}".format(hashname, key, value, ex))
raise ValueError("Redis issue")
return

def redis_get(self, key : str):
result = None
if self._has_redis:
result = self.__redis.get(key)
try:
result = self.__redis.get(key)
except Exception as ex:
self.P("Failed to get key {}: {}".format(key, ex))
raise ValueError("Redis issue")
return result

def redis_gethash(self, hashname : str):
result = None
if self._has_redis:
result = self.__redis.hget(hashname)
try:
result = self.__redis.hget(hashname)
except Exception as ex:
self.P("Failed to get hash {}: {}".format(hashname, ex))
raise ValueError("Redis issue")
return result

0 comments on commit b5a678b

Please sign in to comment.