Skip to content

Commit

Permalink
refactor(appengine): device_alias_to_device_id as ecto query
Browse files Browse the repository at this point in the history
refactor device_alias_to_device_id to use ecto and exandra

Signed-off-by: Francesco Noacco <[email protected]>
  • Loading branch information
noaccOS committed Jan 22, 2025
1 parent 415362f commit 00f083f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -990,12 +990,12 @@ defmodule Astarte.AppEngine.API.Device do
end

def device_alias_to_device_id(realm_name, device_alias) do
with {:ok, client} <- Database.connect(realm: realm_name) do
Queries.device_alias_to_device_id(client, device_alias)
else
not_ok ->
_ = Logger.warning("Database error: #{inspect(not_ok)}.", tag: "db_error")
{:error, :database_error}
result =
Queries.device_alias_to_device_id(realm_name, device_alias)
|> Repo.fetch_one(consistency: :quorum, error: :device_not_found)

with {:ok, name} <- result do
{:ok, name.object_uuid}
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ defmodule Astarte.AppEngine.API.Device.Queries do
alias Astarte.DataAccess.Realms.DeletionInProgress, as: DatabaseDeletionInProgress
alias Astarte.DataAccess.Realms.IndividualDatastream, as: DatabaseIndividualDatastream
alias Astarte.DataAccess.Realms.IndividualProperty, as: DatabaseIndividualProperty
alias Astarte.DataAccess.Realms.Name, as: DatabaseName
alias Astarte.DataAccess.Astarte.KvStore
alias Astarte.DataAccess.Astarte.Realm

Expand Down Expand Up @@ -161,32 +162,6 @@ defmodule Astarte.AppEngine.API.Device.Queries do
limit: ^limit
end

def device_alias_to_device_id(client, device_alias) do
device_id_statement = """
SELECT object_uuid
FROM names
WHERE object_name = :device_alias AND object_type = 1
"""

device_id_query =
DatabaseQuery.new()
|> DatabaseQuery.statement(device_id_statement)
|> DatabaseQuery.put(:device_alias, device_alias)
|> DatabaseQuery.consistency(:quorum)

with {:ok, result} <- DatabaseQuery.call(client, device_id_query),
[object_uuid: device_id] <- DatabaseResult.head(result) do
{:ok, device_id}
else
:empty_dataset ->
{:error, :device_not_found}

not_ok ->
_ = Logger.warning("Database error: #{inspect(not_ok)}.", tag: "db_error")
{:error, :database_error}
end
end

def insert_attribute(client, device_id, attribute_key, attribute_value) do
insert_attribute_statement = """
UPDATE devices
Expand Down Expand Up @@ -433,6 +408,15 @@ defmodule Astarte.AppEngine.API.Device.Queries do
end
end

def device_alias_to_device_id(realm_name, device_alias) do
keyspace = Realm.keyspace_name(realm_name)

from DatabaseName,
prefix: ^keyspace,
select: [:object_uuid],
where: [object_type: 1, object_name: ^device_alias]
end

def set_inhibit_credentials_request(client, device_id, inhibit_credentials_request) do
statement = """
UPDATE devices
Expand Down

0 comments on commit 00f083f

Please sign in to comment.