Skip to content

Commit

Permalink
Metrics By Project / Collection and different aggregations (#2125)
Browse files Browse the repository at this point in the history
* Testing different factory

* nasty work in progress

* project aggregation working

* Remove test file

* collections working

* Collections and joining views

* rename rollup

* clean up

* getting the active days in there

* updated current models

* reenable downstream models

* some fixes

* fixes
  • Loading branch information
ravenac95 authored Sep 11, 2024
1 parent ed27e16 commit 6b26241
Show file tree
Hide file tree
Showing 39 changed files with 2,110 additions and 559 deletions.
273 changes: 0 additions & 273 deletions warehouse/metrics_mesh/lib/factories/factory.py

This file was deleted.

45 changes: 45 additions & 0 deletions warehouse/metrics_mesh/macros/daily_bucket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from sqlmesh import macro
from sqlmesh.core.macros import MacroEvaluator
from sqlglot import expressions as exp


@macro()
def daily_bucket(evaluator: MacroEvaluator, timeExp: exp.Expression):
if evaluator.dialect == "duckdb":
return exp.Anonymous(
this="TIME_BUCKET",
expressions=[
exp.Interval(
this=exp.Literal(this=1, is_string=True),
unit=exp.Var(this="DAY"),
),
timeExp,
],
)
return exp.Anonymous(
this="toStartOfDay",
expressions=[
timeExp,
],
)


@macro()
def weekly_bucket(evaluator: MacroEvaluator, timeExp: exp.Expression):
if evaluator.dialect == "duckdb":
return exp.Anonymous(
this="TIME_BUCKET",
expressions=[
exp.Interval(
this=exp.Literal(this=1, is_string=True),
unit=exp.Var(this="WEEK"),
),
timeExp,
],
)
return exp.Anonymous(
this="toStartOfWeek",
expressions=[
timeExp,
],
)
43 changes: 43 additions & 0 deletions warehouse/metrics_mesh/macros/monthly_bucket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from sqlmesh import macro
from sqlmesh.core.macros import MacroEvaluator
from sqlglot import expressions as exp


@macro()
def time_aggregation_bucket(
evaluator: MacroEvaluator, timeExp: exp.Expression, rollup: str
):
from sqlmesh.core.dialect import parse_one

if evaluator.runtime_stage in ["loading", "creating"]:
return parse_one("STR_TO_DATE('1970-01-01', '%Y-%m-%d')")
if evaluator.engine_adapter.dialect == "duckdb":
rollup_to_interval = {
"daily": "DAY",
"weekly": "WEEK",
"monthly": "MONTH",
}
return exp.Anonymous(
this="TIME_BUCKET",
expressions=[
exp.Interval(
this=exp.Literal(this=1, is_string=False),
unit=exp.Var(this=rollup_to_interval[rollup]),
),
exp.Cast(
this=timeExp,
to=exp.DataType(this=exp.DataType.Type.DATE, nested=False),
),
],
)
rollup_to_clickhouse_function = {
"daily": "toStartOfDay",
"weekly": "toStartOfWeek",
"monthly": "toStartOfMonth",
}
return exp.Anonymous(
this=rollup_to_clickhouse_function[rollup],
expressions=[
timeExp,
],
)
9 changes: 7 additions & 2 deletions warehouse/metrics_mesh/macros/oso_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@


@macro()
def oso_id(_evaluator: MacroEvaluator, *args: exp.Expression):
return exp.SHA2(
def oso_id(evaluator: MacroEvaluator, *args: exp.Expression):
sha = exp.SHA2(
this=exp.Concat(expressions=args, safe=True, coalesce=False),
length=exp.Literal(this=256, is_string=False),
)
if evaluator.runtime_stage in ["loading", "creating"]:
return exp.Literal(this="", is_string=True)
if evaluator.engine_adapter.dialect == "duckdb":
return sha
return exp.ToBase64(this=sha)
Loading

0 comments on commit 6b26241

Please sign in to comment.