Skip to content

Commit

Permalink
Merge pull request #87 from willkg/facet-tweaks
Browse files Browse the repository at this point in the history
Add "total" column and fix rendereing column headers for numbers
  • Loading branch information
willkg authored Aug 14, 2023
2 parents 31189f4 + 543989c commit 75d47de
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 25 deletions.
26 changes: 22 additions & 4 deletions src/crashstats_tools/cmd_supersearchfacet.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,24 @@ def __lt__(self, other):
return True


@functools.total_ordering
class AlwaysLast:
def __eq__(self, other):
# Two AlwaysLast instances are always equal
return type(other) == type(self)

def __lt__(self, other):
# This is always greater than other
return False


def thing_to_key(item):
if isinstance(item, (list, tuple)):
item = item[0]
if item == "--":
return AlwaysFirst()
if item == "total":
return AlwaysLast()
return item


Expand Down Expand Up @@ -293,7 +306,7 @@ def supersearchfacet(

facet_name = params["_facets"][0]

remaining = facet_data["total"]
total = facet_data["total"]
facets = facet_data["facets"]

if facet_name not in facets:
Expand All @@ -305,8 +318,10 @@ def supersearchfacet(
{facet_name: item["term"], "count": item["count"]}
for item in sorted(facet_item_data, key=lambda x: x["count"], reverse=True)
]
remaining -= sum([item["count"] for item in facet_item_data])

records.append({facet_name: "total", "count": total})

remaining = total - sum([item["count"] for item in facet_item_data])
if remaining:
records.append({facet_name: "--", "count": remaining})

Expand Down Expand Up @@ -360,14 +375,17 @@ def supersearchfacet(
verbose=verbose,
)

remaining = facet_data["total"]
total = remaining = facet_data["total"]
facets = facet_data["facets"]

for item in facets.get(facet_name, []):
count = item["count"]
facet_tables[facet_name].setdefault(day_start, {})[item["term"]] = count
facet_tables[facet_name].setdefault(day_start, {})[
str(item["term"])
] = count
remaining -= count
facet_tables[facet_name].setdefault(day_start, {})["--"] = remaining
facet_tables[facet_name].setdefault(day_start, {})["total"] = total

# Normalize the data--make sure table rows have all the values
values = set()
Expand Down
57 changes: 36 additions & 21 deletions tests/test_supersearchfacet.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def test_basic():
product\tcount
Firefox\t5
Fenix\t4
total\t19
--\t10
"""
)
Expand Down Expand Up @@ -112,6 +113,7 @@ def test_host():
product\tcount
Firefox\t5
Fenix\t4
total\t19
--\t10
"""
)
Expand Down Expand Up @@ -161,6 +163,7 @@ def test_token():
product\tcount
Firefox\t5
Fenix\t4
total\t19
--\t10
"""
)
Expand Down Expand Up @@ -215,6 +218,7 @@ def test_dates():
product\tcount
Firefox\t5
Fenix\t4
total\t19
--\t10
"""
)
Expand Down Expand Up @@ -267,6 +271,7 @@ def test_relative_date():
product\tcount
Firefox\t5
Fenix\t4
total\t19
--\t10
"""
)
Expand Down Expand Up @@ -319,6 +324,7 @@ def test_supersearch_url():
product\tcount
Firefox\t5
Fenix\t4
total\t19
--\t10
"""
)
Expand Down Expand Up @@ -367,6 +373,7 @@ def test_markdown():
------- | -----
Firefox | 5
Fenix | 4
total | 19
-- | 10
"""
)
Expand Down Expand Up @@ -420,6 +427,10 @@ def test_json():
"product": "Fenix",
"count": 4
},
{
"product": "total",
"count": 19
},
{
"product": "--",
"count": 10
Expand Down Expand Up @@ -550,11 +561,11 @@ def test_period_daily():
assert result.exit_code == 0
assert result.output == dedent(
"""\
date\t--\tFenix\tFirefox
2022-06-28 00:00:00\t10\t4\t5
2022-06-29 00:00:00\t10\t4\t4
2022-06-30 00:00:00\t10\t3\t6
2022-07-01 00:00:00\t12\t3\t7
date\t--\tFenix\tFirefox\ttotal
2022-06-28 00:00:00\t10\t4\t5\t19
2022-06-29 00:00:00\t10\t4\t4\t18
2022-06-30 00:00:00\t10\t3\t6\t19
2022-07-01 00:00:00\t12\t3\t7\t22
"""
)

Expand All @@ -573,12 +584,12 @@ def test_period_daily():
assert result.exit_code == 0
assert result.output == dedent(
"""\
date | -- | Fenix | Firefox
---- | -- | ----- | -------
2022-06-28 00:00:00 | 10 | 4 | 5
2022-06-29 00:00:00 | 10 | 4 | 4
2022-06-30 00:00:00 | 10 | 3 | 6
2022-07-01 00:00:00 | 12 | 3 | 7
date | -- | Fenix | Firefox | total
---- | -- | ----- | ------- | -----
2022-06-28 00:00:00 | 10 | 4 | 5 | 19
2022-06-29 00:00:00 | 10 | 4 | 4 | 18
2022-06-30 00:00:00 | 10 | 3 | 6 | 19
2022-07-01 00:00:00 | 12 | 3 | 7 | 22
"""
)

Expand All @@ -602,25 +613,29 @@ def test_period_daily():
"date": "2022-06-28 00:00:00",
"--": 10,
"Fenix": 4,
"Firefox": 5
"Firefox": 5,
"total": 19
},
{
"date": "2022-06-29 00:00:00",
"--": 10,
"Fenix": 4,
"Firefox": 4
"Firefox": 4,
"total": 18
},
{
"date": "2022-06-30 00:00:00",
"--": 10,
"Fenix": 3,
"Firefox": 6
"Firefox": 6,
"total": 19
},
{
"date": "2022-07-01 00:00:00",
"--": 12,
"Fenix": 3,
"Firefox": 7
"Firefox": 7,
"total": 22
}
]
"""
Expand Down Expand Up @@ -774,11 +789,11 @@ def test_period_weekly():
assert result.exit_code == 0
assert result.output == dedent(
"""\
date\t--\tFenix\tFirefox
2022-06-01 00:00:00\t12\t3\t7
2022-06-08 00:00:00\t10\t3\t6
2022-06-15 00:00:00\t10\t4\t4
2022-06-22 00:00:00\t10\t4\t5
2022-06-29 00:00:00\t10\t4\t5
date\t--\tFenix\tFirefox\ttotal
2022-06-01 00:00:00\t12\t3\t7\t22
2022-06-08 00:00:00\t10\t3\t6\t19
2022-06-15 00:00:00\t10\t4\t4\t18
2022-06-22 00:00:00\t10\t4\t5\t19
2022-06-29 00:00:00\t10\t4\t5\t19
"""
)

0 comments on commit 75d47de

Please sign in to comment.