Skip to content

Commit

Permalink
Add statistics (#199)
Browse files Browse the repository at this point in the history
* Add statistics

* Add statistics

* Add statistics
  • Loading branch information
joostlek authored Jul 20, 2024
1 parent bf1e4e3 commit a962110
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/aiomealie/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
MealieAuthenticationError,
MealieValidationError,
MealieBadRequestError,
MealieNotFoundError,
)
from aiomealie.mealie import MealieClient
from aiomealie.models import (
Expand All @@ -28,6 +29,7 @@
Instruction,
Ingredient,
Tag,
Statistics,
)

__all__ = [
Expand All @@ -36,6 +38,7 @@
"MealieError",
"MealieAuthenticationError",
"MealieBadRequestError",
"MealieNotFoundError",
"MealieValidationError",
"MealieClient",
"StartupInfo",
Expand All @@ -51,6 +54,7 @@
"MealplanResponse",
"MealplanEntryType",
"ShoppingItem",
"Statistics",
"MutateShoppingItem",
"ShoppingItemsResponse",
"ShoppingList",
Expand Down
7 changes: 7 additions & 0 deletions src/aiomealie/mealie.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
Theme,
UserInfo,
Recipe,
Statistics,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -263,6 +264,12 @@ async def delete_shopping_item(self, item_id: str) -> None:

await self._delete(f"api/groups/shopping/items/{item_id}")

async def get_statistics(self) -> Statistics:
"""Get statistics."""

response = await self._get("api/groups/statistics")
return Statistics.from_json(response)

async def close(self) -> None:
"""Close open client session."""
if self.session and self._close_session:
Expand Down
11 changes: 11 additions & 0 deletions src/aiomealie/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,14 @@ class ShoppingItemsResponse(DataClassORJSONMixin):
"""ShoppingItemsResponse model."""

items: list[ShoppingItem]


@dataclass
class Statistics(DataClassORJSONMixin):
"""Statistics model."""

total_recipes: int = field(metadata=field_options(alias="totalRecipes"))
total_users: int = field(metadata=field_options(alias="totalUsers"))
total_categories: int = field(metadata=field_options(alias="totalCategories"))
total_tags: int = field(metadata=field_options(alias="totalTags"))
total_tools: int = field(metadata=field_options(alias="totalTools"))
9 changes: 9 additions & 0 deletions tests/__snapshots__/test_mealie.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,15 @@
'is_first_login': True,
})
# ---
# name: test_statistics
dict({
'total_categories': 24,
'total_recipes': 765,
'total_tags': 454,
'total_tools': 11,
'total_users': 3,
})
# ---
# name: test_theme
dict({
'dark_accent': '#007A99',
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/statistics.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"totalRecipes": 765,
"totalUsers": 3,
"totalCategories": 24,
"totalTags": 454,
"totalTools": 11
}
14 changes: 14 additions & 0 deletions tests/test_mealie.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,3 +520,17 @@ async def test_delete_shopping_item(
params=None,
json=None,
)


async def test_statistics(
responses: aioresponses,
mealie_client: MealieClient,
snapshot: SnapshotAssertion,
) -> None:
"""Test retrieving statistics."""
responses.get(
f"{MEALIE_URL}/api/groups/statistics",
status=200,
body=load_fixture("statistics.json"),
)
assert await mealie_client.get_statistics() == snapshot

0 comments on commit a962110

Please sign in to comment.