Skip to content

Commit

Permalink
Merge pull request #9 from upmaru/feature/ops-570-global-basic-setting
Browse files Browse the repository at this point in the history
Use basic global config
  • Loading branch information
zacksiri committed Feb 28, 2024
2 parents dba1109 + c94a69e commit 048d508
Show file tree
Hide file tree
Showing 13 changed files with 266 additions and 28 deletions.
71 changes: 71 additions & 0 deletions lib/polar/globals.ex
Original file line number Diff line number Diff line change
@@ -1,2 +1,73 @@
defmodule Polar.Globals do
alias Polar.Repo
alias Polar.Globals.Basic
alias Polar.Globals.Setting

@mappings %{
"basic" => Basic
}

def get(key) do
Setting
|> Repo.get_by(key: key)
|> case do
nil ->
module = Map.fetch!(@mappings, key)

struct(module, %{})

%Setting{value: value} ->
module = Map.fetch!(@mappings, key)

module.parse!(:erlang.binary_to_term(value))
end
end

def save(key, value) when is_map(value) do
case Repo.get_by(Setting, key: key) do
nil ->
create(key, value)

%Setting{} = setting ->
update(setting, value)
end
end

defp create(key, value) when is_map(value) do
module = Map.fetch!(@mappings, key)

case module.parse(value) do
{:ok, params} = result ->
value =
Map.from_struct(params)
|> :erlang.term_to_binary()

Repo.insert!(%Setting{key: key, value: value})

result

error ->
error
end
end

defp update(setting, value) when is_map(value) do
module = Map.fetch!(@mappings, setting.key)

case module.parse(value) do
{:ok, params} = result ->
value =
Map.from_struct(params)
|> :erlang.term_to_binary()

setting
|> Setting.changeset(%{value: value})
|> Repo.update!()

result

error ->
error
end
end
end
30 changes: 30 additions & 0 deletions lib/polar/globals/basic.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule Polar.Globals.Basic do
use Ecto.Schema
import Ecto.Changeset

@primary_key false

embedded_schema do
field :enable_registration, :boolean, default: true
field :versions_per_product, :integer, default: 1
end

def changeset(basic, attrs \\ %{}) do
basic
|> cast(attrs, [:enable_registration, :versions_per_product])
|> validate_required([:enable_registration, :versions_per_product])
|> validate_number(:versions_per_product, less_than_or_equal_to: 3)
end

def parse!(value) do
%__MODULE__{}
|> changeset(value)
|> apply_action!(:insert)
end

def parse(value) do
%__MODULE__{}
|> changeset(value)
|> apply_action(:insert)
end
end
1 change: 1 addition & 0 deletions lib/polar/globals/setting.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ defmodule Polar.Globals.Setting do
setting
|> cast(attrs, [:key, :value])
|> validate_required([:key, :value])
|> validate_inclusion(:key, ["basic"])
end
end
4 changes: 4 additions & 0 deletions lib/polar/streams.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ defmodule Polar.Streams do
to: Version.Manager,
as: :create

defdelegate deactivate_previous_versions(version),
to: Version.Manager,
as: :deactivate_previous

alias __MODULE__.Item

defdelegate record_item_access(item, space_credential),
Expand Down
30 changes: 30 additions & 0 deletions lib/polar/streams/version/manager.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
defmodule Polar.Streams.Version.Manager do
alias Polar.Repo
alias Polar.Globals
alias Polar.Streams.Version

import Ecto.Query, only: [from: 2]

def create(product, attrs) do
%Version{product_id: product.id}
|> Version.changeset(attrs)
|> Repo.insert()
|> case do
{:ok, version} = result ->
deactivate_previous(version)

result

error ->
error
end
end

def deactivate_previous(version) do
basic_setting = Globals.get("basic")
bot = Polar.Accounts.Automation.get_bot!()

from(
v in Version,
where:
v.product_id == ^version.product_id and
v.current_state == ^"active",
offset: ^basic_setting.versions_per_product,
order_by: [desc: :inserted_at]
)
|> Repo.all()
|> Enum.each(fn v ->
Eventful.Transit.perform(v, bot, "deactivate")
end)
end
end
39 changes: 39 additions & 0 deletions test/polar/globals_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
defmodule Polar.GlobalsTest do
use Polar.DataCase, async: true

alias Polar.Globals.Basic

describe "create" do
test "can create basic setting" do
{:ok, setting} = Polar.Globals.save("basic", %{versions_per_product: 3})

assert %Basic{versions_per_product: 3} = setting
end
end

describe "get" do
setup do
{:ok, _setting} = Polar.Globals.save("basic", %{versions_per_product: 3})

:ok
end

test "can get basic setting" do
assert %Basic{versions_per_product: 3} = Polar.Globals.get("basic")
end
end

describe "update" do
setup do
{:ok, _setting} = Polar.Globals.save("basic", %{versions_per_product: 3})

:ok
end

test "can change basic setting" do
{:ok, setting} = Polar.Globals.save("basic", %{versions_per_product: 2})

assert %Basic{versions_per_product: 2} = setting
end
end
end
4 changes: 4 additions & 0 deletions test/polar/streams/item/manager_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ defmodule Polar.Streams.Item.ManagerTest do
setup do
user = user_fixture()

password = Accounts.generate_automation_password()

_bot = bot_fixture(%{password: password})

{:ok, space} = Accounts.create_space(user, %{name: "test-item-increment"})

{:ok, credential} =
Expand Down
10 changes: 10 additions & 0 deletions test/polar/streams/product/manager_test.exs
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
defmodule Polar.Streams.Product.ManagerTest do
use Polar.DataCase, async: true

alias Polar.Accounts
alias Polar.Streams
alias Polar.Streams.Product

import Polar.AccountsFixtures
import Polar.StreamsFixtures

setup do
password = Accounts.generate_automation_password()

_bot = bot_fixture(%{password: password})

:ok
end

describe "filter" do
setup do
{:ok, %Product{} = without_active_versions} =
Expand Down
87 changes: 61 additions & 26 deletions test/polar/streams/version/manager_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ defmodule Polar.Streams.Version.ManagerTest do
use Polar.DataCase, async: true

alias Polar.Streams
alias Polar.Accounts

import Polar.AccountsFixtures
import Polar.StreamsFixtures

setup do
password = Accounts.generate_automation_password()

_bot = bot_fixture(%{password: password})

{:ok, product} =
Streams.create_product(%{
aliases: ["alpine/3.19", "alpine/3.19/default"],
Expand All @@ -23,32 +31,59 @@ defmodule Polar.Streams.Version.ManagerTest do
describe "create_version" do
test "can successfully create new version", %{product: product} do
assert {:ok, _version} =
Streams.create_version(product, %{
serial: "20240209_13:00",
items: [
%{
name: "lxd.tar.gz",
file_type: "lxd.tar.gz",
hash: "35363f3d086271ed5402d61ab18ec03987bed51758c00079b8c9d372ff6d62dd",
size: 876,
is_metadata: true,
path: "images/alpine/edge/amd64/default/20240209_13:00/incus.tar.xz",
combined_hashes: [
%{
name: "combined_squashfs_sha256",
hash: "a9f02be498bf52b7bac7b5b1cfceb115878d257ad86a359a969e61fbd4bfe0bf"
}
]
},
%{
name: "root.squashfs",
file_type: "squashfs",
hash: "47cc4070da1bf17d8364c390…3603f4ed7e9e46582e690d2",
size: 2_982_800,
path: "images/alpine/edge/amd64/default/20240209_13:00/rootfs.tar.xz"
}
]
})
Streams.create_version(product, valid_version_attributes(2))
end
end

describe "deactivate old version on create" do
setup %{product: product} do
{:ok, version} =
Streams.create_version(product, valid_version_attributes(2))

%{existing_version: version}
end

test "creating a new version deactivates old versions", %{
product: product,
existing_version: existing_version
} do
assert {:ok, _version} =
Streams.create_version(product, valid_version_attributes(3))

existing_version = Repo.reload(existing_version)

assert existing_version.current_state == "inactive"
end
end

describe "keep 2 previous version" do
setup %{product: product} do
Polar.Globals.save("basic", %{versions_per_product: 2})

{:ok, version3} =
Streams.create_version(product, valid_version_attributes(3))

{:ok, version4} =
Streams.create_version(product, valid_version_attributes(4))

%{existing_version: version4, to_be_inactive: version3}
end

test "create new version deactivate version 3", %{
product: product,
existing_version: existing_version,
to_be_inactive: to_be_inactive
} do
assert {:ok, _version} =
Streams.create_version(product, valid_version_attributes(5))

to_be_inactive = Repo.reload(to_be_inactive)

assert to_be_inactive.current_state == "inactive"

existing_version = Repo.reload(existing_version)

assert existing_version.current_state == "active"
end
end
end
5 changes: 5 additions & 0 deletions test/polar/streams/version/transitions_test.exs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
defmodule Polar.Streams.Version.TransitionsTest do
use Polar.DataCase, async: true

alias Polar.Accounts
alias Polar.Streams

import Polar.AccountsFixtures

setup do
user = user_fixture()

password = Accounts.generate_automation_password()

_bot = bot_fixture(%{password: password})

{:ok, product} =
Streams.create_product(%{
aliases: ["alpine/3.19", "alpine/3.19/default"],
Expand Down
4 changes: 4 additions & 0 deletions test/polar_web/controllers/stream_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ defmodule PolarWeb.StreamControllerTest do
setup do
user = user_fixture()

password = Accounts.generate_automation_password()

_bot = bot_fixture(%{password: password})

{:ok, space} = Accounts.create_space(user, %{name: "some-test-123"})

{:ok, credential} =
Expand Down
4 changes: 4 additions & 0 deletions test/polar_web/live/root_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ defmodule PolarWeb.RootLiveTest do
setup do
user = user_fixture()

password = Accounts.generate_automation_password()

_bot = bot_fixture(%{password: password})

{:ok, space} = Accounts.create_space(user, %{name: "some-test-123"})

{:ok, credential} =
Expand Down
Loading

0 comments on commit 048d508

Please sign in to comment.