Skip to content

Commit

Permalink
api/incidents route added (#94)
Browse files Browse the repository at this point in the history
api/incidents route added

the route "/v1/incidents" moved from Websocket branch

Reviewed-by: Olha Kashyrina
  • Loading branch information
bakhterets authored Feb 19, 2024
1 parent f230a6c commit 6b5d72c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
68 changes: 62 additions & 6 deletions app/api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from app.models import db

from flask import current_app
from flask import jsonify
from flask import request
from flask.views import MethodView

Expand Down Expand Up @@ -157,7 +158,7 @@ def handling_incidents(
current_app.logger.error("Unexpected ERROR")


def get_component_from_cache(cache_key):
def get_elements_from_cache(cache_key):
cached_value = cache.get(cache_key)
if cached_value:
current_app.logger.debug(f"Cache hit for key: '{cache_key}'")
Expand All @@ -167,6 +168,14 @@ def get_component_from_cache(cache_key):

@bp.route("/v1/component_status", methods=["GET", "POST"])
class ApiComponentStatus(MethodView):
@staticmethod
def get_request_info():
return (
f"Request method: {request.method}, "
f"Request path: {request.path}",
f"Client address: {request.remote_addr}"
)

@bp.arguments(ComponentSearchQueryArgs, location="query")
@bp.response(200)
def get(self, search_args):
Expand All @@ -183,8 +192,9 @@ def get(self, search_args):
"""
ip_address = request.remote_addr
current_app.logger.debug(f"Request from IP address: {ip_address}")
request_info = self.get_request_info()
current_app.logger.debug(request_info)

name = search_args.get("name", "")
attribute_name = search_args.get("attribute_name", None)
attribute_value = search_args.get("attribute_value", None)
Expand All @@ -197,8 +207,9 @@ def get(self, search_args):
f"{attribute if attribute else ''}"
)

cached_component = get_component_from_cache(cache_key)
cached_component = get_elements_from_cache(cache_key)
if cached_component:
current_app.logger.debug("The response was cached")
return [cached_component]

if attribute_name is not None and attribute_value is not None:
Expand Down Expand Up @@ -264,8 +275,9 @@ def post(self, data):
:class:`~status_dashboard.api.schemas.components.IncidentSchema`
object
"""
ip_address = request.remote_addr
current_app.logger.debug(f"Request from IP address: {ip_address}")
request_info = self.get_request_info()
current_app.logger.debug(request_info)

name = data.get("name", None)
impact = data.get("impact", 1)
text = data.get("text", "Incident")
Expand Down Expand Up @@ -361,3 +373,47 @@ def post(self, data):
"not modifying or opening new incident"
)
return incident


@bp.route("/v1/incidents", methods=["GET"])
class ApiIncidents(MethodView):
@staticmethod
def get_request_info():
return (
f"Request method: {request.method}, "
f"Request path: {request.path}",
f"Client address: {request.remote_addr}"
)

@bp.response(200, IncidentSchema(many=True))
def get(self):
"""Get all incidents
Retrieve a list of all incidents.
Example:
.. code-block:: console
curl http://localhost:5000/api/v1/incidents -X GET \\
-H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6Ikp...'
"""
request_info = self.get_request_info()
current_app.logger.debug(request_info)

incident_schema = IncidentSchema()
cache_key = "all_incidents"
cached_incidents = get_elements_from_cache(cache_key)
if cached_incidents:
current_app.logger.debug("The response was cached")
return cached_incidents

incidents = db.session.scalars(db.select(Incident)).all()
if incidents is None:
abort(404, message="No incidents found")

serialized_incidents = incident_schema.dump(incidents, many=True)
cache.set(cache_key, jsonify(serialized_incidents))
return jsonify(serialized_incidents)
4 changes: 2 additions & 2 deletions doc/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
reno>=3.1.0 # Apache-2.0
sphinx # BSD
sphinx-rtd-theme
sphinxcontrib-httpdomain>=1.8.0 # BSD
sphinx-rtd-theme>=2.0.0 # MIT
sphinxcontrib-httpdomain>=1.8.1 # BSD

0 comments on commit 6b5d72c

Please sign in to comment.