Skip to content

Commit

Permalink
feat: add the ability to request shard information
Browse files Browse the repository at this point in the history
  • Loading branch information
Skelmis committed Jan 6, 2024
1 parent 2a682a2 commit cab32a2
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 28 deletions.
14 changes: 9 additions & 5 deletions garven/calculate_dev_cluster.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import os


def get_dev_cluster() -> str:
guild_id = 601219766258106399
def get_cluster_and_shard_for_guild_id(guild_id: int) -> tuple[str, str]:
shard_id = (guild_id >> 22) % int(os.environ["TOTAL_SHARDS"])
number_of_shards_per_cluster = 5
number_of_shards_per_cluster = int(os.environ["SHARDS_PER_CLUSTER"])
clusters = {
cid: [
i
Expand All @@ -14,11 +13,16 @@ def get_dev_cluster() -> str:
+ number_of_shards_per_cluster,
)
]
for cid in range(1, 20)
for cid in range(1, 25) # Arb number of clusters
}

for cluster, shards in clusters.items():
if shard_id in shards:
return str(cluster)
return str(cluster), str(shard_id)

raise ValueError("Maths went wrong")


def get_dev_cluster() -> str:
cluster, _ = get_cluster_and_shard_for_guild_id(601219766258106399)
return cluster
24 changes: 21 additions & 3 deletions garven/routers/aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
from fastapi import Request
from zonis import RequestFailed

from garven.calculate_dev_cluster import get_cluster_and_shard_for_guild_id
from garven.dependencies import get_auth_header
from garven.schema import Statistic, CachedItemsStatistic
from garven.schema import Statistic, CachedItemsStatistic, ShardInfo, Message

if TYPE_CHECKING:
from zonis.server import Server
Expand All @@ -21,11 +22,28 @@
prefix="/aggregate",
dependencies=[Depends(get_auth_header)],
tags=["Aggregate"],
responses={200: {"model": Statistic}},
)


@aggregate_router.get("/guilds/count", description="Fetch an up to date guild count.")
@aggregate_router.get(
"/guilds/{guild_id}/shard_info",
responses={200: {"model": ShardInfo}, 400: {"model": Message}},
description="Fetch the shard and cluster for a guild",
)
async def get_guild_shard_info(request: Request, guild_id: int):
try:
cluster_id, shard_id = get_cluster_and_shard_for_guild_id(guild_id)
except ValueError:
return Message(message="guild_id was incorrect / out of range")

return ShardInfo(shard_id=shard_id, cluster_id=cluster_id)


@aggregate_router.get(
"/guilds/count",
description="Fetch an up to date guild count.",
responses={200: {"model": Statistic}},
)
async def guild_count(request: Request):
z: Server = request.app.zonis
statistic = Statistic(statistic=0)
Expand Down
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 .message import Message, ShardInfo
from .statistic import Statistic, CachedItemsStatistic
from .ratelimited import RateLimited
8 changes: 8 additions & 0 deletions garven/schema/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ class Config:
"message": "Invalid API key.",
}
}


class ShardInfo(BaseModel):
shard_id: str
cluster_id: str

class Config:
schema_extra = {"example": {"shard_id": "9", "cluster_id": "1"}}
1 change: 0 additions & 1 deletion garven/schema/premium/__init__.py

This file was deleted.

18 changes: 0 additions & 18 deletions garven/schema/premium/shared_guilds.py

This file was deleted.

0 comments on commit cab32a2

Please sign in to comment.