Skip to content

Commit

Permalink
Performance improvements: Add Brainstorming Label to idea + broadcast…
Browse files Browse the repository at this point in the history
… lane update (#507)
  • Loading branch information
PragTob authored Dec 23, 2024
1 parent 54845af commit fc8729a
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 82 deletions.
4 changes: 4 additions & 0 deletions lib/mindwendel/brainstormings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ defmodule Mindwendel.Brainstormings do
end
end

def get_bare_brainstorming!(id) do
Repo.get!(Brainstorming, id)
end

@doc """
Gets a single brainstorming with the admin url id
"""
Expand Down
9 changes: 9 additions & 0 deletions lib/mindwendel/brainstormings/idea_idea_label.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,13 @@ defmodule Mindwendel.Brainstormings.IdeaIdeaLabel do
name: :idea_idea_labels_idea_id_idea_label_id_index
)
end

def bare_creation_changeset(idea_idea_label \\ %__MODULE__{}, attrs) do
idea_idea_label
|> cast(attrs, [:idea_id, :idea_label_id])
|> validate_required([:idea_id, :idea_label_id])
|> unique_constraint([:idea_id, :idea_label_id],
name: :idea_idea_labels_idea_id_idea_label_id_index
)
end
end
21 changes: 8 additions & 13 deletions lib/mindwendel/idea_labels.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,24 @@ defmodule Mindwendel.IdeaLabels do
nil
end

def add_idea_label_to_idea(%Idea{} = idea, %IdeaLabel{} = idea_label) do
idea = Repo.preload(idea, :idea_labels)

idea_labels =
(idea.idea_labels ++ [idea_label])
|> Enum.map(&Ecto.Changeset.change/1)

# As the broadcast results in a full reload of the ideas, we don't need to actually update
# the idea struct, a new association is enough
def add_idea_label_to_idea(idea, idea_label_id) do
result =
idea
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_assoc(:idea_labels, idea_labels)
|> Repo.update()
%{idea_id: idea.id, idea_label_id: idea_label_id}
|> IdeaIdeaLabel.bare_creation_changeset()
|> Repo.insert()

Lanes.broadcast_lanes_update(idea.brainstorming_id)
result
end

def remove_idea_label_from_idea(%Idea{} = idea, %IdeaLabel{} = idea_label) do
def remove_idea_label_from_idea(%Idea{} = idea, idea_label_id) do
result =
from(idea_idea_label in IdeaIdeaLabel,
where:
idea_idea_label.idea_id == ^idea.id and
idea_idea_label.idea_label_id == ^idea_label.id
idea_idea_label.idea_label_id == ^idea_label_id
)
|> Repo.delete_all()

Expand Down
15 changes: 6 additions & 9 deletions lib/mindwendel/lanes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ defmodule Mindwendel.Lanes do
def get_lanes_for_brainstorming_with_labels_filtered(id) do
{:ok, brainstorming} = Brainstormings.get_brainstorming(id)

filter_label =
if length(brainstorming.filter_labels_ids) > 0,
do: %{filter_labels_ids: brainstorming.filter_labels_ids},
else: %{}
filter_label = %{filter_labels_ids: brainstorming.filter_labels_ids}

get_lanes_for_brainstorming(id, filter_label)
end
Expand All @@ -89,7 +86,7 @@ defmodule Mindwendel.Lanes do
[%Lane{}, ...]
"""
def get_lanes_for_brainstorming(id, filters \\ %{}) do
def get_lanes_for_brainstorming(id, filters \\ %{filter_labels_ids: []}) do
lane_query =
from lane in Lane,
where: lane.brainstorming_id == ^id,
Expand All @@ -105,6 +102,10 @@ defmodule Mindwendel.Lanes do
|> Repo.preload(ideas: {ideas_advanced_query, [:link, :likes, :idea_labels, :files]})
end

defp build_ideas_query_with_filter(%{filter_labels_ids: []}) do
from(idea in Idea)
end

defp build_ideas_query_with_filter(%{filter_labels_ids: filter_labels_ids}) do
distinct_ideas =
from idea in Idea,
Expand All @@ -118,10 +119,6 @@ defmodule Mindwendel.Lanes do
)
end

defp build_ideas_query_with_filter(%{} = _filters) do
from(idea in Idea)
end

@doc """
Creates a lane.
Expand Down
4 changes: 4 additions & 0 deletions lib/mindwendel/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ defmodule Mindwendel.Repo do
use Ecto.Repo,
otp_app: :mindwendel,
adapter: Ecto.Adapters.Postgres

def count(query) do
aggregate(query, :count)
end
end
31 changes: 9 additions & 22 deletions lib/mindwendel_web/live/idea_live/card_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ defmodule MindwendelWeb.IdeaLive.CardComponent do
alias Mindwendel.Likes

@impl true
def handle_event("delete_idea", %{"id" => id}, socket) do
idea = Ideas.get_idea!(id)
def handle_event("delete_idea", _params, socket) do
idea = socket.assigns.idea

%{current_user: current_user, brainstorming: brainstorming} = socket.assigns

Expand All @@ -19,50 +19,37 @@ defmodule MindwendelWeb.IdeaLive.CardComponent do
{:noreply, socket}
end

def handle_event("like", %{"id" => id}, socket) do
Likes.add_like(id, socket.assigns.current_user.id)
def handle_event("like", _params, socket) do
Likes.add_like(socket.assigns.idea.id, socket.assigns.current_user.id)

{:noreply, socket}
end

def handle_event("unlike", %{"id" => id}, socket) do
Likes.delete_like(id, socket.assigns.current_user.id)
def handle_event("unlike", _params, socket) do
Likes.delete_like(socket.assigns.idea.id, socket.assigns.current_user.id)

{:noreply, socket}
end

def handle_event(
"add_idea_label_to_idea",
%{
"idea-id" => idea_id,
"idea-label-id" => idea_label_id
},
socket
) do
idea = Ideas.get_idea!(idea_id)
idea_label = IdeaLabels.get_idea_label(idea_label_id)

case(IdeaLabels.add_idea_label_to_idea(idea, idea_label)) do
{:ok, _idea} ->
{:noreply, socket}

{:error, _changeset} ->
{:noreply, socket}
end
IdeaLabels.add_idea_label_to_idea(socket.assigns.idea, idea_label_id)
{:noreply, socket}
end

def handle_event(
"remove_idea_label_from_idea",
%{
"idea-id" => idea_id,
"idea-label-id" => idea_label_id
},
socket
) do
idea = Ideas.get_idea!(idea_id)
idea_label = IdeaLabels.get_idea_label(idea_label_id)

IdeaLabels.remove_idea_label_from_idea(idea, idea_label)
IdeaLabels.remove_idea_label_from_idea(socket.assigns.idea, idea_label_id)
{:noreply, socket}
end
end
7 changes: 2 additions & 5 deletions lib/mindwendel_web/live/idea_live/card_component.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
class="float-end ms-3 mb-3"
phx-click="delete_idea"
phx-target={@myself}
phx-value-id={@idea.id}
title={gettext("Delete idea")}
data-confirm={gettext("Are you sure you want to delete this idea?")}
>
Expand Down Expand Up @@ -97,7 +96,6 @@
data-testid={brainstorming_idea_label.id}
phx-click="add_idea_label_to_idea"
phx-target={@myself}
phx-value-idea-id={@idea.id}
title={"Label #{brainstorming_idea_label.name}"}
phx-value-idea-label-id={brainstorming_idea_label.id}
>
Expand All @@ -116,7 +114,6 @@
data-testid={brainstorming_idea_label.id}
phx-click="remove_idea_label_from_idea"
phx-target={@myself}
phx-value-idea-id={@idea.id}
title={"Label #{brainstorming_idea_label.name}"}
phx-value-idea-label-id={brainstorming_idea_label.id}
>
Expand Down Expand Up @@ -145,11 +142,11 @@
</span>
<span class="me-1 text-dark">{length(@idea.likes)}</span>
<%= if Mindwendel.Likes.exists_user_in_likes?(@idea.likes, @current_user.id) do %>
<.link phx-click="unlike" phx-target={@myself} phx-value-id={@idea.id} title="Unlike">
<.link phx-click="unlike" phx-target={@myself} title="Unlike">
<i class="bi-star-fill"></i>
</.link>
<% else %>
<.link phx-click="like" phx-target={@myself} phx-value-id={@idea.id} title="Like">
<.link phx-click="like" phx-target={@myself} title="Like">
<i class="bi-star"></i>
</.link>
<% end %>
Expand Down
14 changes: 7 additions & 7 deletions priv/gettext/default.pot
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ msgid "%{name} - New Idea"
msgstr ""

#: lib/mindwendel_web/live/comment_live/show_component.html.heex:22
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:18
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:17
#, elixir-autogen, elixir-format
msgid "Are you sure you want to delete this idea?"
msgstr ""
Expand Down Expand Up @@ -195,7 +195,7 @@ msgstr ""

#: lib/mindwendel_web/controllers/admin/brainstorming_html/export.html.heex:3
#: lib/mindwendel_web/live/comment_live/show_component.html.heex:40
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:85
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:84
#, elixir-autogen, elixir-format
msgid "By"
msgstr ""
Expand Down Expand Up @@ -451,13 +451,13 @@ msgstr ""
msgid "No lanes available"
msgstr ""

#: lib/mindwendel_web/live/idea_live/card_component.html.heex:25
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:24
#, elixir-autogen, elixir-format
msgid "Edit idea"
msgstr ""

#: lib/mindwendel_web/live/comment_live/show_component.html.heex:30
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:17
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:16
#, elixir-autogen, elixir-format
msgid "Delete idea"
msgstr ""
Expand All @@ -472,7 +472,7 @@ msgstr ""
msgid "Additional Attachment"
msgstr ""

#: lib/mindwendel_web/live/idea_live/card_component.html.heex:75
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:74
#: lib/mindwendel_web/live/idea_live/form_component.html.heex:43
#: lib/mindwendel_web/live/idea_live/show_component.html.heex:27
#, elixir-autogen, elixir-format
Expand Down Expand Up @@ -529,8 +529,8 @@ msgstr ""
msgid "No comments available"
msgstr ""

#: lib/mindwendel_web/live/idea_live/card_component.html.heex:33
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:140
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:32
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:137
#, elixir-autogen, elixir-format
msgid "Show idea"
msgstr ""
Expand Down
4 changes: 2 additions & 2 deletions test/mindwendel/brainstormings_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,8 @@ defmodule Mindwendel.BrainstormingsTest do

like = Factory.insert!(:like, idea: idea)

{:ok, idea} =
IdeaLabels.add_idea_label_to_idea(idea, Enum.at(brainstorming.labels, 0))
{:ok, _idea_idea_label} =
IdeaLabels.add_idea_label_to_idea(idea, Enum.at(brainstorming.labels, 0).id)

idea = idea |> Repo.preload([:idea_labels])

Expand Down
Loading

0 comments on commit fc8729a

Please sign in to comment.