diff --git a/README.md b/README.md index d0c29c6..4ad8a36 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Dune Query Repo -A template for creating repos to manage your Dune queries. The main flow I've created this template for is to turn any dashboard you own into a repository of queries. But you can extend it however you want. +A template for creating repos to manage your Dune queries (using the [Dune CRUD API](https://dune.com/docs/api/api-reference/edit-queries/)). The main flow I've created this template for is to turn any dashboard you own into a repository of queries. But you can extend it however you want. ### Setup Your Repo @@ -14,8 +14,7 @@ A template for creating repos to manage your Dune queries. The main flow I've cr 5. Make any changes you need to directly in the repo. Any time you push a commit to MAIN branch, `push_to_dune.py` will save your changes into Dune directly. -💡: We use the [Dune CRUD API](https://dune.com/docs/api/api-reference/edit-queries/) to manage queries - this does not change how your queries behave in app. - +💡: Names of queries are pulled into the file name the first time `pull_from_dune.py` is run. Changing the file name in app or in folder will not affect each other (they aren't synced). Make sure you leave the `___id.sql` at the end of the file, otherwise the scripts will break! 🛑: If you accidently merge a PR or push a commit that messes up your query in Dune, you can roll back any changes using [query version history](https://dune.com/docs/app/query-editor/version-history). --- diff --git a/queries/query_3237726.sql "b/queries/aggregator_by_volume_\360\237\223\242___3237726.sql" similarity index 88% rename from queries/query_3237726.sql rename to "queries/aggregator_by_volume_\360\237\223\242___3237726.sql" index 9377470..ad14846 100644 --- a/queries/query_3237726.sql +++ "b/queries/aggregator_by_volume_\360\237\223\242___3237726.sql" @@ -39,5 +39,10 @@ FROM LEFT JOIN one_day_volume AS one ON seven."Project" = one."Project" GROUP BY 2 +ORDER BY + 3 DESC NULLS FIRSTme AS seven + LEFT JOIN one_day_volume AS one ON seven."Project" = one."Project" +GROUP BY + 2 ORDER BY 3 DESC NULLS FIRST \ No newline at end of file diff --git a/queries/query_3237721.sql "b/queries/dex_by_volume_\360\237\217\246___3237721.sql" similarity index 89% rename from queries/query_3237721.sql rename to "queries/dex_by_volume_\360\237\217\246___3237721.sql" index 7a075d5..4e0381a 100644 --- a/queries/query_3237721.sql +++ "b/queries/dex_by_volume_\360\237\217\246___3237721.sql" @@ -2,7 +2,6 @@ -- query name: DEX by volume 🏦 -- query link: https://dune.com/queries/3237721 - WITH seven_day_volume AS ( SELECT @@ -41,5 +40,11 @@ WHERE NOT seven.usd_volume IS NULL GROUP BY 2 +ORDER BY + 3 DESC NULLS FIRST ON seven."Project" = one."Project" +WHERE + NOT seven.usd_volume IS NULL +GROUP BY + 2 ORDER BY 3 DESC NULLS FIRST \ No newline at end of file diff --git a/queries/query_3237745.sql b/queries/evm_dex_traders_by_bucket___3237745.sql similarity index 89% rename from queries/query_3237745.sql rename to queries/evm_dex_traders_by_bucket___3237745.sql index 1693129..f900bd8 100644 --- a/queries/query_3237745.sql +++ b/queries/evm_dex_traders_by_bucket___3237745.sql @@ -34,4 +34,9 @@ end as trader_bucket , count(*) FROM all_traders WHERE week >= NOW() - INTERVAL '365' day +group by 1,2e >= 1e6 then '$1m+' +end as trader_bucket +, count(*) +FROM all_traders +WHERE week >= NOW() - INTERVAL '365' day group by 1,2 \ No newline at end of file diff --git a/queries/query_3237742.sql b/queries/weekly_dex_aggregator_volume___3237742.sql similarity index 77% rename from queries/query_3237742.sql rename to queries/weekly_dex_aggregator_volume___3237742.sql index ceff17f..8b497cd 100644 --- a/queries/query_3237742.sql +++ b/queries/weekly_dex_aggregator_volume___3237742.sql @@ -11,6 +11,11 @@ FROM dex_aggregator.trades AS t /* AND block_time < date_trunc('week', Now()) -- Add this line to see stats from current week */ WHERE block_time > NOW() - INTERVAL '365' day +GROUP BY + 1, + 2k', Now()) -- Add this line to see stats from current week */ +WHERE + block_time > NOW() - INTERVAL '365' day GROUP BY 1, 2 \ No newline at end of file diff --git a/queries/query_3237738.sql b/queries/weekly_dex_volume___3237738.sql similarity index 77% rename from queries/query_3237738.sql rename to queries/weekly_dex_volume___3237738.sql index cee30ff..0f8a614 100644 --- a/queries/query_3237738.sql +++ b/queries/weekly_dex_volume___3237738.sql @@ -11,6 +11,11 @@ FROM dex."trades" AS t /* AND block_time < date_trunc('week', Now()) -- Add this line to see stats from current week */ WHERE block_time > NOW() - INTERVAL '365' day +GROUP BY + 1, + 2-- Add this line to see stats from current week */ +WHERE + block_time > NOW() - INTERVAL '365' day GROUP BY 1, 2 \ No newline at end of file diff --git a/queries/query_3237723.sql b/queries/weekly_dex_volume_by_chain___3237723.sql similarity index 76% rename from queries/query_3237723.sql rename to queries/weekly_dex_volume_by_chain___3237723.sql index f80fa25..241b1ec 100644 --- a/queries/query_3237723.sql +++ b/queries/weekly_dex_volume_by_chain___3237723.sql @@ -11,6 +11,11 @@ FROM dex."trades" AS t /* AND block_time < date_trunc('week', Now()) -- Add this line to see stats from current week */ WHERE block_time > NOW() - INTERVAL '365' day +GROUP BY + 1, + 2, Now()) -- Add this line to see stats from current week */ +WHERE + block_time > NOW() - INTERVAL '365' day GROUP BY 1, 2 \ No newline at end of file diff --git a/scripts/pull_from_dune.py b/scripts/pull_from_dune.py index 0a88dbb..8964248 100644 --- a/scripts/pull_from_dune.py +++ b/scripts/pull_from_dune.py @@ -22,13 +22,19 @@ for id in query_ids: query = dune.get_query(id) - print('updating query {}, {}'.format(query.base.query_id, query.base.name)) - - # Check if file exists - file_path = os.path.join(os.path.dirname(__file__), '..', 'queries', f'query_{query.base.query_id}.sql') - if os.path.exists(file_path): + print('PROCESSING: query {}, {}'.format(query.base.query_id, query.base.name)) + + # Check if query file exists in /queries folder + queries_path = os.path.join(os.path.dirname(__file__), '..', 'queries') + files = os.listdir(queries_path) + found_files = [file for file in files if str(id) == file.split('___')[-1].split('.')[0]] + + if len(found_files) != 0: # Update existing file - with open(file_path, 'r+', encoding='utf-8') as file: + file_path = os.path.join(os.path.dirname(__file__), '..', 'queries', found_files[0]) + + print('UPDATE: existing query file: {}'.format(found_files[0])) + with open(file_path, 'r+', encoding='utf-8') as file: #if "query repo:" is in the file, then don't add the text header again if '-- already part of a query repo' in query.sql: file.write(query.sql) @@ -36,7 +42,11 @@ file.write(f'-- already part of a query repo\n-- query name: {query.base.name}\n-- query link: https://dune.com/queries/{query.base.query_id}\n\n\n{query.sql}') else: # Create new file and directories if they don't exist + new_file = f'{query.base.name.replace(" ", "_").lower()[:30]}___{query.base.query_id}.sql' + file_path = os.path.join(os.path.dirname(__file__), '..', 'queries', new_file) os.makedirs(os.path.dirname(file_path), exist_ok=True) + + print('CREATE: new query file: {}'.format(new_file)) with open(file_path, 'w', encoding='utf-8') as file: file.write(f'-- already part of a query repo\n-- query name: {query.base.name}\n-- query link: https://dune.com/queries/{query.base.query_id}\n\n\n{query.sql}') diff --git a/scripts/push_to_dune.py b/scripts/push_to_dune.py index f5ea6a3..f898205 100644 --- a/scripts/push_to_dune.py +++ b/scripts/push_to_dune.py @@ -22,11 +22,15 @@ for id in query_ids: query = dune.get_query(id) - print('updating query {}, {}'.format(query.base.query_id, query.base.name)) - - # Check if file exists - file_path = os.path.join(os.path.dirname(__file__), '..', 'queries', f'query_{query.base.query_id}.sql') - if os.path.exists(file_path): + print('PROCESSING: query {}, {}'.format(query.base.query_id, query.base.name)) + + # Check if query file exists in /queries folder + queries_path = os.path.join(os.path.dirname(__file__), '..', 'queries') + files = os.listdir(queries_path) + found_files = [file for file in files if str(id) == file.split('___')[-1].split('.')[0]] + + if len(found_files) != 0: + file_path = os.path.join(os.path.dirname(__file__), '..', 'queries', found_files[0]) # Read the content of the file with open(file_path, 'r', encoding='utf-8') as file: text = file.read() @@ -37,6 +41,7 @@ # All parameters below are optional query_sql=text, ) + print('SUCCESS: updated query {} to dune'.format(query.base.query_id)) else: - print('file not found, query_{}.sql'.format(query.base.query_id)) + print('ERROR: file not found, query id {}'.format(query.base.query_id))