Skip to content

Commit

Permalink
Merge pull request #46 from ianco/master
Browse files Browse the repository at this point in the history
Post abbreviated status to webhook and email full status
  • Loading branch information
ianco authored Nov 13, 2024
2 parents ae734bc + 6d48dcf commit c56466c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 15 deletions.
41 changes: 37 additions & 4 deletions scripts/orgbook_data_audit.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/python
import os
from os import environ
import psycopg2
import pytz
import datetime
Expand Down Expand Up @@ -29,6 +29,8 @@
MIN_VALID_DATE_TZ = timezone.localize(MIN_VALID_DATE)
MAX_END_DATE_TZ = timezone.localize(MAX_END_DATE)

MAX_ERRORS_TO_POST = int(environ.get('MAX_ERRORS_TO_POST', '15'))


# determine jurisdiction for corp
def get_corp_jurisdiction(corp_typ_cd, corp_class, can_jur_typ_cd, othr_juris_desc):
Expand Down Expand Up @@ -64,6 +66,7 @@ def compare_dates_lear(orgbook_reg_dt, bc_reg_reg_dt):
return False
return (bc_reg_reg_dt == orgbook_reg_dt)


# compare registration dates
def compare_dates_colin(orgbook_reg_dt, bc_reg_reg_dt):
if bc_reg_reg_dt is None or len(bc_reg_reg_dt) == 0:
Expand All @@ -80,13 +83,23 @@ def compare_dates_colin(orgbook_reg_dt, bc_reg_reg_dt):
except (Exception) as error:
return MIN_START_DATE_TZ.astimezone(pytz.utc).isoformat() == orgbook_reg_dt


# compare registration dates
def compare_dates(orgbook_reg_dt, bc_reg_reg_dt, USE_LEAR: bool = False):
if USE_LEAR:
return compare_dates_lear(orgbook_reg_dt, bc_reg_reg_dt)
else:
return compare_dates_colin(orgbook_reg_dt, bc_reg_reg_dt)


# find the nth occurrance of substrung
def find_nth_occurrance(substring, string, n):
res = -1
for i in range(0, n):
res = string.find(substring, res + 1)
return res


def compare_bc_reg_orgbook(
bc_reg_corp_types,
bc_reg_corp_names,
Expand Down Expand Up @@ -238,10 +251,30 @@ def compare_bc_reg_orgbook(
len(reln_list) +
len(active_reln_list))

if 0 < corp_errors:
if MAX_ERRORS_TO_POST < corp_errors:
res = find_nth_occurrance("\n", error_msgs, MAX_ERRORS_TO_POST)
error_msgs_summary = error_msgs[:res+1] + "... etc ..."
log_error(error_msgs_summary, error_msgs)
res = find_nth_occurrance("\n", error_cmds, MAX_ERRORS_TO_POST)
error_cmds_summary = error_cmds[:res+1] + "... etc ..."
log_error(error_cmds_summary, error_cmds)
elif 0 < corp_errors:
log_error(error_msgs)
log_error(error_cmds)

error_summary_summary = ""
error_summary_summary += "Missing in OrgBook: " + str(len(missing_in_orgbook)) + "\n"
error_summary_summary += "Missing in BC Reg: " + str(len(missing_in_bcreg)) + "\n"
error_summary_summary += "Wrong corp type: " + str(len(wrong_corp_type)) + "\n"
error_summary_summary += "Wrong corp name: " + str(len(wrong_corp_name)) + "\n"
error_summary_summary += "Wrong corp status: " + str(len(wrong_corp_status)) + "\n"
error_summary_summary += "Wrong business number: " + str(len(wrong_bus_num)) + "\n"
error_summary_summary += "Wrong corp registration: " + str(len(wrong_corp_reg_dt)) + "\n"
error_summary_summary += "Wrong corp jurisdiction: " + str(len(wrong_corp_juris)) + "\n"
if not USE_LEAR:
error_summary_summary += "Mis-matched OrgBook relationships: " + str(len(reln_list)) + "\n"
error_summary_summary += "Missing OrgBook relationships: " + str(len(active_reln_list)) + "\n"

error_summary = ""
error_summary += "Missing in OrgBook: " + str(len(missing_in_orgbook)) + " " + str(missing_in_orgbook) + "\n"
error_summary += "Missing in BC Reg: " + str(len(missing_in_bcreg)) + " " + str(missing_in_bcreg) + "\n"
Expand All @@ -256,8 +289,8 @@ def compare_bc_reg_orgbook(
error_summary += "Missing OrgBook relationships: " + str(len(active_reln_list)) + " " + str(active_reln_list) + "\n"

if 0 < corp_errors:
log_error(error_summary)
log_error(error_summary_summary, error_summary)
else:
log_info(error_summary)
log_info(error_summary_summary, error_summary)

return wrong_bus_num
32 changes: 21 additions & 11 deletions scripts/rocketchat_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def synchronous_post_url(webhook_url, payload):
return pool.submit(asyncio.run, _post_url(webhook_url, payload)).result(timeout=30)


def post_msg_to_webhook(level, message):
def post_msg_to_webhook(level, message, message_details:str = None):
if level and level <= log_level:
payload = get_webhook_payload(level, message)
if webhook_url and 0 < len(webhook_url):
Expand All @@ -63,28 +63,38 @@ def post_msg_to_webhook(level, message):
except Exception as e:
print(">>> NOT posted webhook, error:", str(e))
else:
print(">>> NOT Posted webhook level", level, "message:\n", message, "\n(no webhook_url)\n")
print(">>> NOT Posted webhook level", level, "message:\n", message_details if message_details else message, "\n(no webhook_url)\n")

try:
success = email_support(payload)
if message_details:
payload = get_webhook_payload(level, message_details)
success = email_support(payload)
else:
success = email_support(payload)
if success:
print(">>> Sent email for level", level)
if message_details:
print(">>> Sent email for level", level)
else:
print(">>> Sent email for level", level)
else:
print(">>> No email sent")
except Exception as e:
print(">>> NOT sent email, error:", str(e))

else:
print(">>> NOT Posted webhook level", level, "(", log_level, "), message:\n", message)
if message_details:
print(">>> NOT Posted webhook level", level, "(", log_level, "), message:\n", message_details)
else:
print(">>> NOT Posted webhook level", level, "(", log_level, "), message:\n", message)


def log_info(message):
post_msg_to_webhook('2', message)
def log_info(message, message_details:str = None):
post_msg_to_webhook('2', message, message_details)


def log_warning(message):
post_msg_to_webhook('1', message)
def log_warning(message, message_details:str = None):
post_msg_to_webhook('1', message, message_details)


def log_error(message):
post_msg_to_webhook('0', message)
def log_error(message, message_details:str = None):
post_msg_to_webhook('0', message, message_details)

0 comments on commit c56466c

Please sign in to comment.