<commit_before>from datetime import datetime
import logging
import decimal
import base64
import json
import time

from lib import config, util

decimal.setcontext(decimal.Context(prec=8, rounding=decimal.ROUND_HALF_EVEN))
D = decimal.Decimal

def calculate_price(base_quantity, quote_quantity, base_divisibility, quote_divisibility):
    if not base_divisibility:
        base_quantity *= config.UNIT
    if not quote_divisibility:
        quote_quantity *= config.UNIT

    try:
        return float(quote_quantity) / float(base_quantity)
    except Exception, e:
        return 0

def format_price(base_quantity, quote_quantity, base_divisibility, quote_divisibility):
    price = calculate_price(base_quantity, quote_quantity, base_divisibility, quote_divisibility)
    return format(price, '.8f')

def get_pairs_with_orders(addresses=[], max_pairs=12):

    pairs_with_orders = []

    sources = '''AND source IN ({})'''.format(','.join(['?' for e in range(0,len(addresses))]))
        
    sql = '''SELECT (MIN(give_asset, get_asset) || '/' || MAX(give_asset, get_asset)) AS pair,
                    COUNT(*) AS order_count
             FROM orders
             WHERE give_asset != get_asset AND status = ? {} 
             GROUP BY pair 
             ORDER BY order_count DESC
             LIMIT ?'''.format(sources)
    
    bindings = ['open'] + addresses + [max_pairs]

    my_pairs = util.call_jsonrpc_api('sql', {'query': sql, 'bindings': bindings})['result']

    for my_pair in my_pairs:
        base_asset, quote_asset = util.assets_to_asset_pair(*tuple(my_pair['pair'].split("/")))
        top_pair = {
            'base_asset': base_asset,
            'quote_asset': quote_asset,
            'my_order_count': my_pair['order_count']
        }
        if my_pair['pair'] == 'BTC/XCP': # XCP/BTC always in first
            pairs_with_orders.insert(0, top_pair)
        else:
            pairs_with_orders.append(top_pair)

    return pairs_with_orders


def get_xcp_or_btc_pairs(asset='XCP', exclude_pairs=[], max_pairs=12, from_time=None):
            
    bindings = []
    
    sql = '''SELECT (CASE
                        WHEN forward_asset = ? THEN backward_asset
                        ELSE forward_asset
                    END) AS base_asset,
                    (CASE
                        WHEN backward_asset = ? THEN backward_asset
                        ELSE forward_asset
                    END) AS quote_asset,
                    (CASE
                        WHEN backward_asset = ? THEN (forward_asset || '/' || backward_asset)
                        ELSE (backward_asset || '/' || forward_asset)
                    END) AS pair,
                    (CASE
                        WHEN forward_asset = ? THEN SUM(backward_quantity)
                        ELSE SUM(forward_quantity)
                    END) AS base_quantity,
                    (CASE
                        WHEN backward_asset = ? THEN SUM(backward_quantity)
                        ELSE SUM(forward_quantity)
                    END) AS quote_quantity '''
    if from_time:
        sql += ''', block_time '''

    sql += '''FROM order_matches '''
    bindings += [asset, asset, asset, asset, asset]

    if from_time:
        sql += '''INNER JOIN blocks ON order_matches.block_index = blocks.block_index '''

    if asset == 'XCP':
        sql += '''WHERE ((forward_asset = ? AND backward_asset != ?) OR (forward_asset != ? AND backward_asset = ?)) '''
        bindings += [asset, 'BTC', 'BTC', asset]
    else:
        sql += '''WHERE ((forward_asset = ?) OR (backward_asset = ?)) '''
        bindings += [asset, asset]

    if len(exclude_pairs) > 0:
        sql += '''AND pair NOT IN ({}) '''.format(','.join(['?' for e in range(0,len(exclude_pairs))]))
        bindings += exclude_pairs

    if from_time:
        sql += '''AND block_time > ? '''
        bindings += [from_time]

    sql += '''AND forward_asset != backward_asset
              GROUP BY pair
              ORDER BY quote_quantity DESC
              LIMIT ?'''
    bindings += [max_pairs]

    return util.call_jsonrpc_api('sql', {'query': sql, 'bindings': bindings})['result']


def get_xcp_and_btc_pairs(exclude_pairs=[], max_pairs=12, from_time=None):

    all_pairs = []

    for currency in ['XCP', 'BTC']:
        currency_pairs = get_xcp_or_btc_pairs(asset=currency, exclude_pairs=exclude_pairs, max_pairs=max_pairs, from_time=from_time)
        for currency_pair in currency_pairs:
            if currency_pair['pair'] == 'XCP/BTC':
                all_pairs.insert(0, currency_pair)
            else:
                all_pairs.append(currency_pair)

    return all_pairs

def get_users_pairs(addresses=[], max_pairs=12):
    
    top_pairs = []<commit_msg>use only completed order_matches to calculate 24h volume<commit_after>from datetime import datetime
import logging
import decimal
import base64
import json
import time

from lib import config, util

decimal.setcontext(decimal.Context(prec=8, rounding=decimal.ROUND_HALF_EVEN))
D = decimal.Decimal

def calculate_price(base_quantity, quote_quantity, base_divisibility, quote_divisibility):
    if not base_divisibility:
        base_quantity *= config.UNIT
    if not quote_divisibility:
        quote_quantity *= config.UNIT

    try:
        return float(quote_quantity) / float(base_quantity)
    except Exception, e:
        return 0

def format_price(base_quantity, quote_quantity, base_divisibility, quote_divisibility):
    price = calculate_price(base_quantity, quote_quantity, base_divisibility, quote_divisibility)
    return format(price, '.8f')

def get_pairs_with_orders(addresses=[], max_pairs=12):

    pairs_with_orders = []

    sources = '''AND source IN ({})'''.format(','.join(['?' for e in range(0,len(addresses))]))
        
    sql = '''SELECT (MIN(give_asset, get_asset) || '/' || MAX(give_asset, get_asset)) AS pair,
                    COUNT(*) AS order_count
             FROM orders
             WHERE give_asset != get_asset AND status = ? {} 
             GROUP BY pair 
             ORDER BY order_count DESC
             LIMIT ?'''.format(sources)
    
    bindings = ['open'] + addresses + [max_pairs]

    my_pairs = util.call_jsonrpc_api('sql', {'query': sql, 'bindings': bindings})['result']

    for my_pair in my_pairs:
        base_asset, quote_asset = util.assets_to_asset_pair(*tuple(my_pair['pair'].split("/")))
        top_pair = {
            'base_asset': base_asset,
            'quote_asset': quote_asset,
            'my_order_count': my_pair['order_count']
        }
        if my_pair['pair'] == 'BTC/XCP': # XCP/BTC always in first
            pairs_with_orders.insert(0, top_pair)
        else:
            pairs_with_orders.append(top_pair)

    return pairs_with_orders


def get_xcp_or_btc_pairs(asset='XCP', exclude_pairs=[], max_pairs=12, from_time=None):
            
    bindings = []
    
    sql = '''SELECT (CASE
                        WHEN forward_asset = ? THEN backward_asset
                        ELSE forward_asset
                    END) AS base_asset,
                    (CASE
                        WHEN backward_asset = ? THEN backward_asset
                        ELSE forward_asset
                    END) AS quote_asset,
                    (CASE
                        WHEN backward_asset = ? THEN (forward_asset || '/' || backward_asset)
                        ELSE (backward_asset || '/' || forward_asset)
                    END) AS pair,
                    (CASE
                        WHEN forward_asset = ? THEN SUM(backward_quantity)
                        ELSE SUM(forward_quantity)
                    END) AS base_quantity,
                    (CASE
                        WHEN backward_asset = ? THEN SUM(backward_quantity)
                        ELSE SUM(forward_quantity)
                    END) AS quote_quantity '''
    if from_time:
        sql += ''', block_time '''

    sql += '''FROM order_matches '''
    bindings += [asset, asset, asset, asset, asset]

    if from_time:
        sql += '''INNER JOIN blocks ON order_matches.block_index = blocks.block_index '''

    if asset == 'XCP':
        sql += '''WHERE ((forward_asset = ? AND backward_asset != ?) OR (forward_asset != ? AND backward_asset = ?)) '''
        bindings += [asset, 'BTC', 'BTC', asset]
    else:
        sql += '''WHERE ((forward_asset = ?) OR (backward_asset = ?)) '''
        bindings += [asset, asset]

    if len(exclude_pairs) > 0:
        sql += '''AND pair NOT IN ({}) '''.format(','.join(['?' for e in range(0,len(exclude_pairs))]))
        bindings += exclude_pairs

    if from_time:
        sql += '''AND block_time > ? '''
        bindings += [from_time]

    sql += '''AND forward_asset != backward_asset
              AND status = ?
              GROUP BY pair
              ORDER BY quote_quantity DESC
              LIMIT ?'''
    bindings += ['completed', max_pairs]

    return util.call_jsonrpc_api('sql', {'query': sql, 'bindings': bindings})['result']


def get_xcp_and_btc_pairs(exclude_pairs=[], max_pairs=12, from_time=None):

    all_pairs = []

    for currency in ['XCP', 'BTC']:
        currency_pairs = get_xcp_or_btc_pairs(asset=currency, exclude_pairs=exclude_pairs, max_pairs=max_pairs, from_time=from_time)
        for currency_pair in currency_pairs:
            if currency_pair['pair'] == 'XCP/BTC':
                all_pairs.insert(0, currency_pair)
            else:
                all_pairs.append(currency_pair)

    return all_pairs

def get_users_pairs(addresses=[], max_pairs=12):
    
    top_pairs = []
{'fraction_non_alphanumeric': 0.11375744280789721, 'fraction_numerical': 0.00396949754517915, 'mean_word_length': 2.255355321319279, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3475057', 'n_tokens_mistral': 2982, 'n_tokens_neox': 2800, 'n_words': 648}