Skip to content

Commit f33618c

Browse files
committed
(appengine) Edit stats queries for exandra
Signed-off-by: Eddy Babetto <eddy.babetto@secomind.com>
1 parent 4ad9af5 commit f33618c

File tree

3 files changed

+25
-70
lines changed

3 files changed

+25
-70
lines changed

apps/astarte_appengine_api/lib/astarte_appengine_api/stats/devices_stats.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#
22
# This file is part of Astarte.
33
#
4-
# Copyright 2019 Ispirata Srl
4+
# Copyright 2019 - 2025 SECO Mind Srl
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.

apps/astarte_appengine_api/lib/astarte_appengine_api/stats/queries.ex

Lines changed: 21 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -17,78 +17,33 @@
1717
#
1818

1919
defmodule Astarte.AppEngine.API.Stats.Queries do
20-
alias Astarte.Core.Realm
20+
alias Astarte.Core.Device
21+
alias Astarte.AppEngine.API.Realm
22+
alias Astarte.AppEngine.API.Repo
23+
alias Astarte.AppEngine.API.Devices.Device
2124
alias Astarte.AppEngine.API.Stats.DevicesStats
22-
alias Astarte.Core.CQLUtils
23-
alias Astarte.AppEngine.API.Config
24-
require Logger
25-
26-
def get_devices_stats(realm) do
27-
Xandra.Cluster.run(:xandra, fn conn ->
28-
with {:ok, total_devices_count} <- get_total_devices_count(conn, realm),
29-
{:ok, connected_devices_count} <- get_connected_devices_count(conn, realm) do
30-
stats = %DevicesStats{
31-
total_devices: total_devices_count,
32-
connected_devices: connected_devices_count
33-
}
3425

35-
{:ok, stats}
36-
else
37-
{:error, reason} ->
38-
_ = Logger.warning("Database error: #{inspect(reason)}.", tag: "db_error")
39-
{:error, :database_error}
40-
end
41-
end)
42-
end
26+
require Logger
4327

44-
defp get_total_devices_count(conn, realm) do
45-
query = """
46-
SELECT count(device_id)
47-
FROM :keyspace.devices
48-
"""
28+
import Ecto.Query
4929

50-
with {:ok, prepared} <- prepare_with_realm(conn, realm, query),
51-
{:ok, %Xandra.Page{} = page} <- Xandra.execute(conn, prepared) do
52-
[%{"system.count(device_id)" => count}] = Enum.to_list(page)
30+
def for_realm(realm_name) do
31+
keyspace = Realm.keyspace_name(realm_name)
5332

54-
{:ok, count}
55-
end
56-
end
33+
device_count = Repo.aggregate(Device, :count, prefix: keyspace)
5734

58-
defp get_connected_devices_count(conn, realm) do
5935
# TODO: we should do this via DataUpdaterPlant instead of using ALLOW FILTERING
60-
query = """
61-
SELECT count(device_id)
62-
FROM :keyspace.devices
63-
WHERE connected=true
64-
ALLOW FILTERING
65-
"""
66-
67-
with {:ok, prepared} <- prepare_with_realm(conn, realm, query),
68-
{:ok, %Xandra.Page{} = page} <- Xandra.execute(conn, prepared, %{}) do
69-
[%{"system.count(device_id)" => count}] = Enum.to_list(page)
70-
71-
{:ok, count}
72-
end
73-
end
74-
75-
# TODO: copypasted from Groups.Queries, this is going to be moved to Astarte.DataAccess
76-
# when we move everything to Xandra
77-
defp prepare_with_realm(conn, realm_name, query) do
78-
keyspace_name =
79-
CQLUtils.realm_name_to_keyspace_name(realm_name, Config.astarte_instance_id!())
80-
81-
with {:valid, true} <- {:valid, Realm.valid_name?(realm_name)},
82-
query_with_keyspace = String.replace(query, ":keyspace", keyspace_name),
83-
{:ok, prepared} <- Xandra.prepare(conn, query_with_keyspace) do
84-
{:ok, prepared}
85-
else
86-
{:valid, false} ->
87-
{:error, :not_found}
88-
89-
{:error, reason} ->
90-
_ = Logger.warning("Database error: #{inspect(reason)}.", tag: "db_error")
91-
{:error, :database_error}
92-
end
36+
online_query =
37+
from Device,
38+
hints: ["ALLOW FILTERING"],
39+
prefix: ^keyspace,
40+
where: [connected: true]
41+
42+
online_count = Repo.aggregate(online_query, :count)
43+
44+
%DevicesStats{
45+
total_devices: device_count,
46+
connected_devices: online_count
47+
}
9348
end
9449
end

apps/astarte_appengine_api/lib/astarte_appengine_api/stats/stats.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#
22
# This file is part of Astarte.
33
#
4-
# Copyright 2019 Ispirata Srl
4+
# Copyright 2019 - 2025 SECO Mind Srl
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
@@ -19,7 +19,7 @@
1919
defmodule Astarte.AppEngine.API.Stats do
2020
alias Astarte.AppEngine.API.Stats.Queries
2121

22-
def get_devices_stats(realm) do
23-
Queries.get_devices_stats(realm)
22+
def get_devices_stats(realm_name) do
23+
Queries.for_realm(realm_name)
2424
end
2525
end

0 commit comments

Comments
 (0)