Skip to content

Commit fe3c6ac

Browse files
committed
Fix games categories and bugs with total category sums
1 parent 4985eac commit fe3c6ac

13 files changed

+114
-72
lines changed

backend/api_app/controllers/companies.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from config import get_logger
3535
from dbcon.queries import (
3636
get_adtech_categories,
37-
get_adtech_category_type,
37+
get_companies_category_type,
3838
get_companies_parent_overview,
3939
get_companies_top,
4040
get_company_adstxt_ad_domain_overview,
@@ -47,7 +47,6 @@
4747
get_company_sdks,
4848
get_company_tree,
4949
get_tag_source_category_totals,
50-
get_tag_source_totals,
5150
get_topapps_for_company,
5251
search_companies,
5352
)
@@ -221,17 +220,14 @@ def get_overviews(
221220

222221
if type_slug:
223222
logger.info("Getting adtech category type")
224-
overview_df = get_adtech_category_type(type_slug, app_category=category)
223+
overview_df = get_companies_category_type(type_slug, app_category=category)
225224
else:
226225
logger.info("Getting companies parent overview")
227226
overview_df = get_companies_parent_overview(app_category=category)
228227

229-
if category:
230-
logger.info("Getting category totals")
231-
tag_source_category_app_counts = get_tag_source_category_totals()
232-
else:
233-
logger.info("Getting category totals")
234-
tag_source_category_app_counts = get_tag_source_totals()
228+
tag_source_category_app_counts = get_tag_source_category_totals(
229+
app_category=category
230+
)
235231

236232
overview_df = overview_df.merge(
237233
tag_source_category_app_counts,
@@ -887,7 +883,7 @@ async def get_companies_search(self: Self, search_term: str) -> list[CompanyDeta
887883

888884
results["app_category"] = "all"
889885

890-
category_totals_df = get_tag_source_totals()
886+
category_totals_df = get_tag_source_category_totals()
891887

892888
overview_df = results.merge(
893889
category_totals_df,

backend/dbcon/queries.py

Lines changed: 93 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ def load_sql_file(file_name: str) -> str:
5050
QUERY_ADTECH_CATEGORY_TYPE = load_sql_file(
5151
"query_adtech_category_type.sql",
5252
)
53-
QUERY_ADTECH_TYPE = load_sql_file("query_adtech_type.sql")
5453
QUERY_COMPANY_TOPAPPS = load_sql_file("query_company_top_apps.sql")
5554
QUERY_COMPANY_TOPAPPS_PARENT = load_sql_file("query_company_top_apps_parent.sql")
5655
QUERY_COMPANY_TOPAPPS_CATEGORY = load_sql_file("query_company_top_apps_category.sql")
@@ -72,7 +71,6 @@ def load_sql_file(file_name: str) -> str:
7271
QUERY_PARENT_COMPANY_CATEGORIES = load_sql_file("query_company_parent_category.sql")
7372
QUERY_COMPANY_CATEGORIES = load_sql_file("query_company_category.sql")
7473
QUERY_TAG_SOURCE_CATEGORY_TOTALS = load_sql_file("query_category_totals.sql")
75-
QUERY_TAG_SOURCE_TOTALS = load_sql_file("query_tag_source_totals.sql")
7674
QUERY_SDKS = load_sql_file("query_sdks.sql")
7775
QUERY_LATEST_SDKS = load_sql_file("query_sdks_latest.sql")
7876
QUERY_USER_REQUESTED_LATEST_SDKS = load_sql_file("query_sdks_user_requested_latest.sql")
@@ -195,23 +193,43 @@ def get_total_counts() -> pd.DataFrame:
195193
return df
196194

197195

198-
def get_adtech_category_type(
196+
def get_companies_category_type(
199197
type_slug: str,
200198
app_category: str | None = None,
201199
) -> pd.DataFrame:
202200
"""Get top companies for a category type."""
203-
if app_category:
204-
df = pd.read_sql(
205-
QUERY_ADTECH_CATEGORY_TYPE,
206-
con=DBCON.engine,
207-
params={"type_slug": type_slug, "app_category": app_category},
208-
)
201+
if app_category and app_category == "games":
202+
app_category = "game%"
203+
df = pd.read_sql(
204+
QUERY_ADTECH_CATEGORY_TYPE,
205+
con=DBCON.engine,
206+
params={"type_slug": type_slug, "app_category": app_category},
207+
)
208+
if app_category is None:
209+
df["app_category"] = "all"
209210
else:
210-
df = pd.read_sql(
211-
QUERY_ADTECH_TYPE, con=DBCON.engine, params={"type_slug": type_slug}
212-
)
211+
df.loc[df["app_category"].isna(), "app_category"] = "None"
212+
if app_category == "game%":
213+
df.loc[
214+
df["app_category"].str.contains("game"),
215+
"app_category",
216+
] = "games"
217+
df = (
218+
df.groupby(
219+
[
220+
"store",
221+
"tag_source",
222+
"company_domain",
223+
"company_name",
224+
"app_category",
225+
"type_url_slug",
226+
]
227+
)[["app_count"]]
228+
.sum()
229+
.reset_index()
230+
)
231+
213232
df["store"] = df["store"].replace({1: "Google Play", 2: "Apple App Store"})
214-
df.loc[df["app_category"].isna(), "app_category"] = "None"
215233
return df
216234

217235

@@ -326,11 +344,28 @@ def get_companies_parent_overview(app_category: str | None = None) -> pd.DataFra
326344
"""Get overview of companies from multiple types like sdk and app-ads.txt."""
327345
logger.info("query companies parent overview start")
328346
if app_category:
347+
if app_category == "games":
348+
app_category = "game%"
329349
df = pd.read_sql(
330350
QUERY_COMPANIES_PARENT_OVERVIEW_CATEGORY,
331351
DBCON.engine,
332352
params={"app_category": app_category},
333353
)
354+
if app_category == "game%":
355+
df["app_category"] = "games"
356+
df = (
357+
df.groupby(
358+
[
359+
"company_domain",
360+
"company_name",
361+
"store",
362+
"app_category",
363+
"tag_source",
364+
]
365+
)[["app_count"]]
366+
.sum()
367+
.reset_index()
368+
)
334369
else:
335370
df = pd.read_sql(QUERY_COMPANIES_PARENT_OVERVIEW, DBCON.engine)
336371
logger.info("query companies parent overview return")
@@ -346,6 +381,8 @@ def get_companies_top(
346381
) -> pd.DataFrame:
347382
"""Get overview of companies from multiple types like sdk and app-ads.txt."""
348383
logger.info("query companies parent top start")
384+
if app_category == "games":
385+
app_category = "game%"
349386
if type_slug:
350387
df = pd.read_sql(
351388
QUERY_COMPANIES_CATEGORY_TYPE_TOP,
@@ -494,19 +531,36 @@ def get_company_parent_categories(company_domain: str) -> pd.DataFrame:
494531
return df
495532

496533

497-
@lru_cache(maxsize=1)
498-
def get_tag_source_category_totals() -> pd.DataFrame:
534+
@lru_cache(maxsize=100)
535+
def get_tag_source_category_totals(app_category: str | None = None) -> pd.DataFrame:
499536
"""Get category totals."""
500-
df = pd.read_sql(QUERY_TAG_SOURCE_CATEGORY_TOTALS, DBCON.engine)
501-
df = df.rename(columns={"app_count": "total_app_count"})
502-
df["store"] = df["store"].replace({1: "Google Play", 2: "Apple App Store"})
503-
return df
504-
537+
if app_category:
538+
if app_category == "games":
539+
app_category = "game%"
540+
df = pd.read_sql(
541+
QUERY_TAG_SOURCE_CATEGORY_TOTALS,
542+
DBCON.engine,
543+
params={"app_category": app_category},
544+
)
545+
if app_category == "game%":
546+
df.loc[
547+
df["app_category"].str.contains("game"),
548+
"app_category",
549+
] = "games"
505550

506-
@lru_cache(maxsize=1)
507-
def get_tag_source_totals() -> pd.DataFrame:
508-
"""Get types totals."""
509-
df = pd.read_sql(QUERY_TAG_SOURCE_TOTALS, DBCON.engine)
551+
else:
552+
df = pd.read_sql(
553+
QUERY_TAG_SOURCE_CATEGORY_TOTALS,
554+
DBCON.engine,
555+
params={"app_category": "%"},
556+
)
557+
df["app_category"] = "all"
558+
df.loc[df["app_category"].isna(), "app_category"] = "None"
559+
df = (
560+
df.groupby(["app_category", "store", "tag_source"])[["app_count"]]
561+
.sum()
562+
.reset_index()
563+
)
510564
df = df.rename(columns={"app_count": "total_app_count"})
511565
df["store"] = df["store"].replace({1: "Google Play", 2: "Apple App Store"})
512566
return df
@@ -597,7 +651,7 @@ def get_single_apps_adstxt(store_id: str) -> pd.DataFrame:
597651
def get_topapps_for_company(
598652
company_domain: str, mapped_category: str | None = None, limit: int = 20
599653
) -> pd.DataFrame:
600-
"""Get top apps for for a network."""
654+
"""Get top apps for for a company."""
601655
if mapped_category == "games":
602656
mapped_category = "game%"
603657

@@ -621,6 +675,20 @@ def get_topapps_for_company(
621675
"mylimit": limit,
622676
},
623677
)
678+
if mapped_category == "game%":
679+
df = (
680+
df.groupby(
681+
[
682+
"company_domain",
683+
"store",
684+
"tag_source",
685+
"name",
686+
"store_id",
687+
]
688+
)[["installs", "rating_count"]]
689+
.sum()
690+
.reset_index()
691+
)
624692
else:
625693
df = pd.read_sql(
626694
QUERY_COMPANY_TOPAPPS_PARENT,

backend/dbcon/sql/query_adtech_category_type.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ FROM
33
frontend.companies_categories_types_app_counts
44
WHERE
55
type_url_slug = :type_slug
6-
AND (app_category = :app_category OR :app_category IS NULL);
6+
AND (app_category LIKE :app_category OR :app_category IS NULL);

backend/dbcon/sql/query_adtech_type.sql

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
SELECT * FROM frontend.total_categories_app_counts;
1+
SELECT * FROM frontend.total_categories_app_counts
2+
WHERE app_category LIKE :app_category;

backend/dbcon/sql/query_companies_category_type_top.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ WITH ranked_apps AS (
1212
frontend.companies_categories_types_app_counts
1313
WHERE
1414
type_url_slug = :type_slug
15-
AND (app_category = :app_category OR :app_category IS NULL)
15+
AND (app_category LIKE :app_category OR :app_category IS NULL)
1616
AND tag_source != 'app_ads_reseller'
1717
AND company_domain NOT ILIKE 'no-%-found'
1818
GROUP BY COALESCE(company_name, company_domain), tag_source, store
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
SELECT
22
company_domain,
3-
company_name,
43
store,
54
app_category,
65
tag_source,
6+
COALESCE(company_name, company_domain) AS company_name,
77
SUM(app_count) AS app_count
88
FROM
99
frontend.companies_parent_app_counts
1010
WHERE
11-
app_category = :app_category
11+
app_category LIKE :app_category
1212
GROUP BY company_domain, company_name, store, app_category, tag_source;

backend/dbcon/sql/query_companies_parent_top.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ WITH ranked_apps AS (
1111
FROM
1212
frontend.companies_parent_app_counts
1313
WHERE
14-
app_category = :app_category OR :app_category IS NULL
14+
(app_category LIKE :app_category OR :app_category IS NULL)
1515
AND tag_source != 'app_ads_reseller'
1616
AND company_domain NOT ILIKE 'no-%-found'
1717
GROUP BY COALESCE(company_name, company_domain), tag_source, store

backend/dbcon/sql/query_company_top_apps_category.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ WITH ranked_apps AS (
44
frontend.company_top_apps
55
WHERE
66
company_domain = :company_domain
7-
AND category = :mapped_category
7+
AND category LIKE :mapped_category
88
AND app_company_category_rank <= :mylimit
99
)
1010

backend/dbcon/sql/query_company_top_apps_category_parent.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ WITH ranked_apps AS (
44
frontend.company_parent_top_apps
55
WHERE
66
company_domain = :company_domain
7-
AND category = :mapped_category
7+
AND category LIKE :mapped_category
88
AND app_company_category_rank <= :mylimit
99
)
1010

0 commit comments

Comments
 (0)