Skip to content

Commit

Permalink
single dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
Rachel Chen authored and Rachel Chen committed Jun 24, 2024
1 parent 0f4eca7 commit 2e0f2cf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 37 deletions.
41 changes: 16 additions & 25 deletions snuba/query/allocation_policies/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,47 +158,38 @@ class AllocationPolicyViolations(SerializableException):
"""

def __str__(self) -> str:
return f"{self.message}, details: {self.violations | self.summary}"
return f"{self.message}, details: {self.violations}, summary: {self.summary}"

@property
def violations(self) -> dict[str, dict[str, Any]]:
return {k: v for k, v in self.quota_allowance.items() if v["can_run"] == False}

@property
def quota_allowance(self) -> dict[str, dict[str, Any]]:
return {
k: v
for k, v in self._quota_allowances_and_summary.items()
if k != "summary"
}
return cast(
dict[str, dict[str, Any]], self.extra_data.get("quota_allowances", {})
)

@property
def summary(self) -> dict[str, Any]:
return {
k: v
for k, v in self._quota_allowances_and_summary.items()
if k == "summary"
}

@property
def _quota_allowances_and_summary(self) -> dict[str, dict[str, Any]]:
return cast(
dict[str, dict[str, Any]],
self.extra_data.get("quota_allowances_and_summary", {}),
)
return cast(dict[str, Any], self.extra_data.get("summary"))

@classmethod
def from_args(
cls, quota_allowances_and_summary: dict[str, Any]
cls,
quota_allowances: dict[str, QuotaAllowance],
summary: dict[str, Any],
) -> "AllocationPolicyViolations":
obj = cls(
quota_allowances_dict = {
key: quota_allowance.to_dict()
for key, quota_allowance in quota_allowances.items()
}

return cls(
"Query on could not be run due to allocation policies",
quota_allowances_and_summary={
key: quota_allowance
for key, quota_allowance in quota_allowances_and_summary.items()
},
quota_allowances=quota_allowances_dict,
summary=summary,
)
return obj


class AllocationPolicy(ABC, metaclass=RegisteredClass):
Expand Down
2 changes: 1 addition & 1 deletion snuba/web/db_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ def _apply_allocation_policies_quota(
stats["quota_allowance"]["summary"] = summary

if not can_run:
raise AllocationPolicyViolations.from_args(stats["quota_allowance"])
raise AllocationPolicyViolations.from_args(quota_allowances, summary)
# Before allocation policies were a thing, the query pipeline would apply
# thread limits in a query processor. That is not necessary if there
# is an allocation_policy in place but nobody has removed that code yet.
Expand Down
23 changes: 12 additions & 11 deletions tests/test_snql_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1311,21 +1311,22 @@ def test_allocation_policy_violation(self) -> None:
"quota_unit": NO_UNITS,
"suggestion": NO_SUGGESTION,
},
"summary": {
"threads_used": 0,
"rejected_by": {
"policy": "RejectAllocationPolicy123",
"quota_used": 0,
"quota_unit": "no_units",
"suggestion": "no_suggestion",
"rejection_threshold": 1000000000000,
},
"throttled_by": {},
}
summary = {
"threads_used": 0,
"rejected_by": {
"policy": "RejectAllocationPolicy123",
"quota_used": 0,
"quota_unit": "no_units",
"suggestion": "no_suggestion",
"rejection_threshold": 1000000000000,
},
"throttled_by": {},
}

assert (
response.json["error"]["message"]
== f"Query on could not be run due to allocation policies, details: {details}"
== f"Query on could not be run due to allocation policies, details: {details}, summary: {summary}"
)

def test_tags_key_column(self) -> None:
Expand Down

0 comments on commit 2e0f2cf

Please sign in to comment.