Skip to content

Commit 1a2bc49

Browse files
alerts.py implementation
1 parent 99a3572 commit 1a2bc49

File tree

3 files changed

+82
-82
lines changed

3 files changed

+82
-82
lines changed

alerts.py

+77-77
Original file line numberDiff line numberDiff line change
@@ -5,80 +5,80 @@
55
from utilities import *
66

77

8-
# class Alerts:
9-
# def __init__(self):
10-
# self.logger_sub_path = "/logger"
11-
#
12-
# @staticmethod
13-
# def get_board_by_id(id):
14-
# url = "https://api.trello.com/1/boards/" + id + "/"
15-
# querystring = {"actions": "all", "boardStars": "none", "cards": "none", "card_pluginData": "false",
16-
# "checklists": "none",
17-
# "customFields": "false",
18-
# "fields": "name,desc,descData,closed,idOrganization,pinned,url,shortUrl,prefs,labelNames",
19-
# "lists": "open", "members": "none", "memberships": "none", "membersInvited": "none",
20-
# "membersInvited_fields": "all", "pluginData": "false", "organization": "false",
21-
# "organization_pluginData": "false", "myPrefs": "false",
22-
# "tags": "false", "key": TRELLO_API_KEY, "token": TRELLO_SERVER_TOKEN}
23-
# requests.request("GET", url, params=querystring)
24-
#
25-
# @staticmethod
26-
# def get_lists_on_a_board(board_id):
27-
# url = "https://api.trello.com/1/boards/" + board_id + "/lists"
28-
# querystring = {"key": TRELLO_API_KEY, "token": TRELLO_SERVER_TOKEN}
29-
# requests.request("GET", url, params=querystring)
30-
#
31-
# @staticmethod
32-
# def get_labels_on_a_board(board_id):
33-
# url = "https://api.trello.com/1/boards/" + board_id + "/labels"
34-
# querystring = {"key": TRELLO_API_KEY, "token": TRELLO_SERVER_TOKEN}
35-
# requests.request("GET", url, params=querystring)
36-
#
37-
# @staticmethod
38-
# def create_a_new_card_with_alert_message(list_id, labels_id, alert_message):
39-
# url = "https://api.trello.com/1/cards"
40-
# name = time.strftime("%Y%m%d") + "_" + time.strftime("%H%M%S") + "_" + "Phoenix_Down_Script_ALERT"
41-
# position = "top"
42-
# querystring = {"key": TRELLO_API_KEY, "token": TRELLO_SERVER_TOKEN, "name": name, "desc": alert_message,
43-
# "pos": position, "idList": list_id, "idLabels": labels_id}
44-
# requests.request("POST", url, params=querystring)
45-
#
46-
# @staticmethod
47-
# def parse_logger_file_and_create_alert_mail_message(logger_file_to_parse):
48-
# warnings_count = 0
49-
# errors_count = 0
50-
# script_starting_fail = True
51-
# script_ending_fail = True
52-
# # opens the file for reading only
53-
# file = open(logger_file_to_parse, "r")
54-
# for line in file.readlines():
55-
# if "[WARNING]" in line:
56-
# warnings_count += 1
57-
# if "[ERROR]" in line:
58-
# errors_count += 1
59-
# if PD_SCRIPT_STARTING_MESSAGE in line:
60-
# script_starting_fail = False
61-
# if PD_SCRIPT_ENDING_MESSAGE in line:
62-
# script_ending_fail = False
63-
# file.close()
64-
# if script_starting_fail or script_ending_fail:
65-
# alert_mail_message = "FATAL ERROR : There was a fatal error during the execution of the script, " \
66-
# "which could not be completed. Please check that the script is working properly !"
67-
# else:
68-
# alert_mail_message = "During the last execution of Phoenix Down Script, " \
69-
# "the logger returned " + str(warnings_count) + \
70-
# " warning messages, and " \
71-
# + str(errors_count) + " error messages. " \
72-
# "Please check that the script is working properly !"
73-
# return alert_mail_message, script_starting_fail, script_ending_fail, warnings_count, errors_count
74-
#
75-
# def run_script(self):
76-
# logger_file_to_parse = get_the_latest_file_in_a_folder(PD_SCRIPT_ROOT_LOGS_PATH + self.logger_sub_path)
77-
#
78-
# callback_alert_infos = self.parse_logger_file_and_create_alert_mail_message(logger_file_to_parse)
79-
# if callback_alert_infos[1] or callback_alert_infos[2] \
80-
# or callback_alert_infos[3] > 0 or callback_alert_infos[4] > 0:
81-
# self.create_a_new_card_with_alert_message(TRELLO_ALERT_BOARD_ALERT_LIST_ID,
82-
# TRELLO_ALERT_URGENT_CUSTOM_LABEL_ID, callback_alert_infos[0])
83-
# self.create_a_new_card_with_alert_message(TRELLO_MBL_BOARD_ADMIN_LIST_ID,
84-
# TRELLO_MBL_URGENT_CUSTOM_LABEL_ID, callback_alert_infos[0])
8+
class Alerts:
9+
def __init__(self):
10+
self.logger_sub_path = "/logger"
11+
12+
@staticmethod
13+
def get_board_by_id(id):
14+
url = "https://api.trello.com/1/boards/" + id + "/"
15+
querystring = {"actions": "all", "boardStars": "none", "cards": "none", "card_pluginData": "false",
16+
"checklists": "none",
17+
"customFields": "false",
18+
"fields": "name,desc,descData,closed,idOrganization,pinned,url,shortUrl,prefs,labelNames",
19+
"lists": "open", "members": "none", "memberships": "none", "membersInvited": "none",
20+
"membersInvited_fields": "all", "pluginData": "false", "organization": "false",
21+
"organization_pluginData": "false", "myPrefs": "false",
22+
"tags": "false", "key": TRELLO_API_KEY, "token": TRELLO_SERVER_TOKEN}
23+
requests.request("GET", url, params=querystring)
24+
25+
@staticmethod
26+
def get_lists_on_a_board(board_id):
27+
url = "https://api.trello.com/1/boards/" + board_id + "/lists"
28+
querystring = {"key": TRELLO_API_KEY, "token": TRELLO_SERVER_TOKEN}
29+
requests.request("GET", url, params=querystring)
30+
31+
@staticmethod
32+
def get_labels_on_a_board(board_id):
33+
url = "https://api.trello.com/1/boards/" + board_id + "/labels"
34+
querystring = {"key": TRELLO_API_KEY, "token": TRELLO_SERVER_TOKEN}
35+
requests.request("GET", url, params=querystring)
36+
37+
@staticmethod
38+
def create_a_new_card_with_alert_message(list_id, labels_id, alert_message):
39+
url = "https://api.trello.com/1/cards"
40+
name = time.strftime("%Y%m%d") + "_" + time.strftime("%H%M%S") + "_" + "Phoenix_Down_Script_ALERT"
41+
position = "top"
42+
querystring = {"key": TRELLO_API_KEY, "token": TRELLO_SERVER_TOKEN, "name": name, "desc": alert_message,
43+
"pos": position, "idList": list_id, "idLabels": labels_id}
44+
requests.request("POST", url, params=querystring)
45+
46+
@staticmethod
47+
def parse_logger_file_and_create_alert_mail_message(logger_file_to_parse):
48+
warnings_count = 0
49+
errors_count = 0
50+
script_starting_fail = True
51+
script_ending_fail = True
52+
# opens the file for reading only
53+
file = open(logger_file_to_parse, "r")
54+
for line in file.readlines():
55+
if "[WARNING]" in line:
56+
warnings_count += 1
57+
if "[ERROR]" in line:
58+
errors_count += 1
59+
if SLIMER_SCRIPT_STARTING_MESSAGE in line:
60+
script_starting_fail = False
61+
if SLIMER_SCRIPT_ENDING_MESSAGE in line:
62+
script_ending_fail = False
63+
file.close()
64+
if script_starting_fail or script_ending_fail:
65+
alert_mail_message = "FATAL ERROR : There was a fatal error during the execution of the script, " \
66+
"which could not be completed. Please check that the script is working properly !"
67+
else:
68+
alert_mail_message = "During the last execution of Phoenix Down Script, " \
69+
"the logger returned " + str(warnings_count) + \
70+
" warning messages, and " \
71+
+ str(errors_count) + " error messages. " \
72+
"Please check that the script is working properly !"
73+
return alert_mail_message, script_starting_fail, script_ending_fail, warnings_count, errors_count
74+
75+
def run_script(self):
76+
logger_file_to_parse = get_the_latest_file_in_a_folder(SLIMER_SCRIPT_ROOT_LOGGER_PATH)
77+
78+
callback_alert_infos = self.parse_logger_file_and_create_alert_mail_message(logger_file_to_parse)
79+
if callback_alert_infos[1] or callback_alert_infos[2] \
80+
or callback_alert_infos[3] > 0 or callback_alert_infos[4] > 0:
81+
self.create_a_new_card_with_alert_message(TRELLO_ALERT_BOARD_ALERT_LIST_ID,
82+
TRELLO_ALERT_URGENT_CUSTOM_LABEL_ID, callback_alert_infos[0])
83+
self.create_a_new_card_with_alert_message(TRELLO_MBL_BOARD_ADMIN_LIST_ID,
84+
TRELLO_MBL_URGENT_CUSTOM_LABEL_ID, callback_alert_infos[0])

logger_setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
file_name = create_timestamped_and_named_file_name(APPLICATION_NAME, LOGGER_FILE_END_NAME)
1717

1818
# creation of the first handler which redirect traces to a log file
19-
file_handler = RotatingFileHandler(SLIMER_SCRIPT_ROOT_LOGS_PATH + '/' + file_name, 'a', 1000000, 1)
19+
file_handler = RotatingFileHandler(SLIMER_SCRIPT_ROOT_LOGGER_PATH + '/' + file_name, 'a', 1000000, 1)
2020

2121
# set level of the first handler to DEBUG
2222
file_handler.setLevel(logging.INFO)
@@ -36,7 +36,7 @@
3636
# LOGGER SCRIPT --------------------------------------------------------------------------------------------------------
3737

3838
def logger_script():
39-
latest_logger_file = get_the_latest_file_in_a_folder(SLIMER_SCRIPT_ROOT_LOGS_PATH)
39+
latest_logger_file = get_the_latest_file_in_a_folder(SLIMER_SCRIPT_ROOT_LOGGER_PATH)
4040

4141
# opens the file for reading only in binary format in order to upload
4242
file = open(latest_logger_file, "rb")

main.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
logging.info(SLIMER_SCRIPT_STARTING_MESSAGE)
2222

23-
logger_folder = create_directory(SLIMER_SCRIPT_ROOT_APP_PATH)
23+
logger_folder = create_directory(SLIMER_SCRIPT_ROOT_LOGS_PATH)
2424

2525
try:
2626
slimer_script()
@@ -31,5 +31,5 @@
3131

3232
logger_script()
3333

34-
# alm = Alerts()
35-
# alm.run_script()
34+
alm = Alerts()
35+
alm.run_script()

0 commit comments

Comments
 (0)