Skip to content

Commit

Permalink
Send router message when orgs hit zero dc
Browse files Browse the repository at this point in the history
  • Loading branch information
vicmgs committed Jun 15, 2023
1 parent 38a0810 commit 97e129e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/console/etl_error_worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ defmodule Console.EtlErrorWorker do
ConsoleWeb.Router.DeviceController.check_org_dc_balance(updated_org, organization.dc_balance)
end

if updated_org.dc_balance <= 0 do
ConsoleWeb.Router.DeviceController.broadcast_router_org_zero_balance(updated_org)
end

if device.last_connected == nil do
{ _, time } = Timex.format(Timex.now, "%H:%M:%S UTC", :strftime)
details = %{
Expand Down
6 changes: 6 additions & 0 deletions lib/console/etl_worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,12 @@ defmodule Console.EtlWorker do
|> Enum.zip(organizations_to_update)

Enum.each(zipped_orgs_before_after, fn tuple ->
updated_org = elem(tuple, 0)

if updated_org.dc_balance <= 0 do
ConsoleWeb.Router.DeviceController.broadcast_router_org_zero_balance(updated_org)
end

ConsoleWeb.Router.DeviceController.check_org_dc_balance(elem(tuple, 0), elem(tuple, 1).dc_balance)
end)
end
Expand Down
11 changes: 11 additions & 0 deletions lib/console_web/controllers/router/device_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule ConsoleWeb.Router.DeviceController do
alias Console.CommunityChannels
alias Console.CommunityFunctions
alias Console.Organizations
alias Console.Organizations.Organization
alias Console.DeviceStats
alias Console.HotspotStats
alias Console.Events
Expand Down Expand Up @@ -476,6 +477,10 @@ defmodule ConsoleWeb.Router.DeviceController do
check_org_dc_balance(organization, prev_dc_balance)
end

if organization.dc_balance <= 0 do
broadcast_router_org_zero_balance(organization)
end

if event_device.last_connected == nil do
{ _, time } = Timex.format(Timex.now, "%H:%M:%S UTC", :strftime)
details = %{
Expand Down Expand Up @@ -733,4 +738,10 @@ defmodule ConsoleWeb.Router.DeviceController do
end
end
end

def broadcast_router_org_zero_balance(%Organization{} = organization) do
ConsoleWeb.Endpoint.broadcast("organization:all", "organization:all:zeroed:dc_balance", %{
"id" => organization.id, "dc_balance" => organization.dc_balance
})
end
end
2 changes: 1 addition & 1 deletion lib/console_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ defmodule ConsoleWeb.Router do
post "/event", DeviceController, :add_device_event
end
post "/devices/update_in_xor_filter", DeviceController, :update_devices_in_xor_filter
resources "/organizations", OrganizationController, only: [:index, :show]
get "/organizations/zero_dc", OrganizationController, :zero_dc
resources "/organizations", OrganizationController, only: [:index, :show]
post "/organizations/burned", OrganizationController, :burned_dc
post "/organizations/manual_update_router_dc", OrganizationController, :manual_update_router_dc
end
Expand Down

0 comments on commit 97e129e

Please sign in to comment.