Skip to content

Commit

Permalink
Filter by topic failing messages (#142)
Browse files Browse the repository at this point in the history
* Filter failing messages by topic

* Restore pagination

* Refactor

* Restore pagination
  • Loading branch information
jjponz authored May 24, 2021
1 parent 4a2f8d9 commit acbe9da
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 25 deletions.
24 changes: 19 additions & 5 deletions lib/postoffice/messaging.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ defmodule Postoffice.Messaging do
alias Postoffice.Messaging.Message
alias Postoffice.Messaging.Publisher
alias Postoffice.Messaging.Topic
alias Postoffice.Messaging
alias Postoffice.Messaging.MessageSearchParams

@doc """
Gets a single message.
Expand Down Expand Up @@ -316,15 +318,27 @@ defmodule Postoffice.Messaging do
|> broadcast_publisher(:publisher_deleted)
end


def list_deleted_publishers do
from(p in Publisher, where: p.deleted == true)
|> Repo.all()
end

def get_failing_messages(%{"page"=> _page, "page_size"=> _page_size} = params) do
from(job in Oban.Job, where: job.state=="retryable")
|> Repo.paginate(params)
|> Map.from_struct
def get_failing_messages(%MessageSearchParams{} = params) when params.topic == "" do
pagination_params = %{"page" => params.page, page_size: params.page_size}

from(job in Oban.Job, where: job.state == "retryable")
|> Repo.paginate(pagination_params)
|> Map.from_struct()
end

def get_failing_messages(%MessageSearchParams{} = params) do
topic = String.trim(params.topic)
pagination_params = %{"page" => params.page, page_size: params.page_size}

from(job in Oban.Job,
where: job.state == "retryable" and fragment("args->>'topic' = ?", ^topic)
)
|> Repo.paginate(pagination_params)
|> Map.from_struct()
end
end
3 changes: 3 additions & 0 deletions lib/postoffice/messaging/message_search_params.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule Postoffice.Messaging.MessageSearchParams do
defstruct [:topic , :page, :page_size]
end
43 changes: 42 additions & 1 deletion lib/postoffice_web/controllers/message_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,57 @@ defmodule PostofficeWeb.MessageController do
use PostofficeWeb, :controller

alias Postoffice.Messaging
alias Postoffice.Messaging.MessageSearchParams
alias Postoffice.HistoricalData

def index(
%{params: %{"topic" => topic}} = conn,
%{"page" => page, "page_size" => page_size})
when topic != "" do
messages =
%MessageSearchParams{
topic: topic,
page: page,
page_size: page_size
}
|> Messaging.get_failing_messages()

render(conn, "index.html",
page_name: "Messages",
messages: messages.entries,
page_number: messages.page_number,
total_pages: messages.total_pages,
topic: topic
)
end

def index(conn, %{"page" => page, "page_size" => page_size}) do
messages =
%MessageSearchParams{
topic: "",
page: page,
page_size: page_size
}
|> Messaging.get_failing_messages()

render(conn, "index.html",
page_name: "Messages",
messages: messages.entries,
page_number: messages.page_number,
total_pages: messages.total_pages,
topic: ""
)
end

def index(conn, %{"page" => page, "page_size" => page_size} = params) do
messages = Messaging.get_failing_messages(params)

render(conn, "index.html",
page_name: "Messages",
messages: messages.entries,
page_number: messages.page_number,
total_pages: messages.total_pages
total_pages: messages.total_pages,
topic: ""
)
end

Expand Down
13 changes: 0 additions & 13 deletions lib/postoffice_web/templates/layout/app.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,6 @@
<span class="navbar-toggler-icon icon-bar"></span>
<span class="navbar-toggler-icon icon-bar"></span>
</button>
<div class="collapse navbar-collapse justify-content-end">
<%= form_for @conn, Routes.message_path(@conn, :index), [method: :get, class: "navbar-form"],fn _f -> %>
<span class="bmd-form-group">
<div class="input-group no-border">
<input type="text" id="id" name="id" value="" class="form-control" placeholder="Search message by id">
<button type="submit" class="btn btn-white btn-round btn-just-icon">
<i class="material-icons">search</i>
<div class="ripple-container"></div>
</button>
</div>
</span>
<% end %>
</div>
</div>
</nav>
<!-- End Navbar -->
Expand Down
17 changes: 15 additions & 2 deletions lib/postoffice_web/templates/message/index.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,32 @@
<span>
<%= link "Prev Page",
class: "btn btn-primary btn-round",
to: Routes.message_path(@conn, :index, page: @page_number - 1, page_size: 100) %>
to: Routes.message_path(@conn, :index, page: @page_number - 1, page_size: 100, topic: @topic) %>
</span>
<% end %>
<%= if @page_number < @total_pages do%>
<span>
<%= link "Next Page",
class: "btn btn-primary btn-round",
to: Routes.message_path(@conn, :index, page: @page_number + 1, page_size: 100) %>
to: Routes.message_path(@conn, :index, page: @page_number + 1, page_size: 100, topic: @topic) %>
</span>
<% end %>
<span>
<p class="font-weight-bold">Pages: <%= @page_number %>/<%= @total_pages %></p>
</span>
<div class="navbar-collapse justify-content-end">
<%= form_for @conn, Routes.message_path(@conn, :index), [method: :get, class: "navbar-form"],fn _f -> %>
<span class="bmd-form-group">
<div class="input-group no-border">
<input type="text" id="topic" name="topic" class="form-control" placeholder="Search message by topic" value=<%= @topic %>>
<button type="submit" class="btn btn-white btn-round btn-just-icon">
<i class="material-icons">search</i>
<div class="ripple-container"></div>
</button>
</div>
</span>
<% end %>
</div>
<table class="table">
<thead>
<tr>
Expand Down
19 changes: 15 additions & 4 deletions test/postoffice/messaging_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Postoffice.MessagingTest do
use Oban.Testing, repo: Postoffice.Repo

alias Postoffice.Messaging
alias Postoffice.Messaging.MessageSearchParams
alias Postoffice.Fixtures

@second_topic_attrs %{
Expand Down Expand Up @@ -239,7 +240,7 @@ defmodule Postoffice.MessagingTest do
end

test "count_failing_jobs/0 no returns retryable jobs when no exists" do
failing_messages = %{"page"=> 1, "page_size"=> 4}
failing_messages = %MessageSearchParams{topic: "", page: 1, page_size: 4}
|> Messaging.get_failing_messages

assert failing_messages == %{entries: [], page_number: 1, page_size: 4, total_entries: 0, total_pages: 1}
Expand All @@ -249,7 +250,7 @@ defmodule Postoffice.MessagingTest do
first_failing_job = Fixtures.create_failing_message(%{id: 1, user_id: 2})
second_failing_job = Fixtures.create_failing_message(%{id: 2, user_id: 3})

failing_messages = %{"page"=> 1, "page_size"=> 4}
failing_messages = %MessageSearchParams{topic: "", page: 1, page_size: 4}
|> Messaging.get_failing_messages

assert failing_messages == %{entries: [first_failing_job, second_failing_job], page_number: 1, page_size: 4, total_entries: 2, total_pages: 1}
Expand All @@ -259,14 +260,24 @@ defmodule Postoffice.MessagingTest do
first_failing_job = Fixtures.create_failing_message(%{id: 1, user_id: 2})
second_failing_job = Fixtures.create_failing_message(%{id: 2, user_id: 3})

failing_messages = %{"page"=> 1, "page_size"=> 1}
failing_messages = %MessageSearchParams{topic: "", page: 1, page_size: 1}
|> Messaging.get_failing_messages
assert failing_messages == %{entries: [first_failing_job], page_number: 1, page_size: 1, total_entries: 2, total_pages: 2}

failing_messages = %{"page"=> 2, "page_size"=> 1}
failing_messages = %MessageSearchParams{topic: "", page: 2, page_size: 1}
|> Messaging.get_failing_messages
assert failing_messages == %{entries: [second_failing_job], page_number: 2, page_size: 1, total_entries: 2, total_pages: 2}
end

test "get_failing_messages/1 returns retryable jobs filtering by topic" do
first_failing_job = Fixtures.create_failing_message(%{topic: "some-topic", user_id: 2})
second_failing_job = Fixtures.create_failing_message(%{topic: "another-topic", user_id: 3})
third_failing_job = Fixtures.create_failing_message(%{topic: "another-topic", user_id: 4})

failing_messages = %MessageSearchParams{topic: "another-topic", page: 1, page_size: 4}
|> Messaging.get_failing_messages

assert failing_messages == %{entries: [second_failing_job, third_failing_job], page_number: 1, page_size: 4, total_entries: 2, total_pages: 1}
end
end
end

0 comments on commit acbe9da

Please sign in to comment.