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

feat(containers): save last status message from device #730

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
3 changes: 2 additions & 1 deletion backend/lib/edgehog/containers/containers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ defmodule Edgehog.Containers do
define :deploy, action: :deploy, args: [:release_id, :device_id]
define :send_deploy_request, action: :send_deploy_request, args: [:deployment]
define :fetch_deployment, action: :read, get_by: [:id]
define :deployment_set_status, action: :set_status, args: [:status]
define :deployment_set_status, action: :set_status, args: [:status, :message]

define :delete_deployment, action: :destroy
end

Expand Down
6 changes: 5 additions & 1 deletion backend/lib/edgehog/containers/deployment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ defmodule Edgehog.Containers.Deployment do
end

update :set_status do
accept [:status]
accept [:status, :message]
end
end

Expand All @@ -98,6 +98,10 @@ defmodule Edgehog.Containers.Deployment do
public? true
end

attribute :message, :string do
public? true
end

timestamps()
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,14 @@ defmodule Edgehog.Triggers.Handler.ManualActions.HandleTrigger do

defp handle_event(%IncomingData{interface: @deployment_event} = event, tenant, _realm_id, _device_id, _timestamp) do
"/" <> deployment_id = event.path
status = event.value["status"]

%{
"status" => status,
"message" => message
} = event.value

with {:ok, deployment} <- Containers.fetch_deployment(deployment_id, tenant: tenant) do
Containers.deployment_set_status(deployment, status, tenant: tenant)
Containers.deployment_set_status(deployment, status, message, tenant: tenant)
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#
# This file is part of Edgehog.
#
# Copyright 2021-2024 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.DeploymentAddStatusMessage do
@moduledoc """
Updates resources based on their most recent snapshots.

This file was autogenerated with `mix ash_postgres.generate_migrations`
"""

use Ecto.Migration

def up do
alter table(:application_deployments) do
add :message, :text
end
end

def down do
alter table(:application_deployments) do
remove :message
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
{
"attributes": [
{
"allow_nil?": false,
"default": "fragment(\"gen_random_uuid()\")",
"generated?": false,
"primary_key?": true,
"references": null,
"size": null,
"source": "id",
"type": "uuid"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "status",
"type": "text"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "message",
"type": "text"
},
{
"allow_nil?": false,
"default": "fragment(\"(now() AT TIME ZONE 'utc')\")",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "inserted_at",
"type": "utc_datetime_usec"
},
{
"allow_nil?": false,
"default": "fragment(\"(now() AT TIME ZONE 'utc')\")",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "updated_at",
"type": "utc_datetime_usec"
},
{
"allow_nil?": false,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": {
"deferrable": false,
"destination_attribute": "tenant_id",
"destination_attribute_default": null,
"destination_attribute_generated": null,
"index?": false,
"match_type": null,
"match_with": null,
"multitenancy": {
"attribute": null,
"global": null,
"strategy": null
},
"name": "application_deployments_tenant_id_fkey",
"on_delete": "delete",
"on_update": null,
"primary_key?": true,
"schema": "public",
"table": "tenants"
},
"size": null,
"source": "tenant_id",
"type": "bigint"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": {
"deferrable": false,
"destination_attribute": "id",
"destination_attribute_default": null,
"destination_attribute_generated": null,
"index?": false,
"match_type": null,
"match_with": null,
"multitenancy": {
"attribute": "tenant_id",
"global": false,
"strategy": "attribute"
},
"name": "application_deployments_device_id_fkey",
"on_delete": null,
"on_update": null,
"primary_key?": true,
"schema": "public",
"table": "devices"
},
"size": null,
"source": "device_id",
"type": "bigint"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": {
"deferrable": false,
"destination_attribute": "id",
"destination_attribute_default": null,
"destination_attribute_generated": null,
"index?": false,
"match_type": null,
"match_with": null,
"multitenancy": {
"attribute": "tenant_id",
"global": false,
"strategy": "attribute"
},
"name": "application_deployments_release_id_fkey",
"on_delete": null,
"on_update": null,
"primary_key?": true,
"schema": "public",
"table": "application_releases"
},
"size": null,
"source": "release_id",
"type": "uuid"
}
],
"base_filter": null,
"check_constraints": [],
"custom_indexes": [
{
"all_tenants?": true,
"concurrently": false,
"error_fields": [
"id",
"tenant_id"
],
"fields": [
{
"type": "atom",
"value": "id"
},
{
"type": "atom",
"value": "tenant_id"
}
],
"include": null,
"message": null,
"name": null,
"nulls_distinct": true,
"prefix": null,
"table": null,
"unique": true,
"using": null,
"where": null
},
{
"all_tenants?": true,
"concurrently": false,
"error_fields": [
"tenant_id"
],
"fields": [
{
"type": "atom",
"value": "tenant_id"
}
],
"include": null,
"message": null,
"name": null,
"nulls_distinct": true,
"prefix": null,
"table": null,
"unique": false,
"using": null,
"where": null
}
],
"custom_statements": [],
"has_create_action": true,
"hash": "6A2085E7A763143A1D5EC2F5A52BE10B13A3EB39A3D25DD328F7C3B7F26DDD3D",
"identities": [
{
"all_tenants?": false,
"base_filter": null,
"index_name": "application_deployments_release_instance_index",
"keys": [
{
"type": "atom",
"value": "device_id"
},
{
"type": "atom",
"value": "release_id"
}
],
"name": "release_instance",
"nils_distinct?": true,
"where": null
}
],
"multitenancy": {
"attribute": "tenant_id",
"global": false,
"strategy": "attribute"
},
"repo": "Elixir.Edgehog.Repo",
"schema": null,
"table": "application_deployments"
}
Original file line number Diff line number Diff line change
Expand Up @@ -428,26 +428,51 @@ defmodule EdgehogWeb.Controllers.AstarteTriggerControllerTest do
interface: "io.edgehog.devicemanager.apps.DeploymentEvent",
path: "/" <> deployment.id,
value: %{
"status" => "Starting",
"message" => ""
"status" => "Error",
"message" => "error message"
}
},
timestamp: DateTime.to_iso8601(DateTime.utc_now())
}

path = Routes.astarte_trigger_path(conn, :process_event, tenant.slug)

conn =
conn
|> put_req_header("astarte-realm", realm.name)
|> post(path, deployment_event)
conn
|> put_req_header("astarte-realm", realm.name)
|> post(path, deployment_event)
|> response(200)

# Deployment must be reloaded from the db
deployment = Ash.get!(Edgehog.Containers.Deployment, deployment.id, tenant: tenant)

assert response(conn, 200)
assert deployment.status == :error
assert deployment.message == "error message"

deployment_event = %{
device_id: device.device_id,
event: %{
type: "incoming_data",
interface: "io.edgehog.devicemanager.apps.DeploymentEvent",
path: "/" <> deployment.id,
value: %{
"status" => "Starting",
"message" => ""
}
},
timestamp: DateTime.to_iso8601(DateTime.utc_now())
}

path = Routes.astarte_trigger_path(conn, :process_event, tenant.slug)

conn
|> put_req_header("astarte-realm", realm.name)
|> post(path, deployment_event)
|> response(200)

deployment = Ash.get!(Edgehog.Containers.Deployment, deployment.id, tenant: tenant)

assert deployment.status == :starting
assert deployment.message == nil
end

test "unset AvailableDeployments deletes an existing deployment", context do
Expand Down
Loading