Skip to content

Commit

Permalink
Merge pull request #31 from ddxv/new-adtech-schema
Browse files Browse the repository at this point in the history
New adtech schema
  • Loading branch information
ddxv authored Feb 18, 2024
2 parents 29da26e + ac694ac commit f204af1
Show file tree
Hide file tree
Showing 24 changed files with 302 additions and 354 deletions.
14 changes: 10 additions & 4 deletions backend/api_app/controllers/categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"""


from typing import Self

import numpy as np
from litestar import Controller, get

Expand All @@ -15,8 +17,9 @@


def category_overview() -> CategoriesOverview:
"""Categories for apps."""
cats = get_appstore_categories()
cats = cats[cats["total_apps"] > 100]
cats = cats[cats["total_apps"] > 100] # noqa: PLR2004 magic number ok

cats["name"] = cats["category"]
cats["name"] = (
Expand Down Expand Up @@ -49,10 +52,13 @@ def category_overview() -> CategoriesOverview:


class CategoryController(Controller):

"""App Store Categories API."""

path = "/api/categories"

@get(path="/", cache=True)
async def get_categories_overview(self) -> CategoriesOverview:
async def get_categories_overview(self: Self) -> CategoriesOverview:
"""Handle GET request for a list of categories.
Returns
Expand All @@ -66,8 +72,8 @@ async def get_categories_overview(self) -> CategoriesOverview:

return overview

@get(path="/{category_id:str}", cache=3600)
async def get_category(self, category_id: str) -> Category:
@get(path="/{category_id:str}", cache=True)
async def get_category(self: Self, category_id: str) -> Category:
"""Handle GET request for a single category.
Returns
Expand Down
92 changes: 92 additions & 0 deletions backend/api_app/controllers/companies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""API endoipoints for companies.
/networks/ returns list of top networks.
/trackers/ returns list of top trackers.
"""

from typing import Self

from litestar import Controller, get
from litestar.exceptions import NotFoundException

from api_app.models import CompanyApps, TopCompanies
from config import get_logger
from dbcon.queries import get_apps_for_company, get_top_companies

logger = get_logger(__name__)


def companies_overview(categories: list[int]) -> TopCompanies:
"""Process networks and return TopCompanies class."""
df = get_top_companies(categories=categories)
df = df[~df["name"].isna()]
df = df.sort_values("app_count", ascending=False)
networks = TopCompanies(companies=df.to_dict(orient="records"))
return networks


class CompaniesController(Controller):

"""API EndPoint return for all ad tech companies."""

path = "/api/"

@get(path="/networks", cache=3600)
async def top_networks(self: Self) -> TopCompanies:
"""Handle GET request for a list of top networks.
Returns
-------
A dictionary representation of the list of networks
each with an id, name, type and total of apps.
"""
logger.info(f"{self.path} start")
overview = companies_overview([1])

return overview

@get(path="/trackers", cache=3600)
async def top_trackers(self: Self) -> TopCompanies:
"""Handle GET request for a list of top trackers.
Returns
-------
A dictionary representation of the list of trackers
each with an id, name, type and total of apps.
"""
logger.info(f"{self.path} start")
overview = companies_overview([2, 3])

return overview

@get(path="/companies/{company_name:str}", cache=3600)
async def get_company_apps(self: Self, company_name: str) -> CompanyApps:
"""Handle GET request for a specific company.
Args:
----
company_name: The name of the company to retrieve apps for.
Returns:
-------
json.
"""
logger.info(f"{self.path} start")
apps_df = get_apps_for_company(company_name, include_parents=True)

if apps_df.empty:
msg = f"Network Name not found: {company_name!r}"
raise NotFoundException(
msg,
status_code=404,
)
apps_dict = apps_df.to_dict(orient="records")

apps = CompanyApps(
title=company_name,
apps=apps_dict,
)
return apps
76 changes: 0 additions & 76 deletions backend/api_app/controllers/networks.py

This file was deleted.

76 changes: 0 additions & 76 deletions backend/api_app/controllers/trackers.py

This file was deleted.

44 changes: 7 additions & 37 deletions backend/api_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,9 @@ class DeveloperApps:


@dataclass
class TrackerApps:
class CompanyApps:

"""A tracker's list of apps."""

title: str
apps: list[AppDetail]


@dataclass
class NetworkApps:

"""A network's list of apps."""
"""A company's list of apps."""

title: str
apps: list[AppDetail]
Expand Down Expand Up @@ -121,45 +112,24 @@ class CategoriesOverview:


@dataclass
class TrackerDetail:
class CompanyDetail:

"""Describes details of a tracker.
Includes its db identifier, name, and the count of its occurrences.
"""

tracker: int
tracker_name: str
count: int


@dataclass
class NetworkDetail:

"""Describes details of a network.
Includes its db identifier, name, and the count of its occurrences.
"""

network: int
network_name: str
company_id: int
name: str
count: int


@dataclass
class TopTrackers:

"""Contains a list of TrackerDetail objects representing the top trackers identified."""

trackers: list[TrackerDetail]


@dataclass
class TopNetworks:
class TopCompanies:

"""Contains a list of NetworkDetail objects representing the top networks identified."""

networks: list[NetworkDetail]
companies: list[CompanyDetail]


@dataclass
Expand Down
6 changes: 2 additions & 4 deletions backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@

from api_app.controllers.apps import AppController
from api_app.controllers.categories import CategoryController
from api_app.controllers.networks import NetworksController
from api_app.controllers.companies import CompaniesController
from api_app.controllers.rankings import RankingsController
from api_app.controllers.trackers import TrackersController

cors_config = CORSConfig(
allow_origins=[
Expand Down Expand Up @@ -39,8 +38,7 @@ class MyOpenAPIController(OpenAPIController):
AppController,
CategoryController,
RankingsController,
TrackersController,
NetworksController,
CompaniesController,
],
cors_config=cors_config,
openapi_config=OpenAPIConfig(
Expand Down
Loading

0 comments on commit f204af1

Please sign in to comment.