Skip to content

Commit

Permalink
Add new dump subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
badboy committed Jan 24, 2023
1 parent 2d02152 commit cef4e43
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- FEATURE: New subcommand: `dump` to dump the metrics data as JSON ([#550](https://github.com/mozilla/glean_parser/pull/550))

## 6.4.0

- BUGFIX: Correct code generation for labeled metrics in Rust ([#533](https://github.com/mozilla/glean_parser/pull/533))
Expand Down
64 changes: 64 additions & 0 deletions glean_parser/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

"""Console script for glean_parser."""

import datetime
import io
from pathlib import Path
import sys

import click
import json


import glean_parser
Expand Down Expand Up @@ -180,6 +182,67 @@ def glinter(input, allow_reserved, allow_missing_files, require_tags):
)


@click.command()
@click.argument(
"input",
type=click.Path(exists=True, dir_okay=False, file_okay=True, readable=True),
nargs=-1,
)
@click.option(
"--allow-reserved",
is_flag=True,
help=(
"If provided, allow the use of reserved fields. "
"Should only be set when building the Glean library itself."
),
)
@click.option(
"--allow-missing-files",
is_flag=True,
help=("Do not treat missing input files as an error."),
)
@click.option(
"--require-tags",
is_flag=True,
help=("Require tags to be specified for metrics and pings."),
)
def dump(input, allow_reserved, allow_missing_files, require_tags):
"""
Dump the list of metrics/pings as JSON to stdout.
"""

results = glean_parser.parser.parse_objects(
[Path(x) for x in input],
{
"allow_reserved": allow_reserved,
"allow_missing_files": allow_missing_files,
"require_tags": require_tags,
},
)
errs = list(results)
assert len(errs) == 0

metrics = {
metric.identifier(): metric.serialize()
for category, probes in results.value.items()
for probe_name, metric in probes.items()
}

def date_serializer(o):
if isinstance(o, datetime.datetime):
return o.isoformat()

print(
json.dumps(
metrics,
sort_keys=True,
indent=2,
separators=(",", ": "),
default=date_serializer,
)
)


@click.command()
@click.option(
"-c",
Expand Down Expand Up @@ -261,6 +324,7 @@ def main(args=None):
main.add_command(translate)
main.add_command(check)
main.add_command(glinter)
main.add_command(dump)
main.add_command(coverage)
main.add_command(data_review_request, "data-review")

Expand Down

0 comments on commit cef4e43

Please sign in to comment.