Example #1
0
    def initialize(self):
        super(IncomeTotal, self).initialize()
        query = InvoicePrototype._db_filter(
            ACCEPTED_INVOICE_FILTER,
            self.db_date_gte('created_at'),
            sender_type=ENTITY_TYPE.XSOLLA,
            currency=CURRENCY_TYPE.PREMIUM).values_list(
                'created_at', 'amount')
        invoices = [(created_at.date(), amount)
                    for created_at, amount in query]

        invoices_values = {}
        for created_at, amount in invoices:
            invoices_values[created_at] = invoices_values.get(created_at,
                                                              0) + amount

        income = InvoicePrototype._db_filter(
            ACCEPTED_INVOICE_FILTER,
            self.db_date_lt('created_at'),
            sender_type=ENTITY_TYPE.XSOLLA,
            currency=CURRENCY_TYPE.PREMIUM).aggregate(
                income=models.Sum('amount'))['income']
        if income is None:
            income = 0

        self.incomes = {}

        for date in days_range(*self._get_interval()):
            income += invoices_values.get(date, 0)
            self.incomes[date] = income
Example #2
0
    def handle(self, *args, **options):

        actions = set(AccountPrototype._db_exclude(action_id=None).values_list('action_id', flat=True).order_by('action_id').distinct())

        for action_id in actions:
            print()
            print('----%s----' % action_id)
            registered_ids = set(AccountPrototype._db_filter(action_id=action_id, is_fast=False).values_list('id', flat=True))

            print('registrations: %d' % len(registered_ids))

            payers_ids = set(InvoicePrototype._db_filter(models.Q(state=INVOICE_STATE.CONFIRMED)|models.Q(state=INVOICE_STATE.FORCED),
                                                        sender_type=ENTITY_TYPE.XSOLLA,
                                                        currency=CURRENCY_TYPE.PREMIUM).values_list('recipient_id', flat=True))

            payers_ids &= registered_ids

            print('payers: %d' % len(payers_ids))

            amounts = InvoicePrototype._db_filter(models.Q(state=INVOICE_STATE.CONFIRMED)|models.Q(state=INVOICE_STATE.FORCED),
                                                  sender_type=ENTITY_TYPE.XSOLLA,
                                                  recipient_id__in=payers_ids,
                                                  currency=CURRENCY_TYPE.PREMIUM).values_list('amount', flat=True)

            amount = sum(amounts)

            print('total gold: %d' % amount)

            if registered_ids:
                print('per account: %.2f' % (float(amount) / len(registered_ids)))

            if payers_ids:
                print('per payer: %.2f' % (float(amount) / len(payers_ids)))
    def handle(self, *args, **options):

        actions = set(AccountPrototype._db_exclude(action_id=None).values_list('action_id', flat=True).order_by('action_id').distinct())

        for action_id in actions:
            print
            print '----%s----' % action_id
            registered_ids = set(AccountPrototype._db_filter(action_id=action_id, is_fast=False).values_list('id', flat=True))

            print 'registrations: %d' % len(registered_ids)

            payers_ids = set(InvoicePrototype._db_filter(models.Q(state=INVOICE_STATE.CONFIRMED)|models.Q(state=INVOICE_STATE.FORCED),
                                                        sender_type=ENTITY_TYPE.XSOLLA,
                                                        currency=CURRENCY_TYPE.PREMIUM).values_list('recipient_id', flat=True))

            payers_ids &= registered_ids

            print 'payers: %d' % len(payers_ids)

            amounts = InvoicePrototype._db_filter(models.Q(state=INVOICE_STATE.CONFIRMED)|models.Q(state=INVOICE_STATE.FORCED),
                                                  sender_type=ENTITY_TYPE.XSOLLA,
                                                  recipient_id__in=payers_ids,
                                                  currency=CURRENCY_TYPE.PREMIUM).values_list('amount', flat=True)

            amount = sum(amounts)

            print 'total gold: %d' % amount

            if registered_ids:
                print 'per account: %.2f' % (float(amount) / len(registered_ids))

            if payers_ids:
                print 'per payer: %.2f' % (float(amount) / len(payers_ids))
Example #4
0
 def get_value(self, date):
     return InvoicePrototype._db_filter(
         ACCEPTED_INVOICE_FILTER,
         self.db_date_lte('created_at', date=date),
         sender_type=ENTITY_TYPE.XSOLLA,
         currency=CURRENCY_TYPE.PREMIUM).values_list(
             'recipient_id').order_by('recipient_id').distinct().count()
Example #5
0
    def get_value(self, date):
        query = AccountPrototype._db_filter(is_fast=False, is_bot=False)

        # do not use accounts registered before payments turn on
        query = query.filter(self.db_date_interval('created_at', date=date, days=-self.PERIOD),
                             self.db_date_gte('created_at', date=statistics_settings.PAYMENTS_START_DATE.date()))
        accounts = dict(query.values_list('id', 'created_at'))

        invoices = list(InvoicePrototype._db_filter(ACCEPTED_INVOICE_FILTER,
                                                    sender_type=ENTITY_TYPE.XSOLLA,
                                                    currency=CURRENCY_TYPE.PREMIUM,
                                                    recipient_id__in=list(accounts.keys())).values_list('created_at', 'recipient_id'))

        account_ids = [id_ for created_at, id_ in invoices]

        if not account_ids:
            return 0

        delays = {account_id: min([(created_at - accounts[account_id])
                                   for created_at, id_ in invoices
                                   if id_==account_id])
                  for account_id in account_ids}

        total_time = reduce(lambda s, v: s+v, list(delays.values()), datetime.timedelta(seconds=0))

        days = float(total_time.total_seconds()) / len(account_ids) / (24*60*60)

        return days
Example #6
0
    def get_value(self, date):
        query = AccountPrototype._db_filter(self.db_date_interval(
            'created_at', date=date, days=-self.PERIOD),
                                            is_fast=False,
                                            is_bot=False)
        accounts = list(query.values_list('id', 'created_at'))

        if not accounts:
            return 0

        total_income = 0
        for account_id, created_at in accounts:
            income = InvoicePrototype._db_filter(
                ACCEPTED_INVOICE_FILTER,
                self.db_date_interval('created_at',
                                      date=created_at,
                                      days=self.DAYS),
                sender_type=ENTITY_TYPE.XSOLLA,
                currency=CURRENCY_TYPE.PREMIUM,
                recipient_id=account_id).aggregate(
                    income=models.Sum('amount'))['income']
            if income is not None:
                total_income += income

        return float(total_income) / len(accounts)
Example #7
0
 def get_invoice_intervals_count(self, days, date):
     starts = list(InvoicePrototype._db_filter(models.Q(state=INVOICE_STATE.CONFIRMED)|models.Q(state=INVOICE_STATE.FORCED),
                                               operation_uid__contains='<%s%d' % (GOODS_GROUP.PREMIUM.uid_prefix, days),
                                               sender_type=ENTITY_TYPE.GAME_LOGIC,
                                               currency=CURRENCY_TYPE.PREMIUM).values_list('created_at', flat=True))
     return len([True
                 for created_at in starts
                 if created_at <= datetime.datetime.combine(date, datetime.time()) < created_at + datetime.timedelta(days=days)] )
Example #8
0
 def get_invoice_infinit_intervals_count(self, date):
     return InvoicePrototype._db_filter(
         models.Q(state=INVOICE_STATE.CONFIRMED)
         | models.Q(state=INVOICE_STATE.FORCED),
         self.db_date_lte('created_at', date),
         operation_uid__contains='<%sinfinit' %
         GOODS_GROUP.PREMIUM.uid_prefix,
         sender_type=ENTITY_TYPE.GAME_LOGIC,
         currency=CURRENCY_TYPE.PREMIUM).count()
Example #9
0
    def get_value(self, date):
        income = InvoicePrototype._db_filter(ACCEPTED_INVOICE_FILTER,
                                             self.db_date_interval('created_at', date=date, days=-self.PERIOD),
                                             sender_type=ENTITY_TYPE.XSOLLA,
                                             currency=CURRENCY_TYPE.PREMIUM).aggregate(income=models.Sum('amount'))['income']
        if income is None:
            return 0

        return income
Example #10
0
    def initialize(self):
        super(Income, self).initialize()
        query = InvoicePrototype._db_filter(ACCEPTED_INVOICE_FILTER,
                                            self.db_date_gte('created_at', date=self.free_date - self.PREFETCH_DELTA),
                                            sender_type=ENTITY_TYPE.XSOLLA, currency=CURRENCY_TYPE.PREMIUM).values_list('created_at', 'amount')
        invoices = [(created_at.date(), amount) for created_at, amount in query]

        self.invoices_values = {}
        for created_at, amount in invoices:
            self.invoices_values[created_at] = self.invoices_values.get(created_at, 0) + amount
Example #11
0
    def get_value(self, date):
        income = InvoicePrototype._db_filter(
            ACCEPTED_INVOICE_FILTER,
            self.db_date_interval('created_at', date=date, days=-self.PERIOD),
            sender_type=ENTITY_TYPE.XSOLLA,
            currency=CURRENCY_TYPE.PREMIUM).aggregate(
                income=models.Sum('amount'))['income']
        if income is None:
            return 0

        return income
Example #12
0
    def get_value(self, date):
        invoices = list(InvoicePrototype._db_filter(ACCEPTED_INVOICE_FILTER,
                                                    self.db_date_interval('created_at', date=date, days=-self.PERIOD),
                                                    sender_type=ENTITY_TYPE.XSOLLA,
                                                    currency=CURRENCY_TYPE.PREMIUM).values_list('recipient_id', 'amount'))

        if not invoices:
            return 0

        recipients = set(self.filter_recipients(next(zip(*invoices))))

        return sum([amount for recipient_id, amount in invoices if recipient_id in recipients], 0)
Example #13
0
    def get_value(self, date):
        incomes = InvoicePrototype._db_filter(ACCEPTED_INVOICE_FILTER,
                                              self.db_date_lte('created_at', date=date),
                                              sender_type=ENTITY_TYPE.XSOLLA,
                                              currency=CURRENCY_TYPE.PREMIUM).values_list('recipient_id', 'amount')

        accounts_incomes = {}
        for recipient_id, amount in incomes:
            accounts_incomes[recipient_id] = accounts_incomes.get(recipient_id, 0) + amount

        return sum([amount
                    for amount in accounts_incomes.values()
                    if self.BORDERS[0] < amount <= self.BORDERS[1]], 0)
Example #14
0
 def get_actual_value(self, date):
     premiums_ids = set(
         AccountPrototype._db_filter(
             self.db_date_gte('premium_end_at',
                              date=date)).values_list('id', flat=True))
     infinit_ids = set(
         InvoicePrototype._db_filter(
             models.Q(state=INVOICE_STATE.CONFIRMED)
             | models.Q(state=INVOICE_STATE.FORCED),
             operation_uid__contains='infinit',
             sender_type=ENTITY_TYPE.GAME_LOGIC,
             currency=CURRENCY_TYPE.PREMIUM).values_list('recipient_id',
                                                         flat=True))
     return len(premiums_ids | infinit_ids)
Example #15
0
 def get_invoice_intervals_count(self, days, date):
     starts = list(
         InvoicePrototype._db_filter(
             models.Q(state=INVOICE_STATE.CONFIRMED)
             | models.Q(state=INVOICE_STATE.FORCED),
             operation_uid__contains='<%s%d' %
             (GOODS_GROUP.PREMIUM.uid_prefix, days),
             sender_type=ENTITY_TYPE.GAME_LOGIC,
             currency=CURRENCY_TYPE.PREMIUM).values_list('created_at',
                                                         flat=True))
     return len([
         True for created_at in starts
         if created_at <= datetime.datetime.combine(date, datetime.time()) <
         created_at + datetime.timedelta(days=days)
     ])
Example #16
0
    def initialize(self):
        super(IncomeTotal, self).initialize()
        query = InvoicePrototype._db_filter(ACCEPTED_INVOICE_FILTER,
                                            self.db_date_gte('created_at'),
                                            sender_type=ENTITY_TYPE.XSOLLA,
                                            currency=CURRENCY_TYPE.PREMIUM).values_list('created_at', 'amount')
        invoices = [(created_at.date(), amount) for created_at, amount in query]

        invoices_values = {}
        for created_at, amount in invoices:
            invoices_values[created_at] = invoices_values.get(created_at, 0) + amount

        income = InvoicePrototype._db_filter(ACCEPTED_INVOICE_FILTER,
                                             self.db_date_lt('created_at'),
                                             sender_type=ENTITY_TYPE.XSOLLA,
                                             currency=CURRENCY_TYPE.PREMIUM).aggregate(income=models.Sum('amount'))['income']
        if income is None:
            income = 0

        self.incomes = {}

        for date in days_range(*self._get_interval()):
            income += invoices_values.get(date, 0)
            self.incomes[date] = income
Example #17
0
    def initialize(self):
        super(Payers, self).initialize()
        invoices = list(InvoicePrototype._db_filter(ACCEPTED_INVOICE_FILTER,
                                                    self.db_date_gte('created_at', date=self.free_date - self.PREFETCH_DELTA),
                                                    sender_type=ENTITY_TYPE.XSOLLA,
                                                    currency=CURRENCY_TYPE.PREMIUM).values_list('created_at', 'recipient_id'))

        self.invoices = {}

        for invoice_data, recipient_id in invoices:
            created_at = invoice_data.date()

            if created_at not in self.invoices:
                self.invoices[created_at] = set()

            self.invoices[created_at].add(recipient_id)
Example #18
0
    def initialize(self):
        super(Income, self).initialize()
        query = InvoicePrototype._db_filter(
            ACCEPTED_INVOICE_FILTER,
            self.db_date_gte('created_at',
                             date=self.free_date - self.PREFETCH_DELTA),
            sender_type=ENTITY_TYPE.XSOLLA,
            currency=CURRENCY_TYPE.PREMIUM).values_list(
                'created_at', 'amount')
        invoices = [(created_at.date(), amount)
                    for created_at, amount in query]

        self.invoices_values = {}
        for created_at, amount in invoices:
            self.invoices_values[created_at] = self.invoices_values.get(
                created_at, 0) + amount
Example #19
0
    def get_value(self, date):
        incomes = InvoicePrototype._db_filter(
            ACCEPTED_INVOICE_FILTER,
            self.db_date_lte('created_at', date=date),
            sender_type=ENTITY_TYPE.XSOLLA,
            currency=CURRENCY_TYPE.PREMIUM).values_list(
                'recipient_id', 'amount')

        accounts_incomes = {}
        for recipient_id, amount in incomes:
            accounts_incomes[recipient_id] = accounts_incomes.get(
                recipient_id, 0) + amount

        return sum([
            amount for amount in accounts_incomes.values()
            if self.BORDERS[0] < amount <= self.BORDERS[1]
        ], 0)
Example #20
0
    def get_value(self, date):
        query = AccountPrototype._db_filter(self.db_date_interval('created_at', date=date, days=-self.PERIOD),
                                            is_fast=False,
                                            is_bot=False)
        accounts_ids = list(query.values_list('id', flat=True))

        if not accounts_ids:
            return 0

        total_income = InvoicePrototype._db_filter(ACCEPTED_INVOICE_FILTER,
                                                   sender_type=ENTITY_TYPE.XSOLLA,
                                                   currency=CURRENCY_TYPE.PREMIUM,
                                                   recipient_id__in=accounts_ids).aggregate(income=models.Sum('amount'))['income']

        if total_income is None:
            return 0

        return float(total_income) / len(accounts_ids)
Example #21
0
    def get_value(self, date):
        invoices = list(
            InvoicePrototype._db_filter(
                ACCEPTED_INVOICE_FILTER,
                self.db_date_interval('created_at',
                                      date=date,
                                      days=-self.PERIOD),
                sender_type=ENTITY_TYPE.XSOLLA,
                currency=CURRENCY_TYPE.PREMIUM).values_list(
                    'recipient_id', 'amount'))

        if not invoices:
            return 0

        recipients = set(self.filter_recipients(next(zip(*invoices))))

        return sum([
            amount
            for recipient_id, amount in invoices if recipient_id in recipients
        ], 0)
Example #22
0
    def initialize(self):
        super(Payers, self).initialize()
        invoices = list(
            InvoicePrototype._db_filter(
                ACCEPTED_INVOICE_FILTER,
                self.db_date_gte('created_at',
                                 date=self.free_date - self.PREFETCH_DELTA),
                sender_type=ENTITY_TYPE.XSOLLA,
                currency=CURRENCY_TYPE.PREMIUM).values_list(
                    'created_at', 'recipient_id'))

        self.invoices = {}

        for invoice_data, recipient_id in invoices:
            created_at = invoice_data.date()

            if created_at not in self.invoices:
                self.invoices[created_at] = set()

            self.invoices[created_at].add(recipient_id)
Example #23
0
    def get_value(self, date):
        query = AccountPrototype._db_filter(is_fast=False, is_bot=False)

        # do not use accounts registered before payments turn on
        query = query.filter(
            self.db_date_interval('created_at', date=date, days=-self.PERIOD),
            self.db_date_gte(
                'created_at',
                date=statistics_settings.PAYMENTS_START_DATE.date()))
        accounts = dict(query.values_list('id', 'created_at'))

        invoices = list(
            InvoicePrototype._db_filter(ACCEPTED_INVOICE_FILTER,
                                        sender_type=ENTITY_TYPE.XSOLLA,
                                        currency=CURRENCY_TYPE.PREMIUM,
                                        recipient_id__in=list(
                                            accounts.keys())).values_list(
                                                'created_at', 'recipient_id'))

        account_ids = [id_ for created_at, id_ in invoices]

        if not account_ids:
            return 0

        delays = {
            account_id: min([(created_at - accounts[account_id])
                             for created_at, id_ in invoices
                             if id_ == account_id])
            for account_id in account_ids
        }

        total_time = reduce(lambda s, v: s + v, list(delays.values()),
                            datetime.timedelta(seconds=0))

        days = float(
            total_time.total_seconds()) / len(account_ids) / (24 * 60 * 60)

        return days
Example #24
0
from dext.views import handler

from the_tale.common.utils.decorators import staff_required
from the_tale.common.utils.resources import Resource

from the_tale.finances.bank.prototypes import AccountPrototype as BankAccountPrototype, InvoicePrototype
from the_tale.finances.bank.relations import ENTITY_TYPE as BANK_ENTITY_TYPE, INVOICE_STATE

from the_tale.accounts.prototypes import AccountPrototype

from the_tale.portal.developers_info.relations import PAYMENT_GROUPS
from the_tale.portal.conf import portal_settings

InvoiceQuery = InvoicePrototype._db_filter(
    models.Q(state=INVOICE_STATE.CONFIRMED)
    | models.Q(state=INVOICE_STATE.FORCED))


class RefererStatistics(
        collections.namedtuple(
            'RefererStatisticsBase',
            ('domain', 'count', 'active_accounts', 'premium_accounts',
             'active_and_premium', 'premium_currency'))):
    def __add__(self, s):
        return RefererStatistics(
            domain=self.domain,
            count=self.count + s.count,
            active_accounts=self.active_accounts + s.active_accounts,
            premium_accounts=self.premium_accounts + s.premium_accounts,
            active_and_premium=self.active_and_premium + s.active_and_premium,
Example #25
0
 def test_check_frozen_expired_invoices__exist(self):
     invoice = self.create_invoice(state=INVOICE_STATE.FROZEN)
     InvoicePrototype._db_filter(
         id=invoice.id).update(updated_at=datetime.datetime.now() -
                               bank_settings.FROZEN_INVOICE_EXPIRED_TIME)
     self.assertTrue(InvoicePrototype.check_frozen_expired_invoices())
Example #26
0
 def get_invoice_infinit_intervals_count(self, date):
     return InvoicePrototype._db_filter(models.Q(state=INVOICE_STATE.CONFIRMED)|models.Q(state=INVOICE_STATE.FORCED),
                                        self.db_date_lte('created_at', date),
                                        operation_uid__contains='<%sinfinit' % GOODS_GROUP.PREMIUM.uid_prefix,
                                        sender_type=ENTITY_TYPE.GAME_LOGIC,
                                        currency=CURRENCY_TYPE.PREMIUM).values_list('created_at', flat=True).count()
Example #27
0
 def get_value(self, date):
     return InvoicePrototype._db_filter(ACCEPTED_INVOICE_FILTER,
                                        self.db_date_lte('created_at', date=date),
                                        sender_type=ENTITY_TYPE.XSOLLA,
                                        currency=CURRENCY_TYPE.PREMIUM).values_list('recipient_id').order_by('recipient_id').distinct().count()
Example #28
0
    def print_funnel(self, year, month, new_users):
        DATE_FROM = datetime.datetime(year, month, 1)

        if month == 12:
            year += 1
            month = 0

        DATE_TO = datetime.datetime(year, month+1, 1)

        fast_registrations = sum(RecordPrototype.select_values(relations.RECORD_TYPE.REGISTRATIONS_TRIES, date_from=DATE_FROM, date_to=DATE_TO))

        registrations = sum(RecordPrototype.select_values(relations.RECORD_TYPE.REGISTRATIONS_COMPLETED, date_from=DATE_FROM, date_to=DATE_TO))

        new_accounts_ids = set(AccountPrototype._db_filter(created_at__gte=DATE_FROM, created_at__lte=DATE_TO).values_list('id', flat=True))
        all_payers_ids = set(InvoicePrototype._db_filter(models.Q(state=INVOICE_STATE.CONFIRMED)|models.Q(state=INVOICE_STATE.FORCED),
                                                         sender_type=ENTITY_TYPE.XSOLLA,
                                                         currency=CURRENCY_TYPE.PREMIUM).values_list('recipient_id', flat=True))

        payers = len(new_accounts_ids & all_payers_ids)

        alive_after_week_ids = set(AccountPrototype._db_filter(created_at__gte=DATE_FROM,
                                                                created_at__lte=DATE_TO,
                                                                active_end_at__gte=models.F('created_at')+datetime.timedelta(days=7)).values_list('id', flat=True))

        alive_after_week = len(alive_after_week_ids & new_accounts_ids)

        alive_after_1_month_ids = set(AccountPrototype._db_filter(created_at__gte=DATE_FROM,
                                                                  created_at__lte=DATE_TO,
                                                                  active_end_at__gte=models.F('created_at')+datetime.timedelta(days=30)).values_list('id', flat=True))
        alive_after_2_month_ids = set(AccountPrototype._db_filter(created_at__gte=DATE_FROM,
                                                                  created_at__lte=DATE_TO,
                                                                  active_end_at__gte=models.F('created_at')+datetime.timedelta(days=60)).values_list('id', flat=True))
        alive_after_3_month_ids = set(AccountPrototype._db_filter(created_at__gte=DATE_FROM,
                                                                  created_at__lte=DATE_TO,
                                                                  active_end_at__gte=models.F('created_at')+datetime.timedelta(days=90)).values_list('id', flat=True))
        alive_after_4_month_ids = set(AccountPrototype._db_filter(created_at__gte=DATE_FROM,
                                                                  created_at__lte=DATE_TO,
                                                                  active_end_at__gte=models.F('created_at')+datetime.timedelta(days=120)).values_list('id', flat=True))
        alive_after_5_month_ids = set(AccountPrototype._db_filter(created_at__gte=DATE_FROM,
                                                                  created_at__lte=DATE_TO,
                                                                  active_end_at__gte=models.F('created_at')+datetime.timedelta(days=150)).values_list('id', flat=True))
        alive_after_6_month_ids = set(AccountPrototype._db_filter(created_at__gte=DATE_FROM,
                                                                  created_at__lte=DATE_TO,
                                                                  active_end_at__gte=models.F('created_at')+datetime.timedelta(days=180)).values_list('id', flat=True))

        alive_after_1_month = len(alive_after_1_month_ids & new_accounts_ids)
        alive_after_2_month = len(alive_after_2_month_ids & new_accounts_ids)
        alive_after_3_month = len(alive_after_3_month_ids & new_accounts_ids)
        alive_after_4_month = len(alive_after_4_month_ids & new_accounts_ids)
        alive_after_5_month = len(alive_after_5_month_ids & new_accounts_ids)
        alive_after_6_month = len(alive_after_6_month_ids & new_accounts_ids)


        print('--------------------------------------')
        print('from %s to %s' % (DATE_FROM.date(), DATE_TO.date()))
        print('visitors: %d' % new_users)
        print('registration tries %d (%.3f from visitors)' % (fast_registrations, float(fast_registrations)/new_users))
        print('registration completed %d (%.3f from visitors)' % (registrations, float(registrations)/new_users))
        print('payers %d (%.3f from registrations)' % (payers, float(payers)/registrations))
        print('alive after week %d (%.3f from registrations)' % (alive_after_week, float(alive_after_week)/registrations))
        print('alive after 1_month %d (%.3f from registrations)' % (alive_after_1_month, float(alive_after_1_month)/registrations))
        print('alive after 2 month %d (%.3f from registrations)' % (alive_after_2_month, float(alive_after_2_month)/registrations))
        print('alive after 3 month %d (%.3f from registrations)' % (alive_after_3_month, float(alive_after_3_month)/registrations))
        print('alive after 4 month %d (%.4f from registrations)' % (alive_after_4_month, float(alive_after_4_month)/registrations))
        print('alive after 5 month %d (%.5f from registrations)' % (alive_after_5_month, float(alive_after_5_month)/registrations))
        print('alive after 6 month %d (%.6f from registrations)' % (alive_after_6_month, float(alive_after_6_month)/registrations))
Example #29
0
    def print_funnel(self, year, month, new_users):
        DATE_FROM = datetime.datetime(year, month, 1)

        if month == 12:
            year += 1
            month = 0

        DATE_TO = datetime.datetime(year, month + 1, 1)

        fast_registrations = sum(
            RecordPrototype.select_values(
                relations.RECORD_TYPE.REGISTRATIONS_TRIES,
                date_from=DATE_FROM,
                date_to=DATE_TO))

        registrations = sum(
            RecordPrototype.select_values(
                relations.RECORD_TYPE.REGISTRATIONS_COMPLETED,
                date_from=DATE_FROM,
                date_to=DATE_TO))

        new_accounts_ids = set(
            AccountPrototype._db_filter(created_at__gte=DATE_FROM,
                                        created_at__lte=DATE_TO).values_list(
                                            'id', flat=True))
        all_payers_ids = set(
            InvoicePrototype._db_filter(
                models.Q(state=INVOICE_STATE.CONFIRMED)
                | models.Q(state=INVOICE_STATE.FORCED),
                sender_type=ENTITY_TYPE.XSOLLA,
                currency=CURRENCY_TYPE.PREMIUM).values_list('recipient_id',
                                                            flat=True))

        payers = len(new_accounts_ids & all_payers_ids)

        alive_after_week_ids = set(
            AccountPrototype._db_filter(
                created_at__gte=DATE_FROM,
                created_at__lte=DATE_TO,
                active_end_at__gte=models.F('created_at') +
                datetime.timedelta(days=7)).values_list('id', flat=True))

        alive_after_week = len(alive_after_week_ids & new_accounts_ids)

        alive_after_1_month_ids = set(
            AccountPrototype._db_filter(
                created_at__gte=DATE_FROM,
                created_at__lte=DATE_TO,
                active_end_at__gte=models.F('created_at') +
                datetime.timedelta(days=30)).values_list('id', flat=True))
        alive_after_2_month_ids = set(
            AccountPrototype._db_filter(
                created_at__gte=DATE_FROM,
                created_at__lte=DATE_TO,
                active_end_at__gte=models.F('created_at') +
                datetime.timedelta(days=60)).values_list('id', flat=True))
        alive_after_3_month_ids = set(
            AccountPrototype._db_filter(
                created_at__gte=DATE_FROM,
                created_at__lte=DATE_TO,
                active_end_at__gte=models.F('created_at') +
                datetime.timedelta(days=90)).values_list('id', flat=True))
        alive_after_4_month_ids = set(
            AccountPrototype._db_filter(
                created_at__gte=DATE_FROM,
                created_at__lte=DATE_TO,
                active_end_at__gte=models.F('created_at') +
                datetime.timedelta(days=120)).values_list('id', flat=True))
        alive_after_5_month_ids = set(
            AccountPrototype._db_filter(
                created_at__gte=DATE_FROM,
                created_at__lte=DATE_TO,
                active_end_at__gte=models.F('created_at') +
                datetime.timedelta(days=150)).values_list('id', flat=True))
        alive_after_6_month_ids = set(
            AccountPrototype._db_filter(
                created_at__gte=DATE_FROM,
                created_at__lte=DATE_TO,
                active_end_at__gte=models.F('created_at') +
                datetime.timedelta(days=180)).values_list('id', flat=True))

        alive_after_1_month = len(alive_after_1_month_ids & new_accounts_ids)
        alive_after_2_month = len(alive_after_2_month_ids & new_accounts_ids)
        alive_after_3_month = len(alive_after_3_month_ids & new_accounts_ids)
        alive_after_4_month = len(alive_after_4_month_ids & new_accounts_ids)
        alive_after_5_month = len(alive_after_5_month_ids & new_accounts_ids)
        alive_after_6_month = len(alive_after_6_month_ids & new_accounts_ids)

        print('--------------------------------------')
        print('from %s to %s' % (DATE_FROM.date(), DATE_TO.date()))
        print('visitors: %d' % new_users)
        print('registration tries %d (%.3f from visitors)' %
              (fast_registrations, float(fast_registrations) / new_users))
        print('registration completed %d (%.3f from visitors)' %
              (registrations, float(registrations) / new_users))
        print('payers %d (%.3f from registrations)' %
              (payers, float(payers) / registrations))
        print('alive after week %d (%.3f from registrations)' %
              (alive_after_week, float(alive_after_week) / registrations))
        print(
            'alive after 1_month %d (%.3f from registrations)' %
            (alive_after_1_month, float(alive_after_1_month) / registrations))
        print(
            'alive after 2 month %d (%.3f from registrations)' %
            (alive_after_2_month, float(alive_after_2_month) / registrations))
        print(
            'alive after 3 month %d (%.3f from registrations)' %
            (alive_after_3_month, float(alive_after_3_month) / registrations))
        print(
            'alive after 4 month %d (%.4f from registrations)' %
            (alive_after_4_month, float(alive_after_4_month) / registrations))
        print(
            'alive after 5 month %d (%.5f from registrations)' %
            (alive_after_5_month, float(alive_after_5_month) / registrations))
        print(
            'alive after 6 month %d (%.6f from registrations)' %
            (alive_after_6_month, float(alive_after_6_month) / registrations))
Example #30
0
 def test_check_frozen_expired_invoices__exist(self):
     invoice = self.create_invoice(state=INVOICE_STATE.FROZEN)
     InvoicePrototype._db_filter(id=invoice.id).update(
         updated_at=datetime.datetime.now() - bank_settings.FROZEN_INVOICE_EXPIRED_TIME
     )
     self.assertTrue(InvoicePrototype.check_frozen_expired_invoices())
Example #31
0
from dext.views import handler

from the_tale.common.utils.decorators import staff_required
from the_tale.common.utils.resources import Resource

from the_tale.finances.bank.prototypes import AccountPrototype as BankAccountPrototype, InvoicePrototype
from the_tale.finances.bank.relations import ENTITY_TYPE as BANK_ENTITY_TYPE, INVOICE_STATE

from the_tale.accounts.prototypes import AccountPrototype

from the_tale.portal.developers_info.relations import PAYMENT_GROUPS
from the_tale.portal.conf import portal_settings


InvoiceQuery = InvoicePrototype._db_filter(
    models.Q(state=INVOICE_STATE.CONFIRMED) | models.Q(state=INVOICE_STATE.FORCED)
)


class RefererStatistics(
    collections.namedtuple(
        "RefererStatisticsBase",
        ("domain", "count", "active_accounts", "premium_accounts", "active_and_premium", "premium_currency"),
    )
):
    def __add__(self, s):
        return RefererStatistics(
            domain=self.domain,
            count=self.count + s.count,
            active_accounts=self.active_accounts + s.active_accounts,
            premium_accounts=self.premium_accounts + s.premium_accounts,