teams-alerter 0.2.2__tar.gz → 0.2.4__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.2.2
3
+ Version: 0.2.4
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.2.2"
7
+ version = "0.2.4"
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"
@@ -2,7 +2,7 @@ import json
2
2
  import traceback
3
3
 
4
4
  from google.cloud import pubsub_v1
5
- from .utils import ErrorUtils, DateUtils, format_email_template_horse
5
+ from .utils import ErrorUtils, DateUtils, format_email_template
6
6
 
7
7
 
8
8
  class TeamsAlerter:
@@ -91,4 +91,33 @@ class TeamsAlerter:
91
91
 
92
92
  def format_email_template(self):
93
93
  self.payload["alert_type"].append("email")
94
- self.payload["email_template_html"] = format_email_template_horse()
94
+
95
+ if self.utils["app_name"] == "health_check_check_horses_stats":
96
+ error_info_list = json.loads(self.payload["detail"]["message"])
97
+ table_data = [("ID CHEVAL", "CHAMP", "POSTGRES", "MONGO", "DIFFERENCE")]
98
+ for error_info in error_info_list["data"]:
99
+ table_data.append(
100
+ (
101
+ error_info["idCheval"],
102
+ error_info["champ"],
103
+ error_info["postgres"],
104
+ error_info["mongo"],
105
+ error_info["difference"],
106
+ )
107
+ )
108
+
109
+ email_object = "Contrôle DATASTREAM - Fiche cheval"
110
+ email_messages = [
111
+ """
112
+ Bonjour, <br>
113
+ Veuillez trouver ci-dessous le tableau récapitulatif du contrôle effectué sur la fiche cheval dans Datastream.
114
+ """,
115
+ f"""
116
+ Env: <strong>{self.utils["env"]}</strong> <br>
117
+ Timestamp: {DateUtils.get_str_utc_timestamp()} <br>
118
+ Champs: <strong>formFigs et/ou totalPrize</strong>
119
+ """,
120
+ ]
121
+
122
+ self.payload["email_template_html"] = format_email_template(email_object, email_messages, table_data)
123
+ print(self.payload["email_template_html"])
@@ -0,0 +1,124 @@
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(email_object, email_messages, table_data):
35
+ html = f"""
36
+ <html>
37
+ <head lang="fr">
38
+ <meta charset="utf-8">
39
+ <meta name="x-apple-disable-message-reformatting">
40
+ <meta name="viewport" content="width=device-width, initial-scale=1">
41
+ <title>Contrôle datastream</title>
42
+ </head>
43
+ <body style="margin:0; padding:0; background:#f5f7fb;">
44
+ <table role="presentation" cellpadding="0" cellspacing="0" border="0" width="100%" style="background:#f5f7fb;">
45
+ <tr>
46
+ <td align="center" style="padding:24px;">
47
+ <table role="presentation" cellpadding="0" cellspacing="0" border="0" style="max-width:100%; background:#ffffff; border-radius:8px; border:1px solid #e6e9ef;">
48
+ <tr>
49
+ <td style="text-align: center; padding-top: 12px;">
50
+ <img style="height: 24px;" src="https://upload.wikimedia.org/wikipedia/fr/f/fd/Logo_Paris_Turf.svg" alt="" srcset="">
51
+ </td>
52
+ </tr>
53
+
54
+ <tr>
55
+ <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;">
56
+ Objet : {email_object}
57
+ </td>
58
+ </tr>
59
+
60
+ {build_html_message(email_messages)}
61
+
62
+ <tr>
63
+ <td style="padding:0 16px 24px 16px;">
64
+ {build_html_table(table_data)}
65
+ </td>
66
+ </tr>
67
+
68
+ <tr>
69
+ <td style="padding:0 24px 16px 24px; font-family:Segoe UI, Arial, sans-serif; font-size:14px; line-height:20px; color:#4b5563;">
70
+ Cordialement,
71
+ </td>
72
+ </tr>
73
+
74
+ <tr>
75
+ <td style="padding:0 24px 24px 24px; font-family:Segoe UI, Arial, sans-serif; font-size:12px; line-height:18px; color:#6b7280;">
76
+ <div style="border-top:1px solid #eef2f7; padding-top:12px;text-align: center;">
77
+ Message automatique – ne pas répondre. <br>
78
+ © 2025 Paris-Turf – Tous droits réservés <br>
79
+ <a href="https://www.paris-turf.com">www.paris-turf.com</a>
80
+ </div>
81
+ </td>
82
+ </tr>
83
+ </table>
84
+ </td>
85
+ </tr>
86
+ </table>
87
+ </body>
88
+ </html>
89
+ """
90
+ return html
91
+
92
+
93
+ def build_html_table(table_data: list):
94
+ html_table = '<table role="presentation" cellpadding="0" cellspacing="0" border="0" width="100%" style="border-collapse:collapse; font-family:Segoe UI, Arial, sans-serif;">'
95
+
96
+ # format header
97
+ html_table += "<tr>"
98
+ for header in table_data[0]:
99
+ html_table += f'<th style="padding:10px; font-size:14px; line-height:20px; color:#111827; border-bottom:1px solid #bbbbbb;">{header}</th>'
100
+ html_table += "</tr>"
101
+
102
+ # format rows
103
+ for row in table_data[1:]:
104
+ html_table += "<tr>"
105
+ for cell in row:
106
+ html_table += f'<td style="padding:10px; font-size:14px; line-height:20px; color:#111827; border-bottom:1px solid #bbbbbb;">{cell}</td>'
107
+ html_table += "</tr>"
108
+
109
+ html_table += "</table>"
110
+
111
+ return html_table
112
+
113
+
114
+ def build_html_message(email_messages: list[str]):
115
+ content = ""
116
+ for email_message in email_messages:
117
+ content += f"""
118
+ <tr>
119
+ <td style="padding:0 24px 16px 24px; font-family:Segoe UI, Arial, sans-serif; font-size:14px; line-height:20px; color:#4b5563;">
120
+ {email_message}
121
+ </td>
122
+ </tr>
123
+ """
124
+ return content
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: teams-alerter
3
- Version: 0.2.2
3
+ Version: 0.2.4
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
@@ -7,4 +7,6 @@ teams_alerter.egg-info/PKG-INFO
7
7
  teams_alerter.egg-info/SOURCES.txt
8
8
  teams_alerter.egg-info/dependency_links.txt
9
9
  teams_alerter.egg-info/requires.txt
10
- teams_alerter.egg-info/top_level.txt
10
+ teams_alerter.egg-info/top_level.txt
11
+ tests/__init__.py
12
+ tests/test_core.py
File without changes
@@ -0,0 +1,51 @@
1
+ import pytest
2
+ import os
3
+ import json
4
+
5
+ from teams_alerter.core import TeamsAlerter
6
+
7
+
8
+ def test_error_handler():
9
+ env = "dev"
10
+ ERROR_UTILS = {
11
+ "env": env,
12
+ "app_project_id": os.environ.get("ERROR_HANDLER_APP_PROJECT_ID", "betin-horse-datastream-" + env),
13
+ "topic_project_id": os.environ.get("ERROR_HANDLER_TOPIC_PROJECT_ID", "betin-horse-datastream-" + env),
14
+ "topic_id": os.environ.get("ERROR_HANDLER_TOPIC_ID", "topic-datastream-errors-" + env),
15
+ "app_name": "health_check_check_horses_stats",
16
+ "teams_channel": "datastream-alerts-" + env,
17
+ }
18
+ diffs_list = [
19
+ {
20
+ "idCheval": 1338148,
21
+ "champ": "formFigs",
22
+ "postgres": '<span style="background-color:yellow; color:#000;">5a</span> 8a 8a 3a (24) 13a 12a 9a 10a Da 7a 7m 9a 10a 8m 6a 0a Da 10a 7a 7a (23) 11a 1a 2a 5a 10a 4a Aa 3a 10a Aa 3a 10a 2a 3a 3a 3a Da 4a 7a (22) 7a 3a 3a 1a 12a 6a 1a 1a 2a 7a 1a 4a 4a 1a 6a 1a 4a 10a (21) 14a 4a 5a 4a 1a 3a 2a 4a 7a 3a 5a 4a 6a 2a 2a 7a 7a 7a (20) 1a 1a 2a 4a 6a 1a 3a 5a 1a 1a 4a 1a 3a 4a 2a 2a (19) 4a 5a Da Da 4a 5Da 3a 5a 1a 4a (18) Da Da 5a 2a',
23
+ "mongo": "8a 8a 3a (24) 13a 12a 9a 10a Da 7a 7m 9a 10a 8m 6a 0a Da 10a 7a 7a (23) 11a 1a 2a 5a 10a 4a Aa 3a 10a Aa 3a 10a 2a 3a 3a 3a Da 4a 7a (22) 7a 3a 3a 1a 12a 6a 1a 1a 2a 7a 1a 4a 4a 1a 6a 1a 4a 10a (21) 14a 4a 5a 4a 1a 3a 2a 4a 7a 3a 5a 4a 6a 2a 2a 7a 7a 7a (20) 1a 1a 2a 4a 6a 1a 3a 5a 1a 1a 4a 1a 3a 4a 2a 2a (19) 4a 5a Da Da 4a 5Da 3a 5a 1a 4a (18) Da Da 5a 2a",
24
+ "difference": "5a manquants dans Mongo",
25
+ },
26
+ {
27
+ "idCheval": 1338148,
28
+ "champ": "totalPrize",
29
+ "postgres": 308670,
30
+ "mongo": 304920,
31
+ "difference": "3750 de moins dans Mongo",
32
+ },
33
+ {
34
+ "idCheval": 1343727,
35
+ "champ": "formFigs",
36
+ "postgres": '<span style="background-color:yellow; color:#000;">4a</span> <span style="background-color:yellow; color:#000;">4a</span> 4m 9m 7a Da 9a 3m (24) 6a 1a 1a 5a 6a 1a Da 4a 6a 4a 3a 5a 2a 0a (23) 9a 6a Da Aa 1a 0a 3a Da 4m 7a 7a Da 3a 4Dista 5a 9a (22) 13a 11a 6a Aa 4a 8a 3a 2a 3a 4a 4a 3a 6a 8a (21) 1a Da 11a 9a 6a 3a 3a 5a 3a Da 2a 4a 1a 11a Da 6a 2a (20) 1a 11a Da 3a Aa 3a 3a 9a 1a 8a 5a (19) 4a 2a 5a 4a 1a 7a',
37
+ "mongo": "4m 9m 7a Da 9a 3m (24) 6a 1a 1a 5a 6a 1a Da 4a 6a 4a 3a 5a 2a 0a (23) 9a 6a Da Aa 1a 0a 3a Da 4m 7a 7a Da 3a 4Dista 5a 9a (22) 13a 11a 6a Aa 4a 8a 3a 2a 3a 4a 4a 3a 6a 8a (21) 1a Da 11a 9a 6a 3a 3a 5a 3a Da 2a 4a 1a 11a Da 6a 2a (20) 1a 11a Da 3a Aa 3a 3a 9a 1a 8a 5a (19) 4a 2a 5a 4a 1a 7a",
38
+ "difference": "4a 4a manquants dans Mongo",
39
+ },
40
+ {
41
+ "idCheval": 1343727,
42
+ "champ": "totalPrize",
43
+ "postgres": 163935,
44
+ "mongo": 159775,
45
+ "difference": "4160 de moins dans Mongo",
46
+ },
47
+ ]
48
+ error = ValueError(
49
+ ValueError(json.dumps({"data": diffs_list, "message": f"[Vérification horses_stats du 2025-08-29]"}))
50
+ )
51
+ TeamsAlerter.handle_error(error, ERROR_UTILS)
@@ -1,114 +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"
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
- <table role="presentation" cellpadding="0" cellspacing="0" border="0" width="100%" style="background:#f5f7fb;">
42
- <tr>
43
- <td align="center" style="padding:24px;">
44
- <table role="presentation" cellpadding="0" cellspacing="0" border="0" style="max-width:100%; background:#ffffff; border-radius:8px; border:1px solid #e6e9ef;">
45
- <tr>
46
- <td style="text-align: center; padding-top: 12px;">
47
- <img style="height: 24px;" src="https://upload.wikimedia.org/wikipedia/fr/f/fd/Logo_Paris_Turf.svg" alt="" srcset="">
48
- </td>
49
- </tr>
50
-
51
- <tr>
52
- <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;">
53
- Objet : {email_object}
54
- </td>
55
- </tr>
56
-
57
- <tr>
58
- <td style="padding:0 24px 16px 24px; font-family:Segoe UI, Arial, sans-serif; font-size:14px; line-height:20px; color:#4b5563;">
59
- {email_message}
60
- </td>
61
- </tr>
62
-
63
- <tr>
64
- <td style="padding:0 16px 24px 16px;">
65
- <table role="presentation" cellpadding="0" cellspacing="0" border="0" width="100%" style="border-collapse:collapse; font-family:Segoe UI, Arial, sans-serif;">
66
- <!-- Header -->
67
- <tr>
68
- <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>
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;">Champ</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;">Postgres</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;">Mongo</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;">Différence</th>
73
- </tr>
74
- <!-- Row 1 -->
75
- <tr>
76
- <td style="padding:10px; font-size:14px; line-height:20px; color:#111827; border-bottom:1px solid #bbbbbb;">12345</td>
77
- <td style="padding:10px; font-size:14px; line-height:20px; color:#111827; border-bottom:1px solid #bbbbbb;">formFigs</td>
78
- <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>
79
- <td style="padding:10px; font-size:14px; line-height:20px; color:#111827; border-bottom:1px solid #bbbbbb;">Da (24) 7a 8a 10a ...</td>
80
- <td style="padding:10px; font-size:14px; line-height:20px; color:#111827; border-bottom:1px solid #bbbbbb;">Début tronqué (8a 6a manquants)</td>
81
- </tr>
82
- <!-- Row 2 -->
83
- <tr>
84
- <td style="padding:10px; font-size:14px; line-height:20px; color:#111827;">123456</td>
85
- <td style="padding:10px; font-size:14px; line-height:20px; color:#111827;">totalPrize</td>
86
- <td style="padding:10px; font-size:14px; line-height:20px; color:#111827;">108220</td>
87
- <td style="padding:10px; font-size:14px; line-height:20px; color:#111827;">108222</td>
88
- <td style="padding:10px; font-size:14px; line-height:20px; color:#111827;">-2</td>
89
- </tr>
90
- </table>
91
- </td>
92
- </tr>
93
-
94
- <tr>
95
- <td style="padding:0 24px 16px 24px; font-family:Segoe UI, Arial, sans-serif; font-size:14px; line-height:20px; color:#4b5563;">
96
- Cordialement,
97
- </td>
98
- </tr>
99
-
100
- <tr>
101
- <td style="padding:0 24px 24px 24px; font-family:Segoe UI, Arial, sans-serif; font-size:12px; line-height:18px; color:#6b7280;">
102
- <div style="border-top:1px solid #eef2f7; padding-top:12px;text-align: center;">
103
- Message automatique – ne pas répondre. <br>
104
- © 2025 Paris-Turf – Tous droits réservés <br>
105
- <a href="https://www.paris-turf.com">www.paris-turf.com</a>
106
- </div>
107
- </td>
108
- </tr>
109
- </table>
110
- </td>
111
- </tr>
112
- </table>
113
- """
114
- return html
File without changes
File without changes