def create_fact(cls, **data):

        event_time = data['date']

        # using geoip we assert what our country code is based upon the ip address chain
        country_code = ''
        for ip_address in data['ip_address_chain']:
            country_code = GEO_IP_LOOKUP.get_country_code(ip_address)
            if country_code:
                break

        # lets now fill in our dimensions
        territory = DimensionTerritory.insert(code=country_code)
        client = DimensionClient.insert(client_id=data['client_id'])
        item = DimensionItem.insert(item_id=data['item_id'])
        device = DimensionDevice.insert(item_id=data['device_id'])
        status = DimensionPlayStatus.insert(status=data['status'])
        user = DimensionUser.insert(internal_user_id=data['internal_user_id'],
                                    external_user_id=data['external_user_id'],
                                    client=client)

        # we create our fact play object
        fshp = FactServicesHeartbeatPlay.objects.create(
            item_id=item,
            user=user,
            device_id=device,
            status=status,
            event_time=event_time,
            country_code=territory,
            event_utc_date=DimensionUTCDate.date_from_datetime(event_time),
            position=get(data, 'position', disallow=['']),  # Preserve 0
            bit_rate=get(data, 'bitrate', disallow=['']),  # Preserve 0
        )

        # for any occuring buffers we create a play buffer, we can then add this to our many to many relation
        for buffer_duration in get(data,
                                   'buffer_events',
                                   disallow=[None, ''],
                                   default=[]):
            fshpb = FactServicesHeartbeatPlayBuffer.objects.create(
                duration=buffer_duration,
                event_utc_date=DimensionUTCDate.date_from_datetime(event_time),
                event_time=event_time)

            fshp.play_buffer.add(fshpb)

        # if we have an init in our event - we can add this and add the foreign key to our play event
        # this will make for good and easy lookup of full fact play data
        init_duration = get(data, 'init_length', disallow=[''])
        if init_duration is not None:
            init_licensed = get(data,
                                'init_licensed',
                                disallow=['', None],
                                default=False)
            FactServicesHeartbeatPlayInit.objects.create(
                heartbeat=fshp,
                duration=init_duration,
                licensed=init_licensed,
                event_utc_date=DimensionUTCDate.date_from_datetime(event_time),
                event_time=event_time)
    def create_fact(cls, logs, **data):

        platform = get_platform(local_data=data, model=FactServicesStorefrontRegistration)
        territory = DimensionTerritory.insert(code=data['territory'])
        client = DimensionClient.insert(client_id=data['client_id'])
        try:
            device = DimensionDevice.insert(
                device_id=data.get('device_id', ''),
                make=data.get('make', ''),
                model=data.get('model', ''),
                os=data.get('os', ''),
                os_version=data.get('version', '')
            )
        except:
            device = None

        try:
            user_info = data.get('attributes', None)
            user = DimensionUser.insert(
                internal_user_id=data.get('internal_user_id', data.get('user_id', '')),
                external_user_id=data.get('external_user_id', ''),
                territory=territory,
                client=client,
                email=data.get('email', None),
                first_name=user_info.get('first_name', '') if user_info else '',
                last_name=user_info.get('last_name', '') if user_info else '',
                marketing_preference=user_info.get('marketing_preference', '') if user_info else '',
                country_of_residence=user_info.get('country_of_residence', '') if user_info else None
            )
        except Exception:
            user = None

        try:
            account = DimensionAccount.insert(
                account_id=data.get('external_user_id', data.get('internal_user_id', '')),
                account_created=data.get('account_created', None)
            )
        except:
            account = None

        fact = FactServicesStorefrontRegistration.objects.create(
            account=account,
            user=user,
            client=client,
            platform=platform,
            device=device,
            event_utc_date=DimensionUTCDate.date_from_datetime(datetime=data['event_time']),
            event_utc_datetime=data['event_time'],
            mnc=data.get('MNC', None),
            mcc=data.get('MCC', None)
        )

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

        logs.completed = exist
        logs.save()
    def create_fact(cls, **data):
        client = DimensionClient.insert(client_id=data['client_id'])
        item = DimensionItem.insert(item_id=data['item_id'])
        data_role = DimensionDataRole.insert(role=data['data_role'])

        definition = DimensionDefinition.insert(definition=data['definition'])

        processing_state = DimensionProcessingState.insert(processing_state=data['processing_state'])
        territory = DimensionTerritory.insert(code=data['territory'])

        asset = DimensionAssets.insert(
            asset_id=data['asset_id'],
            type=data['asset_type']
        )

        provider = DimensionItemProvider.insert(
            True,
            provider_id=data['provider_item_id'],
            provider_name=data['provider'],
            )


        delivery_date = datetime.datetime.strptime(str(data['delivery_date']), settings.DATETIME_FORMAT)
        event_date = datetime.datetime.strptime(data['event_time'], settings.DATETIME_FORMAT)
        event_utc_datetime = datetime.datetime.strptime(data['event_time'], settings.DATETIME_FORMAT)

        fact = FactServicesBackstageAssetMatch.objects.create(
            client=client,
            item=item,
            asset=asset,
            data_role=data_role,
            processing_state=processing_state,
            provider=provider,
            territory=territory,
            spec_name=data['spec_name'],
            delivery_date=delivery_date,
            definition=definition,
            file_size=data['file_size'],
            duration=data['duration'],
            event_date=event_date.date(),
            event_utc_date=DimensionUTCDate.date_from_datetime(datetime=data['event_time']),
            event_utc_datetime=event_utc_datetime
        )

        fact.save()
        if data['used_asset_ids']:
            for asset_id in data['used_asset_ids']:
                asset_dim = DimensionAssets.objects.get_or_create(asset_id=asset_id)
                fact.used_asset_ids.add(asset_dim[0].id)
        else:
            fact.used_asset_ids = ''

        if data['languages']:
            for language in data['languages']:
                lang_dim = DimensionLanguage.objects.get_or_create(iso_code=language)
                fact.languages.add(lang_dim[0].id)
        else:
            fact.languages = ''
        return fact
    def create_fact(cls, **data):

        event_time = data['date']

        # using geoip we assert what our country code is based upon the ip address chain
        country_code = ''
        for ip_address in data['ip_address_chain']:
            country_code = GEO_IP_LOOKUP.get_country_code(ip_address)
            if country_code:
                break

        # lets now fill in our dimensions
        territory = DimensionTerritory.insert(code=country_code)
        client = DimensionClient.insert(client_id=data['client_id'])
        item = DimensionItem.insert(item_id=data['item_id'])
        device = DimensionDevice.insert(item_id=data['device_id'])
        status = DimensionPlayStatus.insert(status=data['status'])
        user = DimensionUser.insert(internal_user_id=data['internal_user_id'],
                                    external_user_id=data['external_user_id'], client=client)

        # we create our fact play object
        fshp = FactServicesHeartbeatPlay.objects.create(
            item_id=item,
            user=user,
            device_id=device,
            status=status,
            event_time=event_time,
            country_code=territory,
            event_utc_date=DimensionUTCDate.date_from_datetime(event_time),
            position=get(data, 'position', disallow=['']),  # Preserve 0
            bit_rate=get(data, 'bitrate', disallow=['']),   # Preserve 0
        )

        # for any occuring buffers we create a play buffer, we can then add this to our many to many relation
        for buffer_duration in get(data, 'buffer_events', disallow=[None, ''], default=[]):
            fshpb = FactServicesHeartbeatPlayBuffer.objects.create(
                duration=buffer_duration,
                event_utc_date=DimensionUTCDate.date_from_datetime(event_time),
                event_time=event_time)

            fshp.play_buffer.add(fshpb)

        # if we have an init in our event - we can add this and add the foreign key to our play event
        # this will make for good and easy lookup of full fact play data
        init_duration = get(data, 'init_length', disallow=[''])
        if init_duration is not None:
            init_licensed = get(data, 'init_licensed', disallow=['', None], default=False)
            FactServicesHeartbeatPlayInit.objects.create(
                heartbeat=fshp,
                duration=init_duration,
                licensed=init_licensed,
                event_utc_date=DimensionUTCDate.date_from_datetime(event_time),
                event_time=event_time)
    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()
Exemplo n.º 8
0
    def create_fact(cls, logs, **data):

        platform = get_platform(local_data=data,
                                model=FactServicesStorefrontRegistration)
        territory = DimensionTerritory.insert(code=data['territory'])
        client = DimensionClient.insert(client_id=data['client_id'])
        try:
            device = DimensionDevice.insert(device_id=data.get(
                'device_id', ''),
                                            make=data.get('make', ''),
                                            model=data.get('model', ''),
                                            os=data.get('os', ''),
                                            os_version=data.get('version', ''))
        except:
            device = None

        try:
            user_info = data.get('attributes', None)
            user = DimensionUser.insert(
                internal_user_id=data.get('internal_user_id',
                                          data.get('user_id', '')),
                external_user_id=data.get('external_user_id', ''),
                territory=territory,
                client=client,
                email=data.get('email', None),
                first_name=user_info.get('first_name', '')
                if user_info else '',
                last_name=user_info.get('last_name', '') if user_info else '',
                marketing_preference=user_info.get('marketing_preference', '')
                if user_info else '',
                country_of_residence=user_info.get('country_of_residence', '')
                if user_info else None)
        except Exception:
            user = None

        try:
            account = DimensionAccount.insert(
                account_id=data.get('external_user_id',
                                    data.get('internal_user_id', '')),
                account_created=data.get('account_created', None))
        except:
            account = None

        fact = FactServicesStorefrontRegistration.objects.create(
            account=account,
            user=user,
            client=client,
            platform=platform,
            device=device,
            event_utc_date=DimensionUTCDate.date_from_datetime(
                datetime=data['event_time']),
            event_utc_datetime=data['event_time'],
            mnc=data.get('MNC', None),
            mcc=data.get('MCC', None))

        try:
            FactServicesStorefrontRegistration.objects.get(pk=fact.pk)
            exist = True
        except FactServicesStorefrontRegistration.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()