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 support for device tags #191

Merged
merged 9 commits into from
Jun 8, 2022
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Add support for device tags ([#191](https://github.com/edgehog-device-manager/edgehog/pull/191))

## [0.5.1] - 2022-06-01
### Added
Expand Down
53 changes: 46 additions & 7 deletions backend/lib/edgehog/astarte.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ defmodule Edgehog.Astarte do
alias Edgehog.Repo

alias Astarte.Client.AppEngine
alias Ecto.Multi
alias Edgehog.Astarte.Cluster
alias Edgehog.Astarte.InterfaceID
alias Edgehog.Astarte.InterfaceVersion
alias Edgehog.Devices

alias Edgehog.Astarte.Device.{
BaseImage,
Expand Down Expand Up @@ -376,6 +378,7 @@ defmodule Edgehog.Astarte do
filters
|> Enum.reduce(Device, &filter_with/2)
|> Repo.all()
|> Repo.preload(:tags)
end

defp filter_with(filter, query) do
Expand Down Expand Up @@ -411,6 +414,20 @@ defmodule Edgehog.Astarte do
{:hardware_type_name, name} ->
from [hardware_type: ht] in ensure_hardware_type(query),
where: ilike(ht.name, ^"%#{name}%")

{:tag, tag} ->
# Need to filter tenant explicitly since prepare_query is not executed for subqueries
tenant_id = Repo.get_tenant_id()

device_ids_matching_tag =
from t in Devices.Tag,
where: ilike(t.name, ^"%#{tag}%") and t.tenant_id == ^tenant_id,
join: dt in Devices.DeviceTag,
on: t.id == dt.tag_id,
select: dt.device_id

from q in query,
where: q.id in subquery(device_ids_matching_tag)
end
end

Expand Down Expand Up @@ -470,6 +487,7 @@ defmodule Edgehog.Astarte do
"""
def get_device!(id) do
Repo.get!(Device, id)
|> Repo.preload(:tags)
end

@doc """
Expand All @@ -485,9 +503,13 @@ defmodule Edgehog.Astarte do

"""
def create_device(%Realm{} = realm, attrs \\ %{}) do
%Device{realm_id: realm.id, tenant_id: Repo.get_tenant_id()}
|> Device.changeset(attrs)
|> Repo.insert()
changeset =
%Device{realm_id: realm.id, tenant_id: Repo.get_tenant_id()}
|> Device.changeset(attrs)

with {:ok, device} <- Repo.insert(changeset) do
{:ok, Repo.preload(device, :tags)}
end
end

@doc """
Expand Down Expand Up @@ -518,9 +540,26 @@ defmodule Edgehog.Astarte do

"""
def update_device(%Device{} = device, attrs) do
device
|> Device.update_changeset(attrs)
|> Repo.update()
Multi.new()
|> Devices.ensure_tags_exist_multi(attrs)
|> Multi.update(:update_device, fn
%{ensure_tags_exist: nil} ->
device
|> Device.update_changeset(attrs)

%{ensure_tags_exist: tags} when is_list(tags) ->
device
|> Device.update_changeset(attrs)
|> Ecto.Changeset.put_assoc(:tags, tags)
end)
|> Repo.transaction()
|> case do
{:ok, %{update_device: device}} ->
{:ok, Repo.preload(device, :tags)}

{:error, _failed_operation, failed_value, _progress_so_far} ->
{:error, failed_value}
end
end

@doc """
Expand Down Expand Up @@ -566,7 +605,7 @@ defmodule Edgehog.Astarte do
"""
def fetch_realm_device(%Realm{id: realm_id}, device_id) do
case Repo.get_by(Device, realm_id: realm_id, device_id: device_id) do
%Device{} = device -> {:ok, device}
%Device{} = device -> {:ok, Repo.preload(device, :tags)}
nil -> {:error, :device_not_found}
end
end
Expand Down
1 change: 1 addition & 0 deletions backend/lib/edgehog/astarte/device.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ defmodule Edgehog.Astarte.Device do
type: :string

has_one :system_model, through: [:system_model_part_number, :system_model]
many_to_many :tags, Devices.Tag, join_through: Devices.DeviceTag, on_replace: :delete

timestamps()
end
Expand Down
53 changes: 53 additions & 0 deletions backend/lib/edgehog/devices.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ defmodule Edgehog.Devices do
alias Edgehog.Devices.SystemModelPartNumber
alias Edgehog.Devices.HardwareType
alias Edgehog.Devices.HardwareTypePartNumber
alias Edgehog.Devices.Tag
alias Edgehog.Assets

@doc """
Expand Down Expand Up @@ -420,4 +421,56 @@ defmodule Edgehog.Devices do
def change_system_model(%SystemModel{} = system_model, attrs \\ %{}) do
SystemModel.changeset(system_model, attrs)
end

@doc """
Inserts the tags passed in attrs within a multi transaction, normalizing them.

Returns the updated `%Ecto.Multi{}`.
"""
def ensure_tags_exist_multi(multi, %{tags: _tags} = attrs) do
multi
|> Multi.run(:cast_tags, fn _repo, _changes ->
data = %{}
types = %{tags: {:array, :string}}

changeset =
{data, types}
|> Ecto.Changeset.cast(attrs, Map.keys(types))

with {:ok, %{tags: tags}} <- Ecto.Changeset.apply_action(changeset, :insert) do
tenant_id = Repo.get_tenant_id()

now =
NaiveDateTime.utc_now()
|> NaiveDateTime.truncate(:second)

tag_maps =
for tag <- tags,
tag = normalize_tag(tag),
tag != "" do
%{name: tag, inserted_at: now, updated_at: now, tenant_id: tenant_id}
end

{:ok, tag_maps}
end
end)
|> Multi.insert_all(:insert_tags, Tag, & &1.cast_tags, on_conflict: :nothing)
|> Multi.run(:ensure_tags_exist, fn repo, %{cast_tags: tag_maps} ->
tag_names = for t <- tag_maps, do: t.name
{:ok, repo.all(from t in Tag, where: t.name in ^tag_names)}
end)
end

def ensure_tags_exist_multi(multi, _attrs) do
# No tags in the update, so we return nil for tags
Multi.run(multi, :ensure_tags_exist, fn _repo, _previous ->
{:ok, nil}
end)
end

defp normalize_tag(tag) do
tag
|> String.trim()
|> String.downcase()
end
end
30 changes: 30 additions & 0 deletions backend/lib/edgehog/devices/device_tag.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#
# This file is part of Edgehog.
#
# Copyright 2022 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

defmodule Edgehog.Devices.DeviceTag do
use Ecto.Schema

@primary_key false
schema "devices_tags" do
field :tenant_id, :integer, autogenerate: {Edgehog.Repo, :get_tenant_id, []}
rbino marked this conversation as resolved.
Show resolved Hide resolved
field :tag_id, :id, primary_key: true
field :device_id, :id, primary_key: true
end
end
39 changes: 39 additions & 0 deletions backend/lib/edgehog/devices/tag.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#
# This file is part of Edgehog.
#
# Copyright 2022 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

defmodule Edgehog.Devices.Tag do
use Ecto.Schema
import Ecto.Changeset

schema "tags" do
field :tenant_id, :integer, autogenerate: {Edgehog.Repo, :get_tenant_id, []}
field :name, :string

timestamps()
end

@doc false
def changeset(tag, attrs) do
tag
|> cast(attrs, [:name])
|> validate_required([:name])
|> unique_constraint([:name, :tenant_id])
end
end
6 changes: 6 additions & 0 deletions backend/lib/edgehog_web/resolvers/devices.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#

defmodule EdgehogWeb.Resolvers.Devices do
alias Edgehog.Astarte
alias Edgehog.Devices
alias Edgehog.Devices.HardwareType
alias Edgehog.Devices.SystemModel
Expand Down Expand Up @@ -168,4 +169,9 @@ defmodule EdgehogWeb.Resolvers.Devices do
_ -> {:ok, nil}
end
end

def extract_device_tags(%Astarte.Device{tags: tags}, _args, _context) do
tag_names = for t <- tags, do: t.name
{:ok, tag_names}
end
end
15 changes: 15 additions & 0 deletions backend/lib/edgehog_web/schema/astarte_types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ defmodule EdgehogWeb.Schema.AstarteTypes do
the name of the device's hardware type.
"""
field :hardware_type_name, :string

@desc """
A string to match against the tags of the device.
The match is case-insensitive and tests whether the string is included in \
one of the tags of the device.
"""
field :tag, :string
end

@desc """
Expand Down Expand Up @@ -409,6 +416,11 @@ defmodule EdgehogWeb.Schema.AstarteTypes do
@desc "The system model of the device."
field :system_model, :system_model

@desc "The tags of the device"
field :tags, non_null(list_of(non_null(:string))) do
resolve &Resolvers.Devices.extract_device_tags/3
rbino marked this conversation as resolved.
Show resolved Hide resolved
end

@desc "List of capabilities supported by the device."
field :capabilities, non_null(list_of(non_null(:device_capability))) do
resolve &Resolvers.Astarte.list_device_capabilities/3
Expand Down Expand Up @@ -538,6 +550,9 @@ defmodule EdgehogWeb.Schema.AstarteTypes do

@desc "The display name of the device."
field :name, :string

@desc "The tags of the device. These replace all the current tags."
field :tags, list_of(non_null(:string))
end

output do
Expand Down
38 changes: 38 additions & 0 deletions backend/priv/repo/migrations/20220426061607_create_tags.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#
# This file is part of Edgehog.
#
# Copyright 2022 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

defmodule Edgehog.Repo.Migrations.CreateTags do
use Ecto.Migration

def change do
create table(:tags) do
add :tenant_id, references(:tenants, column: :tenant_id, on_delete: :delete_all),
null: false

add :name, :string, null: false

timestamps()
end

create index(:tags, [:tenant_id])
create unique_index(:tags, [:id, :tenant_id])
create unique_index(:tags, [:name, :tenant_id])
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#
# This file is part of Edgehog.
#
# Copyright 2022 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

defmodule Edgehog.Repo.Migrations.CreateDevicesTags do
use Ecto.Migration

def change do
create table(:devices_tags, primary_key: false) do
add :tenant_id, references(:tenants, column: :tenant_id, on_delete: :delete_all),
null: false,
primary_key: true

add :tag_id,
references(:tags, with: [tenant_id: :tenant_id], match: :full, on_delete: :restrict),
null: false,
primary_key: true

add :device_id,
references(:devices, with: [tenant_id: :tenant_id], match: :full, on_delete: :delete_all),
null: false,
primary_key: true
end

create index(:devices_tags, [:tag_id, :tenant_id])
create index(:devices_tags, [:device_id, :tenant_id])
end
end
Loading