Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upd slack msg #20

Merged
merged 3 commits into from
Aug 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions python_scripts/test_alert/dbt_test_alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ def log_test_result():
run = json.load(f)

logs = []
messages = {"fail": [], "warn": []}
messages = {
"fail": [],
"warn": []
}
test_count = 0
warn_count = 0
fail_count = 0
Expand All @@ -39,58 +42,57 @@ def log_test_result():
"test_count": test_count,
"warn_count": warn_count,
"fail_count": fail_count,
"elapsed_time": str(datetime.timedelta(seconds=run["elapsed_time"])),
"elapsed_time": str(datetime.timedelta(seconds=run["elapsed_time"]))
}

return dbt_test_result


def create_message(**kwargs):
messageBody = {
"text": f"Hey{' <@here>' if len(kwargs['messages']['fail']) > 0 else ''}, new DBT test results for :{os.environ.get('DATABASE').split('_DEV')[0]}: {os.environ.get('DATABASE')}",
"text": f"Hey{' <!here>' if len(kwargs['messages']['fail']) > 0 else ''}, new DBT test results for :{os.environ.get('DATABASE').split('_DEV')[0]}: {os.environ.get('DATABASE')}",
"attachments": [
{
"color": kwargs["color"],
"fields": [
{
"title": "Total Tests Run",
"value": kwargs["test_count"],
"short": True,
"short": True
},
{
"title": "Total Time Elapsed",
"value": kwargs["elapsed_time"],
"short": True,
"short": True
},
{
"title": "Number of Unsuccessful Tests",
"value": f"Fail: {kwargs['fail_count']}, Warn: {kwargs['warn_count']}",
"short": True,
"short": True
},
{
"title": "Failed Tests:",
"value": "\n".join(kwargs["messages"]["fail"])
if len(kwargs["messages"]["fail"]) > 0
else "None :)",
"short": False,
},
"value": "\n".join(kwargs["messages"]["fail"]) if len(kwargs["messages"]["fail"]) > 0 else "None :)",
"short": False
}
],
"actions": [

{
"type": "button",
"text": "View the full run results",
"text": "View Warnings",
"style": "primary",
"url": "https://github.com/FlipsideCrypto/bitcoin-models/actions/",
"url": "https://github.com/FlipsideCrypto/bitcoin-models/actions/workflows/dbt_test.yml",
"confirm": {
"title": "I haven't figured this part out yet",
"text": "I'm not sure how to get the run id from the workflow run, see all tests by clicking Ok",
"ok_text": "Ok",
"dismiss_text": "Dismiss",
},
"title": f"{kwargs['warn_count']} Warnings",
"text": "\n".join(kwargs["messages"]["warn"]) if len(kwargs["messages"]["warn"]) > 0 else "None :)",
"ok_text": "Continue to GHA",
"dismiss_text": "Dismiss"
}
}
],
]
}
],
]
}

return messageBody
Expand All @@ -109,16 +111,17 @@ def send_alert(webhook_url):
test_count=data["test_count"],
messages=data["messages"],
elapsed_time=data["elapsed_time"],
color="#f44336" if data["fail_count"] > 0 else "#4CAF50",
color="#f44336" if data["fail_count"] > 0 else "#4CAF50"
)

x = requests.post(url, json=send_message)

# test config to continue on error in workflow, so we want to exit with a non-zero code if there are any failures
if data["fail_count"] > 0:
if data['fail_count'] > 0:
sys.exit(1)


if __name__ == "__main__":
if __name__ == '__main__':

webhook_url = os.environ.get("SLACK_WEBHOOK_URL")
send_alert(webhook_url)