2021-09-22 14:17:38 +02:00
|
|
|
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
|
2021-11-30 09:58:37 +01:00
|
|
|
from posthog.settings import SITE_URL
|
2021-10-14 18:42:14 +02:00
|
|
|
from posthog.tasks.status_report import get_instance_licenses
|
2021-09-22 14:17:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
def send_license_usage():
|
|
|
|
license = License.objects.first_valid()
|
2021-10-14 18:42:14 +02:00
|
|
|
user = User.objects.first()
|
2021-09-22 14:17:38 +02:00
|
|
|
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(
|
2021-10-05 14:05:51 +02:00
|
|
|
"select count(1) from events where timestamp >= %(date_from)s and timestamp < %(date_to)s and not startsWith(event, '$$')",
|
2021-09-22 14:17:38 +02:00
|
|
|
{"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(
|
2021-10-14 18:42:14 +02:00
|
|
|
user.distinct_id, # type: ignore
|
2021-09-22 14:17:38 +02:00
|
|
|
"send license usage data error",
|
|
|
|
{
|
|
|
|
"error": response.content,
|
|
|
|
"status_code": response.status_code,
|
|
|
|
"date": date_from.strftime("%Y-%m-%d"),
|
|
|
|
"events_count": events_count,
|
2021-10-14 18:42:14 +02:00
|
|
|
"organization_name": user.current_organization.name, # type: ignore
|
2021-09-22 14:17:38 +02:00
|
|
|
},
|
2021-11-30 09:58:37 +01:00
|
|
|
groups={"organization": str(user.current_organization.id), "instance": SITE_URL,}, # type: ignore
|
2021-09-22 14:17:38 +02:00
|
|
|
)
|
2021-10-14 18:42:14 +02:00
|
|
|
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
|
|
|
|
},
|
2021-11-30 09:58:37 +01:00
|
|
|
groups={"organization": str(user.current_organization.id), "instance": SITE_URL,}, # type: ignore
|
2021-10-14 18:42:14 +02:00
|
|
|
)
|
2021-09-22 14:17:38 +02:00
|
|
|
except Exception as err:
|
|
|
|
posthoganalytics.capture(
|
2021-10-14 18:42:14 +02:00
|
|
|
user.distinct_id, # type: ignore
|
2021-09-22 14:17:38 +02:00
|
|
|
"send license usage data error",
|
2021-10-14 18:42:14 +02:00
|
|
|
{
|
|
|
|
"error": str(err),
|
|
|
|
"date": date_from.strftime("%Y-%m-%d"),
|
|
|
|
"organization_name": user.current_organization.name, # type: ignore
|
|
|
|
},
|
2021-11-30 09:58:37 +01:00
|
|
|
groups={"organization": str(user.current_organization.id), "instance": SITE_URL,}, # type: ignore
|
2021-09-22 14:17:38 +02:00
|
|
|
)
|