97 Zeilen
3.6 KiB
Python
97 Zeilen
3.6 KiB
Python
"""Device limit monitoring utilities"""
|
|
import logging
|
|
from datetime import datetime
|
|
from db import get_connection
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
def check_device_limits():
|
|
"""Check all licenses for device limit violations"""
|
|
conn = get_connection()
|
|
cur = conn.cursor()
|
|
|
|
try:
|
|
# Find licenses approaching or exceeding device limits
|
|
cur.execute("""
|
|
SELECT
|
|
l.id,
|
|
l.license_key,
|
|
l.device_limit,
|
|
COUNT(DISTINCT dr.id) as device_count,
|
|
c.name as customer_name,
|
|
c.email as customer_email
|
|
FROM licenses l
|
|
LEFT JOIN device_registrations dr ON dr.license_id = l.id AND dr.is_active = true
|
|
LEFT JOIN customers c ON c.id = l.customer_id
|
|
WHERE l.is_active = true
|
|
GROUP BY l.id, l.license_key, l.device_limit, c.name, c.email
|
|
HAVING COUNT(DISTINCT dr.id) >= l.device_limit * 0.8 -- 80% threshold
|
|
ORDER BY (COUNT(DISTINCT dr.id)::float / l.device_limit) DESC
|
|
""")
|
|
|
|
warnings = []
|
|
for row in cur.fetchall():
|
|
license_id, license_key, device_limit, device_count, customer_name, customer_email = row
|
|
usage_percent = (device_count / device_limit) * 100 if device_limit > 0 else 0
|
|
|
|
warning = {
|
|
'license_id': license_id,
|
|
'license_key': license_key,
|
|
'customer_name': customer_name or 'Unknown',
|
|
'customer_email': customer_email or 'No email',
|
|
'device_limit': device_limit,
|
|
'device_count': device_count,
|
|
'usage_percent': round(usage_percent, 1),
|
|
'status': 'exceeded' if device_count > device_limit else 'warning'
|
|
}
|
|
warnings.append(warning)
|
|
|
|
if device_count > device_limit:
|
|
logger.warning(f"License {license_key} exceeded device limit: {device_count}/{device_limit}")
|
|
|
|
return warnings
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error checking device limits: {e}")
|
|
return []
|
|
finally:
|
|
cur.close()
|
|
conn.close()
|
|
|
|
def get_device_usage_stats():
|
|
"""Get overall device usage statistics"""
|
|
conn = get_connection()
|
|
cur = conn.cursor()
|
|
|
|
try:
|
|
# Get overall stats
|
|
cur.execute("""
|
|
SELECT
|
|
COUNT(DISTINCT l.id) as total_licenses,
|
|
COUNT(DISTINCT dr.id) as total_devices,
|
|
SUM(l.device_limit) as total_device_limit,
|
|
COUNT(DISTINCT CASE WHEN dr.last_seen_at > NOW() - INTERVAL '24 hours' THEN dr.id END) as active_24h,
|
|
COUNT(DISTINCT CASE WHEN dr.last_seen_at > NOW() - INTERVAL '7 days' THEN dr.id END) as active_7d
|
|
FROM licenses l
|
|
LEFT JOIN device_registrations dr ON dr.license_id = l.id AND dr.is_active = true
|
|
WHERE l.is_active = true
|
|
""")
|
|
|
|
stats = cur.fetchone()
|
|
|
|
return {
|
|
'total_licenses': stats[0] or 0,
|
|
'total_devices': stats[1] or 0,
|
|
'total_device_limit': stats[2] or 0,
|
|
'usage_percent': round((stats[1] / stats[2] * 100) if stats[2] > 0 else 0, 1),
|
|
'active_24h': stats[3] or 0,
|
|
'active_7d': stats[4] or 0,
|
|
'timestamp': datetime.now().isoformat()
|
|
}
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error getting device usage stats: {e}")
|
|
return {}
|
|
finally:
|
|
cur.close()
|
|
conn.close() |