Skip to content

Commit

Permalink
fixes for companes vs app totals
Browse files Browse the repository at this point in the history
  • Loading branch information
ddxv committed Nov 1, 2024
1 parent f305e47 commit 332d0bd
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 76 deletions.
65 changes: 38 additions & 27 deletions backend/api_app/controllers/companies.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@

from api_app.models import (
AppGroup,
CategoryOverview,
CompaniesCategoryOverview,
CompaniesOverview,
CompanyApps,
CompanyAppsOverview,
CompanyCategoryOverview,
CompanyDetail,
CompanyPatterns,
CompanyPatternsDict,
Expand Down Expand Up @@ -160,9 +161,9 @@ def make_top_companies(top_df: pd.DataFrame) -> TopCompaniesShort:
return top_companies_short


def prep_overview_df(
def prep_companies_overview_df(
overview_df: pd.DataFrame, category_totals_df: pd.DataFrame
) -> tuple[pd.DataFrame, CategoryOverview]:
) -> tuple[pd.DataFrame, CompaniesCategoryOverview]:
overview_df = overview_df.merge(
category_totals_df,
on=["app_category", "store", "tag_source"],
Expand Down Expand Up @@ -239,7 +240,9 @@ def get_overviews(

top_companies_short = make_top_companies(top_df)

overview_df, category_overview = prep_overview_df(overview_df, category_totals_df)
overview_df, category_overview = prep_companies_overview_df(
overview_df, category_totals_df
)

results = CompaniesOverview(
companies_overview=overview_df.to_dict(orient="records"),
Expand Down Expand Up @@ -355,9 +358,9 @@ def transform_group(group: pd.DataFrame) -> dict:
return top


def make_category_uniques(df: pd.DataFrame) -> CategoryOverview:
def make_category_uniques(df: pd.DataFrame) -> CompaniesCategoryOverview:
"""Make category sums for overview."""
overview = CategoryOverview()
overview = CompaniesCategoryOverview()

# Precompute boolean masks
is_apple = df["store"].str.contains("Apple")
Expand All @@ -367,45 +370,51 @@ def make_category_uniques(df: pd.DataFrame) -> CategoryOverview:
is_app_ads_direct = df["tag_source"] == "app_ads_direct"

# Function to calculate unique counts
def get_unique_counts(mask: pd.Series) -> int:
def get_unique_company_counts(mask: pd.Series) -> int:
return df.loc[mask, "company_domain"].nunique()

# Calculate overall stats
overall_stats = {
"total_apps": df["company_domain"].nunique(),
"sdk_ios_total_apps": get_unique_counts(is_apple & is_sdk),
"sdk_android_total_apps": get_unique_counts(is_google & is_sdk),
"adstxt_direct_ios_total_apps": get_unique_counts(is_apple & is_app_ads_direct),
"adstxt_direct_android_total_apps": get_unique_counts(
"total_companies": df["company_domain"].nunique(),
"sdk_ios_total_companies": get_unique_company_counts(is_apple & is_sdk),
"sdk_android_total_companies": get_unique_company_counts(is_google & is_sdk),
"adstxt_direct_ios_total_companies": get_unique_company_counts(
is_apple & is_app_ads_direct
),
"adstxt_direct_android_total_companies": get_unique_company_counts(
is_google & is_app_ads_direct,
),
"adstxt_reseller_ios_total_apps": get_unique_counts(
"adstxt_reseller_ios_total_companies": get_unique_company_counts(
is_apple & is_app_ads_reseller,
),
"adstxt_reseller_android_total_apps": get_unique_counts(
"adstxt_reseller_android_total_companies": get_unique_company_counts(
is_google & is_app_ads_reseller,
),
}
overview.update_stats("all", **overall_stats)
overview.update_stats("companies", **overall_stats)

# Calculate stats for each category
categories = df["app_category"].unique()
for cat in categories:
cat_mask = df["app_category"] == cat
cat_stats = {
"total_apps": get_unique_counts(cat_mask),
"sdk_ios_total_apps": get_unique_counts(cat_mask & is_apple & is_sdk),
"sdk_android_total_apps": get_unique_counts(cat_mask & is_google & is_sdk),
"adstxt_direct_ios_total_apps": get_unique_counts(
"total_companies": get_unique_company_counts(cat_mask),
"sdk_ios_total_companies": get_unique_company_counts(
cat_mask & is_apple & is_sdk
),
"sdk_android_total_companies": get_unique_company_counts(
cat_mask & is_google & is_sdk
),
"adstxt_direct_ios_total_companies": get_unique_company_counts(
cat_mask & is_apple & is_app_ads_direct,
),
"adstxt_direct_android_total_apps": get_unique_counts(
"adstxt_direct_android_total_companies": get_unique_company_counts(
cat_mask & is_google & is_app_ads_direct,
),
"adstxt_reseller_ios_total_apps": get_unique_counts(
"adstxt_reseller_ios_total_companies": get_unique_company_counts(
cat_mask & is_apple & is_app_ads_reseller,
),
"adstxt_reseller_android_total_apps": get_unique_counts(
"adstxt_reseller_android_total_companies": get_unique_company_counts(
cat_mask & is_google & is_app_ads_reseller,
),
}
Expand All @@ -414,9 +423,9 @@ def get_unique_counts(mask: pd.Series) -> int:
return overview


def make_category_sums(df: pd.DataFrame) -> CategoryOverview:
def make_company_category_sums(df: pd.DataFrame) -> CompanyCategoryOverview:
"""Make category sums for overview."""
overview = CategoryOverview()
overview = CompanyCategoryOverview()
conditions = {
"sdk_ios": (df["store"].str.contains("Apple")) & (df["tag_source"] == "sdk"),
"sdk_android": (df["store"].str.contains("Google"))
Expand Down Expand Up @@ -585,7 +594,7 @@ async def companies_categories(self: Self, category: str) -> CompaniesOverview:
async def company_overview(
self: Self,
company_name: str,
) -> CategoryOverview:
) -> CompaniesCategoryOverview:
"""Handle GET request for a specific company.
Args:
Expand All @@ -603,7 +612,7 @@ async def company_overview(

df = get_company_overview(company_domain=company_name)

overview = make_category_sums(df=df)
overview = make_company_category_sums(df=df)

return overview

Expand Down Expand Up @@ -978,7 +987,9 @@ async def get_companies_search(self: Self, search_term: str) -> list[CompanyDeta

category_totals_df = get_types_totals()

overview_df, _category_overview = prep_overview_df(results, category_totals_df)
overview_df, _category_overview = prep_companies_overview_df(
results, category_totals_df
)
logger.info(f"{self.path}/{search_term} return")

return overview_df.to_dict(orient="records")
46 changes: 41 additions & 5 deletions backend/api_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,23 @@ class TopCompaniesOverviewShort:


@dataclass
class CategoryAppStats:
class CategoryCompaniesStats:
"""Contains a list of CompanyDetail objects.
Representing the top networks identified.
"""

total_companies: int = 0
adstxt_direct_ios_total_companies: int = 0
adstxt_direct_android_total_companies: int = 0
adstxt_reseller_ios_total_companies: int = 0
adstxt_reseller_android_total_companies: int = 0
sdk_ios_total_companies: int = 0
sdk_android_total_companies: int = 0


@dataclass
class CategoryCompanyStats:
"""Contains a list of CompanyDetail objects.
Representing the top networks identified.
Expand All @@ -226,15 +242,35 @@ class CategoryAppStats:


@dataclass
class CategoryOverview:
class CompaniesCategoryOverview:
"""Contains a dictionary of categories, each with their associated statistics."""

categories: dict[str, CategoryCompaniesStats] = field(default_factory=dict)

def add_category(self, category: str) -> None:
"""Add a category to the overview."""
if category not in self.categories:
self.categories[category] = CategoryCompaniesStats()

def update_stats(self, category: str, **kwargs: int) -> None:
"""Update the stats for a category."""
if category not in self.categories:
self.add_category(category)
for key, value in kwargs.items():
if hasattr(self.categories[category], key):
setattr(self.categories[category], key, value)


@dataclass
class CompanyCategoryOverview:
"""Contains a dictionary of categories, each with their associated statistics."""

categories: dict[str, CategoryAppStats] = field(default_factory=dict)
categories: dict[str, CategoryCompanyStats] = field(default_factory=dict)

def add_category(self, category: str) -> None:
"""Add a category to the overview."""
if category not in self.categories:
self.categories[category] = CategoryAppStats()
self.categories[category] = CategoryCompanyStats()

def update_stats(self, category: str, **kwargs: int) -> None:
"""Update the stats for a category."""
Expand All @@ -254,7 +290,7 @@ class CompaniesOverview:

companies_overview: list[CompanyDetail]
top: TopCompaniesShort
categories: CategoryOverview
categories: CompaniesCategoryOverview


@dataclass
Expand Down
6 changes: 0 additions & 6 deletions frontend/src/lib/AdtechTable.svelte
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
<script lang="ts">
import { homeCategorySelection } from '../stores';
import type { Company } from '../types';
export let tabledata: Company[];
export let tableType: string = 'appcount';
export let storeId: number = 1;
export let store_name: string;
$: category_name = $homeCategorySelection;
function formatNumber(num: number) {
if (num >= 1000000000000) return (num / 1000000000000).toFixed(1).replace(/\.0$/, '') + 'T';
Expand Down
1 change: 0 additions & 1 deletion frontend/src/lib/CompanyOverviewTable.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<!-- @migration-task Error while migrating Svelte code: `<a>` is invalid inside `<tr>` -->
<script lang="ts">
import Pagination from './Pagination.svelte';
Expand Down
13 changes: 7 additions & 6 deletions frontend/src/routes/(newcategorical)/companies/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<h2 class="text-xl font-bold text-gray-800 mb-4">Total Companies</h2>
<p class="text-lg text-gray-700">
<span class="font-semibold text-gray-900"
>{formatNumber(myData.categories.categories.all.total_apps)}</span
>{formatNumber(myData.categories.categories.all.total_companies)}</span
>
</p>
</div>
Expand Down Expand Up @@ -103,20 +103,21 @@
{/snippet}

{#snippet sdkAndroidTotalApps()}
>Android Companies:
{formatNumber(tableData.categories.categories.all.sdk_android_total_apps)}
Android Companies: {formatNumber(
tableData.categories.categories.all.sdk_android_total_companies
)}
{/snippet}
{#snippet sdkIosTotalApps()}
iOS Companies: {formatNumber(tableData.categories.categories.all.sdk_ios_total_apps)}
iOS Companies: {formatNumber(tableData.categories.categories.all.sdk_ios_total_companies)}
{/snippet}
{#snippet adstxtAndroidTotalApps()}
Android Companies: {formatNumber(
tableData.categories.categories.all.adstxt_direct_android_total_apps
tableData.categories.categories.all.adstxt_direct_android_total_companies
)}
{/snippet}
{#snippet adstxtIosTotalApps()}
iOS Companies: {formatNumber(
tableData.categories.categories.all.adstxt_direct_ios_total_apps
tableData.categories.categories.all.adstxt_direct_ios_total_companies
)}
{/snippet}
</CompaniesTableGrid>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,18 @@
{:else}
<CompanyTableGrid>
{#snippet sdkAndroidTotalApps()}
{name} total Android apps: {formatNumber(
detailsData.categories.all.sdk_android_total_apps
)}
Total Android Apps: {formatNumber(detailsData.categories.all.sdk_android_total_apps)}
{/snippet}
{#snippet sdkIosTotalApps()}
{name} total iOS apps: {formatNumber(detailsData.categories.all.sdk_ios_total_apps)}
Total iOS Apps: {formatNumber(detailsData.categories.all.sdk_ios_total_apps)}
{/snippet}
{#snippet adstxtAndroidTotalApps()}
{name} total Android apps: {formatNumber(
Total Android Apps: {formatNumber(
detailsData.categories.all.adstxt_direct_android_total_apps
)}
{/snippet}
{#snippet adstxtIosTotalApps()}
{name} total iOS apps: {formatNumber(
detailsData.categories.all.adstxt_direct_ios_total_apps
)}
Total iOS Apps: {formatNumber(detailsData.categories.all.adstxt_direct_ios_total_apps)}
{/snippet}

{#snippet sdkAndroid()}
Expand Down
Empty file.
Loading

0 comments on commit 332d0bd

Please sign in to comment.