Skip to content

Commit

Permalink
Merge pull request #604 from oda-hub/cc-receivers-matrix-no-sentry
Browse files Browse the repository at this point in the history
Cc receivers matrix no sentry
  • Loading branch information
burnout87 authored Oct 13, 2023
2 parents 9879773 + 30906e9 commit fac1cac
Show file tree
Hide file tree
Showing 7 changed files with 604 additions and 112 deletions.
130 changes: 91 additions & 39 deletions cdci_data_analysis/analysis/matrix_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ def send_incident_report_message(
env.filters['humanize_future'] = humanize_future

matrix_server_url = config.matrix_server_url
matrix_sender_access_token = config.matrix_sender_access_token
receiver_room_id = tokenHelper.get_token_user_matrix_room_id(decoded_token)

incident_report_receivers_room_ids = config.matrix_incident_report_receivers_room_ids
incident_report_sender_personal_access_token = config.matrix_incident_report_sender_personal_access_token

matrix_message_data = {
'request': {
Expand All @@ -126,16 +127,23 @@ def send_incident_report_message(
# open("debug_email_lines_too_long.html", "w").write(email_body_html)
# open("debug_email_lines_too_long.text", "w").write(email_text)
# raise MatrixMessageNotSent(f"message not sent on matrix, lines too long!")
res_content = {
'res_content_incident_reports': []
}

res_data = send_message(url_server=matrix_server_url,
sender_access_token=matrix_sender_access_token,
room_id=receiver_room_id,
message_text=message_text,
message_body_html=message_body_html
)
message_data = {
'message_data_incident_reports': []
}

message_data = res_data['message_data']
res_content = res_data['res_content']
for incident_report_receiver_room_id in incident_report_receivers_room_ids:
res_data_message_receiver = send_message(url_server=matrix_server_url,
sender_access_token=incident_report_sender_personal_access_token,
room_id=incident_report_receiver_room_id,
message_text=message_text,
message_body_html=message_body_html
)
message_data['message_data_incident_reports'].append(res_data_message_receiver['message_data'])
res_content['res_content_incident_reports'].append(res_data_message_receiver['res_content'])

store_incident_report_matrix_message(message_data, scratch_dir, sending_time=sending_time)

Expand Down Expand Up @@ -188,6 +196,8 @@ def send_job_message(
matrix_sender_access_token = config.matrix_sender_access_token
receiver_room_id = tokenHelper.get_token_user_matrix_room_id(decoded_token)

bcc_receivers_room_ids = config.matrix_bcc_receivers_room_ids

matrix_message_data = {
'oda_site': {
'site_name': config.site_name,
Expand Down Expand Up @@ -218,43 +228,54 @@ def send_job_message(
template = env.get_template('matrix_message.html')
message_body_html = template.render(**matrix_message_data)
message_text = textify_matrix_message(message_body_html)
res_data = send_message(url_server=matrix_server_url,
sender_access_token=matrix_sender_access_token,
room_id=receiver_room_id,
message_text=message_text,
message_body_html=message_body_html
)
res_content = {
'res_content_bcc_users': []
}

message_data = {
'message_data_bcc_users': []
}
if receiver_room_id is not None and receiver_room_id != "":
res_data_message_token_user = send_message(url_server=matrix_server_url,
sender_access_token=matrix_sender_access_token,
room_id=receiver_room_id,
message_text=message_text,
message_body_html=message_body_html
)
message_data_token_user = res_data_message_token_user['message_data']
res_content_token_user = res_data_message_token_user['res_content']
message_data['message_data_token_user'] = message_data_token_user
res_content['res_content_token_user'] = res_content_token_user
else:
matrix_helper_logger.warning('a matrix message could not be sent to the token user as no personal room id was '
'provided within the token')

for bcc_receiver_room_id in bcc_receivers_room_ids:
if bcc_receiver_room_id is not None and bcc_receiver_room_id != "":
res_data_message_cc_user = send_message(url_server=matrix_server_url,
sender_access_token=matrix_sender_access_token,
room_id=bcc_receiver_room_id,
message_text=message_text,
message_body_html=message_body_html
)
message_data_cc_user = res_data_message_cc_user['message_data']
message_data['message_data_bcc_users'].append(message_data_cc_user)
res_content_cc_user = res_data_message_cc_user['res_content']
res_content['res_content_bcc_users'].append(res_content_cc_user)

message_data = res_data['message_data']
res_content = res_data['res_content']

store_status_matrix_message_info(message_data, status, scratch_dir, sending_time=sending_time, first_submitted_time=time_request)

return res_content



def send_message(
url_server=None,
sender_access_token=None,
room_id=None,
message_text=None,
message_body_html=None,
):

if url_server is None:
matrix_helper_logger.info('matrix url server not available')
raise MissingRequestParameter('matrix url server not available')
if sender_access_token is None:
matrix_helper_logger.info('matrix sender_access_token not available')
raise MissingRequestParameter('matrix sender_access_token not available')
if room_id is None:
matrix_helper_logger.info('matrix room_id not available')
raise MissingRequestParameter('matrix room_id not available')
if message_text is None or message_body_html is None:
matrix_helper_logger.info('matrix message not available')
raise MissingRequestParameter('matrix message not available')

matrix_helper_logger.info(f"Sending message to the room id: {room_id}")
url = os.path.join(url_server, f'_matrix/client/r0/rooms/{room_id}/send/m.room.message')

Expand All @@ -279,14 +300,14 @@ def send_message(
except json.decoder.JSONDecodeError:
error_msg = res.text
matrix_helper_logger.warning(f"there seems to be some problem in sending a message via matrix:\n"
f"the requested url {url} lead to the error {error_msg}, "
"this might be due to an error in the url or the page requested no longer exists, "
"please check it and try to issue again the request")
f"the requested url {url} lead to the error {error_msg}, "
"this might be due to an error in the url or the page requested no longer exists, "
"please check it and try to issue again the request")
matrix_error_message = error_msg

sentry.capture_message(f'issue in sending a message via matrix, the requested url {url} lead to the error '
f'{matrix_error_message}')
raise MatrixMessageNotSent('issue when performing a request to the product gallery',
raise MatrixMessageNotSent('issue in sending a message via matrix',
status_code=res.status_code,
payload={'matrix_error_message': matrix_error_message})

Expand All @@ -300,12 +321,31 @@ def send_message(
return res_data


def is_matrix_config_ok(config):
if config.matrix_server_url is None:
matrix_helper_logger.info('matrix url server not available')
return False
if config.matrix_sender_access_token is None:
matrix_helper_logger.info('matrix sender_access_token not available')
return False
return True


def is_message_to_send_run_query(status, time_original_request, scratch_dir, job_id, config, decoded_token=None):

log_additional_info_obj = {}
sending_ok = False
config_ok = is_matrix_config_ok(config)
time_check = time_.time()
sentry_for_matrix_message_sending_check = config.sentry_for_matrix_message_sending_check

if config.matrix_server_url is None:
matrix_helper_logger.info('matrix url server not available')
config_ok = False
if config.matrix_sender_access_token is None:
matrix_helper_logger.info('matrix sender_access_token not available')
config_ok = False

# get total request duration
if decoded_token:
# in case the job is just submitted and was not submitted before, at least since some time
Expand Down Expand Up @@ -387,12 +427,24 @@ def is_message_to_send_run_query(status, time_original_request, scratch_dir, job
else:
matrix_helper_logger.info(f'a message on matrix will not be sent because a token was not provided')

return sending_ok
return sending_ok and config_ok


def is_matrix_config_present(config):
url_server = config.matrix_server_url
sender_access_token = config.matrix_sender_access_token

if url_server is None or sender_access_token is None:
matrix_helper_logger.info('matrix url server not available')
return False

return True


def is_message_to_send_callback(status, time_original_request, scratch_dir, config, job_id, decoded_token=None):
log_additional_info_obj = {}
sending_ok = False
config_ok = is_matrix_config_ok(config)
time_check = time_.time()
sentry_for_matrix_message_sending_check = config.sentry_for_matrix_message_sending_check

Expand Down Expand Up @@ -467,7 +519,7 @@ def is_message_to_send_callback(status, time_original_request, scratch_dir, conf
additional_info_obj=log_additional_info_obj
)

return sending_ok
return sending_ok and config_ok


def log_matrix_message_sending_info(status, time_request, scratch_dir, job_id, additional_info_obj=None):
Expand Down
10 changes: 10 additions & 0 deletions cdci_data_analysis/config_dir/conf_env.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ dispatcher:
matrix_server_url: MATRIX_SERVER_URL
# access token of the sender account, from which the messages will be sent from
matrix_sender_access_token: MATRIX_SENDER_ACCESS_TOKEN
# list of additional room ids receivers towards which the message will be sent, besides the room id extracted from the token
matrix_bcc_receivers_room_ids: ['room_id']
# incident report related options, for the messages sent via matrix
incident_report_matrix_options:
# list of room ids receivers towards which the incident report message will be sent
matrix_incident_report_receivers_room_ids: ['room_id']
# personal access token of the sender of the incident report
matrix_incident_report_sender_personal_access_token: MATRIX_INCIDENT_REPORT_SENDER_ACCESS_TOKEN
# list of room ids to which the matrix message should be sent
matrix_cc_receivers_room_id: []
# enable/disable sending of messages via matrix in case of a submitted job status
matrix_message_sending_job_submitted: True
# amount of time (in seconds) elapsed from the sending of the last message on matrix
Expand Down
9 changes: 9 additions & 0 deletions cdci_data_analysis/configurer.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ def __init__(self, cfg_dict, origin=None):
disp_dict['email_options'].get('incident_report_email_options', {}).get('incident_report_receivers_email_addresses', None),
disp_dict.get('matrix_options', {}).get('matrix_server_url', None),
disp_dict.get('matrix_options', {}).get('matrix_sender_access_token', None),
disp_dict.get('matrix_options', {}).get('matrix_bcc_receivers_room_ids', []),
disp_dict.get('matrix_options', {}).get('incident_report_matrix_options', {}).get('matrix_incident_report_receivers_room_ids', []),
disp_dict.get('matrix_options', {}).get('incident_report_matrix_options', {}).get('matrix_incident_report_sender_personal_access_token', None),
disp_dict.get('matrix_options', {}).get('matrix_message_sending_job_submitted', True),
disp_dict.get('matrix_options', {}).get('matrix_message_sending_job_submitted_default_interval', 1800),
disp_dict.get('matrix_options', {}).get('sentry_for_matrix_message_sending_check', False),
Expand Down Expand Up @@ -322,6 +325,9 @@ def set_conf_dispatcher(self,
incident_report_receivers_email_addresses,
matrix_server_url,
matrix_sender_access_token,
matrix_bcc_receivers_room_ids,
matrix_incident_report_receivers_room_ids,
matrix_incident_report_sender_personal_access_token,
matrix_message_sending_job_submitted,
matrix_message_sending_job_submitted_default_interval,
sentry_for_matrix_message_sending_check,
Expand Down Expand Up @@ -369,6 +375,9 @@ def set_conf_dispatcher(self,
self.incident_report_receivers_email_addresses = incident_report_receivers_email_addresses
self.matrix_server_url = matrix_server_url
self.matrix_sender_access_token = matrix_sender_access_token
self.matrix_bcc_receivers_room_ids = matrix_bcc_receivers_room_ids
self.matrix_incident_report_receivers_room_ids = matrix_incident_report_receivers_room_ids
self.matrix_incident_report_sender_personal_access_token = matrix_incident_report_sender_personal_access_token
self.matrix_message_sending_job_submitted = matrix_message_sending_job_submitted
self.matrix_message_sending_job_submitted_default_interval = matrix_message_sending_job_submitted_default_interval
self.sentry_for_matrix_message_sending_check = sentry_for_matrix_message_sending_check
Expand Down
Loading

0 comments on commit fac1cac

Please sign in to comment.