Skip to content

Commit

Permalink
Merge pull request #61 from ddxv/homepage-update
Browse files Browse the repository at this point in the history
Homepage update
  • Loading branch information
ddxv authored Oct 28, 2024
2 parents 749bf1f + d2ff819 commit bcb92d8
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 89 deletions.
120 changes: 77 additions & 43 deletions backend/api_app/controllers/companies.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
CompanyPlatformOverview,
CompanyTypes,
ParentCompanyTree,
PlatformCompanies,
TopCompanies,
TopCompaniesShort,
TopCompaniesOverviewShort,
)
from config import get_logger
from dbcon.queries import (
Expand Down Expand Up @@ -108,6 +109,47 @@ def get_company_apps_new(
)
return results

def make_top_companies(top_df: pd.DataFrame) -> TopCompaniesShort:
top_sdk_df = top_df[top_df["tag_source"] == "sdk"].copy()
top_adstxt_direct_df = top_df[top_df["tag_source"] == "app_ads_direct"].copy()
top_adstxt_reseller_df = top_df[top_df["tag_source"] == "app_ads_reseller"].copy()

top_sdk_df["company_title"] = np.where(
top_sdk_df["company_name"].isna(),
top_sdk_df["company_domain"],
top_sdk_df["company_name"],
)
top_adstxt_direct_df["company_title"] = np.where(
top_adstxt_direct_df["company_name"].isna(),
top_adstxt_direct_df["company_domain"],
top_adstxt_direct_df["company_name"],
)
top_adstxt_reseller_df["company_title"] = np.where(
top_adstxt_reseller_df["company_name"].isna(),
top_adstxt_reseller_df["company_domain"],
top_adstxt_reseller_df["company_name"],
)

top_sdk_df = top_sdk_df.rename(
columns={"company_title": "group", "app_count": "value"},
).sort_values(by=["value"], ascending=True)
top_adstxt_direct_df = top_adstxt_direct_df.rename(
columns={"company_title": "group", "app_count": "value"},
).sort_values(by=["value"], ascending=True)
top_adstxt_reseller_df = top_adstxt_reseller_df.rename(
columns={"company_title": "group", "app_count": "value"},
).sort_values(by=["value"], ascending=True)

top_companies_short = TopCompaniesShort(
sdk=top_sdk_df.to_dict(orient="records"),
adstxt_direct=top_adstxt_direct_df.to_dict(orient="records"),
adstxt_reseller=top_adstxt_reseller_df.to_dict(orient="records"),
)
return top_companies_short





def get_overviews(
category: str | None = None,
Expand Down Expand Up @@ -140,36 +182,6 @@ def get_overviews(
validate="m:1",
)

top_sdk_df = top_df[top_df["tag_source"] == "sdk"].copy()
top_adstxt_direct_df = top_df[top_df["tag_source"] == "app_ads_direct"].copy()
top_adstxt_reseller_df = top_df[top_df["tag_source"] == "app_ads_reseller"].copy()

top_sdk_df["company_title"] = np.where(
top_sdk_df["company_name"].isna(),
top_sdk_df["company_domain"],
top_sdk_df["company_name"],
)
top_adstxt_direct_df["company_title"] = np.where(
top_adstxt_direct_df["company_name"].isna(),
top_adstxt_direct_df["company_domain"],
top_adstxt_direct_df["company_name"],
)
top_adstxt_reseller_df["company_title"] = np.where(
top_adstxt_reseller_df["company_name"].isna(),
top_adstxt_reseller_df["company_domain"],
top_adstxt_reseller_df["company_name"],
)

top_sdk_df = top_sdk_df.rename(
columns={"company_title": "group", "app_count": "value"},
).sort_values(by=["value"], ascending=True)
top_adstxt_direct_df = top_adstxt_direct_df.rename(
columns={"company_title": "group", "app_count": "value"},
).sort_values(by=["value"], ascending=True)
top_adstxt_reseller_df = top_adstxt_reseller_df.rename(
columns={"company_title": "group", "app_count": "value"},
).sort_values(by=["value"], ascending=True)

category_overview = make_category_uniques(df=overview_df)

overview_df = (
Expand Down Expand Up @@ -211,17 +223,11 @@ def get_overviews(
ascending=False,
).head(1000)

top_companies_short = make_top_companies(top_df)

results = CompaniesOverview(
companies_overview=overview_df.to_dict(orient="records"),
sdk=PlatformCompanies(
top=top_sdk_df.to_dict(orient="records"),
),
adstxt_direct=PlatformCompanies(
top=top_adstxt_direct_df.to_dict(orient="records"),
),
adstxt_reseller=PlatformCompanies(
top=top_adstxt_reseller_df.to_dict(orient="records"),
),
top = top_companies_short,
categories=category_overview,
)

Expand Down Expand Up @@ -262,7 +268,7 @@ def append_overall_categories(df: pd.DataFrame) -> pd.DataFrame:
return df


def companies_overview(categories: list[int]) -> TopCompanies:
def old_companies_overview(categories: list[int]) -> TopCompanies:
"""Process networks and return TopCompanies class."""
df = get_top_companies(categories=categories)
df_parents = get_top_companies(
Expand Down Expand Up @@ -844,7 +850,7 @@ async def top_networks(self: Self) -> TopCompanies:
"""
logger.info("GET /api/networks start")
overview = companies_overview(categories=[1])
overview = old_companies_overview(categories=[1])
logger.info("GET /api/networks return")

return overview
Expand All @@ -860,7 +866,7 @@ async def top_trackers(self: Self) -> TopCompanies:
"""
logger.info("GET /api/trackers start")
overview = companies_overview(categories=[2, 3])
overview = old_companies_overview(categories=[2, 3])
logger.info("GET /api/trackers return")

return overview
Expand Down Expand Up @@ -902,3 +908,31 @@ async def adtech_type(
logger.info(f"/companies/types/{type_slug}?{category=} return")

return overview

@get(path="/companies/topshort/", cache=True)
async def get_companies_shortlist_top(self: Self) -> TopCompaniesOverviewShort:
"""Handle GET request for a list of adtech company categories.
Returns
-------
A dictionary representation of the list of categories
each with an id, name, type and total of apps
"""
logger.info(f"{self.path} start")
adnetworks = get_companies_top(type_slug='ad-networks', app_category=None, limit=5)
mmps = get_companies_top(type_slug='ad-attribution', app_category=None, limit=5)
# analytics = get_companies_top(type_slug='analytics', app_category=None, limit=5)
top_ad_networks = make_top_companies(adnetworks)
top_mmps = make_top_companies(mmps)
# top_analytics = make_top_companies(analytics)
logger.info(f"{self.path} return")

top_companies = TopCompaniesOverviewShort(
adnetworks=top_ad_networks,
attribution=top_mmps,
analytics=list(),
)

return top_companies

24 changes: 19 additions & 5 deletions backend/api_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,24 @@ class CompanyDetail:

@dataclass
class PlatformCompanies:
"""Represents companies for a specific platform."""
"""Companies data for a specific platform (iOS/Android)."""
ios: list[dict]
android: list[dict]

@dataclass
class TopCompaniesShort:
"""Represents top companies across different categories."""
sdk: PlatformCompanies
adstxt_direct: PlatformCompanies
adstxt_reseller: PlatformCompanies

@dataclass
class TopCompaniesOverviewShort:
"""Represents top companies across different categories."""
adnetworks: TopCompaniesShort
attribution: TopCompaniesShort
analytics: TopCompaniesShort

top: list[dict]


@dataclass
Expand Down Expand Up @@ -233,9 +248,7 @@ class CompaniesOverview:
"""

companies_overview: list[CompanyDetail]
sdk: PlatformCompanies
adstxt_direct: PlatformCompanies
adstxt_reseller: PlatformCompanies
top: TopCompaniesShort
categories: CategoryOverview


Expand Down Expand Up @@ -320,3 +333,4 @@ class AppRank:

latest: dict
history: dict

4 changes: 2 additions & 2 deletions frontend/src/routes/(newcategorical)/companies/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@

{#snippet card2()}
<WhiteCard
><CompaniesBarChart plotData={myData.sdk.top} plotTitle="Top SDK Companies" /></WhiteCard
><CompaniesBarChart plotData={myData.top.sdk} plotTitle="Top SDK Companies" /></WhiteCard
>
{/snippet}
{#snippet card3()}
<WhiteCard
><CompaniesBarChart
plotData={myData.adstxt_direct.top}
plotData={myData.top.adstxt_direct}
plotTitle="Top Adstxt Companies"
/></WhiteCard
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@

{#snippet card2()}
<WhiteCard >
<CompaniesBarChart plotData={myData.sdk.top} plotTitle="Top SDK Companies" />
<CompaniesBarChart plotData={myData.top.sdk} plotTitle="Top SDK Companies" />
</WhiteCard>
{/snippet}
{#snippet card3()}
<WhiteCard
><CompaniesBarChart
plotData={myData.adstxt_direct.top}
plotData={myData.top.adstxt_direct}
plotTitle="Top Adstxt Companies"
/></WhiteCard
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@
{:then myData}
{#if typeof myData == 'string'}
<p class="text-red-500 text-center">Failed to load company details.</p>
{:else if myData && myData.categories}
{:else}
<CompaniesLayout>
<WhiteCard slot="card1">
{#snippet card1()}
<WhiteCard>
<div class="bg-white p-6 rounded-lg shadow-md">
<h2 class="text-xl font-bold text-gray-800 mb-4">Total Ad Tech Companies</h2>
<p class="text-lg text-gray-700">
Expand All @@ -58,17 +59,19 @@
</p>
</div>
</WhiteCard>

<WhiteCard slot="card2"
><CompaniesBarChart plotData={myData.sdk.top} plotTitle="Top SDK Companies" /></WhiteCard
{/snippet}
{#snippet card2()}
<WhiteCard
><CompaniesBarChart plotData={myData.top.sdk} plotTitle="Top SDK Companies" /></WhiteCard
>
<!-- {#if $page.params.type != 'ad-networks'} -->
<WhiteCard slot="card3"><CompaniesBarChart
plotData={myData.adstxt_direct.top}
plotData={myData.top.adstxt_direct}
plotTitle="Top Adstxt Companies"
/></WhiteCard
>
<!-- {/if} -->
{/snippet}
</CompaniesLayout>
{/if}
{:catch error}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import type { PageData } from './$types';
import { type CompaniesOverview } from '../../../../../../types';
interface Props {
data: PageData;
}
Expand Down
7 changes: 5 additions & 2 deletions frontend/src/routes/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const csr: boolean = true;

import type { PageServerLoad } from './$types.js';

export const load: PageServerLoad = async ({ params, setHeaders, url }) => {
export const load: PageServerLoad = async ({ setHeaders }) => {
const emptyResponse = {};
setHeaders({
'cache-control': 'max-age=3600'
Expand All @@ -14,12 +14,15 @@ export const load: PageServerLoad = async ({ params, setHeaders, url }) => {

const androidGameRanks = fetch(`http://localhost:8000/api/rankings/1/1/36/short`);
const iOSGameRanks = fetch(`http://localhost:8000/api/rankings/2/4/62/short`);

const topCompanies = fetch(`http://localhost:8000/api/companies/topshort`);

return {
androidAppRanks: androidAppRanks.then((resp) => resp.json()),
iOSAppRanks: iOSAppRanks.then((resp) => resp.json()),
androidGameRanks: androidGameRanks.then((resp) => resp.json()),
iOSGameRanks: iOSGameRanks.then((resp) => resp.json())
iOSGameRanks: iOSGameRanks.then((resp) => resp.json()),
topCompanies: topCompanies.then((resp) => resp.json())
};
} catch (error) {
console.error('Failed to load app data:', error);
Expand Down
Loading

0 comments on commit bcb92d8

Please sign in to comment.