File size: 3,293 Bytes
1b4b6d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""Usage accounting and entitlement enforcement."""
from __future__ import annotations

import json
from typing import Any

from utils.db import safe_execute, safe_query
from utils.plans import get_plan_for_user, ttl_cache
from utils.db import is_sqlite


EVENT_PROJECT = "project_created"
EVENT_AI_CALL = "ai_call"
EVENT_EXPORT = "export"


def log_usage(
    user_id: int | None,
    event_type: str,
    quantity: int = 1,
    project_id: int | None = None,
    metadata: dict[str, Any] | None = None,
) -> None:
    safe_execute(
        """
        INSERT INTO qto_usage_logs (user_id, project_id, event_type, quantity, metadata)
        VALUES (%s, %s, %s, %s, %s)
        """,
        (
            user_id,
            project_id,
            event_type,
            quantity,
            json.dumps(metadata or {}, ensure_ascii=False, default=str),
        ),
    )


@ttl_cache(ttl_seconds=60)
def monthly_usage(user_id: int, event_type: str) -> int:
    if is_sqlite():
        df = safe_query(
            """
            SELECT COALESCE(SUM(quantity), 0) AS total
            FROM qto_usage_logs
            WHERE user_id=%s
              AND event_type=%s
              AND created_at >= date('now', 'start of month')
            """,
            (user_id, event_type),
        )
    else:
        df = safe_query(
            """
            SELECT COALESCE(SUM(quantity), 0) AS total
            FROM qto_usage_logs
            WHERE user_id=%s
              AND event_type=%s
              AND created_at >= DATE_FORMAT(CURRENT_DATE, '%%Y-%%m-01')
            """,
            (user_id, event_type),
        )
    if df.empty:
        return 0
    return int(df.iloc[0]["total"] or 0)


def check_limit(user: dict, event_type: str, amount: int = 1) -> tuple[bool, str]:
    user_id = int(user.get("id"))
    role = user.get("role")
    plan = get_plan_for_user(user_id, role)
    
    extra_projects = 0
    if event_type == EVENT_PROJECT:
        try:
            df_extra = safe_query("SELECT extra_projects_allowance FROM qto_users WHERE id=%s", (user_id,))
            if not df_extra.empty:
                extra_projects = int(df_extra.iloc[0]["extra_projects_allowance"] or 0)
        except Exception as e:
            print(f"Error fetching extra projects: {e}")

    limits = {
        EVENT_PROJECT: plan.projects + extra_projects,
        EVENT_AI_CALL: plan.ai_calls,
        EVENT_EXPORT: plan.exports,
    }
    limit = limits.get(event_type)
    if limit is None:
        return True, "OK"
    used = monthly_usage(user_id, event_type)
    if used + amount > limit:
        # Check if they are just blocked by base plan or also exhausted extras
        if event_type == EVENT_PROJECT and used >= (plan.projects + extra_projects):
            return False, f"Plan limit reached for {event_type}: {used}/{limit}. You have 0 Extra Projects remaining."
        return False, f"Plan limit reached for {event_type}: {used}/{limit}"
    return True, "OK"


def check_file_size(user: dict, size_bytes: int) -> tuple[bool, str]:
    plan = get_plan_for_user(int(user.get("id")), user.get("role"))
    max_bytes = plan.max_file_mb * 1024 * 1024
    if size_bytes > max_bytes:
        return False, f"File exceeds plan limit: {plan.max_file_mb} MB"
    return True, "OK"