Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

refactor: Refactors analytics into telemetry module #20

Merged
merged 3 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</a>
<a href="https://www.codacy.com/gh/quack-ai/contribution-api/dashboard?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=quack-ai/contribution-api&amp;utm_campaign=Badge_Grade"><img src="https://app.codacy.com/project/badge/Grade/f8b24d4f9f674ef487b0889b2aa90e9c"/></a>
<a href="https://codecov.io/gh/quack-ai/contribution-api">
<img src="https://img.shields.io/codecov/c/github/quack-ai/contribution-api.svg?logo=codecov&style=flat-square" alt="Test coverage percentage">
<img src="https://img.shields.io/codecov/c/github/quack-ai/contribution-api.svg?logo=codecov&style=flat-square&token=fkT0jQefhO" alt="Test coverage percentage">
</a>
</p>
<p align="center">
Expand Down
10 changes: 5 additions & 5 deletions src/app/api/api_v1/endpoints/guidelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
from fastapi import APIRouter, Depends, Path, Security, status

from app.api.dependencies import get_current_user, get_guideline_crud
from app.core.analytics import analytics_client
from app.crud import GuidelineCRUD
from app.models import Guideline, UserScope
from app.schemas.guidelines import ContentUpdate, GuidelineCreate, GuidelineEdit, OrderUpdate
from app.services.telemetry import telemetry_client

router = APIRouter()

Expand All @@ -24,7 +24,7 @@
user=Security(get_current_user, scopes=[UserScope.USER, UserScope.ADMIN]),
) -> Guideline:
guideline = await guidelines.create(payload)
analytics_client.capture(user.id, event="guideline-creation", properties={"repo_id": payload.repo_id})
telemetry_client.capture(user.id, event="guideline-creation", properties={"repo_id": payload.repo_id})

Check warning on line 27 in src/app/api/api_v1/endpoints/guidelines.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/guidelines.py#L27

Added line #L27 was not covered by tests
return guideline


Expand Down Expand Up @@ -53,7 +53,7 @@
user=Security(get_current_user, scopes=[UserScope.USER, UserScope.ADMIN]),
) -> Guideline:
guideline = await guidelines.update(guideline_id, ContentUpdate(**payload.dict(), updated_at=datetime.utcnow()))
analytics_client.capture(user.id, event="guideline-content", properties={"repo_id": guideline.repo_id})
telemetry_client.capture(user.id, event="guideline-content", properties={"repo_id": guideline.repo_id})

Check warning on line 56 in src/app/api/api_v1/endpoints/guidelines.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/guidelines.py#L56

Added line #L56 was not covered by tests
return guideline


Expand All @@ -65,7 +65,7 @@
user=Security(get_current_user, scopes=[UserScope.USER, UserScope.ADMIN]),
) -> Guideline:
guideline = await guidelines.update(guideline_id, OrderUpdate(order=order_idx, updated_at=datetime.utcnow()))
analytics_client.capture(user.id, event="guideline-order", properties={"repo_id": guideline.repo_id})
telemetry_client.capture(user.id, event="guideline-order", properties={"repo_id": guideline.repo_id})

Check warning on line 68 in src/app/api/api_v1/endpoints/guidelines.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/guidelines.py#L68

Added line #L68 was not covered by tests
return guideline


Expand All @@ -77,4 +77,4 @@
) -> None:
guideline = cast(Guideline, await guidelines.get(guideline_id, strict=True))
await guidelines.delete(guideline_id)
analytics_client.capture(user.id, event="guideline-deletion", properties={"repo_id": guideline.repo_id})
telemetry_client.capture(user.id, event="guideline-deletion", properties={"repo_id": guideline.repo_id})

Check warning on line 80 in src/app/api/api_v1/endpoints/guidelines.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/guidelines.py#L80

Added line #L80 was not covered by tests
10 changes: 5 additions & 5 deletions src/app/api/api_v1/endpoints/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
from pydantic import HttpUrl

from app.api.dependencies import get_user_crud
from app.core.analytics import analytics_client
from app.core.config import settings
from app.core.security import create_access_token, hash_password, verify_password
from app.crud import UserCRUD
from app.models import UserScope
from app.schemas.login import GHAccessToken, GHToken, GHTokenRequest, Token, TokenRequest
from app.schemas.users import UserCreation
from app.services.telemetry import telemetry_client

router = APIRouter()

Expand Down Expand Up @@ -72,7 +72,7 @@
# create access token using user user_id/user_scopes
token_data = {"sub": str(user.id), "scopes": user.scope.split()}
token = await create_access_token(token_data, settings.ACCESS_TOKEN_UNLIMITED_MINUTES)
analytics_client.capture(user.id, event="user-login", properties={"login": user.login})
telemetry_client.capture(user.id, event="user-login", properties={"login": user.login})

Check warning on line 75 in src/app/api/api_v1/endpoints/login.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/login.py#L75

Added line #L75 was not covered by tests

return Token(access_token=token, token_type="bearer") # nosec B106 # noqa S106

Expand Down Expand Up @@ -101,7 +101,7 @@
user = await users.get(gh_user["id"], strict=False)
# Register if non existing
if user is None:
analytics_client.identify(
telemetry_client.identify(

Check warning on line 104 in src/app/api/api_v1/endpoints/login.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/login.py#L104

Added line #L104 was not covered by tests
gh_user["id"],
properties={
"login": gh_user["login"],
Expand All @@ -118,11 +118,11 @@
scope=UserScope.USER,
)
)
analytics_client.capture(user.id, event="user-creation", properties={"login": gh_user["login"]})
telemetry_client.capture(user.id, event="user-creation", properties={"login": gh_user["login"]})

Check warning on line 121 in src/app/api/api_v1/endpoints/login.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/login.py#L121

Added line #L121 was not covered by tests

# create access token using user user_id/user_scopes
token_data = {"sub": str(user.id), "scopes": user.scope.split()}
token = await create_access_token(token_data, settings.ACCESS_TOKEN_UNLIMITED_MINUTES)
analytics_client.capture(user.id, event="user-login", properties={"login": user.login})
telemetry_client.capture(user.id, event="user-login", properties={"login": user.login})

Check warning on line 126 in src/app/api/api_v1/endpoints/login.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/login.py#L126

Added line #L126 was not covered by tests

return Token(access_token=token, token_type="bearer") # nosec B106 # noqa S106
16 changes: 8 additions & 8 deletions src/app/api/api_v1/endpoints/repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
from fastapi import APIRouter, Depends, HTTPException, Path, Security, status

from app.api.dependencies import get_current_user, get_guideline_crud, get_repo_crud
from app.core.analytics import analytics_client
from app.crud import GuidelineCRUD, RepositoryCRUD
from app.models import Guideline, Repository, User, UserScope
from app.schemas.guidelines import OrderUpdate
from app.schemas.repos import GuidelineOrder, RepoCreate, RepoCreation, RepoUpdate
from app.services.telemetry import telemetry_client

router = APIRouter()

Expand All @@ -29,13 +29,13 @@
if entry is not None:
await repos.update(payload.id, RepoUpdate(removed_at=None))
entry.removed_at = None
analytics_client.capture(
telemetry_client.capture(

Check warning on line 32 in src/app/api/api_v1/endpoints/repos.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/repos.py#L32

Added line #L32 was not covered by tests
user.id, event="repo-enable", properties={"repo_id": payload.id, "full_name": payload.full_name}
)
return entry

repo = await repos.create(RepoCreation(**payload.dict(), installed_by=user.id))
analytics_client.capture(
telemetry_client.capture(

Check warning on line 38 in src/app/api/api_v1/endpoints/repos.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/repos.py#L38

Added line #L38 was not covered by tests
user.id, event="repo-creation", properties={"repo_id": payload.id, "full_name": payload.full_name}
)
return repo
Expand All @@ -56,7 +56,7 @@
user=Security(get_current_user, scopes=[UserScope.USER, UserScope.ADMIN]),
) -> List[Repository]:
entries = await repos.fetch_all() if user.scope == UserScope.ADMIN else await repos.fetch_all(("owner_id", user.id))
analytics_client.capture(user.id, event="repo-fetch")
telemetry_client.capture(user.id, event="repo-fetch")

Check warning on line 59 in src/app/api/api_v1/endpoints/repos.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/repos.py#L59

Added line #L59 was not covered by tests
return [elt for elt in entries]


Expand All @@ -81,7 +81,7 @@
await guidelines.update(guideline_id, OrderUpdate(order=order_idx, updated_at=datetime.utcnow()))
for order_idx, guideline_id in enumerate(payload.guideline_ids)
]
analytics_client.capture(user.id, event="guideline-order", properties={"repo_id": repo_id})
telemetry_client.capture(user.id, event="guideline-order", properties={"repo_id": repo_id})

Check warning on line 84 in src/app/api/api_v1/endpoints/repos.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/repos.py#L84

Added line #L84 was not covered by tests
return guideline_list


Expand All @@ -92,7 +92,7 @@
user=Security(get_current_user, scopes=[UserScope.USER, UserScope.ADMIN]),
) -> Repository:
repo = await repos.update(repo_id, RepoUpdate(removed_at=datetime.utcnow()))
analytics_client.capture(user.id, event="repo-disable", properties={"repo_id": repo_id})
telemetry_client.capture(user.id, event="repo-disable", properties={"repo_id": repo_id})

Check warning on line 95 in src/app/api/api_v1/endpoints/repos.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/repos.py#L95

Added line #L95 was not covered by tests
return repo


Expand All @@ -103,7 +103,7 @@
user=Security(get_current_user, scopes=[UserScope.USER, UserScope.ADMIN]),
) -> Repository:
repo = await repos.update(repo_id, RepoUpdate(removed_at=None))
analytics_client.capture(user.id, event="repo-enable", properties={"repo_id": repo_id})
telemetry_client.capture(user.id, event="repo-enable", properties={"repo_id": repo_id})

Check warning on line 106 in src/app/api/api_v1/endpoints/repos.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/repos.py#L106

Added line #L106 was not covered by tests
return repo


Expand All @@ -114,7 +114,7 @@
user=Security(get_current_user, scopes=[UserScope.ADMIN]),
) -> None:
await repos.delete(repo_id)
analytics_client.capture(user.id, event="repo-delete", properties={"repo_id": repo_id})
telemetry_client.capture(user.id, event="repo-delete", properties={"repo_id": repo_id})

Check warning on line 117 in src/app/api/api_v1/endpoints/repos.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/repos.py#L117

Added line #L117 was not covered by tests


@router.get("/{repo_id}/guidelines", status_code=status.HTTP_200_OK)
Expand Down
6 changes: 3 additions & 3 deletions src/app/api/api_v1/endpoints/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
from fastapi import APIRouter, Depends, Path, Security, status

from app.api.dependencies import get_current_user, get_user_crud
from app.core.analytics import analytics_client
from app.core.security import hash_password
from app.crud import UserCRUD
from app.models import User, UserScope
from app.schemas.users import Cred, CredHash, UserCreate, UserCreation
from app.services.telemetry import telemetry_client

router = APIRouter()

Expand All @@ -29,7 +29,7 @@
user = await users.create(
UserCreation(id=payload.id, login=payload.login, hashed_password=pwd, scope=payload.scope)
)
analytics_client.capture(payload.id, event="user-creation", properties={"login": payload.login})
telemetry_client.capture(payload.id, event="user-creation", properties={"login": payload.login})

Check warning on line 32 in src/app/api/api_v1/endpoints/users.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/users.py#L32

Added line #L32 was not covered by tests
return user


Expand Down Expand Up @@ -68,4 +68,4 @@
user=Security(get_current_user, scopes=[UserScope.ADMIN]),
) -> None:
await users.delete(user_id)
analytics_client.capture(user_id, event="user-deletion", properties={"login": user.login})
telemetry_client.capture(user_id, event="user-deletion", properties={"login": user.login})

Check warning on line 71 in src/app/api/api_v1/endpoints/users.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/users.py#L71

Added line #L71 was not covered by tests
14 changes: 7 additions & 7 deletions src/app/core/analytics.py → src/app/services/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@

logger = logging.getLogger("uvicorn.error")

__all__ = ["analytics_client"]
__all__ = ["telemetry_client"]


class AnalyticsClient:
def __init__(self, ph_api_key: Union[str, None] = None) -> None:
self.is_enabled = isinstance(ph_api_key, str)
if isinstance(ph_api_key, str):
self.ph_client = Posthog(project_api_key=ph_api_key, host="https://eu.posthog.com")
class TelemetryClient:
def __init__(self, api_key: Union[str, None] = None) -> None:
self.is_enabled = isinstance(api_key, str)
if isinstance(api_key, str):
self.ph_client = Posthog(project_api_key=api_key, host="https://eu.posthog.com")

Check warning on line 22 in src/app/services/telemetry.py

View check run for this annotation

Codecov / codecov/patch

src/app/services/telemetry.py#L22

Added line #L22 was not covered by tests
logger.info("PostHog enabled")

def capture(self, *args, **kwargs) -> None:
Expand All @@ -31,4 +31,4 @@
self.ph_client.identify(*args, **kwargs)


analytics_client = AnalyticsClient(ph_api_key=settings.POSTHOG_KEY)
telemetry_client = TelemetryClient(api_key=settings.POSTHOG_KEY)
Loading