@cryptotaxi247 / CoPilot / commits / cca655fd

get influxdb alerts from influxdb_alerts

Taylor committed Jul 13, 2023 at 21:22 UTC cca655fdcc768ff5897aea15fb83511c5ae658e8
2 files changed +76 -2
backend/app/routes/influxdb.py
+12
@@ -43,6 +43,18 @@ def get_check_query(check_id: str) -> jsonify:
43 check_query = service.collect_check_query(check_id)
44 return jsonify(check_query)
45
46 +@bp.route("/influxdb/alerts", methods=["GET"])
47 +def get_alerts() -> jsonify:
48 + """
49 + Endpoint to list all alerts from the `influxdb_alerts` table.
50 +
51 + Returns:
52 + jsonify: A JSON response containing the list of all alerts from InfluxDB.
53 + """
54 + logger.info("Received request to get all InfluxDB alerts")
55 + service = InfluxDBAlertsService.from_connector_details("InfluxDB")
56 + alerts = service.collect_alerts()
57 + return jsonify(alerts)
58
59 @bp.route("/influxdb/alert", methods=["POST"])
60 def put_alert() -> jsonify:
backend/app/services/InfluxDB/alerts.py
+64 -2
@@ -1,4 +1,4 @@
1 -from typing import Dict
1 +from typing import Dict, Union, List
2
3 import requests
4 from loguru import logger
@@ -107,7 +107,7 @@ class InfluxDBAlertsService:
107
108 def validate_payload(self, data: Dict[str, object]) -> str:
109 """
110 - Validates the payload received from the Sublime alert webhook.
110 + Validates the payload received from the influxdb alert webhook.
111
112 Args:
113 data (Dict[str, object]): The data received from the webhook.
@@ -137,3 +137,65 @@ class InfluxDBAlertsService:
137 influxbd_alert = InfluxDBAlerts(check_name=check_name, message=message)
138 db.session.add(influxbd_alert)
139 db.session.commit()
140 +
141 + def collect_alerts(self) -> Dict[str, Union[bool, str, List[Dict[str, str]]]]:
142 + """
143 + Collects alerts from `influxdb_alerts` table in the database.
144 +
145 + Returns:
146 + Dict[str, Union[bool, str, List[Dict[str, str]]]]: A dictionary containing the success status,
147 + a message, and potentially the message details.
148 + """
149 + if not self._are_influxdb_details_collected():
150 + return {
151 + "message": "Failed to collect influxdb details",
152 + "success": False,
153 + }
154 +
155 + alerts = self._collect_alerts_from_db()
156 + if alerts["success"] is False:
157 + return alerts
158 +
159 + return {
160 + "message": "Successfully collected alerts",
161 + "success": True,
162 + "alerts": alerts["alerts"],
163 + }
164 +
165 + def _are_influxdb_details_collected(self) -> bool:
166 + """
167 + Checks whether the details for the influxdb connector were successfully collected.
168 +
169 + Returns:
170 + bool: True if all details were collected, False otherwise.
171 + """
172 + return all([self.connector_url, self.connector_api_key])
173 +
174 + def _collect_alerts_from_db(self) -> Dict[str, Union[bool, str, List[Dict[str, str]]]]:
175 + """
176 + Collects alerts from `influxdb_alerts` table in the database.
177 +
178 + Returns:
179 + Dict[str, Union[bool, str, List[Dict[str, str]]]]: A dictionary containing the success status,
180 + a message, and potentially the message details.
181 + """
182 + try:
183 + alerts = InfluxDBAlerts.query.all()
184 + except Exception as e:
185 + logger.error(f"Failed to collect alerts from database. Error: {e}")
186 + return {
187 + "message": "Failed to collect alerts from database",
188 + "success": False,
189 + }
190 +
191 + return {
192 + "message": "Successfully collected alerts from database",
193 + "success": True,
194 + "alerts": [
195 + {
196 + "check_name": alert.check_name,
197 + "message": alert.message,
198 + }
199 + for alert in alerts
200 + ],
201 + }