Skip to content

Commit 0bbbf5d

Browse files
committed
Move keyspace translations to public functions in query files
Signed-off-by: Eddy Babetto <eddy.babetto@secomind.com>
1 parent 57d2b5b commit 0bbbf5d

File tree

9 files changed

+68
-84
lines changed

9 files changed

+68
-84
lines changed

apps/astarte_appengine_api/lib/astarte_appengine_api/auth/auth.ex

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,10 @@
1818

1919
defmodule Astarte.AppEngine.API.Auth do
2020
alias Astarte.AppEngine.API.Queries
21-
alias Astarte.AppEngine.API.Config
22-
alias Astarte.Core.CQLUtils
2321

2422
require Logger
2523

2624
def fetch_public_key(realm) do
27-
keyspace = CQLUtils.realm_name_to_keyspace_name(realm, Config.astarte_instance_id!())
28-
29-
Queries.fetch_public_key(keyspace)
25+
Queries.fetch_public_key(realm)
3026
end
3127
end

apps/astarte_appengine_api/lib/astarte_appengine_api/device/device.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ defmodule Astarte.AppEngine.API.Device do
4040
alias Astarte.DataAccess.Device, as: DeviceQueries
4141
alias Astarte.DataAccess.Interface, as: InterfaceQueries
4242
alias Ecto.Changeset
43-
alias Astarte.Core.CQLUtils
43+
4444
require Logger
4545

4646
def list_devices!(realm_name, params) do

apps/astarte_appengine_api/lib/astarte_appengine_api/device/queries.ex

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ defmodule Astarte.AppEngine.API.Device.Queries do
3131
alias Astarte.Core.CQLUtils
3232
alias Astarte.Core.Device
3333
alias Astarte.Core.InterfaceDescriptor
34-
alias Astarte.AppEngine.API.Realm
34+
alias Astarte.AppEngine.API.Realm, as: DataAccessRealm
3535
alias Astarte.AppEngine.API.Devices.Device, as: DatabaseDevice
3636
alias Astarte.AppEngine.API.Endpoint, as: DatabaseEndpoint
3737

3838
require CQEx
3939
require Logger
4040

4141
def retrieve_interfaces_list(realm_name, device_id) do
42-
keyspace = keyspace_name(realm_name)
42+
keyspace = DataAccessRealm.keyspace_name(realm_name)
4343

4444
query =
4545
from d in DatabaseDevice,
@@ -53,7 +53,7 @@ defmodule Astarte.AppEngine.API.Device.Queries do
5353
end
5454

5555
def retrieve_all_endpoint_ids_for_interface!(realm_name, interface_id, opts \\ []) do
56-
keyspace = keyspace_name(realm_name)
56+
keyspace = DataAccessRealm.keyspace_name(realm_name)
5757

5858
query =
5959
from DatabaseEndpoint,
@@ -71,7 +71,7 @@ defmodule Astarte.AppEngine.API.Device.Queries do
7171
end
7272

7373
def retrieve_all_endpoints_for_interface!(realm_name, interface_id) do
74-
keyspace = keyspace_name(realm_name)
74+
keyspace = DataAccessRealm.keyspace_name(realm_name)
7575

7676
query =
7777
from DatabaseEndpoint,
@@ -83,7 +83,7 @@ defmodule Astarte.AppEngine.API.Device.Queries do
8383
end
8484

8585
def retrieve_mapping(realm_name, interface_id, endpoint_id) do
86-
keyspace = keyspace_name(realm_name)
86+
keyspace = DataAccessRealm.keyspace_name(realm_name)
8787

8888
query =
8989
from DatabaseEndpoint,
@@ -105,7 +105,7 @@ defmodule Astarte.AppEngine.API.Device.Queries do
105105
end
106106

107107
def interface_has_explicit_timestamp?(realm_name, interface_id) do
108-
keyspace = keyspace_name(realm_name)
108+
keyspace = DataAccessRealm.keyspace_name(realm_name)
109109
do_interface_has_explicit_timestamp?(keyspace, interface_id)
110110
end
111111

@@ -125,7 +125,7 @@ defmodule Astarte.AppEngine.API.Device.Queries do
125125
end
126126

127127
def fetch_datastream_maximum_storage_retention(realm_name) do
128-
keyspace = keyspace_name(realm_name)
128+
keyspace = DataAccessRealm.keyspace_name(realm_name)
129129
group = "realm_config"
130130
key = "datastream_maximum_storage_retention"
131131

@@ -145,16 +145,19 @@ defmodule Astarte.AppEngine.API.Device.Queries do
145145
opts
146146
) do
147147
columns = default_endpoint_column_selection(endpoint_row)
148+
keyspace = DataAccessRealm.keyspace_name(realm_name)
148149

149150
opts = %{opts | limit: 1}
150151

151-
do_get_datastream_values(realm_name, device_id, interface_row, endpoint_id, path, opts)
152+
do_get_datastream_values(keyspace, device_id, interface_row, endpoint_id, path, opts)
152153
|> select(^columns)
153154
|> Repo.fetch_one()
154155
end
155156

156157
def retrieve_all_endpoint_paths!(realm_name, device_id, interface_id, endpoint_id) do
157-
find_endpoints(realm_name, "individual_properties", device_id, interface_id, endpoint_id)
158+
keyspace = DataAccessRealm.keyspace_name(realm_name)
159+
160+
find_endpoints(keyspace, "individual_properties", device_id, interface_id, endpoint_id)
158161
|> select([:path])
159162
|> Repo.all()
160163
end
@@ -176,7 +179,7 @@ defmodule Astarte.AppEngine.API.Device.Queries do
176179
# TODO: use received value_timestamp when needed
177180
# TODO: :reception_timestamp_submillis is just a place holder right now
178181

179-
keyspace = keyspace_name(realm_name)
182+
keyspace = DataAccessRealm.keyspace_name(realm_name)
180183

181184
# Ecto expects microsecond precision
182185
{reception, reception_submillis} = split_datetime_to_ms_and_submillis(reception_timestamp)
@@ -223,7 +226,7 @@ defmodule Astarte.AppEngine.API.Device.Queries do
223226

224227
# TODO: :reception_timestamp_submillis is just a place holder right now
225228
%InterfaceDescriptor{interface_id: interface_id, storage: storage} = interface_descriptor
226-
keyspace_name = Realm.keyspace_name(realm_name)
229+
keyspace_name = DataAccessRealm.keyspace_name(realm_name)
227230

228231
q =
229232
from v in storage,
@@ -257,7 +260,7 @@ defmodule Astarte.AppEngine.API.Device.Queries do
257260
opts
258261
) do
259262
value_column = CQLUtils.type_to_db_column_name(endpoint.value_type)
260-
keyspace = keyspace_name(realm_name)
263+
keyspace = DataAccessRealm.keyspace_name(realm_name)
261264

262265
{timestamp_ms, timestamp_submillis} = split_ms_and_submillis(timestamp)
263266

@@ -295,7 +298,7 @@ defmodule Astarte.AppEngine.API.Device.Queries do
295298
opts
296299
) do
297300
value_column = CQLUtils.type_to_db_column_name(endpoint.value_type)
298-
keyspace = keyspace_name(realm_name)
301+
keyspace = DataAccessRealm.keyspace_name(realm_name)
299302
{timestamp_ms, timestamp_submillis} = split_ms_and_submillis(timestamp)
300303

301304
attributes = %{
@@ -325,7 +328,7 @@ defmodule Astarte.AppEngine.API.Device.Queries do
325328
timestamp,
326329
opts
327330
) do
328-
keyspace = keyspace_name(realm_name)
331+
keyspace = DataAccessRealm.keyspace_name(realm_name)
329332
interface_id = interface_descriptor.interface_id
330333

331334
endpoint_rows =
@@ -452,7 +455,7 @@ defmodule Astarte.AppEngine.API.Device.Queries do
452455
end
453456

454457
def retrieve_device_status(realm_name, device_id) do
455-
keyspace = keyspace_name(realm_name)
458+
keyspace = DataAccessRealm.keyspace_name(realm_name)
456459
fields = [:device_id | @device_status_columns_without_device_id]
457460

458461
query = from(DatabaseDevice, prefix: ^keyspace, select: ^fields)
@@ -470,7 +473,7 @@ defmodule Astarte.AppEngine.API.Device.Queries do
470473
end
471474

472475
def retrieve_devices_list(realm_name, limit, retrieve_details?, previous_token) do
473-
keyspace = keyspace_name(realm_name)
476+
keyspace = DataAccessRealm.keyspace_name(realm_name)
474477

475478
field_selection =
476479
if retrieve_details? do
@@ -517,7 +520,7 @@ defmodule Astarte.AppEngine.API.Device.Queries do
517520
end
518521

519522
def device_alias_to_device_id(realm_name, device_alias) do
520-
keyspace = keyspace_name(realm_name)
523+
keyspace = DataAccessRealm.keyspace_name(realm_name)
521524
do_device_alias_to_device_id(keyspace, device_alias)
522525
end
523526

@@ -532,7 +535,7 @@ defmodule Astarte.AppEngine.API.Device.Queries do
532535
end
533536

534537
def insert_attribute(realm_name, device_id, attribute_key, attribute_value) do
535-
keyspace = keyspace_name(realm_name)
538+
keyspace = DataAccessRealm.keyspace_name(realm_name)
536539
new_attribute = %{attribute_key => attribute_value}
537540

538541
query =
@@ -547,7 +550,7 @@ defmodule Astarte.AppEngine.API.Device.Queries do
547550
end
548551

549552
def delete_attribute(realm_name, device_id, attribute_key) do
550-
keyspace = keyspace_name(realm_name)
553+
keyspace = DataAccessRealm.keyspace_name(realm_name)
551554
query = from(d in DatabaseDevice, select: d.attributes)
552555
opts = [prefix: keyspace, consistency: :quorum]
553556

@@ -574,7 +577,7 @@ defmodule Astarte.AppEngine.API.Device.Queries do
574577
end
575578

576579
def insert_alias(realm_name, device_id, alias_tag, alias_value) do
577-
keyspace = keyspace_name(realm_name)
580+
keyspace = DataAccessRealm.keyspace_name(realm_name)
578581

579582
name = %Name{
580583
object_name: alias_value,
@@ -615,7 +618,7 @@ defmodule Astarte.AppEngine.API.Device.Queries do
615618
end
616619

617620
def delete_alias(realm_name, device_id, alias_tag) do
618-
keyspace = keyspace_name(realm_name)
621+
keyspace = DataAccessRealm.keyspace_name(realm_name)
619622

620623
query =
621624
from d in DatabaseDevice,
@@ -665,7 +668,7 @@ defmodule Astarte.AppEngine.API.Device.Queries do
665668
end
666669

667670
def set_inhibit_credentials_request(realm_name, device_id, inhibit_credentials_request) do
668-
keyspace = keyspace_name(realm_name)
671+
keyspace = DataAccessRealm.keyspace_name(realm_name)
669672

670673
query =
671674
from DatabaseDevice,
@@ -679,7 +682,7 @@ defmodule Astarte.AppEngine.API.Device.Queries do
679682
end
680683

681684
def retrieve_object_datastream_values(realm_name, device_id, interface_row, path, columns, opts) do
682-
keyspace = keyspace_name(realm_name)
685+
keyspace = DataAccessRealm.keyspace_name(realm_name)
683686

684687
query_limit = query_limit(opts)
685688
timestamp_column = timestamp_column(opts.explicit_timestamp)
@@ -710,9 +713,10 @@ defmodule Astarte.AppEngine.API.Device.Queries do
710713
) do
711714
value_column = CQLUtils.type_to_db_column_name(endpoint_row.value_type) |> String.to_atom()
712715
columns = [:path, value_column]
716+
keyspace = DataAccessRealm.keyspace_name(realm_name)
713717

714718
find_endpoints(
715-
realm_name,
719+
keyspace,
716720
interface_row.storage,
717721
device_id,
718722
interface_row.interface_id,
@@ -732,9 +736,10 @@ defmodule Astarte.AppEngine.API.Device.Queries do
732736
opts
733737
) do
734738
columns = default_endpoint_column_selection(endpoint_row)
739+
keyspace = DataAccessRealm.keyspace_name(realm_name)
735740

736741
query =
737-
do_get_datastream_values(realm_name, device_id, interface_row, endpoint_id, path, opts)
742+
do_get_datastream_values(keyspace, device_id, interface_row, endpoint_id, path, opts)
738743

739744
values = query |> select(^columns) |> Repo.all()
740745
count = query |> select([d], count(d.value_timestamp)) |> Repo.one!()
@@ -743,22 +748,20 @@ defmodule Astarte.AppEngine.API.Device.Queries do
743748
end
744749

745750
def value_type_query(realm_name, interface_id, endpoint_id) do
746-
keyspace = keyspace_name(realm_name)
751+
keyspace = DataAccessRealm.keyspace_name(realm_name)
747752
query = from DatabaseEndpoint, select: [:value_type]
748753

749754
Repo.get_by!(query, [interface_id: interface_id, endpoint_id: endpoint_id], prefix: keyspace)
750755
end
751756

752757
defp do_get_datastream_values(
753-
realm_name,
758+
keyspace,
754759
device_id,
755760
interface_row,
756761
endpoint_id,
757762
path,
758763
opts
759764
) do
760-
keyspace = keyspace_name(realm_name)
761-
762765
query_limit = query_limit(opts)
763766

764767
# Check the explicit user defined limit to know if we have to reorder data
@@ -813,9 +816,7 @@ defmodule Astarte.AppEngine.API.Device.Queries do
813816
|> where(^filter_to)
814817
end
815818

816-
defp find_endpoints(realm_name, table_name, device_id, interface_id, endpoint_id) do
817-
keyspace = keyspace_name(realm_name)
818-
819+
defp find_endpoints(keyspace, table_name, device_id, interface_id, endpoint_id) do
819820
from(table_name, prefix: ^keyspace)
820821
|> where(device_id: ^device_id, interface_id: ^interface_id, endpoint_id: ^endpoint_id)
821822
end
@@ -833,13 +834,6 @@ defmodule Astarte.AppEngine.API.Device.Queries do
833834
[value_column | default_endpoint_column_selection()]
834835
end
835836

836-
defp keyspace_name(realm_name) do
837-
Astarte.Core.CQLUtils.realm_name_to_keyspace_name(
838-
realm_name,
839-
Astarte.DataAccess.Config.astarte_instance_id!()
840-
)
841-
end
842-
843837
defp timestamp_column(explicit_timestamp?) do
844838
case explicit_timestamp? do
845839
nil -> :reception_timestamp

0 commit comments

Comments
 (0)