Reapply "Have Glean stable views parse datetime metrics as timestamps (bug 1771733) (#3343)" (#3453)

This reverts commit c43bb6c608.
This commit is contained in:
Sean Rose 2022-12-12 16:54:30 -08:00 коммит произвёл GitHub
Родитель a2ec2afeec
Коммит 756f4aea23
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 61 добавлений и 2 удалений

Просмотреть файл

Просмотреть файл

@ -0,0 +1,6 @@
---
description: |
Parses a Glean datetime metric string value as a BigQuery timestamp.
See https://mozilla.github.io/glean/book/reference/metrics/datetime.html
friendly_name: Parse Datetime

Просмотреть файл

@ -0,0 +1,30 @@
CREATE OR REPLACE FUNCTION glean.parse_datetime(datetime_string STRING)
RETURNS TIMESTAMP AS (
COALESCE(
SAFE.PARSE_TIMESTAMP('%FT%H:%M:%E*S%Ez', datetime_string),
SAFE.PARSE_TIMESTAMP('%FT%H:%M%Ez', datetime_string),
SAFE.PARSE_TIMESTAMP('%FT%H%Ez', datetime_string),
SAFE.PARSE_TIMESTAMP('%F%Ez', datetime_string)
)
);
-- Tests
SELECT
assert.equals(
TIMESTAMP '2000-01-02 03:04:05.123456',
glean.parse_datetime('2000-01-02T03:04:05.123456789+00:00')
),
assert.equals(
TIMESTAMP '2000-01-02 03:04:05.123456',
glean.parse_datetime('2000-01-02T03:04:05.123456+00:00')
),
assert.equals(
TIMESTAMP '2000-01-02 03:04:05.123',
glean.parse_datetime('2000-01-02T03:04:05.123+00:00')
),
assert.equals(TIMESTAMP '2000-01-02 03:04:05', glean.parse_datetime('2000-01-02T03:04:05+00:00')),
assert.equals(TIMESTAMP '2000-01-02 09:04:05', glean.parse_datetime('2000-01-02T03:04:05-06:00')),
assert.equals(TIMESTAMP '2000-01-02 03:04:00', glean.parse_datetime('2000-01-02T03:04+00:00')),
assert.equals(TIMESTAMP '2000-01-02 03:00:00', glean.parse_datetime('2000-01-02T03+00:00')),
assert.equals(TIMESTAMP '2000-01-02 00:00:00', glean.parse_datetime('2000-01-02+00:00')),
assert.null(glean.parse_datetime('2000-01-02 00:00:00 Z'))

Просмотреть файл

@ -139,11 +139,34 @@ def write_view_if_not_exists(target_project: str, sql_dir: Path, schema: SchemaF
and schema.bq_table == "metrics_v1"
):
# todo: use mozfun udfs
replacements += [
metrics_source = (
"mozdata.udf.normalize_fenix_metrics"
"(client_info.telemetry_sdk_build, metrics)"
" AS metrics"
)
else:
metrics_source = "metrics"
if metrics_datetime_fields := [
metrics_datetime_field["name"]
for field in schema.schema
if field["name"] == "metrics"
for metrics_field in field["fields"]
if metrics_field["name"] == "datetime"
for metrics_datetime_field in metrics_field["fields"]
]:
replacements += [
f"(SELECT AS STRUCT {metrics_source}.* REPLACE (STRUCT("
+ ", ".join(
field_select
for field in metrics_datetime_fields
for field_select in (
f"mozfun.glean.parse_datetime(metrics.datetime.{field}) AS {field}",
f"metrics.datetime.{field} AS raw_{field}",
)
)
+ ") AS datetime)) AS metrics"
]
elif metrics_source != "metrics":
replacements += [f"{metrics_source} AS metrics"]
if schema.bq_dataset_family == "firefox_desktop":
# FOG does not provide an app_name, so we inject the one that
# people already associate with desktop Firefox per bug 1672191.