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):

        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):

        # 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()
Beispiel #6
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()