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

Automatically join a room #622

Merged
merged 14 commits into from
Nov 8, 2023
145 changes: 109 additions & 36 deletions cdci_data_analysis/analysis/matrix_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,24 +129,39 @@
# 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_content_incident_reports': [],
'res_content_incident_reports_failed': []
}

message_data = {
'message_data_incident_reports': []
}

for incident_report_receiver_room_id in incident_report_receivers_room_ids:
res_data_message_receiver = send_message(
logger,
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'])
if incident_report_receiver_room_id is not None and incident_report_receiver_room_id != "":
try:
join_room(

Check warning on line 143 in cdci_data_analysis/analysis/matrix_helper.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/analysis/matrix_helper.py#L141-L143

Added lines #L141 - L143 were not covered by tests
logger,
url_server=matrix_server_url,
sender_access_token=incident_report_sender_personal_access_token,
room_id=incident_report_receiver_room_id
)
res_data_message_receiver = send_message(

Check warning on line 149 in cdci_data_analysis/analysis/matrix_helper.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/analysis/matrix_helper.py#L149

Added line #L149 was not covered by tests
logger,
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'])
except MatrixMessageNotSent as e:
sentry.capture_message(f'message sending via matrix failed {e}')
logger.warning(f"Issue in sending a message in the room {incident_report_receiver_room_id} using matrix: {e.message}")
res_content['res_content_incident_reports_failed'].append(f"Issue in sending a message in the room {incident_report_receiver_room_id} using matrix: {e.message}")

Check warning on line 162 in cdci_data_analysis/analysis/matrix_helper.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/analysis/matrix_helper.py#L157-L162

Added lines #L157 - L162 were not covered by tests
else:
logger.warning('a incident report matrix message could not be sent as an invalid room id was provided')

Check warning on line 164 in cdci_data_analysis/analysis/matrix_helper.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/analysis/matrix_helper.py#L164

Added line #L164 was not covered by tests

store_incident_report_matrix_message(message_data, scratch_dir, sending_time=sending_time)

Expand Down Expand Up @@ -233,50 +248,108 @@
message_body_html = template.render(**matrix_message_data)
message_text = textify_matrix_message(message_body_html)
res_content = {
'res_content_bcc_users': []
'res_content_bcc_users': [],
'res_content_bcc_users_failed': []
}

message_data = {
'message_data_bcc_users': []
}
if receiver_room_id is not None and receiver_room_id != "":
res_data_message_token_user = send_message(
logger,
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:
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(
try:
join_room(

Check warning on line 260 in cdci_data_analysis/analysis/matrix_helper.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/analysis/matrix_helper.py#L259-L260

Added lines #L259 - L260 were not covered by tests
logger,
url_server=matrix_server_url,
sender_access_token=matrix_sender_access_token,
room_id=receiver_room_id
)
res_data_message_token_user = send_message(

Check warning on line 266 in cdci_data_analysis/analysis/matrix_helper.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/analysis/matrix_helper.py#L266

Added line #L266 was not covered by tests
logger,
url_server=matrix_server_url,
sender_access_token=matrix_sender_access_token,
room_id=bcc_receiver_room_id,
room_id=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_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
except MatrixMessageNotSent as e:
logger.warning(f"Issue in sending a message in the room {receiver_room_id} using matrix: {e.message}")
res_content['res_content_token_user_failure'] = f"Issue in sending a message in the room {receiver_room_id} using matrix: {e.message}"

Check warning on line 280 in cdci_data_analysis/analysis/matrix_helper.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/analysis/matrix_helper.py#L274-L280

Added lines #L274 - L280 were not covered by tests
else:
logger.warning('a matrix message could not be sent to the token user as no personal room id was '

Check warning on line 282 in cdci_data_analysis/analysis/matrix_helper.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/analysis/matrix_helper.py#L282

Added line #L282 was not covered by tests
'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 != "":
try:
join_room(

Check warning on line 288 in cdci_data_analysis/analysis/matrix_helper.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/analysis/matrix_helper.py#L285-L288

Added lines #L285 - L288 were not covered by tests
logger,
url_server=matrix_server_url,
sender_access_token=matrix_sender_access_token,
room_id=bcc_receiver_room_id
)
res_data_message_cc_user = send_message(

Check warning on line 294 in cdci_data_analysis/analysis/matrix_helper.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/analysis/matrix_helper.py#L294

Added line #L294 was not covered by tests
logger,
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)
except MatrixMessageNotSent as e:
logger.warning(f"Issue in sending a message in the room {bcc_receiver_room_id} using matrix: {e.message}")
res_content['res_content_bcc_users_failed'].append(f"Issue in sending a message in the room {bcc_receiver_room_id} using matrix: {e.message}")

Check warning on line 308 in cdci_data_analysis/analysis/matrix_helper.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/analysis/matrix_helper.py#L302-L308

Added lines #L302 - L308 were not covered by tests

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

return res_content


def join_room(
logger,
url_server=None,
sender_access_token=None,
room_id=None,
):
logger.info(f"Joining room wth id: {room_id}")
url = os.path.join(url_server, f'_matrix/client/v3/rooms/{room_id}/join')

Check warning on line 322 in cdci_data_analysis/analysis/matrix_helper.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/analysis/matrix_helper.py#L321-L322

Added lines #L321 - L322 were not covered by tests

headers = {

Check warning on line 324 in cdci_data_analysis/analysis/matrix_helper.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/analysis/matrix_helper.py#L324

Added line #L324 was not covered by tests
'Authorization': ' '.join(['Bearer', sender_access_token]),
'Content-type': 'application/json'
}

res = requests.post(url, headers=headers)

Check warning on line 329 in cdci_data_analysis/analysis/matrix_helper.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/analysis/matrix_helper.py#L329

Added line #L329 was not covered by tests

msg_response_data = None
if res.status_code in [403, 429]:
msg_response_data = res.json()
error_code = ""
if "errcode" in msg_response_data:
error_code = msg_response_data["errcode"]
error = ""
if "error" in msg_response_data:
error = msg_response_data["error"]
logger.info(f"Could not join the room: {room_id}, for the following reason: {error_code} - {error}")

Check warning on line 340 in cdci_data_analysis/analysis/matrix_helper.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/analysis/matrix_helper.py#L331-L340

Added lines #L331 - L340 were not covered by tests

sentry.capture_message(f"Could not join the room: {room_id}, for the following reason: {error_code}: {error}")
raise MatrixMessageNotSent(f"Could not join the room: {room_id}, for the following reason: {error_code}: {error}",

Check warning on line 343 in cdci_data_analysis/analysis/matrix_helper.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/analysis/matrix_helper.py#L342-L343

Added lines #L342 - L343 were not covered by tests
status_code=res.status_code,
payload={'matrix_error_message': f"{error_code} - {error}"})

elif res.status_code == 200:
logger.info(f"Successfully joined the room: {room_id}")

Check warning on line 348 in cdci_data_analysis/analysis/matrix_helper.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/analysis/matrix_helper.py#L347-L348

Added lines #L347 - L348 were not covered by tests

return msg_response_data

Check warning on line 350 in cdci_data_analysis/analysis/matrix_helper.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/analysis/matrix_helper.py#L350

Added line #L350 was not covered by tests


def send_message(
logger,
url_server=None,
Expand Down
12 changes: 5 additions & 7 deletions cdci_data_analysis/flask_app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1054,21 +1054,19 @@
scratch_dir=scratch_dir
)

report_incident_status['martix_message_report_status'] = 'incident report message successfully sent via matrix'
matrix_message_report_status = 'incident report message successfully sent via matrix'
if len(res_content['res_content_incident_reports_failed']) >= 1:
matrix_message_report_status = 'sending of an incident report message via matrix failed'

Check warning on line 1059 in cdci_data_analysis/flask_app/app.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/flask_app/app.py#L1059

Added line #L1059 was not covered by tests

report_incident_status['martix_message_report_status'] = matrix_message_report_status
report_incident_status['martix_message_report_status_details'] = {
"res_content": res_content
}
except matrix_helper.MatrixMessageNotSent as e:
report_incident_status['martix_message_report_status'] = 'sending message via matrix failed'
logging.warning(f'message sending via matrix failed: {e}')
sentry.capture_message(f'message sending via matrix failed {e}')

except MissingRequestParameter as e:
report_incident_status['martix_message_report_status'] = 'sending message via matrix failed'
logging.warning(f'parameter missing during call back: {e}')

# response = jsonify({'report_incident_status': report_incident_status})

return report_incident_status


Expand Down
133 changes: 64 additions & 69 deletions cdci_data_analysis/flask_app/dispatcher_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -1151,49 +1151,44 @@
logging.warning(f'issue when {step}: {e}')
sentry.capture_message(f'issue when {step}: {e}')

try:
if is_message_to_send:
time_request = time_original_request
time_request_first_submitted = matrix_helper.get_first_submitted_matrix_message_time(self.scratch_dir)
if time_request_first_submitted is not None:
time_request = time_request_first_submitted

res_content = matrix_helper.send_job_message(
config=self.app.config['conf'],
logger=self.logger,
decoded_token=self.decoded_token,
token=self.token,
job_id=self.job_id,
session_id=self.par_dic['session_id'],
status=status,
instrument=self.instrument_name,
status_details=status_details,
product_type=product_type,
time_request=time_request,
request_url=products_url,
api_code=email_api_code,
scratch_dir=self.scratch_dir)

matrix_message_status_details = {
"res_content": res_content
}
if status_details is not None:
matrix_message_status_details['status_details'] = status_details
if is_message_to_send:
time_request = time_original_request
time_request_first_submitted = matrix_helper.get_first_submitted_matrix_message_time(self.scratch_dir)
if time_request_first_submitted is not None:
time_request = time_request_first_submitted

Check warning on line 1158 in cdci_data_analysis/flask_app/dispatcher_query.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/flask_app/dispatcher_query.py#L1155-L1158

Added lines #L1155 - L1158 were not covered by tests

res_content = matrix_helper.send_job_message(

Check warning on line 1160 in cdci_data_analysis/flask_app/dispatcher_query.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/flask_app/dispatcher_query.py#L1160

Added line #L1160 was not covered by tests
config=self.app.config['conf'],
logger=self.logger,
decoded_token=self.decoded_token,
token=self.token,
job_id=self.job_id,
session_id=self.par_dic['session_id'],
status=status,
instrument=self.instrument_name,
status_details=status_details,
product_type=product_type,
time_request=time_request,
request_url=products_url,
api_code=email_api_code,
scratch_dir=self.scratch_dir)

matrix_message_status_details = {

Check warning on line 1176 in cdci_data_analysis/flask_app/dispatcher_query.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/flask_app/dispatcher_query.py#L1176

Added line #L1176 was not covered by tests
"res_content": res_content
}
if status_details is not None:
matrix_message_status_details['status_details'] = status_details

Check warning on line 1180 in cdci_data_analysis/flask_app/dispatcher_query.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/flask_app/dispatcher_query.py#L1179-L1180

Added lines #L1179 - L1180 were not covered by tests

job.write_dataserver_status(status_dictionary_value=status,
full_dict=self.par_dic,
matrix_message_status='matrix message sent',
matrix_message_status_details=json.dumps(matrix_message_status_details))
else:
job.write_dataserver_status(status_dictionary_value=status, full_dict=self.par_dic)
matrix_message_status = 'matrix message sent'
if 'res_content_token_user_failure' in res_content or len(res_content['res_content_bcc_users_failed']) >= 1:
matrix_message_status = 'sending message via matrix failed'

Check warning on line 1184 in cdci_data_analysis/flask_app/dispatcher_query.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/flask_app/dispatcher_query.py#L1182-L1184

Added lines #L1182 - L1184 were not covered by tests

except matrix_helper.MatrixMessageNotSent as e:
job.write_dataserver_status(status_dictionary_value=status,
full_dict=self.par_dic,
matrix_message_status='sending message via matrix failed',
matrix_message_status_details=e.payload)
logging.warning(f'matrix message sending failed: {e}')
sentry.capture_message(f'sending matrix message failed {e.message}')
matrix_message_status=matrix_message_status,
matrix_message_status_details=json.dumps(matrix_message_status_details))
else:
job.write_dataserver_status(status_dictionary_value=status, full_dict=self.par_dic)

try:
# TODO for a future implementation
Expand Down Expand Up @@ -1973,37 +1968,37 @@
self.job_id,
self.app.config['conf'],
decoded_token=self.decoded_token):
try:
time_request = self.time_request
time_request_first_submitted = matrix_helper.get_first_submitted_matrix_message_time(self.scratch_dir)
if time_request_first_submitted is not None:
time_request = time_request_first_submitted

res_content = matrix_helper.send_job_message(
config=self.app.config['conf'],
logger=self.logger,
decoded_token=self.decoded_token,
token=self.token,
job_id=self.job_id,
session_id=self.par_dic['session_id'],
status=query_new_status,
instrument=self.instrument.name,
product_type=product_type,
time_request=time_request,
request_url=products_url,
api_code=email_api_code,
scratch_dir=self.scratch_dir)

matrix_message_status_details = json.dumps({
"res_content": res_content
})
query_out.set_status_field('matrix_message_status', 'matrix message sent')
query_out.set_status_field('matrix_message_status_details', matrix_message_status_details)
except matrix_helper.MatrixMessageNotSent as e:
query_out.set_status_field('matrix_message_status', 'sending matrix message failed')
query_out.set_status_field('matrix_message_status_details', e.payload)
logging.warning(f'matrix message sending failed: {e}')
sentry.capture_message(f'sending matrix message failed {e.message}')
time_request = self.time_request
time_request_first_submitted = matrix_helper.get_first_submitted_matrix_message_time(self.scratch_dir)
if time_request_first_submitted is not None:
time_request = time_request_first_submitted

Check warning on line 1975 in cdci_data_analysis/flask_app/dispatcher_query.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/flask_app/dispatcher_query.py#L1972-L1975

Added lines #L1972 - L1975 were not covered by tests

res_content = matrix_helper.send_job_message(

Check warning on line 1977 in cdci_data_analysis/flask_app/dispatcher_query.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/flask_app/dispatcher_query.py#L1977

Added line #L1977 was not covered by tests
config=self.app.config['conf'],
logger=self.logger,
decoded_token=self.decoded_token,
token=self.token,
job_id=self.job_id,
session_id=self.par_dic['session_id'],
status=query_new_status,
instrument=self.instrument.name,
product_type=product_type,
time_request=time_request,
request_url=products_url,
api_code=email_api_code,
scratch_dir=self.scratch_dir)

matrix_message_status_details = json.dumps({

Check warning on line 1992 in cdci_data_analysis/flask_app/dispatcher_query.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/flask_app/dispatcher_query.py#L1992

Added line #L1992 was not covered by tests
"res_content": res_content
})

matrix_message_status = 'matrix message sent'
if 'res_content_token_user_failure' in res_content or len(res_content['res_content_bcc_users_failed']) >= 1:
matrix_message_status = 'sending message via matrix failed'

Check warning on line 1998 in cdci_data_analysis/flask_app/dispatcher_query.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/flask_app/dispatcher_query.py#L1996-L1998

Added lines #L1996 - L1998 were not covered by tests

query_out.set_status_field('matrix_message_status', matrix_message_status)
query_out.set_status_field('matrix_message_status_details', matrix_message_status_details)

Check warning on line 2001 in cdci_data_analysis/flask_app/dispatcher_query.py

View check run for this annotation

Codecov / codecov/patch

cdci_data_analysis/flask_app/dispatcher_query.py#L2000-L2001

Added lines #L2000 - L2001 were not covered by tests

if email_helper.is_email_to_send_run_query(self.logger,
query_new_status,
Expand Down
Loading
Loading