Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes get_markets and get_daily_volume #80

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions services/bitshares_elasticsearch_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def get_markets(self, from_date, to_date, base=None, quote=None):
"query": {
"bool": {
"filter": [
{ "term": { "operation_type": 4 } },
{ "term": { "operation_type": 4 } }, # NOTE: may logically return duplicate data since not filtering by `is_maker == true`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The restriction below (L121) ensured that it does not return duplicates

Q('term', operation_history__op_object__fill_price__quote__asset_id__keyword=config.CORE_ASSET_ID)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The duplication is logical, because on every order match, two fill_order_operation entries are stored, one for each side, and their fill_price is exactly the same. So the fill_price__quote__asset_id filter does not work as intended. The receives__asset_id filter (my code) may work.

We should not filter by is_maker here though. Actually we need the duplicate data. I'll update the comment.

Copy link

@sschiessl-bcp sschiessl-bcp Oct 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yea I remember... The explorer API that I am using for blocksights is a complete rewrite

def get_markets(from_date, to_date, base=None, quote=None):
    ...
    s = s.extra(size=0)
    s = s.source([])
    s = s.filter(
        Q('term', operation_type=4) &
        Q('range', block_data__block_time={'gte': from_date, 'lte': to_date})
    )
    if base:
        s = s.filter(
            Q('term', operation_history__op_object__pays__asset_id=base)
        )
    if quote:
        s = s.filter(
            Q('term', operation_history__op_object__receives__asset_id=quote)
        )
    a = A(
        "composite",
        sources=[
            { "base_asset_id": { "terms" : { "field": "operation_history.op_object.pays.asset_id.keyword" } } },
            { "quote_asset_id": { "terms" : { "field": "operation_history.op_object.receives.asset_id.keyword" } } }
        ],
        size=10000
    ).metric(
        'base_volume', 'sum', field='operation_history.op_object.pays.amount'
    ).metric(
        'quote_volume', 'sum', field='operation_history.op_object.receives.amount'
    )
    s.aggs.bucket(
        'pairs',
        a
    )
    response = s.execute()
    ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's correct then.
So I think the issue on blocksights is that we should not add amounts in different assets together.

Copy link
Member Author

@abitmore abitmore Oct 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should not add amounts in different assets together.

Should not sort by them either.

{
"range": {
"block_data.block_time": {
Expand All @@ -45,21 +45,23 @@ def get_markets(self, from_date, to_date, base=None, quote=None):
"composite" : {
"size": 10000, # TODO use a generator function instead of a big size, see https://github.com/elastic/elasticsearch-dsl-py/blob/master/examples/composite_agg.py#L21
"sources" : [
{ "base": { "terms" : { "field": "operation_history.op_object.fill_price.base.asset_id.keyword" } } },
{ "quote": { "terms" : { "field": "operation_history.op_object.fill_price.quote.asset_id.keyword" } } }
{ "base": { "terms" : { "field": "operation_history.op_object.pays.asset_id.keyword" } } },
{ "quote": { "terms" : { "field": "operation_history.op_object.receives.asset_id.keyword" } } }
]
},
"aggs": {
"volume": { "sum" : { "field" : "operation_history.op_object.fill_price.quote.amount" } }
"volume": { "sum" : { "field" : "operation_history.op_object.receives.amount" } }
# NOTE: perhaps better return both `pays.amount` and `receives.amount` (in different fields but not add them together),
# because it does not make much sense to return only `receives.amount` when filtering by `pays.asset_id`.
}
}
}
}

if base:
query['query']['bool']['filter'].append({ "term": { "operation_history.op_object.fill_price.base.asset_id.keyword": base } })
query['query']['bool']['filter'].append({ "term": { "operation_history.op_object.pays.asset_id.keyword": base } })
if quote:
query['query']['bool']['filter'].append({ "term": { "operation_history.op_object.fill_price.quote.asset_id.keyword": quote } })
query['query']['bool']['filter'].append({ "term": { "operation_history.op_object.receives.asset_id.keyword": quote } })

client = connections.get_connection('operations')
response = client.search(index="bitshares-*", body=query)
Expand Down Expand Up @@ -124,11 +126,11 @@ def get_daily_volume(self, from_date, to_date):
s = s.query('bool', filter = [
Q('term', operation_type=4),
Q('range', block_data__block_time={'gte': from_date, 'lte': to_date}),
Q('term', operation_history__op_object__fill_price__quote__asset_id__keyword=config.CORE_ASSET_ID)
Q('term', operation_history__op_object__receives__asset_id__keyword=config.CORE_ASSET_ID)
])

a = A('date_histogram', field='block_data.block_time', interval='1d', format='yyyy-MM-dd') \
.metric('volume', 'sum', field='operation_history.op_object.fill_price.quote.amount')
.metric('volume', 'sum', field='operation_history.op_object.receives.amount')
s.aggs.bucket('volume_over_time', a)

response = s.execute()
Expand Down