Skip to content

Commit

Permalink
chore: add event logger to reports/alerts CRUD (#20249)
Browse files Browse the repository at this point in the history
* add event logger to report CRUD

* added assert metric to tests

* added unique name to action

* testing

* with inline log context
  • Loading branch information
AAfghahi authored Jun 6, 2022
1 parent 77e326f commit c131f02
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 38 deletions.
35 changes: 33 additions & 2 deletions superset/reports/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from superset.constants import MODEL_API_RW_METHOD_PERMISSION_MAP, RouteMethod
from superset.dashboards.filters import DashboardAccessFilter
from superset.databases.filters import DatabaseFilter
from superset.extensions import event_logger
from superset.models.reports import ReportSchedule
from superset.reports.commands.bulk_delete import BulkDeleteReportScheduleCommand
from superset.reports.commands.create import CreateReportScheduleCommand
Expand Down Expand Up @@ -227,8 +228,12 @@ def ensure_alert_reports_enabled(self) -> Optional[Response]:
@expose("/<int:pk>", methods=["DELETE"])
@protect()
@safe
@statsd_metrics
@permission_name("delete")
@statsd_metrics
@event_logger.log_this_with_context(
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.delete",
log_to_statsd=False,
)
def delete(self, pk: int) -> Response:
"""Delete a Report Schedule
---
Expand Down Expand Up @@ -281,7 +286,9 @@ def delete(self, pk: int) -> Response:
@statsd_metrics
@permission_name("post")
@requires_json
def post(self) -> Response:
def post(
self,
) -> Response:
"""Creates a new Report Schedule
---
post:
Expand Down Expand Up @@ -319,6 +326,16 @@ def post(self) -> Response:
"""
try:
item = self.add_model_schema.load(request.json)
# normally this would be covered by a decorator, however
# due to this model being formatted incorrectly the data
# needed some manipulation.
event_logger.log_with_context(
action="ReportScheduleRestApi.post",
dashboard_id=request.json.get("dashboard", None),
chart_id=request.json.get("chart", None),
report_format=request.json.get("report_format", None),
active=request.json.get("active", None),
)
# This validates custom Schema with custom validations
except ValidationError as error:
return self.response_400(message=error.messages)
Expand Down Expand Up @@ -390,6 +407,16 @@ def put(self, pk: int) -> Response:
"""
try:
item = self.edit_model_schema.load(request.json)
# normally this would be covered by a decorator, however
# due to this model being formatted incorrectly the data
# needed some manipulation.
event_logger.log_with_context(
action="ReportScheduleRestApi.put",
dashboard_id=request.json.get("dashboard", None),
chart_id=request.json.get("chart", None),
report_format=request.json.get("report_format", None),
active=request.json.get("active", None),
)
# This validates custom Schema with custom validations
except ValidationError as error:
return self.response_400(message=error.messages)
Expand All @@ -416,6 +443,10 @@ def put(self, pk: int) -> Response:
@safe
@statsd_metrics
@rison(get_delete_ids_schema)
@event_logger.log_this_with_context(
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.bulk_delete",
log_to_statsd=False,
)
def bulk_delete(self, **kwargs: Any) -> Response:
"""Delete bulk Report Schedule layers
---
Expand Down
Loading

0 comments on commit c131f02

Please sign in to comment.