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 574a23a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 26 deletions.
41 changes: 17 additions & 24 deletions snuba/query/allocation_policies/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,39 +166,32 @@ def violations(self) -> dict[str, dict[str, Any]]:

@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", {}),
)
summary = self.extra_data.get("summary")
assert summary is not None
return {"summary": 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
3 changes: 2 additions & 1 deletion snuba/web/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,8 @@ def dataset_query(
{
"error": details,
"timing": timer.for_json(),
"quota_allowance": getattr(cause, "quota_allowance", {}),
"quota_allowance": getattr(cause, "quota_allowance", {})
| getattr(cause, "summary", {}),
**exception.extra,
}
),
Expand Down
1 change: 1 addition & 0 deletions tests/test_snql_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,7 @@ def test_allocation_policy_violation(self) -> None:
"throttled_by": {},
},
}

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

0 comments on commit 574a23a

Please sign in to comment.