Skip to content

Commit

Permalink
SVCPLAN-6428: Fix crashing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
PhongT16 committed Dec 2, 2024
1 parent 68c7e43 commit 440f79a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
13 changes: 4 additions & 9 deletions IndividualCalendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,10 @@ def get_individual_calendars(start_date, end_date, group_members, access_token):
'''

if response.status_code != 200:
logger.error(f"status code: {response.status_code}")
logger.error(f"start date: {start_date}")
logger.error(f"end date: {end_date}")
logger.error(f"group members: {group_members}")
logger.error(f"access_token: {access_token}")
logger.error(f"response header: {response.headers}")
logger.error(f"response header type: {type(response.headers)}")
message = 'Unable to retrieve individual calendar from the getSchedule endpoint'
message = 'Critical: Unable to retrieve individual calendar from the getSchedule endpoint'
utils.send_email(message, access_token)
#logger.error(response.json())
logger.error(f"response.text: \"{response.text}\"")
logger.error(f"response: {response}")
raise ConnectionError(message)

return response.json()
Expand Down Expand Up @@ -186,6 +179,8 @@ def filter(events):
Returns:
SimpleEvent list: a filtered list of events
"""
if not events:
return None

filtered_events = []
events.sort()
Expand Down
16 changes: 11 additions & 5 deletions OutlookCalendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
from msal import PublicClientApplication
import IndividualCalendar
import GenerateReport

import sys

def process_args():
parser = argparse.ArgumentParser(
prog = 'vacation_calendar_sync',
Expand Down Expand Up @@ -60,8 +61,10 @@ def retrieve_and_update_calendars(current_date, end_date, group_members, groupin
# Create a list of lists in chunks of size grouping
for group in [group_members[i : i + grouping] for i in range(0, len(group_members), grouping)]:
individual_calendars = IndividualCalendar.get_individual_calendars(current_date, end_date, group, access_token)
individual_calendars_events.extend(IndividualCalendar.process_individual_calendars(individual_calendars, current_date, end_date))

individual_events_block = IndividualCalendar.process_individual_calendars(individual_calendars, current_date, end_date)
if individual_events_block:
individual_calendars_events.extend(individual_events_block)

# Retrieve the shared calendar and process it
shared_calendar_id = SharedCalendar.get_shared_calendar_id(configs['shared_calendar_name'], access_token)
shared_calendar = SharedCalendar.get_shared_calendar(shared_calendar_id, current_date, end_date, access_token)
Expand Down Expand Up @@ -136,6 +139,9 @@ def main(configs):
time.sleep(configs['update_interval'])

if __name__ == '__main__':
# Redirects stdout stderr to out.log
sys.stderr = open('out.log', 'w')

configs = utils.get_configurations()

formater = logging.Formatter('%(name)s:%(asctime)s:%(filename)s:%(levelname)s:%(message)s')
Expand All @@ -151,9 +157,9 @@ def main(configs):
logger.setLevel(logging.DEBUG)
logger.addHandler(rotate_file_handler_info)

stream_handler = logging.StreamHandler()
stream_handler = logging.StreamHandler(sys.stderr)
stream_handler.setLevel(logging.DEBUG)
stream_handler.setFormatter(fmt=logging.Formatter('%(name)s:%(asctime)s:%(filename)s:%(levelname)s:%(message)s'))
logger.addHandler(stream_handler)

main(configs)
main(configs)
37 changes: 24 additions & 13 deletions SharedCalendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ def get_shared_calendar(shared_calendar_id, start_date, end_date, access_token):

if (response.status_code != 200):
message = f'Unable to retrieve shared calendar from {endpoint} endpoint'
utils.send_email(message)
#utils.send_email(message) BEFORE
utils.send_email(message, access_token)
#logger.error(response.json())
logger.error(f"response.text: {response.text}")
raise ConnectionError(message)
Expand Down Expand Up @@ -304,12 +305,18 @@ def check_add_response(batch, batch_responses, access_token):
if response["status"] == 201: # 201 is the response for Created
logger.info("Event {subject} on {date} was successfully added".format(subject=response['body']['subject'], date=response['body']['start']['dateTime']))
else:
id = int(response['id'])
subject = batch['requests'][id - 1]['body']['subject']
date = batch['requests'][id - 1]['body']['start']['dateTime']
logger.warning(f"Event {subject} on {date} was unccessfully added")
logger.warning(f"Error: {response['body']['error']}")
message = message + f"Event {subject} on {date} was unccessfully added\n"
event_id = response.get('id', None)
if event_id:
event_id = int(event_id)
subject = batch['requests'][event_id - 1]['body']['subject']
date = batch['requests'][event_id - 1]['body']['start']['dateTime']
logger.warning(f"Event {subject} on {date} was unccessfully added")
else:
logger.warning(f"An error occured")

err_body = response.get('body', None)
if err_body:
logger.warning(f"Error: {err_body}")

# if (len(message) != 0):
# utils.send_email(user_client, access_token, message)
Expand All @@ -330,7 +337,10 @@ def check_deleted_response(batch, batch_responses, access_token, info):
logger.info(f"Event {event[1]} on {event[2]} was succesfully deleted")
else:
logger.warning(f"Event {event[1]} on {event[2]} was unsuccesfully deleted")
logger.warning(f"Error: {response['body']['error']}")
#logger.warning(f"Error: {response['body']['error']}")
err_body = response.get('body', None)
if err_body:
logger.warning(f"Error: {err_body}")


def post_batch(access_token, batches, info=None):
Expand All @@ -353,10 +363,13 @@ def post_batch(access_token, batches, info=None):
response = requests.post(endpoint, data=json.dumps(batch), headers=header)
#print(batch)
if response.status_code != 200:
message = "Unable to post batch \n" + str(response.json()["error"])
response_as_dict = response.json()
err_body = response_as_dict.get('error', None)
message = f"Unable to post batch: {err_body}\n "
#utils.send_email(user_client, access_token, message)
utils.send_email(message, access_token)
logger.warning(message)
logger.warning(f"response.text: {response.text}")
#logger.warning(f"response.text: {response.text}")
#logger.warning(response.json())
continue

Expand Down Expand Up @@ -434,6 +447,4 @@ def create_category(access_token, category_name, category_color):
logger.error(f"response.text: {response.text}")
raise ConnectionError(message)
#print("category created")
return category_name


return category_name

0 comments on commit 440f79a

Please sign in to comment.