Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
So the script does not log the db credentials 🙄.
  • Loading branch information
zechmeister committed Mar 1, 2024
1 parent 90d93a7 commit 9f0d1d9
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions query_and_submit.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import yaml
import urllib.parse


def read_secret_file(key):
try:
with open(key, "r") as secret_file:
Expand Down Expand Up @@ -45,16 +46,22 @@ def read_secret_file(key):
sql = query["countSql"]
condition_keyword = "and" if re.search("where", sql, re.IGNORECASE) else "where"
timestamp_col = query["timestampColumn"]
metrics[metric_name] = json.loads(
subprocess.check_output(
[
"psql",
db_uri,
"-Atc",
f"{sql} {condition_keyword} \"{timestamp_col}\" between '{interval_start}' and '{interval_end}'",
]

try:
metrics[metric_name] = json.loads(
subprocess.check_output(
[
"psql",
db_uri,
"-Atc",
f"{sql} {condition_keyword} \"{timestamp_col}\" between '{interval_start}' and '{interval_end}'",
]
)
)
)
except subprocess.CalledProcessError as e:
print(f"Command execution failed with exit status {e.returncode}.")
except Exception as e:
print("An error occurred while executing the script")

response = requests.post(
url=config["endpoint"],
Expand Down

0 comments on commit 9f0d1d9

Please sign in to comment.