|
17 | 17 | # |
18 | 18 |
|
19 | 19 | 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 |
21 | 24 | 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 | | - } |
34 | 25 |
|
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 |
43 | 27 |
|
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 |
49 | 29 |
|
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) |
53 | 32 |
|
54 | | - {:ok, count} |
55 | | - end |
56 | | - end |
| 33 | + device_count = Repo.aggregate(Device, :count, prefix: keyspace) |
57 | 34 |
|
58 | | - defp get_connected_devices_count(conn, realm) do |
59 | 35 | # 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 | + } |
93 | 48 | end |
94 | 49 | end |
0 commit comments