Skip to content

Commit

Permalink
fix: Running mix format. #245
Browse files Browse the repository at this point in the history
  • Loading branch information
LuchoTurtle committed Jan 9, 2023
1 parent 36d941a commit 8cde939
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 70 deletions.
1 change: 0 additions & 1 deletion lib/app/item.ex
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ defmodule App.Item do

Ecto.Adapters.SQL.query!(Repo, sql)
|> map_columns_to_values()

end

@doc """
Expand Down
26 changes: 20 additions & 6 deletions lib/app_web/live/app_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ defmodule AppWeb.AppLive do
@impl true
def mount(_params, _session, socket) do
# subscribe to the channel
if connected?(socket), do:
AppWeb.Endpoint.subscribe(@topic)
AppWeb.Endpoint.subscribe(@stats_topic)
if connected?(socket), do: AppWeb.Endpoint.subscribe(@topic)
AppWeb.Endpoint.subscribe(@stats_topic)

person_id = get_person_id(socket.assigns)
items = Item.items_with_timers(person_id)
Expand Down Expand Up @@ -54,7 +53,13 @@ defmodule AppWeb.AppLive do
})

AppWeb.Endpoint.broadcast(@topic, "update", :create)
AppWeb.Endpoint.broadcast(@stats_topic, "item", {:create, payload: %{person_id: person_id}})

AppWeb.Endpoint.broadcast(
@stats_topic,
"item",
{:create, payload: %{person_id: person_id}}
)

{:noreply, assign(socket, text_value: "", selected_tags: [])}
end

Expand Down Expand Up @@ -123,7 +128,13 @@ defmodule AppWeb.AppLive do
})

AppWeb.Endpoint.broadcast(@topic, "update", {:start, item.id})
AppWeb.Endpoint.broadcast(@stats_topic, "timer", {:create, payload: %{person_id: person_id}})

AppWeb.Endpoint.broadcast(
@stats_topic,
"timer",
{:create, payload: %{person_id: person_id}}
)

{:noreply, socket}
end

Expand Down Expand Up @@ -238,7 +249,10 @@ defmodule AppWeb.AppLive do
end

@impl true
def handle_info(%Broadcast{topic: @stats_topic, event: _event, payload: _payload}, socket) do
def handle_info(
%Broadcast{topic: @stats_topic, event: _event, payload: _payload},
socket
) do
{:noreply, socket}
end

Expand Down
19 changes: 12 additions & 7 deletions lib/app_web/live/stats_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ defmodule AppWeb.StatsLive do

@impl true
def mount(_params, _session, socket) do

# subscribe to the channel
if connected?(socket), do: AppWeb.Endpoint.subscribe(@stats_topic)

Expand All @@ -24,12 +23,14 @@ defmodule AppWeb.StatsLive do
end

@impl true
def handle_info(%Broadcast{topic: @stats_topic, event: "item", payload: payload}, socket) do

def handle_info(
%Broadcast{topic: @stats_topic, event: "item", payload: payload},
socket
) do
metrics = socket.assigns.metrics

case payload do
{:create, payload: payload} ->

updated_metrics =
Enum.map(metrics, fn row ->
if row.person_id == payload.person_id do
Expand All @@ -40,18 +41,21 @@ defmodule AppWeb.StatsLive do
end)

{:noreply, assign(socket, metrics: updated_metrics)}

_ ->
{:noreply, socket}
end
end

@impl true
def handle_info(%Broadcast{topic: @stats_topic, event: "timer", payload: payload}, socket) do

def handle_info(
%Broadcast{topic: @stats_topic, event: "timer", payload: payload},
socket
) do
metrics = socket.assigns.metrics

case payload do
{:create, payload: payload} ->

updated_metrics =
Enum.map(metrics, fn row ->
if row.person_id == payload.person_id do
Expand All @@ -62,6 +66,7 @@ defmodule AppWeb.StatsLive do
end)

{:noreply, assign(socket, metrics: updated_metrics)}

_ ->
{:noreply, socket}
end
Expand Down
85 changes: 44 additions & 41 deletions lib/app_web/live/stats_live.html.heex
Original file line number Diff line number Diff line change
@@ -1,43 +1,46 @@
<main class="font-sans container mx-auto">
<div class="relative overflow-x-auto mt-12">
<h1 class="mb-12 text-xl font-extrabold leading-none tracking-tight text-gray-900 md:text-5xl lg:text-6xl dark:text-white">
User metrics
</h1>
<table class="w-full text-sm text-left text-gray-500 dark:text-gray-400">
<thead class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400">
<tr>
<th scope="col" class="px-6 py-3">
Username
</th>
<th scope="col" class="px-6 py-3">
Id
</th>
<th scope="col" class="px-6 py-3">
Number of items
</th>
<th scope="col" class="px-6 py-3">
Number of timers
</th>
</tr>
</thead>
<tbody>
<%= for metric <- @metrics do %>
<tr class="bg-white border-b dark:bg-gray-800 dark:border-gray-700">
<th scope="row" class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">
<%= metric.name %>
</th>
<td class="px-6 py-4">
<%= metric.person_id %>
</td>
<td class="px-6 py-4">
<%= metric.num_items %>
</td>
<td class="px-6 py-4">
<%= metric.num_timers %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
<div class="relative overflow-x-auto mt-12">
<h1 class="mb-12 text-xl font-extrabold leading-none tracking-tight text-gray-900 md:text-5xl lg:text-6xl dark:text-white">
User metrics
</h1>
<table class="w-full text-sm text-left text-gray-500 dark:text-gray-400">
<thead class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400">
<tr>
<th scope="col" class="px-6 py-3">
Username
</th>
<th scope="col" class="px-6 py-3">
Id
</th>
<th scope="col" class="px-6 py-3">
Number of items
</th>
<th scope="col" class="px-6 py-3">
Number of timers
</th>
</tr>
</thead>
<tbody>
<%= for metric <- @metrics do %>
<tr class="bg-white border-b dark:bg-gray-800 dark:border-gray-700">
<th
scope="row"
class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white"
>
<%= metric.name %>
</th>
<td class="px-6 py-4">
<%= metric.person_id %>
</td>
<td class="px-6 py-4">
<%= metric.num_items %>
</td>
<td class="px-6 py-4">
<%= metric.num_timers %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
</main>
6 changes: 5 additions & 1 deletion test/app/item_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,11 @@ defmodule App.ItemTest do
})

{:ok, _timer2} =
Timer.start(%{item_id: item2.id, person_id: item2.person_id, start: started})
Timer.start(%{
item_id: item2.id,
person_id: item2.person_id,
start: started
})

# list person with number of timers and items
person_with_items_timers = Item.person_with_item_and_timer_count()
Expand Down
57 changes: 43 additions & 14 deletions test/app_web/live/stats_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ defmodule AppWeb.StatsLiveTest do
{:ok, _timer} = Timer.start(%{item_id: item.id, start: started})

assert render(page_live) =~ "User metrics"
assert render(page_live) =~ "2" # num of items
assert render(page_live) =~ "1" # num of timers
# num of items
assert render(page_live) =~ "2"
# num of timers
assert render(page_live) =~ "1"
end

test "handle broadcast when item is created", %{conn: conn} do
Expand All @@ -43,16 +45,28 @@ defmodule AppWeb.StatsLiveTest do
{:ok, page_live, _html} = live(conn, "/stats")

assert render(page_live) =~ "User metrics"
assert render(page_live) =~ "1" # num of items

# num of items
assert render(page_live) =~ "1"

# Creating another item.
AppWeb.Endpoint.broadcast("stats", "item", {:create, payload: %{person_id: @person_id}})
assert render(page_live) =~ "2" # num of items
AppWeb.Endpoint.broadcast(
"stats",
"item",
{:create, payload: %{person_id: @person_id}}
)

# num of items
assert render(page_live) =~ "2"

# Broadcasting update. Shouldn't effect anything in the page
AppWeb.Endpoint.broadcast("stats", "item", {:update, payload: %{person_id: @person_id}})
assert render(page_live) =~ "2" # num of items
AppWeb.Endpoint.broadcast(
"stats",
"item",
{:update, payload: %{person_id: @person_id}}
)

# num of items
assert render(page_live) =~ "2"
end

test "handle broadcast when timer is created", %{conn: conn} do
Expand All @@ -63,19 +77,34 @@ defmodule AppWeb.StatsLiveTest do
{:ok, page_live, _html} = live(conn, "/stats")

assert render(page_live) =~ "User metrics"
assert render(page_live) =~ "0" # num of timers
# num of timers
assert render(page_live) =~ "0"

# Creating a timer.
AppWeb.Endpoint.broadcast("stats", "timer", {:create, payload: %{person_id: @person_id}})
assert render(page_live) =~ "1" # num of timers
AppWeb.Endpoint.broadcast(
"stats",
"timer",
{:create, payload: %{person_id: @person_id}}
)

# num of timers
assert render(page_live) =~ "1"

# Broadcasting update. Shouldn't effect anything in the page
AppWeb.Endpoint.broadcast("stats", "timer", {:update, payload: %{person_id: @person_id}})
assert render(page_live) =~ "1" # num of timers
AppWeb.Endpoint.broadcast(
"stats",
"timer",
{:update, payload: %{person_id: @person_id}}
)

# num of timers
assert render(page_live) =~ "1"
end

defp create_person(_) do
person = Person.create_person(%{"person_id" => @person_id, "name" => "guest"})
person =
Person.create_person(%{"person_id" => @person_id, "name" => "guest"})

%{person: person}
end
end

0 comments on commit 8cde939

Please sign in to comment.