From d28f51e2db26ecb1a48abdaf901cd13abba174f2 Mon Sep 17 00:00:00 2001 From: james Date: Tue, 24 Oct 2023 15:27:48 +0800 Subject: [PATCH] Fix redirect for home, make API faster --- backend/api_app/controllers/apps.py | 19 +++++++++++-------- backend/dbcon/queries.py | 15 +++++++++++++++ frontend/src/routes/apps/[id]/+page.svelte | 14 ++++++++++++-- .../collections/[collection]/+page.svelte | 5 +++++ 4 files changed, 43 insertions(+), 10 deletions(-) diff --git a/backend/api_app/controllers/apps.py b/backend/api_app/controllers/apps.py index 67556a3..465cfc6 100644 --- a/backend/api_app/controllers/apps.py +++ b/backend/api_app/controllers/apps.py @@ -26,12 +26,7 @@ def get_string_date_from_days_ago(days: int) -> str: def get_app_overview_dict(collection: str) -> Collection: category_limit = 20 - collections = { - "new_weekly": {"title": "New Apps this Week"}, - "new_monthly": {"title": "New Apps this Month"}, - "new_yearly": {"title": "New Apps this Year"}, - "top": {"title": "Alltime Top"}, - } + df = query_recent_apps(collection=collection, limit=category_limit) categories_dicts = [] groups = df.groupby(["mapped_category"]) @@ -54,7 +49,7 @@ def get_app_overview_dict(collection: str) -> Collection: ) ) response_collection = Collection( - title=collections[collection]["title"], categories=categories_dicts + title=COLLECTIONS[collection]["title"], categories=categories_dicts ) return response_collection @@ -73,11 +68,11 @@ async def get_apps_overview(self, request: Request, collection: str) -> Collecti Returns: A dictionary representation of the list of apps for homepasge """ - request.logger.info("inside a request") logger.info(f"{self.path} start") print(f"collection={collection}") home_dict = get_app_overview_dict(collection=collection) + logger.info(f"{self.path} return") return home_dict @get(path="/{store_id:str}", cache=3600) @@ -104,3 +99,11 @@ async def get_app_detail(self, store_id: str) -> AppDetail: app_dict["history_table"] = app_hist.to_html() return app_dict + + +COLLECTIONS = { + "new_weekly": {"title": "New Apps this Week"}, + "new_monthly": {"title": "New Apps this Month"}, + "new_yearly": {"title": "New Apps this Year"}, + "top": {"title": "Alltime Top"}, +} diff --git a/backend/dbcon/queries.py b/backend/dbcon/queries.py index a5727dc..dbe7975 100644 --- a/backend/dbcon/queries.py +++ b/backend/dbcon/queries.py @@ -52,6 +52,21 @@ def query_recent_apps(collection: str, limit=20): ORDER BY rating_count DESC NULLS LAST ); """ + sel_query = f"""WITH NumberedRows AS ( + SELECT + {my_cols}, + ROW_NUMBER() OVER (PARTITION BY store, mapped_category + ORDER BY + CASE WHEN store = 1 THEN installs ELSE rating_count END DESC NULLS LAST + ) AS rn + FROM {table_name} + ) + SELECT + {my_cols} + FROM NumberedRows + WHERE rn <= {limit} + ; + """ df = pd.read_sql(sel_query, con=DBCON.engine) groups = df.groupby("store") for _store, group in groups: diff --git a/frontend/src/routes/apps/[id]/+page.svelte b/frontend/src/routes/apps/[id]/+page.svelte index 46c976d..0ca8a47 100644 --- a/frontend/src/routes/apps/[id]/+page.svelte +++ b/frontend/src/routes/apps/[id]/+page.svelte @@ -13,14 +13,24 @@

{data.myapp.name}

{#if data.myapp.icon_url_512} - {data.myapp.name} + {data.myapp.name} {/if}
{#if data.myapp.featured_image} - {data.myapp.name} + {data.myapp.name} {/if}
diff --git a/frontend/src/routes/collections/[collection]/+page.svelte b/frontend/src/routes/collections/[collection]/+page.svelte index f9fdb8a..675f38d 100644 --- a/frontend/src/routes/collections/[collection]/+page.svelte +++ b/frontend/src/routes/collections/[collection]/+page.svelte @@ -33,6 +33,7 @@ class="h-auto object-fill rounded-lg" src={app.featured_image_url} alt={app.name} + referrerpolicy="no-referrer" />
@@ -40,6 +41,7 @@ class="h-auto w-1/4 p-3 rounded-lg" src={app.icon_url_512} alt={app.name} + referrerpolicy="no-referrer" />
@@ -50,11 +52,13 @@ class="h-auto max-w-full rounded-lg" src={app.phone_image_url_1} alt={app.name} + referrerpolicy="no-referrer" /> {app.name} {:else} @@ -62,6 +66,7 @@ class="h-auto max-w-full rounded-lg" src={app.icon_url_512} alt={app.name} + referrerpolicy="no-referrer" /> {/if}