Skip to content

Commit a01069f

Browse files
authored
refactor(sqlmesh): parse out columns used for metric naming and fix join condition (#5272)
1 parent 65fc9d5 commit a01069f

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

warehouse/oso_sqlmesh/models/marts/metrics/metrics_v0.sql

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,19 @@ MODEL (
1616
metric_id = 'The unique identifier for the metric, generated by OSO.',
1717
metric_source = 'The source of the metric, typically "OSO".',
1818
metric_namespace = 'The namespace grouping of the metric.',
19-
metric_name = 'The name of the metric, such as "S7_M1_devtooling_reward".',
20-
display_name = 'A human-readable name for the metric, such as "S7 M1 Dev Tooling Reward".',
19+
metric_name = 'The name of the metric, such as "GITHUB_stars_daily".',
20+
metric_model = 'The model of the metric, such as "stars".',
21+
metric_event_source = 'The event source of the metric, such as "GITHUB".',
22+
metric_time_aggregation = 'The time aggregation of the metric, such as "daily".',
23+
display_name = 'A human-readable name for the metric, such as "GitHub Stars Daily".',
2124
description = 'A description of the metric, providing context and details about what it measures.',
2225
rendered_sql = 'The SQL query used to compute the metric, if applicable.',
2326
sql_source_path = 'The path to the SQL file that defines the metric.'
2427
)
2528
);
2629

30+
@DEF(metric_regex_pattern, '^([A-Z0-9.()_]+)_([a-z0-9_]+)_(daily|weekly|monthly|quarterly|biannually|yearly|over_all_time)$');
31+
2732
WITH unioned_metric_names AS (
2833
SELECT
2934
*
@@ -53,47 +58,50 @@ all_timeseries_metric_names AS (
5358
SELECT DISTINCT
5459
metric
5560
FROM unioned_metric_names
56-
), all_metrics_metadata AS (
61+
),
62+
parsed_metric_names AS (
5763
SELECT
58-
metric,
64+
metric AS metric_name,
65+
REGEXP_EXTRACT(metric, @metric_regex_pattern,1) AS event_source,
66+
REGEXP_EXTRACT(metric, @metric_regex_pattern,2) AS metric_model,
67+
REGEXP_EXTRACT(metric, @metric_regex_pattern,3) AS time_aggregation
68+
FROM all_timeseries_metric_names
69+
),
70+
all_metrics_metadata AS (
71+
SELECT
72+
metric AS metric_model,
5973
display_name,
6074
description,
6175
sql_source_path,
6276
rendered_sql
6377
FROM oso.metrics_metadata
64-
), time_aggregations AS (
65-
SELECT
66-
time_aggregation
67-
FROM (VALUES
68-
('daily'),
69-
('weekly'),
70-
('monthly'),
71-
('quarterly'),
72-
('biannually'),
73-
('yearly'),
74-
('over_all_time')
75-
) AS t(time_aggregation)
76-
), metrics_v0_no_casting AS (
78+
),
79+
metrics_v0_no_casting AS (
7780
SELECT
78-
@oso_id('OSO', 'oso', t.metric) AS metric_id,
81+
@oso_id('OSO', 'oso', p.metric_name) AS metric_id,
7982
'OSO' AS metric_source,
8083
'oso' AS metric_namespace,
81-
t.metric AS metric_name,
82-
COALESCE(m.display_name, t.metric) AS display_name,
84+
p.metric_name AS metric_name,
85+
p.metric_model AS metric_model,
86+
p.event_source AS metric_event_source,
87+
p.time_aggregation AS metric_time_aggregation,
88+
COALESCE(m.display_name, p.metric_model) AS display_name,
8389
COALESCE(m.description, 'TODO') AS description,
8490
COALESCE(m.rendered_sql, []) AS rendered_sql,
8591
COALESCE(m.sql_source_path, 'TODO') AS sql_source_path,
8692
'UNKNOWN' AS aggregation_function
87-
FROM all_timeseries_metric_names AS t
88-
CROSS JOIN time_aggregations AS ta
93+
FROM parsed_metric_names AS p
8994
LEFT JOIN all_metrics_metadata AS m
90-
ON t.metric LIKE '%' || m.metric || '_' || ta.time_aggregation
95+
ON p.metric_model = m.metric_model
9196
)
9297
SELECT
9398
metric_id::VARCHAR,
9499
metric_source::VARCHAR,
95100
metric_namespace::VARCHAR,
96101
metric_name::VARCHAR,
102+
metric_model::VARCHAR,
103+
metric_event_source::VARCHAR,
104+
metric_time_aggregation::VARCHAR,
97105
display_name::VARCHAR,
98106
description::VARCHAR,
99107
rendered_sql,

0 commit comments

Comments
 (0)