* Add cloud connection statuses on user page * Add fixes for oncall hobby docker-compose installation * Fix for links and for cloud user status at User settings * Delete cloud api token on cloud disconnect * Merge branch 'dev' into cloud_connection_statuses_on_user_page * Fix cloud statuses for users * Fix usagestats service * Fix phone verification message in users table * added request after syncing user * Add endpoint to create CloudHeartbeat and polish code * Fix imports * Check token and heartbeat setting in setup_hertbeat_integration * Add macthed_users_count in cloud users * Sync users on token change * Fix query param * Fix tests * Heartbit button logic, tab width fix, coount users fix * Solve problem of existent cloud heartbeat integration * Solve problem of existent cloud heartbeat integration 2 * Solve problem of existent cloud heartbeat integration 3 * fix build * build fix, styles for env variables description Co-authored-by: Ildar Iskhakov <ildar.iskhakov@grafana.com> Co-authored-by: Yulia Shanyrova <yulia.shanyrova@grafana.com>
53 lines
1.6 KiB
Python
53 lines
1.6 KiB
Python
import logging
|
|
import platform
|
|
from dataclasses import asdict, dataclass
|
|
|
|
import requests
|
|
from django.apps import apps
|
|
from django.conf import settings
|
|
from django.db.models import Sum
|
|
|
|
from apps.alerts.models import AlertGroupCounter
|
|
from apps.oss_installation.utils import active_oss_users_count
|
|
|
|
USAGE_STATS_URL = "https://stats.grafana.org/oncall-usage-report"
|
|
USAGE_STATS_HTTP_TIMEOUT = 500
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@dataclass
|
|
class UsageStatsReport:
|
|
version: str
|
|
os: str
|
|
arch: str
|
|
usage_stats_id: str
|
|
metrics: dict
|
|
|
|
|
|
class UsageStatsService:
|
|
def get_usage_stats_report(self):
|
|
OssInstallation = apps.get_model("oss_installation", "OssInstallation")
|
|
metrics = {}
|
|
metrics["active_users_count"] = active_oss_users_count()
|
|
total_alert_groups = AlertGroupCounter.objects.aggregate(Sum("value")).get("value__sum", None)
|
|
if total_alert_groups is None:
|
|
total_alert_groups = 0
|
|
metrics["alert_groups_count"] = total_alert_groups
|
|
|
|
usage_stats_id = OssInstallation.objects.get_or_create()[0].installation_id
|
|
|
|
return UsageStatsReport(
|
|
usage_stats_id=str(usage_stats_id),
|
|
os=platform.system(),
|
|
arch=platform.machine(),
|
|
version=settings.VERSION,
|
|
metrics=metrics,
|
|
)
|
|
|
|
def send_usage_stats_report(self):
|
|
report = self.get_usage_stats_report()
|
|
try:
|
|
requests.post(url=USAGE_STATS_URL, json=asdict(report), timeout=USAGE_STATS_HTTP_TIMEOUT)
|
|
except requests.exceptions.RequestException as e:
|
|
logging.info(f"Failed to send_usage_stats_report. msg={str(e)}")
|