santiment / sanpy

Santiment API Python Client
MIT License
96 stars 29 forks source link

Add execute_sql function #170

Closed IvanIvanoff closed 1 year ago

IvanIvanoff commented 1 year ago

Add helper functions to easily execute SQL:

san.execute_sql(query="""
  SELECT
    get_metric_name(metric_id) AS metric,
    get_asset_name(asset_id) AS asset,
    dt,
    argMax(value, computed_at)
  FROM daily_metrics_v2
  WHERE
    asset_id = get_asset_id({{slug}}) AND
    metric_id = get_metric_id({{metric}}) AND
    dt >= now() - INTERVAL {{last_n_days}} DAY
  GROUP BY dt, metric_id, asset_id
  ORDER BY dt ASC
""",
parameters={'slug': 'bitcoin', 'metric': 'daily_active_addresses', 'last_n_days': 7},
set_index="dt")
dt                                         metric   asset  value                     
2023-03-22T00:00:00Z  daily_active_addresses  bitcoin                    941446.0
2023-03-23T00:00:00Z  daily_active_addresses  bitcoin                    913215.0
2023-03-24T00:00:00Z  daily_active_addresses  bitcoin                    884271.0
2023-03-25T00:00:00Z  daily_active_addresses  bitcoin                    906851.0
2023-03-26T00:00:00Z  daily_active_addresses  bitcoin                    835596.0
2023-03-27T00:00:00Z  daily_active_addresses  bitcoin                   1052637.0
2023-03-28T00:00:00Z  daily_active_addresses  bitcoin                    311566.0