-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: make posthog optional (use mock if not env not set)
- Loading branch information
Nandan
committed
Feb 10, 2025
1 parent
b0e1086
commit ebedbad
Showing
16 changed files
with
228 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from app.modules.utils.analytics_service import ( | ||
AnalyticsService, | ||
PosthogAnalyticsService, | ||
MockAnalyticsService, | ||
) | ||
from dotenv import load_dotenv | ||
from starlette.datastructures import State | ||
from fastapi import Request | ||
import logging | ||
import os | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
# Analytics service configuration | ||
|
||
|
||
def init_analytics_service() -> AnalyticsService: | ||
POSTHOG_API_KEY = os.getenv("POSTHOG_API_KEY") or "" | ||
POSTHOG_HOST = os.getenv("POSTHOG_HOST") or "" | ||
|
||
if POSTHOG_API_KEY != "" and POSTHOG_HOST != "": | ||
logger.info(f"using PostHog analytics service with host {POSTHOG_HOST}") | ||
return PosthogAnalyticsService(POSTHOG_API_KEY, POSTHOG_HOST) | ||
|
||
logger.info( | ||
"no AnalyticsService envs found (POSTHOG_API_KEY, POSTHOG_HOST), using MockAnalytics service" | ||
) | ||
return MockAnalyticsService() | ||
|
||
|
||
def get_analytics_service(request: Request) -> AnalyticsService: | ||
return request.app.state.analytics_service | ||
|
||
|
||
# State initialization | ||
|
||
|
||
def init_state(state: State): | ||
load_dotenv(override=True) | ||
state.analytics_service = init_analytics_service() | ||
return state |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.