teams-alerter 0.1.0__py3-none-any.whl

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.
__init__.py ADDED
@@ -0,0 +1,4 @@
1
+ from .core import TeamsAlerter
2
+ from .utils import DateUtils, ErrorUtils
3
+
4
+ __all__ = ["TeamsAlerter", "DateUtils", "ErrorUtils"]
core.py ADDED
@@ -0,0 +1,77 @@
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
+ def __init__(
10
+ self,
11
+ utils,
12
+ app_name,
13
+ teams_channel,
14
+ detail,
15
+ level,
16
+ environment=None,
17
+ url_log=None,
18
+ timestamp=None,
19
+ teams_template=None,
20
+ ):
21
+ self.utils = utils
22
+ self.app_name = app_name
23
+ self.teams_channel = teams_channel
24
+ self.detail = detail
25
+ self.environment = environment
26
+ self.level = level
27
+ self.timestamp = timestamp
28
+ self.url_log = url_log
29
+ self.teams_template = teams_template
30
+
31
+ @staticmethod
32
+ def handle_error(error: Exception, utils: ErrorUtils) -> None:
33
+ error_type = type(error).__name__
34
+ error_message = str(error)
35
+ error_traceback = traceback.format_exc()
36
+
37
+ teams_alerter = TeamsAlerter(
38
+ utils=utils,
39
+ app_name="datastream-transaction-pub",
40
+ teams_channel=f"datastream-{utils['env']}-alerts",
41
+ detail=f"Error type: {error_type}\nError message: {error_message}\nError traceback: {error_traceback}",
42
+ level="ERROR",
43
+ )
44
+
45
+ teams_alerter.publish_alert()
46
+
47
+ def publish_alert(self):
48
+ # Formatage du payload
49
+ utc_timestamp_minus_5min = DateUtils.get_str_utc_timestamp_minus_5min()
50
+ utc_timestamp = DateUtils.get_str_utc_timestamp()
51
+ payload = json.dumps(
52
+ {
53
+ "app_name": self.app_name,
54
+ "teams_channel": self.teams_channel,
55
+ "detail": self.detail,
56
+ "level": self.level,
57
+ "environment": self.utils["env"],
58
+ "url_log": f"https://console.cloud.google.com/logs/query;query=resource.type%3D%22gce_instance%22%0Aresource.labels.project_id%3D%22{self.utils['app_project_id']}%22;cursorTimestamp={utc_timestamp_minus_5min};duration=PT10M?referrer=search&hl=fr&inv=1&invt=Ab3WcQ&project={self.utils['app_project_id']}",
59
+ "timestamp": utc_timestamp,
60
+ "teams_template": "card",
61
+ }
62
+ )
63
+
64
+ # CrΓ©ation d'un Γ©diteur
65
+ publisher = pubsub_v1.PublisherClient()
66
+ topic_path = publisher.topic_path(self.utils["topic_project_id"], self.utils["topic_id"])
67
+
68
+ # Message Γ  publier
69
+ data = payload.encode("utf-8")
70
+
71
+ # Publier le message
72
+ try:
73
+ publish_future = publisher.publish(topic_path, data)
74
+ publish_future.result()
75
+
76
+ except Exception as e:
77
+ self.utils["logger"](f"πŸŸ₯Une erreur s'est produite lors de la publication du message : {e}")
setup.py ADDED
@@ -0,0 +1,11 @@
1
+ from setuptools import setup, find_packages
2
+
3
+ setup(
4
+ name="teams-alerter",
5
+ version="0.1.0",
6
+ packages=find_packages(),
7
+ install_requires=[
8
+ "google-cloud-logging",
9
+ "google-cloud-pubsub",
10
+ ],
11
+ )
@@ -0,0 +1,100 @@
1
+ Metadata-Version: 2.4
2
+ Name: teams-alerter
3
+ Version: 0.1.0
4
+ Summary: Module pour envoyer des alertes Teams via Pub/Sub
5
+ Author-email: Toki <t.bakotondrabe-ext@paris-turf.com>
6
+ Requires-Python: >=3.8
7
+ Description-Content-Type: text/markdown
8
+ Requires-Dist: google-cloud-logging
9
+ Requires-Dist: google-cloud-pubsub
10
+
11
+ # πŸ› οΈ Teams Alerter
12
+
13
+ A lightweight Python module to send alerts to Microsoft Teams via Google Cloud Pub/Sub.
14
+
15
+ ---
16
+
17
+ ## πŸ“¦ How to Build the Module
18
+
19
+ Make sure you have the `build` package installed:
20
+
21
+ ```bash
22
+ python -m pip install --upgrade build
23
+ python -m build
24
+ ```
25
+
26
+ This will generate `.whl` and `.tar.gz` files inside the `dist/` folder.
27
+
28
+ ---
29
+
30
+ ## πŸ”§ Install the Module Locally
31
+
32
+ From your project directory (with your virtual environment activated):
33
+
34
+ ```bash
35
+ pip install ../teams-alerter/dist/teams_alerter-0.1.0-py3-none-any.whl
36
+ ```
37
+
38
+ > Adjust the path according to your actual `.whl` file location.
39
+
40
+ ---
41
+
42
+ ## πŸš€ How to Use the Module
43
+
44
+ ```python
45
+ from teams_alerter import TeamsAlerter
46
+
47
+ # Example: error handling
48
+ try:
49
+ raise RuntimeError("Test error")
50
+ except Exception as error:
51
+ TeamsAlerter.handle_error(error, utils)
52
+ ```
53
+
54
+ ---
55
+
56
+ ## πŸ“€ How to Publish to PyPI
57
+
58
+ 1. Install `twine`:
59
+
60
+ ```bash
61
+ pip install twine
62
+ ```
63
+
64
+ 2. Upload the distribution files:
65
+
66
+ ```bash
67
+ twine upload dist/*
68
+ ```
69
+
70
+ > A PyPI account is required: https://pypi.org/account/register/
71
+
72
+ ---
73
+
74
+ ## πŸ“ Recommended Module Structure
75
+
76
+ ```
77
+ teams-alerter/
78
+ β”œβ”€β”€ src/
79
+ β”‚ └── teams_alerter/
80
+ β”‚ β”œβ”€β”€ __init__.py
81
+ β”‚ β”œβ”€β”€ core.py
82
+ β”‚ └── utils.py
83
+ β”œβ”€β”€ pyproject.toml
84
+ β”œβ”€β”€ README.md
85
+ └── dist/
86
+ ```
87
+
88
+ ---
89
+
90
+ ## βœ… Requirements
91
+
92
+ - Python β‰₯ 3.8
93
+ - Google Cloud SDK if you are using Pub/Sub
94
+ - Dependencies:
95
+ - `google-cloud-logging`
96
+ - `google-cloud-pubsub`
97
+
98
+ These dependencies are already listed in your `pyproject.toml`.
99
+
100
+ ---
@@ -0,0 +1,8 @@
1
+ __init__.py,sha256=J6zyyTfUNVElXD0wsTqBwp-VtcApbzYGUPpChrdcbpY,131
2
+ core.py,sha256=xKu6ZVxxk4GcZfeAI2e_WSjo_sQp8iGXdJhv8RTVQeI,2751
3
+ setup.py,sha256=L5j3EiwUnHcg1wYTir4zwKDpxUIgx8nV4GCZ_RqlHCA,224
4
+ utils.py,sha256=1rCiwatP_ZbIPCQlttk1rkV1ZZqQNOimobbQMnL1LYM,615
5
+ teams_alerter-0.1.0.dist-info/METADATA,sha256=kUqYVhOkKNCTxc3nYTGkVOrGTLpQY2fZScbSgYJe2c4,1955
6
+ teams_alerter-0.1.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
7
+ teams_alerter-0.1.0.dist-info/top_level.txt,sha256=dYDJBJIunOoTT5JlA_nbcxXFP06gFhXAde5v5bxKh0A,26
8
+ teams_alerter-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.9.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,4 @@
1
+ __init__
2
+ core
3
+ setup
4
+ utils
utils.py ADDED
@@ -0,0 +1,24 @@
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
+
14
+
15
+ class DateUtils:
16
+ @staticmethod
17
+ def get_str_utc_timestamp():
18
+ dt = datetime.datetime.utcnow()
19
+ return dt.strftime("%Y-%m-%dT%H:%M:%S") + ".000000000Z"
20
+
21
+ @staticmethod
22
+ def get_str_utc_timestamp_minus_5min():
23
+ dt = datetime.datetime.utcnow() - datetime.timedelta(minutes=5)
24
+ return dt.strftime("%Y-%m-%dT%H:%M:%S") + ".000000000Z"