diff --git a/src/crashstats_tools/cmd_supersearchfacet.py b/src/crashstats_tools/cmd_supersearchfacet.py index 8960359..1e31e6e 100644 --- a/src/crashstats_tools/cmd_supersearchfacet.py +++ b/src/crashstats_tools/cmd_supersearchfacet.py @@ -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 @@ -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: @@ -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}) @@ -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() diff --git a/tests/test_supersearchfacet.py b/tests/test_supersearchfacet.py index 0e8cfca..631d37e 100644 --- a/tests/test_supersearchfacet.py +++ b/tests/test_supersearchfacet.py @@ -64,6 +64,7 @@ def test_basic(): product\tcount Firefox\t5 Fenix\t4 + total\t19 --\t10 """ ) @@ -112,6 +113,7 @@ def test_host(): product\tcount Firefox\t5 Fenix\t4 + total\t19 --\t10 """ ) @@ -161,6 +163,7 @@ def test_token(): product\tcount Firefox\t5 Fenix\t4 + total\t19 --\t10 """ ) @@ -215,6 +218,7 @@ def test_dates(): product\tcount Firefox\t5 Fenix\t4 + total\t19 --\t10 """ ) @@ -267,6 +271,7 @@ def test_relative_date(): product\tcount Firefox\t5 Fenix\t4 + total\t19 --\t10 """ ) @@ -319,6 +324,7 @@ def test_supersearch_url(): product\tcount Firefox\t5 Fenix\t4 + total\t19 --\t10 """ ) @@ -367,6 +373,7 @@ def test_markdown(): ------- | ----- Firefox | 5 Fenix | 4 + total | 19 -- | 10 """ ) @@ -420,6 +427,10 @@ def test_json(): "product": "Fenix", "count": 4 }, + { + "product": "total", + "count": 19 + }, { "product": "--", "count": 10 @@ -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 """ ) @@ -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 """ ) @@ -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 } ] """ @@ -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 """ )