~homeworkprod/byceps

ref: 4237b3ec9496efe95dcce82bea3207ab9de4d520 byceps/byceps/services/user/stats_service.py -rw-r--r-- 1.6 KiB
4237b3ec — Jochen Kupperschmidt Move ticketing blueprint into `site` subpackage 1 year, 11 months ago
                                                                                
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
"""
byceps.services.user.stats_service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2020 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from datetime import datetime, timedelta

from .models.user import User as DbUser


def count_users() -> int:
    """Return the number of users."""
    return DbUser.query \
        .count()


def count_users_created_since(delta: timedelta) -> int:
    """Return the number of user accounts created since `delta` ago."""
    filter_starts_at = datetime.utcnow() - delta

    return DbUser.query \
        .filter(DbUser.created_at >= filter_starts_at) \
        .count()


def count_active_users() -> int:
    """Return the number of active user accounts.

    Uninitialized, suspended or deleted accounts are excluded.
    """
    return DbUser.query \
        .filter_by(initialized=True) \
        .filter_by(suspended=False) \
        .filter_by(deleted=False) \
        .count()


def count_uninitialized_users() -> int:
    """Return the number of uninitialized user accounts.

    Suspended or deleted accounts are excluded.
    """
    return DbUser.query \
        .filter_by(initialized=False) \
        .filter_by(suspended=False) \
        .filter_by(deleted=False) \
        .count()


def count_suspended_users() -> int:
    """Return the number of suspended user accounts."""
    return DbUser.query \
        .filter_by(suspended=True) \
        .filter_by(deleted=False) \
        .count()


def count_deleted_users() -> int:
    """Return the number of deleted user accounts."""
    return DbUser.query \
        .filter_by(deleted=True) \
        .count()