Skip to content

Commit

Permalink
Update bigquery name to be in the metadata script
Browse files Browse the repository at this point in the history
  • Loading branch information
robhudson committed Jan 13, 2021
1 parent 9c49d3f commit 7239dcd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
27 changes: 27 additions & 0 deletions scripts/build-glean-metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import json
import os
import re

import glean
import stringcase
Expand All @@ -15,6 +16,31 @@ def _serialize_sets(obj):
return obj


# ETL specific snakecase taken from:
# https://github.com/mozilla/bigquery-etl/blob/master/bigquery_etl/util/common.py
#
# Search for all camelCase situations in reverse with arbitrary lookaheads.
REV_WORD_BOUND_PAT = re.compile(
r"""
\b # standard word boundary
|(?<=[a-z][A-Z])(?=\d*[A-Z]) # A7Aa -> A7|Aa boundary
|(?<=[a-z][A-Z])(?=\d*[a-z]) # a7Aa -> a7|Aa boundary
|(?<=[A-Z])(?=\d*[a-z]) # a7A -> a7|A boundary
""",
re.VERBOSE,
)


def etl_snake_case(line: str) -> str:
"""Convert a string into a snake_cased string."""
# replace non-alphanumeric characters with spaces in the reversed line
subbed = re.sub(r"[^\w]|_", " ", line[::-1])
# apply the regex on the reversed string
words = REV_WORD_BOUND_PAT.split(subbed)
# filter spaces between words and snake_case and reverse again
return "_".join([w.lower() for w in words if w.strip()])[::-1]


# First, get the apps we're using
apps = [app for app in glean.GleanApp.get_apps()]

Expand Down Expand Up @@ -78,6 +104,7 @@ def _serialize_sets(obj):
stable_ping_table_names=stable_ping_table_names,
metric_type=metric_type,
metric_table_name=f"metrics.{metric_type}.{metric_name_snakecase}",
glam_etl_name=etl_snake_case(metric.identifier),
)

open(os.path.join(app_metrics_dir, f"{metric.identifier}.json"), "w").write(
Expand Down
33 changes: 9 additions & 24 deletions src/pages/MetricDetail.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
const metricDataPromise = getMetricData(params.app, metricName);
function getGlamUrl(app, metric) {
function getGlamUrlTemplate(app) {
const map = {
fenix: {
product: "fenix",
Expand All @@ -28,34 +28,16 @@
};
if (Object.keys(map).includes(app)) {
const p = map[app];
return `https://glam.telemetry.mozilla.org/${p.product}/probe/${snakeCase(metric)}/explore?app_id=${p.app_id}`;
return function makeGlamUrl(metric) {
return `https://glam.telemetry.mozilla.org/${p.product}/probe/${metric}/explore?app_id=${p.app_id}`;
};
}
// The app isn't one GLAM supports so return nothing.
return null;
}
/*
* Regular expression description (since JS doesn't have an ignore whitespace flag):
* \b+ // Standard word boundary
* |(?<=[a-z][A-Z])(?=\d*[A-Z]) // A7Aa -> A7|Aa boundary
* |(?<=[a-z][A-Z])(?=\d*[a-z]) // a7Aa -> a7|Aa boundary
* |(?<=[A-Z])(?=\d*[a-z]) // a7A -> a7|A boundary
*/
function snakeCase(str) {
// Convert a string into a snake_cased string.
// Replace non-alphanumeric characters with spaces in the reversed string.
const subbed = str.split('').reverse().join('').replace(/[^\w]|_/g, ' ');
// Apply the regexp on the reversed string.
const words = subbed.split(/\b|(?<=[a-z][A-Z])(?=\d*[A-Z])|(?<=[a-z][A-Z])(?=\d*[a-z])|(?<=[A-Z])(?=\d*[a-z])/);
// Filter spaces between words and snake_case.
const filtered = words.filter((w) => w.trim() !== '').map((w) => w.toLowerCase()).join('_');
// Return string reversed again.
return filtered.split('').reverse().join('')
}
const glamUrl = getGlamUrl(params.app, metricName);
const glamUrl = getGlamUrlTemplate(params.app);
function getMetricDocumentationURI(type) {
const sourceDocs = "https://mozilla.github.io/glean/book/user/metrics/";
Expand Down Expand Up @@ -258,7 +240,10 @@
{#if glamUrl}
<tr>
<td>GLAM</td>
<td><a href={glamUrl}>{glamUrl}</a></td>
<td>
<a
href={glamUrl(metric.bigquery_names.glam_etl_name)}>{glamUrl(metric.bigquery_names.glam_etl_name)}</a>
</td>
</tr>
{/if}
</table>
Expand Down

0 comments on commit 7239dcd

Please sign in to comment.