Skip to content

Commit

Permalink
Add --denote-weekends (#54)
Browse files Browse the repository at this point in the history
This makes it easier to line up weekends and weekend lull in numbers.
  • Loading branch information
willkg committed Oct 11, 2023
1 parent b3e193d commit 3c14e64
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/crashstats_tools/cmd_supersearchfacet.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,29 @@ def convert_facet_data(facet_data, facet_name, total):
return {facet_name: records}


DATE_TEMPLATES = [
"%Y-%m-%d",
"%Y-%m-%dT%H:%M:%S+%z",
]


def is_weekend(value):
for template in DATE_TEMPLATES:
try:
dt = datetime.datetime.strptime(value, template)
return dt.weekday() in [5, 6]
except ValueError:
continue
return False


def fix_value(value, denote_weekends):
if denote_weekends and isinstance(value, str) and is_weekend(value):
return f"{value} **"

return str(value)


@click.command(
context_settings={"allow_extra_args": True, "ignore_unknown_options": True}
)
Expand Down Expand Up @@ -230,6 +253,13 @@ def convert_facet_data(facet_data, facet_name, total):
"when stdout is not an interactive terminal automatically"
),
)
@click.option(
"--denote-weekends/--no-denote-weekends",
default=False,
help=(
"This will add a * for values that are datestamps and on a Saturday or Sunday."
),
)
@click.pass_context
def supersearchfacet(
ctx,
Expand All @@ -241,6 +271,7 @@ def supersearchfacet(
format_type,
verbose,
color,
denote_weekends,
):
"""Fetches facet data from Crash Stats using Super Search
Expand Down Expand Up @@ -402,6 +433,11 @@ def supersearchfacet(
headers.remove(facet_name)
headers = [facet_name] + sorted(headers, key=thing_to_key)

records = [
{key: fix_value(val, denote_weekends) for key, val in record.items()}
for record in records
]

if not first_thing:
console.print()

Expand All @@ -412,7 +448,7 @@ def supersearchfacet(
table.add_column(column, justify="left")

for record in records:
table.add_row(*[str(record[header]) for header in headers])
table.add_row(*[record[header] for header in headers])
console.print(table)

elif format_type == "csv":
Expand Down
142 changes: 142 additions & 0 deletions tests/test_supersearchfacet.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,148 @@ def test_relative_date():
)


@freezegun.freeze_time("2022-07-01 12:00:00")
@responses.activate
def test_denote_weekends():
supersearch_data = {
"hits": [],
"total": 837148,
"facets": {
"histogram_date": [
{
"term": "2022-06-24T00:00:00+00:00",
"count": 130824,
"facets": {
"product": [
{"term": "Firefox", "count": 62097},
{"term": "Fenix", "count": 49585},
{"term": "Thunderbird", "count": 18202},
{"term": "Focus", "count": 938},
{"term": "MozillaVPN", "count": 1},
{"term": "ReferenceBrowser", "count": 1},
]
},
},
{
"term": "2022-06-25T00:00:00+00:00",
"count": 129534,
"facets": {
"product": [
{"term": "Firefox", "count": 60180},
{"term": "Fenix", "count": 49559},
{"term": "Thunderbird", "count": 18888},
{"term": "Focus", "count": 907},
]
},
},
{
"term": "2022-06-26T00:00:00+00:00",
"count": 127166,
"facets": {
"product": [
{"term": "Firefox", "count": 59486},
{"term": "Fenix", "count": 48276},
{"term": "Thunderbird", "count": 18532},
{"term": "Focus", "count": 871},
{"term": "MozillaVPN", "count": 1},
]
},
},
{
"term": "2022-06-27T00:00:00+00:00",
"count": 122340,
"facets": {
"product": [
{"term": "Firefox", "count": 55451},
{"term": "Fenix", "count": 49019},
{"term": "Thunderbird", "count": 16913},
{"term": "Focus", "count": 957},
]
},
},
{
"term": "2022-06-28T00:00:00+00:00",
"count": 99098,
"facets": {
"product": [
{"term": "Fenix", "count": 49089},
{"term": "Firefox", "count": 41220},
{"term": "Thunderbird", "count": 7858},
{"term": "Focus", "count": 931},
]
},
},
{
"term": "2022-06-29T00:00:00+00:00",
"count": 96583,
"facets": {
"product": [
{"term": "Fenix", "count": 50284},
{"term": "Firefox", "count": 38363},
{"term": "Thunderbird", "count": 7040},
{"term": "Focus", "count": 895},
{"term": "MozillaVPN", "count": 1},
]
},
},
{
"term": "2022-06-30T00:00:00+00:00",
"count": 131603,
"facets": {
"product": [
{"term": "Firefox", "count": 62530},
{"term": "Fenix", "count": 49355},
{"term": "Thunderbird", "count": 18934},
{"term": "Focus", "count": 784},
]
},
},
],
},
}

responses.add(
responses.GET,
DEFAULT_HOST + "/api/SuperSearch/",
match=[
responses.matchers.query_param_matcher(
{
"_histogram.date": "product",
"date": [">=2022-06-24", "<2022-07-01"],
"_results_number": "0",
}
),
],
status=200,
json=supersearch_data,
)
runner = CliRunner()
result = runner.invoke(
cli=cmd_supersearchfacet.supersearchfacet,
args=[
"--_histogram.date=product",
"--relative-range=7d",
"--denote-weekends",
"--format=tab",
],
env={"COLUMNS": "100"},
)
assert result.exit_code == 0
assert result.output == dedent(
"""\
product
histogram_date\t--\tFenix\tFirefox\tFocus\tMozillaVPN\tReferenceBrowser\tThunderbird\ttotal
2022-06-24\t0\t49585\t62097\t938\t1\t1\t18202\t130824
2022-06-25 **\t0\t49559\t60180\t907\t0\t0\t18888\t129534
2022-06-26 **\t0\t48276\t59486\t871\t1\t0\t18532\t127166
2022-06-27\t0\t49019\t55451\t957\t0\t0\t16913\t122340
2022-06-28\t0\t49089\t41220\t931\t0\t0\t7858\t99098
2022-06-29\t0\t50284\t38363\t895\t1\t0\t7040\t96583
2022-06-30\t0\t49355\t62530\t784\t0\t0\t18934\t131603
"""
)


@freezegun.freeze_time("2022-07-01 12:00:00")
@responses.activate
def test_supersearch_url():
Expand Down

0 comments on commit 3c14e64

Please sign in to comment.