teams-alerter 0.1.9__tar.gz → 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: teams-alerter
3
- Version: 0.1.9
3
+ Version: 0.2.0
4
4
  Summary: Module pour envoyer des alertes Teams via Pub/Sub
5
5
  Author-email: Toki <t.bakotondrabe-ext@paris-turf.com>
6
6
  License: MIT
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "teams-alerter"
7
- version = "0.1.9"
7
+ version = "0.2.0"
8
8
  description = "Module pour envoyer des alertes Teams via Pub/Sub"
9
9
  authors = [{ name = "Toki", email = "t.bakotondrabe-ext@paris-turf.com" }]
10
10
  readme = "README.md"
@@ -0,0 +1,94 @@
1
+ import json
2
+ import traceback
3
+
4
+ from google.cloud import pubsub_v1
5
+ from .utils import ErrorUtils, DateUtils, format_email_template_horse
6
+
7
+
8
+ class TeamsAlerter:
9
+
10
+ def __init__(
11
+ self,
12
+ utils: ErrorUtils,
13
+ payload: None,
14
+ ):
15
+ self.utils = utils
16
+ self.payload = payload
17
+
18
+ @staticmethod
19
+ def handle_error(error: Exception, utils: ErrorUtils) -> None:
20
+ error_type = type(error).__name__
21
+ error_message = str(error)
22
+ error_traceback = traceback.format_exc()
23
+ utc_timestamp = DateUtils.get_str_utc_timestamp()
24
+ utc_timestamp_minus_5min = DateUtils.get_str_utc_timestamp_minus_5min()
25
+ utc_timestamp_plus_5min = DateUtils.get_str_utc_timestamp_plus_5min()
26
+ url_log = f"https://console.cloud.google.com/logs/query;cursorTimestamp={utc_timestamp};startTime={utc_timestamp_minus_5min};endTime={utc_timestamp_plus_5min}?referrer=search&hl=fr&inv=1&invt=Ab5Y1Q&project={utils['app_project_id']}"
27
+ # detail = f"Error type: {error_type}\nError message: {error_message}\nError traceback: {error_traceback}"
28
+ detail = {"type": error_type, "message": error_message, "traceback": error_traceback}
29
+ level = "ERROR"
30
+
31
+ teams_alerter = TeamsAlerter(utils)
32
+ teams_alerter.format_payload(detail, level, url_log, utc_timestamp)
33
+ teams_alerter.publish_alert()
34
+
35
+ def publish_alert(self):
36
+ # Création d'un éditeur
37
+ publisher = pubsub_v1.PublisherClient()
38
+ topic_path = publisher.topic_path(self.utils["topic_project_id"], self.utils["topic_id"])
39
+
40
+ # Message à publier
41
+ data = json.dumps(self.payload).encode("utf-8")
42
+
43
+ # Publier le message
44
+ try:
45
+ publish_future = publisher.publish(topic_path, data)
46
+ publish_future.result()
47
+
48
+ except Exception as e:
49
+ self.utils["logger"](f"🟥Une erreur s'est produite lors de la publication du message : {e}")
50
+
51
+ def format_payload(self, detail, level, url_log, utc_timestamp):
52
+ app_list = {
53
+ "teams": [
54
+ "health_check_check_pg_wal_slot",
55
+ "health_check_check_meetings_ids",
56
+ "health_check_check_races_ids",
57
+ "health_check_check_partants_data",
58
+ "health_check_check_runners_ids",
59
+ ],
60
+ "email": [
61
+ "health_check_check_horses_stats",
62
+ ],
63
+ }
64
+
65
+ # base payload
66
+ self.payload = {
67
+ # base info
68
+ "app_name": self.utils["app_name"],
69
+ "detail": detail,
70
+ "level": level,
71
+ "environment": self.utils["env"],
72
+ "url_log": url_log,
73
+ "timestamp": utc_timestamp,
74
+ # alerting info
75
+ "alert_type": [], # teams, email
76
+ # "teams_channel": "",
77
+ # "teams_template": "",
78
+ # "email_template_html": "",
79
+ }
80
+
81
+ if self.utils["app_name"] in app_list["teams"]:
82
+ self.format_teams_template()
83
+
84
+ if self.utils["app_name"] in app_list["email"]:
85
+ self.format_email_template()
86
+
87
+ def format_teams_template(self):
88
+ self.payload["alert_type"].append("teams")
89
+ self.payload["teams_channel"] = self.utils["teams_channel"]
90
+ self.payload["teams_template"] = "card"
91
+
92
+ def format_email_template(self):
93
+ self.payload["alert_type"].append("email")
94
+ self.payload["email_template_html"] = format_email_template_horse()
@@ -0,0 +1,116 @@
1
+ import datetime
2
+
3
+ from typing import TypedDict
4
+ from google.cloud import logging
5
+
6
+
7
+ class ErrorUtils(TypedDict):
8
+ logger: logging.Logger
9
+ env: str
10
+ app_project_id: str
11
+ topic_project_id: str
12
+ topic_id: str
13
+ app_name: str
14
+ teams_channel: str
15
+
16
+
17
+ class DateUtils:
18
+ @staticmethod
19
+ def get_str_utc_timestamp():
20
+ dt = datetime.datetime.utcnow()
21
+ return dt.strftime("%Y-%m-%dT%H:%M:%S") + ".000000000Z"
22
+
23
+ @staticmethod
24
+ def get_str_utc_timestamp_minus_5min():
25
+ dt = datetime.datetime.utcnow() - datetime.timedelta(minutes=5)
26
+ return dt.strftime("%Y-%m-%dT%H:%M:%S") + ".000000000Z"
27
+
28
+ @staticmethod
29
+ def get_str_utc_timestamp_plus_5min():
30
+ dt = datetime.datetime.utcnow() + datetime.timedelta(minutes=5)
31
+ return dt.strftime("%Y-%m-%dT%H:%M:%S") + ".000000000Z"
32
+
33
+
34
+ def format_email_template_horse():
35
+ email_object = "Contrôle DATASTREAM - Fiche cheval"
36
+ email_message = """
37
+ Bonjour, <br>
38
+ Veuillez trouver ci-dessous le tableau récapitulatif du contrôle effectué sur la fiche cheval dans Datastream (champs formFigs et totalPrize) le 25/08/2025 à 13:15:00 UTC.
39
+ """
40
+ html = f"""
41
+ <body style="margin:0; padding:0; background:#f5f7fb;">
42
+ <table role="presentation" cellpadding="0" cellspacing="0" border="0" width="100%" style="background:#f5f7fb;">
43
+ <tr>
44
+ <td align="center" style="padding:24px;">
45
+ <table role="presentation" cellpadding="0" cellspacing="0" border="0" style="max-width:100%; background:#ffffff; border-radius:8px; border:1px solid #e6e9ef;">
46
+ <tr>
47
+ <td style="text-align: center; padding-top: 12px;">
48
+ <img style="height: 24px;" src="https://upload.wikimedia.org/wikipedia/fr/f/fd/Logo_Paris_Turf.svg" alt="" srcset="">
49
+ </td>
50
+ </tr>
51
+
52
+ <tr>
53
+ <td style="padding:24px 24px 12px 24px; font-family:Segoe UI, Arial, sans-serif; font-size:20px; line-height:26px; color:#111827; font-weight:700;">
54
+ Objet : {email_object}
55
+ </td>
56
+ </tr>
57
+
58
+ <tr>
59
+ <td style="padding:0 24px 16px 24px; font-family:Segoe UI, Arial, sans-serif; font-size:14px; line-height:20px; color:#4b5563;">
60
+ {email_message}
61
+ </td>
62
+ </tr>
63
+
64
+ <tr>
65
+ <td style="padding:0 16px 24px 16px;">
66
+ <table role="presentation" cellpadding="0" cellspacing="0" border="0" width="100%" style="border-collapse:collapse; font-family:Segoe UI, Arial, sans-serif;">
67
+ <!-- Header -->
68
+ <tr>
69
+ <th align="left" style="padding:12px 10px; font-size:12px; line-height:16px; color:#374151; text-transform:uppercase; letter-spacing:.5px; border-bottom:2px solid #e5e7eb; background:#f9fafb;">ID cheval</th>
70
+ <th align="left" style="padding:12px 10px; font-size:12px; line-height:16px; color:#374151; text-transform:uppercase; letter-spacing:.5px; border-bottom:2px solid #e5e7eb; background:#f9fafb;">Champ</th>
71
+ <th align="left" style="padding:12px 10px; font-size:12px; line-height:16px; color:#374151; text-transform:uppercase; letter-spacing:.5px; border-bottom:2px solid #e5e7eb; background:#f9fafb;">Postgres</th>
72
+ <th align="left" style="padding:12px 10px; font-size:12px; line-height:16px; color:#374151; text-transform:uppercase; letter-spacing:.5px; border-bottom:2px solid #e5e7eb; background:#f9fafb;">Mongo</th>
73
+ <th align="left" style="padding:12px 10px; font-size:12px; line-height:16px; color:#374151; text-transform:uppercase; letter-spacing:.5px; border-bottom:2px solid #e5e7eb; background:#f9fafb;">Différence</th>
74
+ </tr>
75
+ <!-- Row 1 -->
76
+ <tr>
77
+ <td style="padding:10px; font-size:14px; line-height:20px; color:#111827; border-bottom:1px solid #bbbbbb;">12345</td>
78
+ <td style="padding:10px; font-size:14px; line-height:20px; color:#111827; border-bottom:1px solid #bbbbbb;">formFigs</td>
79
+ <td style="padding:10px; font-size:14px; line-height:20px; color:#111827; border-bottom:1px solid #bbbbbb;">8a 6a Da (24) 7a 8a 10a ...</td>
80
+ <td style="padding:10px; font-size:14px; line-height:20px; color:#111827; border-bottom:1px solid #bbbbbb;">Da (24) 7a 8a 10a ...</td>
81
+ <td style="padding:10px; font-size:14px; line-height:20px; color:#111827; border-bottom:1px solid #bbbbbb;">Début tronqué (8a 6a manquants)</td>
82
+ </tr>
83
+ <!-- Row 2 -->
84
+ <tr>
85
+ <td style="padding:10px; font-size:14px; line-height:20px; color:#111827;">123456</td>
86
+ <td style="padding:10px; font-size:14px; line-height:20px; color:#111827;">totalPrize</td>
87
+ <td style="padding:10px; font-size:14px; line-height:20px; color:#111827;">108220</td>
88
+ <td style="padding:10px; font-size:14px; line-height:20px; color:#111827;">108222</td>
89
+ <td style="padding:10px; font-size:14px; line-height:20px; color:#111827;">-2</td>
90
+ </tr>
91
+ </table>
92
+ </td>
93
+ </tr>
94
+
95
+ <tr>
96
+ <td style="padding:0 24px 16px 24px; font-family:Segoe UI, Arial, sans-serif; font-size:14px; line-height:20px; color:#4b5563;">
97
+ Cordialement,
98
+ </td>
99
+ </tr>
100
+
101
+ <tr>
102
+ <td style="padding:0 24px 24px 24px; font-family:Segoe UI, Arial, sans-serif; font-size:12px; line-height:18px; color:#6b7280;">
103
+ <div style="border-top:1px solid #eef2f7; padding-top:12px;text-align: center;">
104
+ Message automatique – ne pas répondre. <br>
105
+ © 2025 Paris-Turf – Tous droits réservés <br>
106
+ <a href="https://www.paris-turf.com">www.paris-turf.com</a>
107
+ </div>
108
+ </td>
109
+ </tr>
110
+ </table>
111
+ </td>
112
+ </tr>
113
+ </table>
114
+ </body>
115
+ """
116
+ return html
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: teams-alerter
3
- Version: 0.1.9
3
+ Version: 0.2.0
4
4
  Summary: Module pour envoyer des alertes Teams via Pub/Sub
5
5
  Author-email: Toki <t.bakotondrabe-ext@paris-turf.com>
6
6
  License: MIT
@@ -1,62 +0,0 @@
1
- import json
2
- import traceback
3
-
4
- from google.cloud import pubsub_v1
5
- from .utils import ErrorUtils, DateUtils
6
-
7
-
8
- class TeamsAlerter:
9
-
10
- def __init__(
11
- self,
12
- utils: ErrorUtils,
13
- ):
14
- self.utils = utils
15
-
16
- @staticmethod
17
- def handle_error(error: Exception, utils: ErrorUtils) -> None:
18
- error_type = type(error).__name__
19
- error_message = str(error)
20
- error_traceback = traceback.format_exc()
21
- utc_timestamp = DateUtils.get_str_utc_timestamp()
22
- utc_timestamp_minus_5min = DateUtils.get_str_utc_timestamp_minus_5min()
23
- utc_timestamp_plus_5min = DateUtils.get_str_utc_timestamp_plus_5min()
24
- url_log = f"https://console.cloud.google.com/logs/query;cursorTimestamp={utc_timestamp};startTime={utc_timestamp_minus_5min};endTime={utc_timestamp_plus_5min}?referrer=search&hl=fr&inv=1&invt=Ab5Y1Q&project={utils['app_project_id']}"
25
- # detail = f"Error type: {error_type}\nError message: {error_message}\nError traceback: {error_traceback}"
26
- detail = {"type": error_type, "message": error_message, "traceback": error_traceback}
27
- level = "ERROR"
28
-
29
- teams_alerter = TeamsAlerter(utils)
30
-
31
- teams_alerter.publish_alert(detail, level, url_log, "card", utc_timestamp)
32
-
33
- def publish_alert(self, detail, level, url_log, teams_template, utc_timestamp):
34
-
35
- # Formatage du payload
36
- payload = json.dumps(
37
- {
38
- "app_name": self.utils["app_name"],
39
- "teams_channel": self.utils["teams_channel"],
40
- "detail": detail,
41
- "level": level,
42
- "environment": self.utils["env"],
43
- "url_log": url_log,
44
- "timestamp": utc_timestamp,
45
- "teams_template": teams_template,
46
- }
47
- )
48
-
49
- # Création d'un éditeur
50
- publisher = pubsub_v1.PublisherClient()
51
- topic_path = publisher.topic_path(self.utils["topic_project_id"], self.utils["topic_id"])
52
-
53
- # Message à publier
54
- data = payload.encode("utf-8")
55
-
56
- # Publier le message
57
- try:
58
- publish_future = publisher.publish(topic_path, data)
59
- publish_future.result()
60
-
61
- except Exception as e:
62
- self.utils["logger"](f"🟥Une erreur s'est produite lors de la publication du message : {e}")
@@ -1,31 +0,0 @@
1
- import datetime
2
-
3
- from typing import TypedDict
4
- from google.cloud import logging
5
-
6
-
7
- class ErrorUtils(TypedDict):
8
- logger: logging.Logger
9
- env: str
10
- app_project_id: str
11
- topic_project_id: str
12
- topic_id: str
13
- app_name: str
14
- teams_channel: str
15
-
16
-
17
- class DateUtils:
18
- @staticmethod
19
- def get_str_utc_timestamp():
20
- dt = datetime.datetime.utcnow()
21
- return dt.strftime("%Y-%m-%dT%H:%M:%S") + ".000000000Z"
22
-
23
- @staticmethod
24
- def get_str_utc_timestamp_minus_5min():
25
- dt = datetime.datetime.utcnow() - datetime.timedelta(minutes=5)
26
- return dt.strftime("%Y-%m-%dT%H:%M:%S") + ".000000000Z"
27
-
28
- @staticmethod
29
- def get_str_utc_timestamp_plus_5min():
30
- dt = datetime.datetime.utcnow() + datetime.timedelta(minutes=5)
31
- return dt.strftime("%Y-%m-%dT%H:%M:%S") + ".000000000Z"
File without changes
File without changes