def create_fact(cls, logs, **data):
        # dims
        platform = save_platform(data, event='Storefront Subscription')

        territory = DimensionTerritory.insert(code=data['territory']) if exists('territory', data) else None

        client = DimensionClient.insert(client_id=data['client_id']) if exists('client_id', data) else None

        du = save_user(data, territory, client, event='Storefront Subscription')

        dst = DST.insert(
            period=data['subscription_period'] if exists('subscription_period', data) else None,
            recurrence=data['subscription_recurrence'] if exists('subscription_recurrence', data) else None
        ) if any([d in data for d in ['subscription_period', 'subscription_recurrence']]) else None

        dss_status, _ = DSS.insert(event=int(data['subscription_status'])) if exists('subscription_status', data) else None

        state, err = get_rule(
            status=data.get('subscription_status', None),
            current_state=FactServicesStorefrontSubscription.get_last_event(
                subscription_id=data.get('subscription_id', None)
            ),
            window_ongoing=bool(data.get('window_on_going', False))
        )

        dsse, _ = DSSe.objects.get_or_create(state=state)

        item = DimensionItem.insert(item_id=data.get('item_id')) if exists('item_id', data) else None
        product = DimensionProduct.insert(product_id=data.get('product_id')) if exists('product_id', data) else None
        usage_right = DimensionRight.insert(
            right_id=data.get('window_usage_right_id')
        ) if exists('window_usage_right_id', data) else None

        window = DimensionWindow.insert(
            window_id=data.get('window_id', None),
            item=item,
            product=product,
            start_date=data['window_start_date'],
            end_date=data['window_end_date'],
            pricing_id=data['window_pricing_id'],
            usage_right_id=usage_right,
            tier=data['window_tier'],
            deleted=data['window_deleted_date'],
            window_type=data['window_type'],
            on_going=data['window_on_going'],
            repeat_count=data['window_repeat_count'],
            auto_upgrade=data['window_auto_upgrade'],
            allow_repurchase=data['window_allow_repurchase'],
            apple_product_id=data['apple_product_id'],
        )if any([i in data for i in [
            'window_id',
            'item_id',
            'product_id',
            'window_start_date',
            'window_end_date',
            'window_pricing_id',
            'window_usage_right_id',
            'window_tier',
            'window_deleted_date',
            'window_type',
            'window_on_going',
            'window_repeat_count',
            'window_auto_upgrade',
            'window_allow_repurchase',
            'apple_product_id',
        ]]) else None

        # fact create
        fact = FactServicesStorefrontSubscription.objects.create(
            user=du,
            subscription_id=data.get('subscription_id', None),
            subscription_type=dst,
            subscription_status=dss_status,
            subscription_state=dsse,
            subscription_state_error=err,
            platform=platform,
            window=window,
            transaction_id=data.get('transaction_id', None),
            event_utc_date=DimensionUTCDate.date_from_datetime(datetime=data['event_time']),
            event_utc_datetime=data.get('event_time', None)
        )

        try:
            FactServicesStorefrontSubscription.objects.get(pk=fact.pk)
            exist = True
        except FactServicesStorefrontSubscription.DoesNotExist:
            exist = False

        logs.completed = exist
        logs.save()
    def create_fact(cls, logs, **data):

        # insert dims
        item = DimensionItem.insert(
            item_id=data.get('item_id')) if exists('item_id', data) else None
        product = DimensionProduct.insert(
            product_id=data.get('product_id')) if exists('product_id',
                                                         data) else None
        right = DimensionRight.insert(right_id=data.get('right_id')) if exists(
            'right_id', data) else None
        currency = DimensionCurrency.insert(
            code=data.get('currency')) if exists('currency', data) else None
        retail_model = DimensionRetailModel.insert(
            model=data.get('retail_model')) if exists('retail_model',
                                                      data) else None
        definition = DimensionDefinition.insert(
            definition=data.get('definition')) if exists('definition',
                                                         data) else None

        window = DimensionWindow.insert(window_id=data.get('window_id', None),
                                        product=product,
                                        usage_right=right,
                                        item=item)
        transaction_status = DimensionStoreTransactionStatus.insert(
            status=data.get('transaction_status')) if exists(
                'transaction_status', data) else None

        platform = save_platform(data, event='Storefront Transaction')

        territory = DimensionTerritory.insert(
            code=data.get('territory')) if exists('territory', data) else None
        client = DimensionClient.insert(
            client_id=data.get('client_id')) if exists('client_id',
                                                       data) else None

        user = save_user(data,
                         territory,
                         client,
                         event='Storefront Transaction')

        account = DimensionAccount.insert(account_id=data.get(
            'account_id',
            data.get('external_user_id', data.get('internal_user_id', None))))

        try:
            device = DimensionDevice.insert(
                make=data.get('make', ''),
                model=data.get('model', ''),
                os=data.get('os', ''),
                os_version=data.get('version', ''),
            ) if any([d in data
                      for d in ['make', 'model', 'os', 'version']]) else None
        except:
            log.exception('')

        try:
            trans = cls.objects.get(
                transaction_id=data.get('transaction_id', ''))
            trans.last_4_digits = data.get('last_4_digits', None)
            trans.event_utc_datetime = data.get('event_time',
                                                timezone.now().isoformat())
            trans.event_utc_date = DimensionUTCDate.date_from_datetime(
                data.get('event_time',
                         timezone.now().isoformat()))
            trans.save()
        except cls.DoesNotExist as e:
            trans = cls.objects.create(
                item=item,
                product=product,
                window=window,
                transaction_id=data.get('transaction_id', None),
                transaction_status=transaction_status,
                user=user,
                account=account,
                right=right,
                price=data.get('price', 0.00),
                currency=currency,
                retail_model=retail_model,
                definition=definition,
                client=client,
                territory=territory,
                platform=platform,
                device=device,
                mnc=data.get('MNC', None),
                mcc=data.get('MCC', None),
                last_4_digits=data.get('last_4_digits', None),
                event_utc_datetime=data.get('event_time',
                                            timezone.now().isoformat()),
                event_utc_date=DimensionUTCDate.date_from_datetime(
                    data.get('event_time',
                             timezone.now().isoformat())),
            )

        try:
            cls.objects.get(pk=trans.pk)
            exist = True
        except cls.DoesNotExist:
            exist = False

        logs.completed = exist
        logs.save()
    def create_fact(cls, logs, **data):

        # insert dims
        item = DimensionItem.insert(item_id=data.get('item_id')) if exists('item_id', data) else None
        product = DimensionProduct.insert(product_id=data.get('product_id')) if exists('product_id', data) else None
        right = DimensionRight.insert(right_id=data.get('right_id')) if exists('right_id', data) else None
        currency = DimensionCurrency.insert(code=data.get('currency')) if exists('currency', data) else None
        retail_model = DimensionRetailModel.insert(
            model=data.get('retail_model')
        ) if exists('retail_model', data) else None
        definition = DimensionDefinition.insert(
            definition=data.get('definition')
        ) if exists('definition', data) else None

        window = DimensionWindow.insert(
            window_id=data.get('window_id', None),
            product=product,
            usage_right=right,
            item=item
        )
        transaction_status = DimensionStoreTransactionStatus.insert(
            status=data.get('transaction_status')
        ) if exists('transaction_status', data) else None

        platform = save_platform(data, event='Storefront Transaction')

        territory = DimensionTerritory.insert(code=data.get('territory')) if exists('territory', data) else None
        client = DimensionClient.insert(client_id=data.get('client_id')) if exists('client_id', data) else None

        user = save_user(data, territory, client, event='Storefront Transaction')

        account = DimensionAccount.insert(
            account_id=data.get(
                'account_id', data.get(
                    'external_user_id', data.get(
                        'internal_user_id', None
                    )
                )
            )
        )

        try:
            device = DimensionDevice.insert(
                make=data.get('make', ''),
                model=data.get('model', ''),
                os=data.get('os', ''),
                os_version=data.get('version', ''),
            )if any([
                d in data for d in ['make', 'model', 'os', 'version']
            ]) else None
        except:
            log.exception('')

        try:
            trans = cls.objects.get(transaction_id=data.get('transaction_id', ''))
            trans.last_4_digits = data.get('last_4_digits', None)
            trans.event_utc_datetime = data.get('event_time', timezone.now().isoformat())
            trans.event_utc_date = DimensionUTCDate.date_from_datetime(data.get('event_time', timezone.now().isoformat()))
            trans.save()
        except cls.DoesNotExist as e:
            trans = cls.objects.create(
                item=item,
                product=product,
                window=window,
                transaction_id=data.get('transaction_id', None),
                transaction_status=transaction_status,
                user=user,
                account=account,
                right=right,
                price=data.get('price', 0.00),
                currency=currency,
                retail_model=retail_model,
                definition=definition,
                client=client,
                territory=territory,
                platform=platform,
                device=device,
                mnc=data.get('MNC', None),
                mcc=data.get('MCC', None),
                last_4_digits=data.get('last_4_digits', None),
                event_utc_datetime=data.get('event_time', timezone.now().isoformat()),
                event_utc_date=DimensionUTCDate.date_from_datetime(data.get('event_time', timezone.now().isoformat())),
            )

        try:
            cls.objects.get(pk=trans.pk)
            exist = True
        except cls.DoesNotExist:
            exist = False

        logs.completed = exist
        logs.save()
    def create_fact(cls, logs, **data):
        # dims
        platform = save_platform(data, event='Storefront Subscription')

        territory = DimensionTerritory.insert(
            code=data['territory']) if exists('territory', data) else None

        client = DimensionClient.insert(
            client_id=data['client_id']) if exists('client_id', data) else None

        du = save_user(data,
                       territory,
                       client,
                       event='Storefront Subscription')

        dst = DST.insert(
            period=data['subscription_period'] if exists(
                'subscription_period', data) else None,
            recurrence=data['subscription_recurrence']
            if exists('subscription_recurrence', data) else None) if any([
                d in data
                for d in ['subscription_period', 'subscription_recurrence']
            ]) else None

        dss_status, _ = DSS.insert(
            event=int(data['subscription_status'])) if exists(
                'subscription_status', data) else None

        state, err = get_rule(
            status=data.get('subscription_status', None),
            current_state=FactServicesStorefrontSubscription.get_last_event(
                subscription_id=data.get('subscription_id', None)),
            window_ongoing=bool(data.get('window_on_going', False)))

        dsse, _ = DSSe.objects.get_or_create(state=state)

        item = DimensionItem.insert(
            item_id=data.get('item_id')) if exists('item_id', data) else None
        product = DimensionProduct.insert(
            product_id=data.get('product_id')) if exists('product_id',
                                                         data) else None
        usage_right = DimensionRight.insert(
            right_id=data.get('window_usage_right_id')) if exists(
                'window_usage_right_id', data) else None

        window = DimensionWindow.insert(
            window_id=data.get('window_id', None),
            item=item,
            product=product,
            start_date=data['window_start_date'],
            end_date=data['window_end_date'],
            pricing_id=data['window_pricing_id'],
            usage_right_id=usage_right,
            tier=data['window_tier'],
            deleted=data['window_deleted_date'],
            window_type=data['window_type'],
            on_going=data['window_on_going'],
            repeat_count=data['window_repeat_count'],
            auto_upgrade=data['window_auto_upgrade'],
            allow_repurchase=data['window_allow_repurchase'],
            apple_product_id=data['apple_product_id'],
        ) if any([
            i in data for i in [
                'window_id',
                'item_id',
                'product_id',
                'window_start_date',
                'window_end_date',
                'window_pricing_id',
                'window_usage_right_id',
                'window_tier',
                'window_deleted_date',
                'window_type',
                'window_on_going',
                'window_repeat_count',
                'window_auto_upgrade',
                'window_allow_repurchase',
                'apple_product_id',
            ]
        ]) else None

        # fact create
        fact = FactServicesStorefrontSubscription.objects.create(
            user=du,
            subscription_id=data.get('subscription_id', None),
            subscription_type=dst,
            subscription_status=dss_status,
            subscription_state=dsse,
            subscription_state_error=err,
            platform=platform,
            window=window,
            transaction_id=data.get('transaction_id', None),
            event_utc_date=DimensionUTCDate.date_from_datetime(
                datetime=data['event_time']),
            event_utc_datetime=data.get('event_time', None))

        try:
            FactServicesStorefrontSubscription.objects.get(pk=fact.pk)
            exist = True
        except FactServicesStorefrontSubscription.DoesNotExist:
            exist = False

        logs.completed = exist
        logs.save()