Skip to content

Instantly share code, notes, and snippets.

@elijahbenizzy
Created September 15, 2022 00:19
Show Gist options
  • Save elijahbenizzy/0bda541a89da88c4400309273b3990e3 to your computer and use it in GitHub Desktop.
Save elijahbenizzy/0bda541a89da88c4400309273b3990e3 to your computer and use it in GitHub Desktop.
Metrics Layer using Hamilton
### metrics_layer.py
import pandas as pd
from hamilton import driver
def workspaces(db: str, table: str='workspaces') -> pd.DataFrame:
return query("SELECT * from {db}.{table}".format(db))
def count_workspaces(workspaces: pd.DataFrame, grain: str) -> pd.Series:
"""Pandas code that does...
1. Group by grain
2. Count(distinct)
3. Return time-series of workspace counts
"""
return # the pandas code described above
def count_active_workspaces(workspaces: pd.DataFrame, grain: str) -> pd.Series:
"""Pandas code that does...
1. Filter to active
2. Group by grain
3. Return time-series of active workspace counts
"""
return # the pandas code described above
def activation_rate(count_workspaces: pd.Series, count_active_workspaces: pd.Series) -> pd.Series:
return 100*count_workspaces/count_active_workspaces
### driver.py
driver = driver.Driver(config={'db' : ...})
daily_activation_rate = driver.execute(['activation_rate'], inputs={'grain': 'day'})
weekly_activation_rate = driver.execute(['activation_rate'], inputs={'grain': 'week'})
monthly_activation_rate = driver.execute(['activation_rate'], inputs={'grain': 'month'})
# There is another approach or two that put them all into the same execution context,
# But this was clearer to demonstrate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment