Skip to content

Commit

Permalink
feat!(pagination): update_campaigns relay
Browse files Browse the repository at this point in the history
Part of #779
- Paginate `UpdateCampaign`'s `:update_targets` relationship with relay
- Paginate `updata_campaigns` query with relay

Signed-off-by: Luca Zaninotto <[email protected]>
  • Loading branch information
lusergit committed Jan 10, 2025
1 parent 096bd19 commit 354fc32
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Edgehog.
#
# Copyright 2023-2024 SECO Mind Srl
# Copyright 2023 - 2025 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.
Expand Down Expand Up @@ -40,6 +40,8 @@ defmodule Edgehog.UpdateCampaigns.UpdateCampaign do

graphql do
type :update_campaign

paginate_relationship_with update_targets: :relay
end

actions do
Expand Down
5 changes: 3 additions & 2 deletions backend/lib/edgehog/update_campaigns/update_campaigns.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Edgehog.
#
# Copyright 2023-2024 SECO Mind Srl
# Copyright 2023 - 2025 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.
Expand Down Expand Up @@ -41,7 +41,8 @@ defmodule Edgehog.UpdateCampaigns do

list UpdateCampaign, :update_campaigns, :read do
description "Returns a list of update campaigns."
paginate_with nil
paginate_with :keyset
relay? true
end

get UpdateChannel, :update_channel, :read do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Edgehog.
#
# Copyright 2023-2024 SECO Mind Srl
# Copyright 2023 - 2025 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.
Expand Down Expand Up @@ -62,7 +62,7 @@ defmodule EdgehogWeb.Schema.Mutation.CreateUpdateCampaignTest do
assert update_campaign_data["updateChannel"]["id"] == update_channel_id
assert update_campaign_data["updateChannel"]["name"] == update_channel.name
assert update_campaign_data["updateChannel"]["handle"] == update_channel.handle
assert [target_data] = update_campaign_data["updateTargets"]
assert [target_data] = extract_nodes!(update_campaign_data["updateTargets"]["edges"])
assert target_data["status"] == "IDLE"
assert target_data["device"]["id"] == AshGraphql.Resource.encode_relay_id(device)

Expand All @@ -81,7 +81,7 @@ defmodule EdgehogWeb.Schema.Mutation.CreateUpdateCampaignTest do
assert update_campaign_data["name"] == "My Update Campaign"
assert update_campaign_data["status"] == "FINISHED"
assert update_campaign_data["outcome"] == "SUCCESS"
assert update_campaign_data["updateTargets"] == []
assert update_campaign_data["updateTargets"]["edges"] == []

# Check that no executor got started
update_campaign = fetch_update_campaign_from_graphql_id!(tenant, update_campaign_data["id"])
Expand Down Expand Up @@ -353,9 +353,13 @@ defmodule EdgehogWeb.Schema.Mutation.CreateUpdateCampaignTest do
handle
}
updateTargets {
status
device {
id
edges {
node {
status
device {
id
}
}
}
}
}
Expand Down Expand Up @@ -461,4 +465,8 @@ defmodule EdgehogWeb.Schema.Mutation.CreateUpdateCampaignTest do
[{pid, _}] -> {:ok, pid}
end
end

defp extract_nodes!(data) do
Enum.map(data, &Map.fetch!(&1, "node"))
end
end
38 changes: 25 additions & 13 deletions backend/test/edgehog_web/schema/query/update_campaign_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Edgehog.
#
# Copyright 2023-2024 SECO Mind Srl
# Copyright 2023 - 2025 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.
Expand Down Expand Up @@ -92,7 +92,7 @@ defmodule EdgehogWeb.Schema.Query.UpdateCampaignTest do
assert response_rollout_mechanism["forceDowngrade"] ==
update_campaign.rollout_mechanism.value.force_downgrade

assert [target] = update_campaign_data["updateTargets"]
assert [target] = extract_nodes!(update_campaign_data["updateTargets"]["edges"])
assert target["status"] == "IDLE"

assert target["device"]["id"] == AshGraphql.Resource.encode_relay_id(device)
Expand Down Expand Up @@ -120,13 +120,17 @@ defmodule EdgehogWeb.Schema.Query.UpdateCampaignTest do
query ($id: ID!) {
updateCampaign(id: $id) {
updateTargets {
id
status
retryCount
latestAttempt
completionTimestamp
otaOperation {
id
edges {
node {
id
status
retryCount
latestAttempt
completionTimestamp
otaOperation {
id
}
}
}
}
}
Expand All @@ -138,7 +142,7 @@ defmodule EdgehogWeb.Schema.Query.UpdateCampaignTest do
|> update_campaign_query()
|> extract_result!()

assert [update_target] = update_campaign_data["updateTargets"]
assert [update_target] = extract_nodes!(update_campaign_data["updateTargets"]["edges"])

assert update_target["id"] == AshGraphql.Resource.encode_relay_id(target)
assert update_target["status"] == "SUCCESSFUL"
Expand Down Expand Up @@ -264,9 +268,13 @@ defmodule EdgehogWeb.Schema.Query.UpdateCampaignTest do
handle
}
updateTargets {
status
device {
id
edges {
node {
status
device {
id
}
}
}
}
}
Expand Down Expand Up @@ -305,4 +313,8 @@ defmodule EdgehogWeb.Schema.Query.UpdateCampaignTest do

id
end

defp extract_nodes!(data) do
Enum.map(data, &Map.fetch!(&1, "node"))
end
end
102 changes: 64 additions & 38 deletions backend/test/edgehog_web/schema/query/update_campaigns_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Edgehog.
#
# Copyright 2023-2024 SECO Mind Srl
# Copyright 2023 - 2025 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.
Expand Down Expand Up @@ -65,7 +65,8 @@ defmodule EdgehogWeb.Schema.Query.UpdateCampaignsTest do
tenant: tenant
)

[update_campaign_data] = [tenant: tenant] |> update_campaigns_query() |> extract_result!()
[update_campaign_data] =
[tenant: tenant] |> update_campaigns_query() |> extract_result!() |> extract_nodes!()

assert update_campaign_data["name"] == update_campaign.name
assert update_campaign_data["status"] == "IDLE"
Expand All @@ -91,7 +92,7 @@ defmodule EdgehogWeb.Schema.Query.UpdateCampaignsTest do
assert response_rollout_mechanism["forceDowngrade"] ==
update_campaign.rollout_mechanism.value.force_downgrade

assert [target] = update_campaign_data["updateTargets"]
assert [target] = extract_nodes!(update_campaign_data["updateTargets"]["edges"])
assert target["status"] == "IDLE"

assert target["device"]["id"] == AshGraphql.Resource.encode_relay_id(device)
Expand All @@ -104,24 +105,35 @@ defmodule EdgehogWeb.Schema.Query.UpdateCampaignsTest do
document = """
query {
updateCampaigns {
updateTargets {
id
status
retryCount
latestAttempt
completionTimestamp
otaOperation {
id
edges {
node {
updateTargets {
edges {
node {
id
status
retryCount
latestAttempt
completionTimestamp
otaOperation {
id
}
}
}
}
}
}
}
}
"""

[update_campaign_data] =
[document: document, tenant: tenant] |> update_campaigns_query() |> extract_result!()
[document: document, tenant: tenant]
|> update_campaigns_query()
|> extract_result!()
|> extract_nodes!()

assert [update_target] = update_campaign_data["updateTargets"]
assert [update_target] = extract_nodes!(update_campaign_data["updateTargets"]["edges"])

assert update_target["id"] == AshGraphql.Resource.encode_relay_id(target)
assert update_target["status"] == "FAILED"
Expand All @@ -138,30 +150,38 @@ defmodule EdgehogWeb.Schema.Query.UpdateCampaignsTest do
default_document = """
query {
updateCampaigns {
name
status
outcome
rolloutMechanism {
... on PushRollout {
maxFailurePercentage
maxInProgressUpdates
otaRequestRetries
otaRequestTimeoutSeconds
forceDowngrade
}
}
baseImage {
version
url
}
updateChannel {
name
handle
}
updateTargets {
status
device {
id
edges {
node {
name
status
outcome
rolloutMechanism {
... on PushRollout {
maxFailurePercentage
maxInProgressUpdates
otaRequestRetries
otaRequestTimeoutSeconds
forceDowngrade
}
}
baseImage {
version
url
}
updateChannel {
name
handle
}
updateTargets {
edges {
node {
status
device {
id
}
}
}
}
}
}
}
Expand All @@ -177,7 +197,9 @@ defmodule EdgehogWeb.Schema.Query.UpdateCampaignsTest do
defp extract_result!(result) do
assert %{
data: %{
"updateCampaigns" => update_campaigns
"updateCampaigns" => %{
"edges" => update_campaigns
}
}
} = result

Expand All @@ -187,4 +209,8 @@ defmodule EdgehogWeb.Schema.Query.UpdateCampaignsTest do

update_campaigns
end

defp extract_nodes!(data) do
Enum.map(data, &Map.fetch!(&1, "node"))
end
end

0 comments on commit 354fc32

Please sign in to comment.