Skip to content

Commit

Permalink
feat: expose cached item counters
Browse files Browse the repository at this point in the history
  • Loading branch information
Skelmis committed Jan 4, 2024
1 parent 0400663 commit 9709a96
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
33 changes: 32 additions & 1 deletion garven/routers/aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from zonis import RequestFailed

from garven.dependencies import get_auth_header
from garven.schema import Statistic
from garven.schema import Statistic, CachedItemsStatistic

if TYPE_CHECKING:
from zonis.server import Server
Expand Down Expand Up @@ -45,3 +45,34 @@ async def guild_count(request: Request):
)

return statistic


@aggregate_router.get(
"/cached/count", description="Fetch the counts of cached items in each cluster."
)
async def cached_item_counter(request: Request):
z: Server = request.app.zonis
partial_response = False
data: dict[str, dict[str, int]] = await z.request_all("cached_item_count")
totals: dict[str, int] = {k: 0 for k in data.keys()}

for key, value in data.items():
if isinstance(value, RequestFailed):
partial_response = True
log.error("/cached/count WS threw '%s'", value.response_data)
continue

totals[key] += value

statistic = CachedItemsStatistic(
per_cluster=data, partial_response=partial_response, total_counts=totals
)

cluster_count = int(os.environ["CLUSTER_COUNT"])
if len(data.keys()) != cluster_count:
statistic.partial_response = True
log.error(
"/cached/count did not get a response from all %s clusters", cluster_count
)

return statistic
2 changes: 1 addition & 1 deletion garven/schema/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .message import Message
from .statistic import Statistic
from .statistic import Statistic, CachedItemsStatistic
from .ratelimited import RateLimited
10 changes: 10 additions & 0 deletions garven/schema/statistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,13 @@ class Statistic(BaseModel):

class Config:
schema_extra = {"example": {"statistic": 500, "partial_response": False}}


class CachedItemsStatistic(BaseModel):
total_counts: dict[str, int]
per_cluster: dict[str, dict[str, int]]
partial_response: bool = Field(
False,
description="This will be true when the returned statistic "
"does not accurately represent the entire expected dataset.",
)

0 comments on commit 9709a96

Please sign in to comment.