|
3 | 3 | from datetime import datetime, timedelta |
4 | 4 | from enum import Enum |
5 | 5 | from math import floor |
6 | | -from typing import TypedDict |
| 6 | +from typing import Any, TypedDict |
7 | 7 |
|
8 | 8 | import sentry_sdk |
9 | 9 | from django.db.models import Max |
@@ -522,6 +522,25 @@ def _validate_text_widget(self, data): |
522 | 522 |
|
523 | 523 | return data |
524 | 524 |
|
| 525 | + def _validate_tracemetrics_equation_constraints(self, data) -> dict[str, Any]: |
| 526 | + if not data.get("widget_type") == DashboardWidgetTypes.TRACEMETRICS: |
| 527 | + return data |
| 528 | + |
| 529 | + # Tracemetrics timeseries widgets only support a single equation per query |
| 530 | + if data.get("display_type") in { |
| 531 | + DashboardWidgetDisplayTypes.LINE_CHART, |
| 532 | + DashboardWidgetDisplayTypes.AREA_CHART, |
| 533 | + DashboardWidgetDisplayTypes.BAR_CHART, |
| 534 | + }: |
| 535 | + for query in data.get("queries"): |
| 536 | + aggregates = query.get("aggregates") or [] |
| 537 | + if any(is_equation(aggregate) for aggregate in aggregates) and len(aggregates) > 1: |
| 538 | + raise serializers.ValidationError( |
| 539 | + {"queries": "Tracemetrics timeseries widgets support at most one equation."} |
| 540 | + ) |
| 541 | + |
| 542 | + return data |
| 543 | + |
525 | 544 | def validate(self, data): |
526 | 545 | self.query_warnings = {"queries": [], "columns": {}} |
527 | 546 |
|
@@ -570,6 +589,9 @@ def validate(self, data): |
570 | 589 | ) |
571 | 590 |
|
572 | 591 | if data.get("queries"): |
| 592 | + if data.get("widget_type") == DashboardWidgetTypes.TRACEMETRICS: |
| 593 | + self._validate_tracemetrics_equation_constraints(data) |
| 594 | + |
573 | 595 | # Check each query to see if they have an issue or discover error depending on the type of the widget |
574 | 596 | for query in data.get("queries"): |
575 | 597 | if len(query.get("columns", [])) > 0: |
|
0 commit comments