rename Ammo.get_average_cost and Ammo.get_historical_count

This commit is contained in:
shibao 2023-06-05 22:48:58 -04:00
parent 9edeb1e803
commit a35f43d6df
6 changed files with 46 additions and 50 deletions

View File

@ -132,17 +132,17 @@ defmodule Cannery.Ammo do
## Examples ## Examples
iex> get_average_cost_for_type( iex> get_average_cost(
...> %Type{id: 123, user_id: 456}, ...> %Type{id: 123, user_id: 456},
...> %User{id: 456} ...> %User{id: 456}
...> ) ...> )
1.50 1.50
""" """
@spec get_average_cost_for_type(Type.t(), User.t()) :: float() | nil @spec get_average_cost(Type.t(), User.t()) :: float() | nil
def get_average_cost_for_type(%Type{id: type_id} = type, user) do def get_average_cost(%Type{id: type_id} = type, user) do
[type] [type]
|> get_average_cost_for_types(user) |> get_average_costs(user)
|> Map.get(type_id) |> Map.get(type_id)
end end
@ -152,16 +152,16 @@ defmodule Cannery.Ammo do
## Examples ## Examples
iex> get_average_cost_for_types( iex> get_average_costs(
...> [%Type{id: 123, user_id: 456}], ...> [%Type{id: 123, user_id: 456}],
...> %User{id: 456} ...> %User{id: 456}
...> ) ...> )
1.50 1.50
""" """
@spec get_average_cost_for_types([Type.t()], User.t()) :: @spec get_average_costs([Type.t()], User.t()) ::
%{optional(Type.id()) => float()} %{optional(Type.id()) => float()}
def get_average_cost_for_types(types, %User{id: user_id}) do def get_average_costs(types, %User{id: user_id}) do
type_ids = type_ids =
types types
|> Enum.map(fn %Type{id: type_id, user_id: ^user_id} -> type_id end) |> Enum.map(fn %Type{id: type_id, user_id: ^user_id} -> type_id end)
@ -289,17 +289,17 @@ defmodule Cannery.Ammo do
## Examples ## Examples
iex> get_historical_count_for_type( iex> get_historical_count(
...> %Type{id: 123, user_id: 456}, ...> %Type{id: 123, user_id: 456},
...> %User{id: 456} ...> %User{id: 456}
...> ) ...> )
5 5
""" """
@spec get_historical_count_for_type(Type.t(), User.t()) :: non_neg_integer() @spec get_historical_count(Type.t(), User.t()) :: non_neg_integer()
def get_historical_count_for_type(%Type{id: type_id} = type, user) do def get_historical_count(%Type{id: type_id} = type, user) do
[type] [type]
|> get_historical_count_for_types(user) |> get_historical_counts(user)
|> Map.get(type_id, 0) |> Map.get(type_id, 0)
end end
@ -308,16 +308,16 @@ defmodule Cannery.Ammo do
## Examples ## Examples
iex> get_historical_count_for_types( iex> get_historical_counts(
...> [%Type{id: 123, user_id: 456}], ...> [%Type{id: 123, user_id: 456}],
...> %User{id: 456} ...> %User{id: 456}
...> ) ...> )
%{123 => 5} %{123 => 5}
""" """
@spec get_historical_count_for_types([Type.t()], User.t()) :: @spec get_historical_counts([Type.t()], User.t()) ::
%{optional(Type.id()) => non_neg_integer()} %{optional(Type.id()) => non_neg_integer()}
def get_historical_count_for_types(types, %User{id: user_id} = user) do def get_historical_counts(types, %User{id: user_id} = user) do
used_counts = ActivityLog.get_grouped_used_counts(user, types: types, group_by: :type_id) used_counts = ActivityLog.get_grouped_used_counts(user, types: types, group_by: :type_id)
round_counts = get_grouped_round_count(user, types: types, group_by: :type_id) round_counts = get_grouped_round_count(user, types: types, group_by: :type_id)

View File

@ -153,13 +153,13 @@ defmodule CanneryWeb.Components.TypeTableComponent do
round_counts = Ammo.get_grouped_round_count(current_user, types: types, group_by: :type_id) round_counts = Ammo.get_grouped_round_count(current_user, types: types, group_by: :type_id)
packs_count = Ammo.get_grouped_packs_count(current_user, types: types, group_by: :type_id) packs_count = Ammo.get_grouped_packs_count(current_user, types: types, group_by: :type_id)
average_costs = types |> Ammo.get_average_cost_for_types(current_user) average_costs = Ammo.get_average_costs(types, current_user)
[used_counts, historical_round_counts, historical_pack_counts, used_pack_counts] = [used_counts, historical_round_counts, historical_pack_counts, used_pack_counts] =
if show_used do if show_used do
[ [
ActivityLog.get_grouped_used_counts(current_user, types: types, group_by: :type_id), ActivityLog.get_grouped_used_counts(current_user, types: types, group_by: :type_id),
types |> Ammo.get_historical_count_for_types(current_user), Ammo.get_historical_counts(types, current_user),
Ammo.get_grouped_packs_count(current_user, Ammo.get_grouped_packs_count(current_user,
types: types, types: types,
group_by: :type_id, group_by: :type_id,

View File

@ -14,7 +14,7 @@ defmodule CanneryWeb.ExportController do
total_pack_counts = total_pack_counts =
Ammo.get_grouped_packs_count(current_user, types: types, group_by: :type_id, show_used: true) Ammo.get_grouped_packs_count(current_user, types: types, group_by: :type_id, show_used: true)
average_costs = types |> Ammo.get_average_cost_for_types(current_user) average_costs = Ammo.get_average_costs(types, current_user)
types = types =
types types

View File

@ -69,7 +69,7 @@ defmodule CanneryWeb.TypeLive.Show do
Ammo.get_packs_count(current_user, type_id: type.id, show_used: :only_used), Ammo.get_packs_count(current_user, type_id: type.id, show_used: :only_used),
Ammo.get_packs_count(current_user, type_id: type.id, show_used: true), Ammo.get_packs_count(current_user, type_id: type.id, show_used: true),
ActivityLog.get_used_count(current_user, type_id: type.id), ActivityLog.get_used_count(current_user, type_id: type.id),
type |> Ammo.get_historical_count_for_type(current_user) Ammo.get_historical_count(type, current_user)
] ]
else else
[nil, nil, nil, nil, nil] [nil, nil, nil, nil, nil]
@ -94,7 +94,7 @@ defmodule CanneryWeb.TypeLive.Show do
containers: containers, containers: containers,
cprs: packs |> Ammo.get_cprs(current_user), cprs: packs |> Ammo.get_cprs(current_user),
last_used_dates: packs |> ActivityLog.get_last_used_dates(current_user), last_used_dates: packs |> ActivityLog.get_last_used_dates(current_user),
avg_cost_per_round: type |> Ammo.get_average_cost_for_type(current_user), avg_cost_per_round: Ammo.get_average_cost(type, current_user),
rounds: Ammo.get_round_count(current_user, type_id: type.id), rounds: Ammo.get_round_count(current_user, type_id: type.id),
original_counts: original_counts, original_counts: original_counts,
used_rounds: used_rounds, used_rounds: used_rounds,

View File

@ -222,7 +222,7 @@ defmodule Cannery.AmmoTest do
] ]
end end
test "get_average_cost_for_type/2 gets average cost for type", test "get_average_cost/2 gets average cost for type",
%{type: type, current_user: current_user, container: container} do %{type: type, current_user: current_user, container: container} do
{1, [_pack]} = {1, [_pack]} =
pack_fixture( pack_fixture(
@ -232,7 +232,7 @@ defmodule Cannery.AmmoTest do
current_user current_user
) )
assert 25.0 = Ammo.get_average_cost_for_type(type, current_user) assert 25.0 = Ammo.get_average_cost(type, current_user)
{1, [_pack]} = {1, [_pack]} =
pack_fixture( pack_fixture(
@ -242,7 +242,7 @@ defmodule Cannery.AmmoTest do
current_user current_user
) )
assert 25.0 = Ammo.get_average_cost_for_type(type, current_user) assert 25.0 = Ammo.get_average_cost(type, current_user)
{1, [_pack]} = {1, [_pack]} =
pack_fixture( pack_fixture(
@ -252,7 +252,7 @@ defmodule Cannery.AmmoTest do
current_user current_user
) )
assert 40.0 = Ammo.get_average_cost_for_type(type, current_user) assert 40.0 = Ammo.get_average_cost(type, current_user)
{1, [_pack]} = {1, [_pack]} =
pack_fixture( pack_fixture(
@ -262,21 +262,21 @@ defmodule Cannery.AmmoTest do
current_user current_user
) )
assert 37.5 = Ammo.get_average_cost_for_type(type, current_user) assert 37.5 = Ammo.get_average_cost(type, current_user)
end end
test "get_average_cost_for_types/2 gets average costs for types", %{ test "get_average_costs/2 gets average costs for types", %{
type: %{id: type_id} = type, type: %{id: type_id} = type,
current_user: current_user, current_user: current_user,
container: container container: container
} do } do
assert %{} == [type] |> Ammo.get_average_cost_for_types(current_user) assert %{} == [type] |> Ammo.get_average_costs(current_user)
%{id: another_type_id} = another_type = type_fixture(current_user) %{id: another_type_id} = another_type = type_fixture(current_user)
assert %{} == assert %{} ==
[type, another_type] [type, another_type]
|> Ammo.get_average_cost_for_types(current_user) |> Ammo.get_average_costs(current_user)
{1, [_pack]} = {1, [_pack]} =
pack_fixture( pack_fixture(
@ -288,7 +288,7 @@ defmodule Cannery.AmmoTest do
assert %{another_type_id => 25.0} == assert %{another_type_id => 25.0} ==
[type, another_type] [type, another_type]
|> Ammo.get_average_cost_for_types(current_user) |> Ammo.get_average_costs(current_user)
{1, [_pack]} = {1, [_pack]} =
pack_fixture( pack_fixture(
@ -298,7 +298,7 @@ defmodule Cannery.AmmoTest do
current_user current_user
) )
average_costs = [type, another_type] |> Ammo.get_average_cost_for_types(current_user) average_costs = [type, another_type] |> Ammo.get_average_costs(current_user)
assert %{^type_id => 25.0} = average_costs assert %{^type_id => 25.0} = average_costs
assert %{^another_type_id => 25.0} = average_costs assert %{^another_type_id => 25.0} = average_costs
@ -311,7 +311,7 @@ defmodule Cannery.AmmoTest do
current_user current_user
) )
average_costs = [type, another_type] |> Ammo.get_average_cost_for_types(current_user) average_costs = [type, another_type] |> Ammo.get_average_costs(current_user)
assert %{^type_id => 25.0} = average_costs assert %{^type_id => 25.0} = average_costs
assert %{^another_type_id => 25.0} = average_costs assert %{^another_type_id => 25.0} = average_costs
@ -324,7 +324,7 @@ defmodule Cannery.AmmoTest do
current_user current_user
) )
average_costs = [type, another_type] |> Ammo.get_average_cost_for_types(current_user) average_costs = [type, another_type] |> Ammo.get_average_costs(current_user)
assert %{^type_id => 40.0} = average_costs assert %{^type_id => 40.0} = average_costs
assert %{^another_type_id => 25.0} = average_costs assert %{^another_type_id => 25.0} = average_costs
@ -337,7 +337,7 @@ defmodule Cannery.AmmoTest do
current_user current_user
) )
average_costs = [type, another_type] |> Ammo.get_average_cost_for_types(current_user) average_costs = [type, another_type] |> Ammo.get_average_costs(current_user)
assert %{^type_id => 37.5} = average_costs assert %{^type_id => 37.5} = average_costs
assert %{^another_type_id => 25.0} = average_costs assert %{^another_type_id => 25.0} = average_costs
@ -408,68 +408,64 @@ defmodule Cannery.AmmoTest do
assert %{^another_type_id => 1} = round_counts assert %{^another_type_id => 1} = round_counts
end end
test "get_historical_count_for_type/2 gets accurate total round count for type", test "get_historical_count/2 gets accurate total round count for type",
%{type: type, current_user: current_user, container: container} do %{type: type, current_user: current_user, container: container} do
assert 0 = Ammo.get_historical_count_for_type(type, current_user) assert 0 = Ammo.get_historical_count(type, current_user)
{1, [first_pack]} = pack_fixture(%{count: 1}, type, container, current_user) {1, [first_pack]} = pack_fixture(%{count: 1}, type, container, current_user)
assert 1 = Ammo.get_historical_count_for_type(type, current_user) assert 1 = Ammo.get_historical_count(type, current_user)
{1, [pack]} = pack_fixture(%{count: 50}, type, container, current_user) {1, [pack]} = pack_fixture(%{count: 50}, type, container, current_user)
assert 51 = Ammo.get_historical_count_for_type(type, current_user) assert 51 = Ammo.get_historical_count(type, current_user)
shot_record_fixture(%{count: 26}, current_user, pack) shot_record_fixture(%{count: 26}, current_user, pack)
assert 51 = Ammo.get_historical_count_for_type(type, current_user) assert 51 = Ammo.get_historical_count(type, current_user)
shot_record_fixture(%{count: 1}, current_user, first_pack) shot_record_fixture(%{count: 1}, current_user, first_pack)
assert 51 = Ammo.get_historical_count_for_type(type, current_user) assert 51 = Ammo.get_historical_count(type, current_user)
end end
test "get_historical_count_for_types/2 gets accurate total round counts for types", test "get_historical_counts/2 gets accurate total round counts for types",
%{ %{
type: %{id: type_id} = type, type: %{id: type_id} = type,
current_user: current_user, current_user: current_user,
container: container container: container
} do } do
assert %{} == [type] |> Ammo.get_historical_count_for_types(current_user) assert %{} == [type] |> Ammo.get_historical_counts(current_user)
{1, [first_pack]} = pack_fixture(%{count: 1}, type, container, current_user) {1, [first_pack]} = pack_fixture(%{count: 1}, type, container, current_user)
assert %{type_id => 1} == assert %{type_id => 1} ==
[type] |> Ammo.get_historical_count_for_types(current_user) [type] |> Ammo.get_historical_counts(current_user)
%{id: another_type_id} = another_type = type_fixture(current_user) %{id: another_type_id} = another_type = type_fixture(current_user)
{1, [_pack]} = pack_fixture(%{count: 1}, another_type, container, current_user) {1, [_pack]} = pack_fixture(%{count: 1}, another_type, container, current_user)
historical_counts = historical_counts = [type, another_type] |> Ammo.get_historical_counts(current_user)
[type, another_type] |> Ammo.get_historical_count_for_types(current_user)
assert %{^type_id => 1} = historical_counts assert %{^type_id => 1} = historical_counts
assert %{^another_type_id => 1} = historical_counts assert %{^another_type_id => 1} = historical_counts
{1, [pack]} = pack_fixture(%{count: 50}, type, container, current_user) {1, [pack]} = pack_fixture(%{count: 50}, type, container, current_user)
historical_counts = historical_counts = [type, another_type] |> Ammo.get_historical_counts(current_user)
[type, another_type] |> Ammo.get_historical_count_for_types(current_user)
assert %{^type_id => 51} = historical_counts assert %{^type_id => 51} = historical_counts
assert %{^another_type_id => 1} = historical_counts assert %{^another_type_id => 1} = historical_counts
shot_record_fixture(%{count: 26}, current_user, pack) shot_record_fixture(%{count: 26}, current_user, pack)
historical_counts = historical_counts = [type, another_type] |> Ammo.get_historical_counts(current_user)
[type, another_type] |> Ammo.get_historical_count_for_types(current_user)
assert %{^type_id => 51} = historical_counts assert %{^type_id => 51} = historical_counts
assert %{^another_type_id => 1} = historical_counts assert %{^another_type_id => 1} = historical_counts
shot_record_fixture(%{count: 1}, current_user, first_pack) shot_record_fixture(%{count: 1}, current_user, first_pack)
historical_counts = historical_counts = [type, another_type] |> Ammo.get_historical_counts(current_user)
[type, another_type] |> Ammo.get_historical_count_for_types(current_user)
assert %{^type_id => 51} = historical_counts assert %{^type_id => 51} = historical_counts
assert %{^another_type_id => 1} = historical_counts assert %{^another_type_id => 1} = historical_counts

View File

@ -90,7 +90,7 @@ defmodule CanneryWeb.ExportControllerTest do
"load_grains" => type.load_grains, "load_grains" => type.load_grains,
"shot_charge_weight" => type.shot_charge_weight, "shot_charge_weight" => type.shot_charge_weight,
"dram_equivalent" => type.dram_equivalent, "dram_equivalent" => type.dram_equivalent,
"average_cost" => type |> Ammo.get_average_cost_for_type(current_user), "average_cost" => Ammo.get_average_cost(type, current_user),
"round_count" => Ammo.get_round_count(current_user, type_id: type.id), "round_count" => Ammo.get_round_count(current_user, type_id: type.id),
"used_count" => ActivityLog.get_used_count(current_user, type_id: type.id), "used_count" => ActivityLog.get_used_count(current_user, type_id: type.id),
"pack_count" => Ammo.get_packs_count(current_user, type_id: type.id), "pack_count" => Ammo.get_packs_count(current_user, type_id: type.id),