Skip to content

Commit

Permalink
Added pyproject and new ruff rules
Browse files Browse the repository at this point in the history
  • Loading branch information
ddxv committed Jan 11, 2024
1 parent 6aa39d8 commit e048714
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 93 deletions.
10 changes: 9 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repos:
rev: v0.1.11
hooks:
- id: ruff
args: [--fix]
# Mypy
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.8.0
Expand All @@ -20,7 +21,14 @@ repos:
rev: v4.0.0-alpha.8
hooks:
- id: prettier
args: ['--config', 'frontend/.prettierrc', '--ignore-unknown', '--write', '--check']
args:
[
"--config",
"frontend/.prettierrc",
"--ignore-unknown",
"--write",
"--check",
]
language: node
entry: frontend/node_modules/.bin/prettier
require_serial: true
Expand Down
64 changes: 37 additions & 27 deletions backend/api_app/controllers/apps.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import datetime
import pandas as pd
import urllib.parse

import numpy as np
import pandas as pd
from litestar import Controller, get
from litestar.exceptions import NotFoundException

from api_app.models import (
AppDetail,
Collection,
AppGroup,
AppRank,
Category,
Collection,
DeveloperApps,
AppRank,
)
from config import get_logger
from dbcon.queries import (
get_single_app,
query_recent_apps,
query_app_history,
search_apps,
query_single_developer,
query_ranks_for_app,
query_recent_apps,
query_single_developer,
search_apps,
)
from litestar import Controller, get
from litestar.exceptions import NotFoundException
import urllib.parse

logger = get_logger(__name__)

Expand All @@ -46,7 +47,7 @@ def get_app_history(app_dict: dict) -> dict:
app_hist = query_app_history(store_app)
app_dict["histogram"] = app_hist.sort_values(["id"]).tail(1)["histogram"].values[0]
app_dict["history_table"] = app_hist.drop(["id", "store_app"], axis=1).to_dict(
orient="records"
orient="records",
)
app_hist["group"] = app_name
app_hist = app_hist[
Expand All @@ -58,7 +59,7 @@ def get_app_history(app_dict: dict) -> dict:
app_hist = app_hist.sort_values(xaxis_col)
app_hist["date_change"] = app_hist[xaxis_col] - app_hist[xaxis_col].shift(1)
app_hist["days_changed"] = app_hist["date_change"].apply(
lambda x: np.nan if pd.isnull(x) else x.days
lambda x: np.nan if pd.isnull(x) else x.days,
)
change_metrics = []
for metric in metrics:
Expand Down Expand Up @@ -90,7 +91,7 @@ def get_app_history(app_dict: dict) -> dict:
"installs_rate_of_change": "Installs Rate of Change",
"rating_count_rate_of_change": "Rating Count Rate of Change",
"review_count_rate_of_change": "Review Count Rate of Change",
}
},
)
)
# TODO: KEEP?
Expand Down Expand Up @@ -145,10 +146,11 @@ def get_app_overview_dict(collection: str) -> Collection:
key=category_key,
google=AppGroup(title="Google", apps=google_dicts),
ios=AppGroup(title="iOS", apps=ios_dicts),
)
),
)
response_collection = Collection(
title=COLLECTIONS[collection]["title"], categories=categories_dicts
title=COLLECTIONS[collection]["title"],
categories=categories_dicts,
)
return response_collection

Expand All @@ -158,13 +160,14 @@ class AppController(Controller):

@get(path="/collections/{collection:str}", cache=3600)
async def get_apps_overview(self, collection: str) -> Collection:
"""
Handles a GET request for a list of apps
"""Handles a GET request for a list of apps
Args:
----
collection:collection
Returns:
-------
A dictionary representation of the list of apps for homepasge
"""
logger.info(f"{self.path} start")
Expand All @@ -176,19 +179,20 @@ async def get_apps_overview(self, collection: str) -> Collection:

@get(path="/{store_id:str}", cache=3600)
async def get_app_detail(self, store_id: str) -> AppDetail:
"""
Handles a GET request for a specific app.
"""Handles a GET request for a specific app.
store_id (str): The id of the app to retrieve.
Returns:
Returns
-------
json
"""
logger.info(f"{self.path} start")
app_df = get_single_app(store_id)
if app_df.empty:
raise NotFoundException(
f"Store ID not found: {store_id!r}", status_code=404
f"Store ID not found: {store_id!r}",
status_code=404,
)
app_dict = app_df.to_dict(orient="records")[0]
app_hist_dict = get_app_history(app_dict)
Expand All @@ -197,20 +201,22 @@ async def get_app_detail(self, store_id: str) -> AppDetail:

@get(path="/{store_id:str}/ranks", cache=3600)
async def app_ranks(self, store_id: str) -> AppRank:
"""
Handles a GET request for a specific app ranks.
"""Handles a GET request for a specific app ranks.
Args:
----
store_id (str): The id of the store to retrieve.
Returns:
-------
json
"""
logger.info(f"{self.path} start")
df = query_ranks_for_app(store_id=store_id)
if df.empty:
raise NotFoundException(
f"Ranks not found for {store_id!r}", status_code=404
f"Ranks not found for {store_id!r}",
status_code=404,
)
df["rank_group"] = df["collection"] + ": " + df["category"]
latest_dict = df[df["crawled_date"].max() == df["crawled_date"]][
Expand All @@ -229,27 +235,31 @@ async def app_ranks(self, store_id: str) -> AppRank:

@get(path="/developers/{developer_id:str}", cache=3600)
async def get_developer_apps(self, developer_id: str) -> DeveloperApps:
"""
Handles a GET request for a specific developer.
"""Handles a GET request for a specific developer.
Args:
----
developer_id (str): The id of the developer to retrieve.
Returns:
-------
json
"""
logger.info(f"{self.path} start")
apps_df = query_single_developer(developer_id)

if apps_df.empty:
raise NotFoundException(
f"Store ID not found: {developer_id!r}", status_code=404
f"Store ID not found: {developer_id!r}",
status_code=404,
)
developer_name = apps_df.to_dict(orient="records")[0]["developer_name"]
apps_dict = apps_df.to_dict(orient="records")

developer_apps = DeveloperApps(
developer_id=developer_id, title=developer_name, apps=apps_dict
developer_id=developer_id,
title=developer_name,
apps=apps_dict,
)
return developer_apps

Expand Down
18 changes: 8 additions & 10 deletions backend/api_app/controllers/categories.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import numpy as np
from litestar import Controller, get

from api_app.models import CategoriesOverview, Category, AppGroup
from api_app.models import AppGroup, CategoriesOverview, Category
from config import get_logger


from dbcon.queries import get_appstore_categories, get_category_top_apps_by_installs
from litestar import Controller, get

logger = get_logger(__name__)

Expand Down Expand Up @@ -54,10 +52,10 @@ class CategoryController(Controller):

@get(path="/", cache=True)
async def get_categories_overview(self) -> CategoriesOverview:
"""
Handles a GET request for a list of categories
"""Handles a GET request for a list of categories
Returns:
Returns
-------
A dictionary representation of the list of categories
each with an id, name, type and total of apps
"""
Expand All @@ -68,10 +66,10 @@ async def get_categories_overview(self) -> CategoriesOverview:

@get(path="/{category_id:str}", cache=3600)
async def get_category(self, category_id: str) -> Category:
"""
Handles a GET request for a single category
"""Handles a GET request for a single category
Returns:
Returns
-------
A dictionary representation of a category
with ios and google apps
"""
Expand Down
59 changes: 34 additions & 25 deletions backend/api_app/controllers/rankings.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import datetime

import pandas as pd
from litestar import Controller, get

from api_app.models import (
RankingOverview,
StoreCollections,
StoreCategoryDetail,
StoreCollections,
StoreRankings,
)
from config import get_logger
import pandas as pd
import datetime

from dbcon.queries import (
get_store_collection_category_map,
get_most_recent_top_ranks,
get_history_top_ranks,
get_most_recent_top_ranks,
get_store_collection_category_map,
)
from litestar import Controller, get

logger = get_logger(__name__)

Expand Down Expand Up @@ -47,7 +48,7 @@ def ranking_map() -> RankingOverview:
collection_id=collection_id,
collection_name=collection_name,
categories=category_details,
)
),
)
overview.stores_rankings.append(rankings)
return overview
Expand All @@ -58,10 +59,10 @@ class RankingsController(Controller):

@get(path="/", cache=True)
async def get_ranking_overview(self) -> RankingOverview:
"""
Handles a GET request for a list of ranking collecitons and categories
"""Handles a GET request for a list of ranking collecitons and categories
Returns:
Returns
-------
A dictionary representation of the list of categories
each with an id, name, type and total of apps
"""
Expand All @@ -72,12 +73,15 @@ async def get_ranking_overview(self) -> RankingOverview:

@get(path="/{store:int}/{collection:int}/{category:int}/short", cache=40000)
async def get_short_ranks_for_category(
self, store: int, collection: int, category: int
self,
store: int,
collection: int,
category: int,
) -> dict:
"""
Handles a GET request for a store/collection/category rank
"""Handles a GET request for a store/collection/category rank
Returns:
Returns
-------
A dictionary representation of a category
with ios and google apps
"""
Expand All @@ -93,12 +97,15 @@ async def get_short_ranks_for_category(

@get(path="/{store:int}/{collection:int}/{category:int}", cache=3600)
async def get_ranks_for_category(
self, store: int, collection: int, category: int
self,
store: int,
collection: int,
category: int,
) -> dict:
"""
Handles a GET request for a store/collection/category rank
"""Handles a GET request for a store/collection/category rank
Returns:
Returns
-------
A dictionary representation of a category
with ios and google apps
"""
Expand All @@ -114,12 +121,15 @@ async def get_ranks_for_category(

@get(path="/{store:int}/{collection:int}/{category:int}/history", cache=3600)
async def get_ranks_history_for_category(
self, store: int, collection: int, category: int
self,
store: int,
collection: int,
category: int,
) -> dict:
"""
Handles a GET request for a store/collection/category rank
"""Handles a GET request for a store/collection/category rank
Returns:
Returns
-------
A list of dictionary representation of a category history
with ios or google apps
"""
Expand All @@ -143,8 +153,7 @@ async def get_ranks_history_for_category(
.reset_index()
)
df.loc[
df["crawled_date"].dt.date
>= datetime.datetime.now(datetime.timezone.utc).date(),
df["crawled_date"].dt.date >= datetime.datetime.now(datetime.UTC).date(),
"crawled_date",
] = last_crawled_date
df["crawled_date"] = pd.to_datetime(df["crawled_date"]).dt.strftime("%Y-%m-%d")
Expand Down
Loading

0 comments on commit e048714

Please sign in to comment.