Skip to content

Commit

Permalink
fixes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
telezhnaya committed Nov 16, 2021
1 parent d22f594 commit 9e5a1cd
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,16 @@ def sql_select(self):
# Other receipts from the next day will be naturally ignored.
# Transactions border remains the same, taking only transactions for the specified day.
# If you want to change 10 minutes constant, fix it also in PeriodicAggregations.is_indexer_ready

# Conditions on receipts timestamps are added because of performance issues:
# Joining 2 relatively small tables work much faster (4-6s VS 70-150s)
# Conditions on transactions timestamps are required by design.
# Though, they were placed into JOIN section also because of performance issues. Not sure why,
# but it changes the query plan to a better one and gives much better performance
return '''
SELECT
receipts.receiver_account_id,
COUNT(DISTINCT transactions.transaction_hash) AS transactions_count
COUNT(DISTINCT transactions.transaction_hash) AS ingoing_transactions_count
FROM transactions
LEFT JOIN receipts ON receipts.originated_from_transaction_hash = transactions.transaction_hash
AND transactions.block_timestamp >= %(from_timestamp)s
Expand All @@ -67,6 +73,6 @@ def start_of_range(self, timestamp: int) -> int:
return daily_start_of_range(timestamp)

@staticmethod
def prepare_data(parameters: list, **kwargs) -> list:
computed_for = datetime.datetime.utcfromtimestamp(kwargs['start_of_range']).strftime('%Y-%m-%d')
def prepare_data(parameters: list, *, start_of_range=None, **kwargs) -> list:
computed_for = datetime.datetime.utcfromtimestamp(start_of_range).strftime('%Y-%m-%d')
return [(computed_for, account_id, count) for (account_id, count) in parameters]
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ def start_of_range(self, timestamp: int) -> int:
return daily_start_of_range(timestamp)

@staticmethod
def prepare_data(parameters: list, **kwargs) -> list:
computed_for = datetime.datetime.utcfromtimestamp(kwargs['start_of_range']).strftime('%Y-%m-%d')
def prepare_data(parameters: list, *, start_of_range=None, **kwargs) -> list:
computed_for = datetime.datetime.utcfromtimestamp(start_of_range).strftime('%Y-%m-%d')
return [(computed_for, account_id, count) for (account_id, count) in parameters]
4 changes: 2 additions & 2 deletions aggregations/db_tables/daily_receipts_per_contract_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ def start_of_range(self, timestamp: int) -> int:
return daily_start_of_range(timestamp)

@staticmethod
def prepare_data(parameters: list, **kwargs) -> list:
computed_for = datetime.datetime.utcfromtimestamp(kwargs['start_of_range']).strftime('%Y-%m-%d')
def prepare_data(parameters: list, *, start_of_range=None, **kwargs) -> list:
computed_for = datetime.datetime.utcfromtimestamp(start_of_range).strftime('%Y-%m-%d')
return [(computed_for, contract_id, count) for (contract_id, count) in parameters]
8 changes: 4 additions & 4 deletions aggregations/periodic_aggregations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import datetime

from .sql_aggregations import SqlAggregations
from .db_tables import time_range_json, to_nanos
from .db_tables import time_range_json


class PeriodicAggregations(SqlAggregations):
Expand All @@ -26,11 +26,11 @@ def collect(self, requested_timestamp: int) -> list:
return self.prepare_data(result, start_of_range=from_timestamp)

@staticmethod
def prepare_data(parameters: list, **kwargs) -> list:
def prepare_data(parameters: list, *, start_of_range=None, **kwargs) -> list:
# We usually have one-value returns, we need to merge it with corresponding date
if len(parameters[0]) == 1:
assert len(parameters) == 1, 'Only one value expected. Can\'t be sure that we need to add timestamp'
computed_for = datetime.datetime.utcfromtimestamp(kwargs['start_of_range'])
computed_for = datetime.datetime.utcfromtimestamp(start_of_range)
parameters = [(computed_for, parameters[0][0] or 0)]
return [(computed_for.strftime('%Y-%m-%d'), data) for (computed_for, data) in parameters]

Expand All @@ -46,4 +46,4 @@ def is_indexer_ready(self, needed_timestamp):
latest_timestamp = indexer_cursor.fetchone()[0]
# Adding 10 minutes to be sure that all the data is collected
# Important for DailyIngoingTransactionsPerAccountCount
return latest_timestamp >= needed_timestamp + to_nanos(10 * 60)
return latest_timestamp >= needed_timestamp + 10 * 60

0 comments on commit 9e5a1cd

Please sign in to comment.