Skip to content

Commit

Permalink
Add paused field to Channel
Browse files Browse the repository at this point in the history
  • Loading branch information
ismaelbej committed Oct 8, 2024
1 parent f95e791 commit 507c01b
Show file tree
Hide file tree
Showing 8 changed files with 1,075 additions and 59 deletions.
21 changes: 10 additions & 11 deletions lib/ask/channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ defmodule Ask.Channel do
field :settings, :map
field :patterns, Ask.Ecto.Type.JSON, default: []
field :status, Ask.Ecto.Type.JSON, virtual: true
field :paused, :boolean, default: false

belongs_to :user, Ask.User
has_many :respondent_group_channels, Ask.RespondentGroupChannel, on_delete: :delete_all
many_to_many :projects, Ask.Project, join_through: Ask.ProjectChannel, on_replace: :delete
Expand All @@ -36,7 +38,7 @@ defmodule Ask.Channel do
"""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:name, :type, :provider, :base_url, :settings, :user_id, :patterns])
|> cast(params, [:name, :type, :provider, :base_url, :settings, :user_id, :patterns, :paused])
|> validate_required([:name, :type, :provider, :settings, :user_id])
|> validate_patterns
|> assoc_constraint(:user)
Expand Down Expand Up @@ -84,24 +86,21 @@ defmodule Ask.Channel do
end

def with_status(channel) do
status = channel.id |> ChannelStatusServer.get_channel_status()

status =
case status do
status = unless channel.paused do
channel.id
|> ChannelStatusServer.get_channel_status()
|> case do
:up -> %{status: "up"}
:unknown -> %{status: "unknown"}
down_or_error -> down_or_error
end
else
%{status: "paused"}
end

%{channel | status: status}
end

def update_status(channel, channel_status) do
channel.id
|> ChannelStatusServer.update_channel_status(channel_status)
with_status(channel)
end

defp validate_patterns(changeset) do
changeset
|> validate_patterns_not_empty
Expand Down
25 changes: 10 additions & 15 deletions lib/ask/runtime/channel_status_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ defmodule Ask.Runtime.ChannelStatusServer do
GenServer.call(@server_ref, {:get_channel_status, channel_id})
end

def update_channel_status(channel_id, channel_status) do
defp update_channel_status(channel_id, channel_status) do
GenServer.cast(@server_ref, {:update, {channel_id, channel_status}})
end

Expand All @@ -43,14 +43,17 @@ defmodule Ask.Runtime.ChannelStatusServer do
Survey.running_channels()
|> Repo.preload(:user)
|> Enum.each(fn c ->
previous_status = get_status_from_state(c.id, state)

spawn(fn ->
status = ChannelBroker.check_status(c.id)
timestamp = Timex.now()
unless c.paused do
previous_status = get_status_from_state(c.id, state)

process_channel_status_change(status, previous_status, timestamp, c)
end)
spawn(fn ->
status = ChannelBroker.check_status(c.id)
timestamp = Timex.now()

process_channel_status_change(status, previous_status, timestamp, c)
end)
end
end)

{:noreply, state}
Expand All @@ -71,10 +74,6 @@ defmodule Ask.Runtime.ChannelStatusServer do
Logger.info("ChannelStatusServer: #{message}")
end

defp process_channel_status_change({:down, _messages}, %{status: :paused}, _timestamp, _channel) do
nil
end

defp process_channel_status_change({:down, _messages}, %{status: :down}, _timestamp, _channel) do
nil
end
Expand All @@ -90,10 +89,6 @@ defmodule Ask.Runtime.ChannelStatusServer do
})
end

defp process_channel_status_change({:error, _code}, %{status: :paused}, _timestamp, _channel) do
nil
end

defp process_channel_status_change({:error, _code}, %{status: :error}, _timestamp, _channel) do
nil
end
Expand Down
10 changes: 10 additions & 0 deletions lib/ask_web/controllers/channel_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,14 @@ defmodule AskWeb.ChannelController do

render(conn, "show.json", channel: channel |> Repo.preload([:projects, :user]))
end

def pause(conn, %{"id" => id}) do
channel_params = %{"paused" => true}
AskWeb.ChannelController.update(conn, %{"id" => id, "channel" => channel_params})
end

def unpause(conn, %{"id" => id}) do
channel_params = %{"paused" => false}
AskWeb.ChannelController.update(conn, %{"id" => id, "channel" => channel_params})
end
end
6 changes: 5 additions & 1 deletion lib/ask_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ defmodule AskWeb.Router do
as: :update_archived_status
end

resources "/channels", ChannelController, except: [:new, :edit]
resources "/channels", ChannelController, except: [:new, :edit] do
post "/pause", ChannelController, :pause, as: :pause
post "/unpause", ChannelController, :unpause, as: :unpause
end

get "/audios/tts", AudioController, :tts
resources "/audios", AudioController, only: [:create, :show]
resources "/authorizations", OAuthClientController, only: [:index, :delete]
Expand Down
9 changes: 9 additions & 0 deletions priv/repo/migrations/20241007192540_pause_channel.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Ask.Repo.Migrations.PauseChannel do
use Ecto.Migration

def change do
alter table(:channels) do
add :paused, :boolean, default: false
end
end
end
Loading

0 comments on commit 507c01b

Please sign in to comment.