Skip to content

Commit

Permalink
Paused channels should appear in the down channels list
Browse files Browse the repository at this point in the history
  • Loading branch information
ismaelbej committed Oct 15, 2024
1 parent 0dd2343 commit 0cd80bc
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
22 changes: 20 additions & 2 deletions lib/ask/channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ defmodule Ask.Channel do
end

def with_status(channel) do
status = unless channel.paused do
status = channel |> get_status()

%{channel | status: status}
end

def get_status(channel) do
unless channel.paused do
channel.id
|> ChannelStatusServer.get_channel_status()
|> case do
Expand All @@ -97,8 +103,20 @@ defmodule Ask.Channel do
else
%{status: "paused"}
end
end

%{channel | status: status}
def is_paused?(channel) do
channel.paused
end

def is_down?(channel) do
channel
|> get_status()
|> case do
%{status: "up"} -> false
%{status: "unknown"} -> false
_ -> true
end
end

defp validate_patterns(changeset) do
Expand Down
6 changes: 2 additions & 4 deletions lib/ask/runtime/survey_broker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Ask.Runtime.SurveyBroker do
import Ecto

alias Ask.{
Channel,
Repo,
Logger,
Survey,
Expand Down Expand Up @@ -131,10 +132,7 @@ defmodule Ask.Runtime.SurveyBroker do

channel_is_down? =
channels
|> Enum.any?(fn c ->
status = c.id |> ChannelStatusServer.get_channel_status()
status != :up && status != :unknown
end)
|> Enum.any?(&(&1 |> Channel.is_paused?() || &1 |> Channel.is_down?()))

poll_survey(survey, now, channel_is_down?)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/ask/survey.ex
Original file line number Diff line number Diff line change
Expand Up @@ -525,8 +525,8 @@ defmodule Ask.Survey do

down_channels =
channels
|> Enum.map(&(&1.id |> ChannelStatusServer.get_channel_status()))
|> Enum.filter(&(&1 != :up && &1 != :unknown))
|> Enum.map(&(&1 |> Channel.get_status()))
|> Enum.filter(&(&1[:status] != "up" && &1[:status] != "unknown"))

%{survey | down_channels: down_channels}
end
Expand Down
18 changes: 15 additions & 3 deletions test/ask_web/controllers/survey_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ defmodule AskWeb.SurveyControllerTest do
survey_1 = insert(:survey, project: project, state: :running)
survey_2 = insert(:survey, project: project, state: :running)
survey_3 = insert(:survey, project: project, state: :running)
survey_4 = insert(:survey, project: project, state: :running)

up_channel =
TestChannel.create_channel(user, "test", TestChannel.settings(TestChannel.new(), 1))
Expand All @@ -337,10 +338,19 @@ defmodule AskWeb.SurveyControllerTest do
TestChannel.settings(TestChannel.new(), 3, :error)
)

setup_surveys_with_channels([survey_1, survey_2, survey_3], [
paused_channel =
TestChannel.create_channel(
user,
"test",
TestChannel.settings(TestChannel.new(), 3, :error),
%{paused: true}
)

setup_surveys_with_channels([survey_1, survey_2, survey_3, survey_4], [
up_channel,
down_channel,
error_channel
error_channel,
paused_channel,
])

ChannelStatusServer.poll(pid)
Expand All @@ -350,7 +360,7 @@ defmodule AskWeb.SurveyControllerTest do

conn = get(conn, project_survey_path(conn, :index, project.id))

[survey_1, survey_2, survey_3] = json_response(conn, 200)["data"]
[survey_1, survey_2, survey_3, survey_4] = json_response(conn, 200)["data"]
assert survey_1["down_channels"] == []

[%{"status" => "down", "messages" => [], "timestamp" => t1, "name" => "test"}] =
Expand All @@ -364,6 +374,8 @@ defmodule AskWeb.SurveyControllerTest do
assert t2
assert code

assert [%{"status" => "paused"}] = survey_4["down_channels"]

ChannelBrokerSupervisor.terminate_children()
ChannelBrokerAgent.clear()
end
Expand Down

0 comments on commit 0cd80bc

Please sign in to comment.