mirror of
https://github.com/PostHog/posthog.git
synced 2024-11-28 18:26:15 +01:00
7de761dff0
* Update some posthoganalytics.capture calls * Update report_user_logged_in tracking * Update report_team_member_invited * Update report_bulk_invited * Update report_org_usage * Update AnalyticsDestroyModelMixin * Update action creation/updating tracking * Update annotations tracking * Update cohort tracking * Update dashboards tracking * Update feature flags tracking * Update imports, sessions tracking * More typeignores * Default properties blank * update metadata default * Stringify * Fix some tests * Kill debug code * Update FF tests * Update test_organization * Update test_organization_invites * Update test_user * Fix org usage report * update test_send_license_usage * Update organization tests * Respect dry-run on errors as well, log * Update organization properties on report * Assert organization props * Remove dead imports * Fix test
68 lines
2.8 KiB
Python
68 lines
2.8 KiB
Python
import posthoganalytics
|
|
import requests
|
|
from dateutil.relativedelta import relativedelta
|
|
from django.utils import timezone
|
|
|
|
from ee.clickhouse.client import sync_execute
|
|
from ee.models.license import License
|
|
from posthog.models import User
|
|
from posthog.settings import SITE_URL
|
|
from posthog.tasks.status_report import get_instance_licenses
|
|
|
|
|
|
def send_license_usage():
|
|
license = License.objects.first_valid()
|
|
user = User.objects.first()
|
|
if not license:
|
|
return
|
|
try:
|
|
date_from = (timezone.now() - relativedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0)
|
|
date_to = timezone.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
|
events_count = sync_execute(
|
|
"select count(1) from events where timestamp >= %(date_from)s and timestamp < %(date_to)s and not startsWith(event, '$$')",
|
|
{"date_from": date_from, "date_to": date_to},
|
|
)[0][0]
|
|
response = requests.post(
|
|
"https://license.posthog.com/licenses/usage",
|
|
data={"date": date_from.strftime("%Y-%m-%d"), "key": license.key, "events_count": events_count,},
|
|
)
|
|
|
|
response.raise_for_status()
|
|
if not response.ok:
|
|
posthoganalytics.capture(
|
|
user.distinct_id, # type: ignore
|
|
"send license usage data error",
|
|
{
|
|
"error": response.content,
|
|
"status_code": response.status_code,
|
|
"date": date_from.strftime("%Y-%m-%d"),
|
|
"events_count": events_count,
|
|
"organization_name": user.current_organization.name, # type: ignore
|
|
},
|
|
groups={"organization": str(user.current_organization.id), "instance": SITE_URL,}, # type: ignore
|
|
)
|
|
return
|
|
|
|
posthoganalytics.capture(
|
|
user.distinct_id, # type: ignore
|
|
"send license usage data",
|
|
{
|
|
"date": date_from.strftime("%Y-%m-%d"),
|
|
"events_count": events_count,
|
|
"license_keys": get_instance_licenses(),
|
|
"organization_name": user.current_organization.name, # type: ignore
|
|
},
|
|
groups={"organization": str(user.current_organization.id), "instance": SITE_URL,}, # type: ignore
|
|
)
|
|
except Exception as err:
|
|
posthoganalytics.capture(
|
|
user.distinct_id, # type: ignore
|
|
"send license usage data error",
|
|
{
|
|
"error": str(err),
|
|
"date": date_from.strftime("%Y-%m-%d"),
|
|
"organization_name": user.current_organization.name, # type: ignore
|
|
},
|
|
groups={"organization": str(user.current_organization.id), "instance": SITE_URL,}, # type: ignore
|
|
)
|