Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "Never Connected" and "Disconnected" fields #843

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
defmodule Astarte.AppEngine.API.Stats.DevicesStats do
defstruct [
:total_devices,
:connected_devices
:connected_devices,
:disconnected_devices,
:never_connected_devices
]
end
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ defmodule Astarte.AppEngine.API.Stats.Queries do
def get_devices_stats(realm) do
Xandra.Cluster.run(:xandra, fn conn ->
with {:ok, total_devices_count} <- get_total_devices_count(conn, realm),
{:ok, connected_devices_count} <- get_connected_devices_count(conn, realm) do
{:ok, connected_devices_count} <- get_connected_devices_count(conn, realm),
{:ok, disconnected_devices_count} <- get_disconnected_devices_count(conn, realm),
{:ok, never_connected_devices_count} <- get_never_connected_devices_count(conn, realm) do
stats = %DevicesStats{
total_devices: total_devices_count,
connected_devices: connected_devices_count
connected_devices: connected_devices_count,
disconnected_devices: disconnected_devices_count,
never_connected_devices: never_connected_devices_count
}

{:ok, stats}
Expand Down Expand Up @@ -71,6 +75,40 @@ defmodule Astarte.AppEngine.API.Stats.Queries do
end
end


defp get_disconnected_devices_count(conn, realm) do
query = """
SELECT count(device_id)
FROM :realm.devices
WHERE connected=false
ALLOW FILTERING
"""

with {:ok, prepared} <- prepare_with_realm(conn, realm, query),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will move away from doing string interpolation for realm name. Add a TODO here to remind us of changing the query.

{:ok, %Xandra.Page{} = page} <- Xandra.execute(conn, prepared, %{}) do
[%{"system.count(device_id)" => count}] = Enum.to_list(page)

{:ok, count}
end
end

defp get_never_connected_devices_count(conn, realm) do
query = """
SELECT device_id, last_connection
FROM :realm.devices
"""

with {:ok, prepared} <- prepare_with_realm(conn, realm, query),
{:ok, %Xandra.Page{} = page} <- Xandra.execute(conn, prepared, %{}) do
devices = Enum.to_list(page)
Comment on lines +102 to +103
Copy link
Collaborator

@Annopaolo Annopaolo Dec 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will happen here if the number of never connected devices is bigger than the page size?

never_connected_count = devices
|> Enum.filter(fn %{"last_connection" => nil} -> true; _ -> false end)
|> length()

{:ok, never_connected_count}
end
end

# TODO: copypasted from Groups.Queries, this is going to be moved to Astarte.DataAccess
# when we move everything to Xandra
defp prepare_with_realm(conn, realm_name, query) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ defmodule Astarte.AppEngine.APIWeb.StatsView do
def render("devices_stats.json", %{devices_stats: devices_stats}) do
%{
total_devices: devices_stats.total_devices,
connected_devices: devices_stats.connected_devices
connected_devices: devices_stats.connected_devices,
disconnected_devices: devices_stats.disconnected_devices,
never_connected_devices: devices_stats.never_connected_devices
}
end
end
Loading