Skip to content

Commit 718a7c0

Browse files
committed
Put real data into linepoint plot
1 parent b07c747 commit 718a7c0

File tree

8 files changed

+240499
-100
lines changed

8 files changed

+240499
-100
lines changed

.elixir-tools/next-ls.log

+240,239
Large diffs are not rendered by default.

fullstack/lib/fullstack/customers.ex

+3-8
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ defmodule Fullstack.Customers do
55

66
import Ecto.Query, warn: false
77
alias Fullstack.Repo
8-
98
alias Fullstack.Customers.Customer
9+
alias Fullstack.Utils.Charts.ChartsDates
10+
1011
def customers_count, do: Repo.aggregate(Customer, :count, :id)
1112

1213
@doc """
@@ -25,20 +26,14 @@ defmodule Fullstack.Customers do
2526
|> Repo.all()
2627
end
2728

28-
defp default_date_range() do
29-
until = DateTime.utc_now()
30-
from = Timex.shift(until, years: -1)
31-
%{"from" => from, "until" => until}
32-
end
33-
3429
defp maybe_select_fields(query, %{"only" => only_fields} = _params) when is_list(only_fields) do
3530
select(query, ^only_fields)
3631
end
3732

3833
defp maybe_select_fields(query, _params), do: query
3934

4035
defp maybe_from_created_range(query, params) do
41-
date_range = Map.merge(default_date_range(), params)
36+
date_range = Map.merge(ChartsDates.default_date_range(), params)
4237

4338
where(
4439
query,

fullstack/lib/fullstack/financial.ex

+17-81
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ defmodule Fullstack.Financial do
55
alias Fullstack.Financial.{Transaction, Transactions}
66
alias Fullstack.Customers
77
alias Fullstack.Customers.Customer
8+
alias Fullstack.Utils.Charts.ChartsDates
89

910
defdelegate count_transactions, to: Transactions
1011
defdelegate count_transactions(transactions), to: Transactions
@@ -22,13 +23,13 @@ defmodule Fullstack.Financial do
2223
|> set_top_transactions()
2324
|> set_top_customers()
2425
|> set_transactions_per_month()
25-
|> set_transactions_per_day()
26+
# |> set_transactions_per_day()
2627
|> Map.drop([:transactions, :monthly_transactions, :daily_transactions])
2728
end
2829

2930
def build_customers_analytics(params \\ %{}) do
3031
Customers.list_customers(%{"only" => [:id, :inserted_at]})
31-
|> parse_chart_data
32+
|> ChartsDates.parse_chart_data()
3233
end
3334

3435
def to_transaction(transaction) do
@@ -71,90 +72,25 @@ defmodule Fullstack.Financial do
7172
end
7273

7374
def set_transactions_per_month(%{transactions: transactions} = data) do
74-
[current_year, _m, _d] = Date.utc_today() |> parse_date()
75-
76-
grouped_transactions =
77-
transactions
78-
|> Enum.filter(&year_filter(&1.inserted_at, current_year))
79-
|> Enum.group_by(&month_year_transactions_grouper/1)
75+
grouped_transactions = ChartsDates.parse_chart_data(transactions)
8076

8177
data
8278
|> Map.put(:monthly_transactions, grouped_transactions)
83-
|> Map.put(:monthly_data, parse_chart_data(grouped_transactions))
84-
end
85-
86-
defp parse_chart_data([%Customer{} | _] = customers) do
87-
customers
88-
|> Enum.reduce(%{}, fn c, acc ->
89-
acc
90-
|> Map.update(
91-
c.inserted_at
92-
|> NaiveDateTime.to_date()
93-
|> to_string()
94-
|> String.split("-")
95-
|> Enum.take(2),
96-
1,
97-
&(&1 + 1)
98-
)
99-
end)
100-
|> Enum.map(fn {[_, m], value} ->
101-
[Timex.month_name(String.to_integer(m)), value]
102-
end)
103-
end
104-
105-
defp parse_chart_data(transactions) do
106-
transactions
107-
|> Enum.map(fn {k, v} ->
108-
{count, amount} =
109-
Enum.reduce(v, {0, 0}, fn trx, {count, amount} ->
110-
{count + 1, trx.amount + amount}
111-
end)
112-
113-
[" #{elem(k, 1)}", count, amount / 100_000]
114-
end)
115-
end
116-
117-
defp year_filter(transaction_date, current_year) do
118-
[y, _m, _d] = parse_date(transaction_date)
119-
y == current_year
79+
|> Map.put(:monthly_data, grouped_transactions)
12080
end
12181

122-
defp month_year_transactions_grouper(transaction) do
123-
[trx_year, trx_month, _d] =
124-
transaction
125-
|> Map.fetch!(:inserted_at)
126-
|> parse_date()
127-
128-
{trx_year, trx_month}
129-
end
130-
131-
defp set_transactions_per_day(%{monthly_transactions: transactions} = data) do
132-
[current_year, current_month, _d] = Date.utc_today() |> parse_date()
133-
134-
grouped_transactions =
135-
transactions
136-
|> Map.get({current_year, current_month})
137-
|> Enum.group_by(&day_month_transactions_grouper/1)
138-
139-
data
140-
|> Map.put(:daily_transactions, grouped_transactions)
141-
|> Map.put(:daily_data, parse_chart_data(grouped_transactions))
142-
end
143-
144-
defp day_month_transactions_grouper(transaction) do
145-
[_y, trx_month, trx_day] =
146-
transaction
147-
|> Map.fetch!(:inserted_at)
148-
|> parse_date()
149-
150-
{trx_month, trx_day}
151-
end
152-
153-
defp parse_date(date) do
154-
date
155-
|> Date.to_string()
156-
|> String.split("-")
157-
end
82+
## defp set_transactions_per_day(%{monthly_transactions: transactions} = data) do
83+
## [current_year, current_month, _d] = Date.utc_today() |> parse_date()
84+
##
85+
## grouped_transactions =
86+
## transactions
87+
## |> Map.get({current_year, current_month})
88+
## |> Enum.group_by(&day_month_transactions_grouper/1)
89+
##
90+
## data
91+
## |> Map.put(:daily_transactions, grouped_transactions)
92+
## |> Map.put(:daily_data, ChartsDates.parse_chart_data(grouped_transactions))
93+
## end
15894

15995
def set_transactions_total_amount(transactions) do
16096
Enum.reduce(transactions, 0, fn trx, acc -> trx.amount + acc end)

fullstack/lib/fullstack/financial/transactions.ex

+25-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ defmodule Fullstack.Financial.Transactions do
1616

1717
import Ecto.Query, warn: false
1818
alias Fullstack.Repo
19-
19+
alias Fullstack.Utils.Charts.Dates
2020
alias Fullstack.Financial.Transaction
2121

2222
def count_transactions(transactions) do
@@ -45,8 +45,14 @@ defmodule Fullstack.Financial.Transactions do
4545
|> Repo.all()
4646
end
4747

48-
defp with_date_range(query, %{"dates" => %{"from" => from, "upto" => upto}}) do
49-
query
48+
defp with_date_range(query, %{"dates" => %{"from" => from, "iuntil" => until}} = params) do
49+
date_range = Map.merge(Dates.default_date_range(), params)
50+
51+
where(
52+
query,
53+
[t],
54+
t.inserted_at >= ^date_range["from"] and t.inserted_at <= ^date_range["until"]
55+
)
5056
end
5157

5258
defp with_date_range(query, _params), do: query
@@ -75,6 +81,22 @@ defmodule Fullstack.Financial.Transactions do
7581

7682
defp from_pos(query, _params), do: query
7783

84+
defp maybe_select_fields(query, %{"only" => only_fields} = _params) when is_list(only_fields) do
85+
select(query, ^only_fields)
86+
end
87+
88+
defp maybe_select_fields(query, _params), do: query
89+
90+
defp maybe_from_created_range(query, params) do
91+
date_range = Map.merge(Dates.default_date_range(), params)
92+
93+
where(
94+
query,
95+
[c],
96+
c.inserted_at >= ^date_range["from"] and c.inserted_at <= ^date_range["until"]
97+
)
98+
end
99+
78100
@doc """
79101
Returns the list of transactions.
80102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
defmodule Fullstack.Utils.Charts.ChartsDates do
2+
def parse_chart_data([%Fullstack.Customers.Customer{} | _] = customers) do
3+
customers
4+
|> Enum.reduce(%{}, fn c, acc ->
5+
acc
6+
|> Map.update(
7+
c.inserted_at
8+
|> NaiveDateTime.to_date()
9+
|> to_string()
10+
|> String.split("-")
11+
|> Enum.take(2),
12+
1,
13+
&(&1 + 1)
14+
)
15+
end)
16+
|> Enum.map(fn {[_, m], value} ->
17+
[Timex.month_name(String.to_integer(m)), value]
18+
end)
19+
end
20+
21+
def parse_chart_data(transactions) do
22+
transactions
23+
|> Enum.reduce(%{}, fn trx, acc ->
24+
key =
25+
trx.inserted_at
26+
|> NaiveDateTime.to_date()
27+
|> to_string()
28+
|> String.split("-")
29+
|> Enum.take(2)
30+
31+
values =
32+
acc
33+
|> Map.get(key, %{})
34+
|> Map.update(:count, 1, &(&1 + 1))
35+
|> Map.update(:amount, 0.0, &(&1 + trx.amount))
36+
37+
Map.put(acc, key, values)
38+
end)
39+
|> Enum.map(fn {[_, m], %{count: count, amount: amount}} ->
40+
[
41+
String.to_integer(m),
42+
count,
43+
Float.ceil(amount / 100_000, 2)
44+
]
45+
end)
46+
end
47+
48+
def default_date_range() do
49+
until = DateTime.utc_now()
50+
from = Timex.shift(until, years: -1)
51+
%{"from" => from, "until" => until}
52+
end
53+
54+
def year_filter(transaction_date, current_year) do
55+
[y, _m, _d] = parse_date(transaction_date)
56+
y == current_year
57+
end
58+
59+
def month_year_transactions_grouper(transaction) do
60+
[trx_year, trx_month, _d] =
61+
transaction
62+
|> Map.fetch!(:inserted_at)
63+
|> parse_date()
64+
65+
{trx_year, trx_month}
66+
end
67+
68+
def day_month_transactions_grouper(transaction) do
69+
[_y, trx_month, trx_day] =
70+
transaction
71+
|> Map.fetch!(:inserted_at)
72+
|> parse_date()
73+
74+
{trx_month, trx_day}
75+
end
76+
77+
def parse_date(date) do
78+
date
79+
|> Date.to_string()
80+
|> String.split("-")
81+
end
82+
end

fullstack/lib/fullstack_web/live/public/public_transactions.ex

+2-7
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,9 @@ defmodule FullstackWeb.Public.TransactionsLive.PublicTransactions do
231231
## [calc_x(x, i, time_series) | series_data]
232232
end
233233

234-
dbg(data)
234+
data = socket.assigns.info.monthly_data
235235

236-
series_cols =
237-
for s <- 1..series do
238-
"Series #{s}"
239-
end
240-
241-
dbg(series_cols)
236+
series_cols = ["Count", "Amount"]
242237

243238
test_data =
244239
case needs_update do

fullstack/lib/fullstack_web/live/public/public_transactions.html.heex

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
</div>
8585
<div class="items-center ">
8686
<pre class="text-gray-300 bg-gray-800 code p-4 rounded-md whitespace-pre overflow-x-auto">
87-
<code> <%= inspect(@info.daily_data, pretty: true) %>
87+
<code> <%= inspect(@info.monthly_data, pretty: true) %>
8888
</code>
8989
</pre>
9090
</div>

0 commit comments

Comments
 (0)