-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cria flow para alertar sobre falhas de conexão com o banco de dados d…
…a Jaé / Remove filtro `SMTR: ` do flow janitor / Desativa materialização da `viagem_planejada_planejamento` (#366) * altera local da task log_discord * cria flow de verificacao de conexao com a jae * move task log_discord * cria funcao para testar conexao com banco de dados * remove prefixo do filtro de flows * flow config * ajusta mensagem do discord * desativa materializacao viagem_planejada_planejamento * add changelog * link pr * trata dados nulos
- Loading branch information
Showing
14 changed files
with
206 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,3 +85,5 @@ class constants(Enum): # pylint: disable=c0103 | |
"data_transacao", | ||
], | ||
) | ||
|
||
ALERT_WEBHOOK = "alertas_bilhetagem" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# -*- coding: utf-8 -*- | ||
from prefeitura_rio.pipelines_utils.logging import log | ||
from sqlalchemy import create_engine | ||
from sqlalchemy.exc import OperationalError | ||
|
||
ENGINE_MAPPING = { | ||
"mysql": {"driver": "pymysql", "port": "3306"}, | ||
"postgresql": {"driver": "psycopg2", "port": "5432"}, | ||
} | ||
|
||
|
||
def create_database_url( | ||
engine: str, | ||
host: str, | ||
user: str, | ||
password: str, | ||
database: str, | ||
) -> str: | ||
""" | ||
Cria a URL para se conectar a um banco de dados | ||
Args: | ||
engine (str): O banco de dados (postgresql ou mysql) | ||
host (str): O host do banco de dados | ||
user (str): O usuário para se conectar | ||
password (str): A senha do usuário | ||
database (str): O nome da base (schema) | ||
Returns: | ||
str: a URL de conexão | ||
""" | ||
engine_info = ENGINE_MAPPING[engine] | ||
driver = engine_info["driver"] | ||
port = engine_info["port"] | ||
return f"{engine}+{driver}://{user}:{password}@{host}:{port}/{database}" | ||
|
||
|
||
def test_database_connection( | ||
engine: str, | ||
host: str, | ||
user: str, | ||
password: str, | ||
database: str, | ||
) -> tuple[bool, str]: | ||
""" | ||
Testa se é possível se conectar a um banco de dados | ||
Args: | ||
engine (str): O banco de dados (postgresql ou mysql) | ||
host (str): O host do banco de dados | ||
user (str): O usuário para se conectar | ||
password (str): A senha do usuário | ||
database (str): O nome da base (schema) | ||
Returns: | ||
bool: Se foi possível se conectar ou não | ||
str: String do erro | ||
""" | ||
url = create_database_url( | ||
engine=engine, | ||
host=host, | ||
user=user, | ||
password=password, | ||
database=database, | ||
) | ||
connection = create_engine(url) | ||
log(f"Tentando conexão com o banco de dados {database}") | ||
try: | ||
with connection.connect() as _: | ||
log("Conexão bem-sucedida!") | ||
return True, None | ||
except OperationalError as e: | ||
log("Conexão falhou", level="warning") | ||
return False, str(e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters