Skip to content

Commit

Permalink
add name to queries
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewhong5297 committed Nov 29, 2023
1 parent 37c2ccd commit 134d1e6
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 16 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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).

---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
-- query name: DEX by volume 🏦
-- query link: https://dune.com/queries/3237721


WITH
seven_day_volume AS (
SELECT
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 16 additions & 6 deletions scripts/pull_from_dune.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,31 @@

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)
else:
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}')

Expand Down
17 changes: 11 additions & 6 deletions scripts/push_to_dune.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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))

0 comments on commit 134d1e6

Please sign in to comment.