コード例 #1
0
    def test_key_generator_one_date_only(self):
        filter_data = {
            "date": "2018-10-12",
        }

        cache_key = generate_cache_key(filter_data, "2018-10-12")
        # Start with 2018-10-12 then
        # X_X as all items were required,
        # X as all geos are required and
        # X as all target_user are required
        expected_response = '2018-10-12_X_X_X_X'
        self.assertEqual(cache_key, expected_response)
コード例 #2
0
    def test_key_generator_user(self):
        filter_data = {
            "date__lt": "2018-10-12",
            "date__gt": "2018-10-9",
            "target_user_id": 1,
        }

        cache_key = generate_cache_key(filter_data, "2018-10-12")
        # Start with 2018-10-12 then
        # X_X as all items were required,
        # X as all geos are required and
        # 1 for target_user 1
        expected_response = '2018-10-12_X_X_X_1'
        self.assertEqual(cache_key, expected_response)
コード例 #3
0
    def test_key_generator_item_geo_user(self):
        filter_data = {
            "date__lt": "2018-10-12",
            "date__gt": "2018-10-9",
            "item_id": 1,
            "geo_id": 1,
            "target_user_id": 1,
        }

        cache_key = generate_cache_key(filter_data, "2018-10-12")
        expected_response = '2018-10-12_I_1_1_1'
        # Start with 2018-10-12 then
        # I_1 for the item 1,
        # 1 for geo 1 and
        # 1 for target_user 1
        self.assertEqual(cache_key, expected_response)
コード例 #4
0
    def get(self, request, format=None):
        filter_data = request.GET.dict()

        yesterday = (date.today() - timedelta(days=1))
        data_to_send = None
        if request.GET.get('date') == yesterday.strftime("%Y-%m-%d"):
            data_to_send = cache.get(yesterday.strftime("%Y-%m-%d"))

        if data_to_send is None:
            if ('date' not in request.GET) & ('date__gt' in request.GET) & (
                    'date__lt' in request.GET
            ) & (request.GET.get('date__gt') != request.GET.get('date__lt')):
                # get_serialized_queried data
                filter_data['date__gt'] = datetime.strptime(
                    request.GET.get('date__gt'),
                    CHART_DATE_FORMAT_FOR_DATETIME)
                filter_data['date__lt'] = datetime.strptime(
                    request.GET.get('date__lt'),
                    CHART_DATE_FORMAT_FOR_DATETIME)

                logging.warning(filter_data)
                day_range = filter_data['date__lt'] - filter_data['date__gt']
                day_range = day_range.days + 1
                initial_date = filter_data['date__gt']
                last_date = filter_data['date__lt']
                last_value_from_cache = True
                temp_filter = filter_data.copy()
                bought_data = []
                sold_data = []
                earned_data = []

                for counting_days in range(0, day_range):
                    day = initial_date + timedelta(days=counting_days)
                    cache_key = generate_cache_key(filter_data, day)
                    cache_value = cache.get(cache_key)

                    if cache_value is not None:
                        if not last_value_from_cache:
                            temp_filter['date__lt'] = day
                            not_cached_chart_data = get_chart_data(
                                filter_data=temp_filter)
                            bought_data.extend(
                                not_cached_chart_data.get(
                                    "bought_report_chart"))
                            sold_data.extend(
                                not_cached_chart_data.get("sold_report_chart"))
                            earned_data.extend(
                                not_cached_chart_data.get(
                                    "earned_report_chart"))

                        bought_data.append(cache_value['earned_report_chart'])
                        sold_data.append(cache_value['bought_report_chart'])
                        earned_data.append(cache_value['sold_report_chart'])
                        last_value_from_cache = True

                    else:
                        if day == last_date:
                            logging.warning('List ended')
                            temp_filter['date__lt'] = day
                            not_cached_chart_data = get_chart_data(
                                filter_data=temp_filter)
                            bought_data.extend(
                                not_cached_chart_data.get(
                                    "bought_report_chart"))
                            sold_data.extend(
                                not_cached_chart_data.get("sold_report_chart"))
                            earned_data.extend(
                                not_cached_chart_data.get(
                                    "earned_report_chart"))
                        else:
                            if last_value_from_cache:
                                temp_filter['date__gt'] = day
                        last_value_from_cache = False

                chart_date_format_message = CHART_DATE_FORMAT_FOR_AMCHARTS

            else:
                date_required = filter_data.get('date')
                data_to_send = cache.get(date_required)
                if data_to_send is None:
                    # get_serialized_queried_data_hourly
                    filter_data['date__gt'] = datetime.strptime(
                        filter_data.get('date'),
                        CHART_DATE_FORMAT_FOR_DATETIME)
                    filter_data['date__gt'] = filter_data[
                        'date__gt'] - timedelta(seconds=1)
                    filter_data['date__lt'] = filter_data[
                        'date__gt'] + timedelta(days=1)
                    filter_data.pop('date')
                    logging.warning(filter_data)

                    bought_serializer = BuySerializerWithTime(data=filter_data)
                    bought_serializer.is_valid()
                    bought_data = list(
                        bought_serializer.data.get("bought_report_chart"))
                    sold_serializer = SellSerializerWithTime(data=filter_data)
                    sold_serializer.is_valid()
                    sold_data = list(
                        sold_serializer.data.get("sold_report_chart"))
                    earned_serializer = EarnSerializerWithTime(
                        data={
                            'filter_data': filter_data.copy(),
                            'bought_data': bought_data,
                            'sold_data': sold_data,
                            'send_to_cache': True,
                        })
                    earned_serializer.is_valid()
                    earned_data = earned_serializer.data.get(
                        "earned_report_chart")
                    chart_date_format_message = CHART_DATETIME_FORMAT_FOR_AMCHARTS

            data_to_send = {
                "earned_report_chart": earned_data,
                "bought_report_chart": bought_data,
                "sold_report_chart": sold_data,
                "chart_date_format": chart_date_format_message,
            }

        return JsonResponse(data_to_send)
コード例 #5
0
def create_random_user_accounts():
    random_transaction_number = randrange(1, MAX_DAILY_TRANSACTION)

    try:
        day = Buy.objects.all().order_by('-date')[0].date + timedelta(1)
    except IndexError:
        try:
            day = Sell.objects.all().order_by('-date')[0].date + timedelta(1)
        except IndexError:
            pass

    for i in range(1, random_transaction_number + 1):
        temp_hours = randrange(0, 23)
        temp_minutes = randrange(0, 59)
        temp_seconds = randrange(0, 59)
        logging.warning(temp_hours)
        logging.warning(day)
        temp_date = day + timedelta(hours=temp_hours, minutes=temp_minutes, seconds=temp_seconds)

        target_user_amount = TargetUser.objects.all().count()
        item_amount = Item.objects.all().count()
        geo_amount = Geo.objects.all().count()

        target_user_amount_id = randrange(0, target_user_amount) + 1
        temp_item_id = randrange(0, item_amount) + 1
        temp_geo_id = randrange(0, geo_amount) + 1

        transaction_amount = randrange(INITIAL_AMOUNT * 0.1, INITIAL_AMOUNT * 0.9)
        transaction_price = Item(id=temp_item_id).generate_next_price()

        if randrange(0, 2):
            temp_transaction = Buy(
                item_id=temp_item_id,
                amount=transaction_amount,
                unit_price=transaction_price,
                date=temp_date,
                target_user_id=target_user_amount_id,
                geo_id=temp_geo_id,
            )
        else:
            temp_transaction = Sell(
                item_id=temp_item_id,
                amount=transaction_amount,
                unit_price=transaction_price,
                date=temp_date,
                target_user_id=target_user_amount_id,
                geo_id=temp_geo_id,
            )

        filter_data = {
            "date": temp_date,
            "item___id": temp_item_id,
            "geo_id": temp_geo_id,
            "target_user_id": target_user_amount_id,
        }
        cache_key = generate_cache_key(filter_data,temp_date)
        cache.delete(cache_key)

        logging.warning(temp_transaction)

        try:
            temp_transaction.save()
        except Exception as e:
            logging.error(e)
            logging.error('item_id %s' % temp_transaction.item)
            logging.error('item %s' % temp_transaction.item)
            logging.error('amount %s' % temp_transaction.amount)
            logging.error('unit_price %s' % temp_transaction.unit_price)
            logging.error('date %s' % temp_transaction.date)
            logging.error('target_user %s' % temp_transaction.target_user)
            logging.error('geo_id %s' % temp_transaction.geo)
    to_return = '%s random transactions created with success!' % i
    logging.info(to_return)
    return to_return