Skip to content
This repository has been archived by the owner on Jun 11, 2023. It is now read-only.

Commit

Permalink
Merge 4bd4dd9 into 9b7d8c7
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomassaro committed Aug 13, 2017
2 parents 9b7d8c7 + 4bd4dd9 commit 02032f6
Show file tree
Hide file tree
Showing 80 changed files with 1,965 additions and 246 deletions.
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ config :helix,
Helix.Hardware.Repo,
Helix.Log.Repo,
Helix.Network.Repo,
Helix.NPC.Repo,
Helix.Universe.Repo,
Helix.Process.Repo,
Helix.Server.Repo,
Helix.Software.Repo
Expand Down
6 changes: 0 additions & 6 deletions config/npc/dev.exs

This file was deleted.

4 changes: 2 additions & 2 deletions config/npc/config.exs → config/universe/config.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use Mix.Config

config :helix, Helix.NPC.Repo,
priv: "priv/repo/npc",
config :helix, Helix.Universe.Repo,
priv: "priv/repo/universe",
pool_size: 3,
adapter: Ecto.Adapters.Postgres,
username: System.get_env("HELIX_DB_USER") || "postgres",
Expand Down
6 changes: 6 additions & 0 deletions config/universe/dev.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use Mix.Config

prefix = System.get_env("HELIX_DB_PREFIX") || "helix"

config :helix, Helix.Universe.Repo,
database: prefix <> "_dev_universe"
6 changes: 3 additions & 3 deletions config/npc/prod.exs → config/universe/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use Mix.Config

prefix = "${HELIX_DB_PREFIX}"

config :helix, Helix.NPC.Repo,
pool_size: "${HELIX_DB_POOL_SIZE}",
config :helix, Helix.Universe.Repo,
size: "${HELIX_DB_POOL_SIZE}",
username: "${HELIX_DB_USER}",
password: "${HELIX_DB_PASS}",
hostname: "${HELIX_DB_HOST}",
database: prefix <> "_prod_npc"
database: prefix <> "_prod_universe"
4 changes: 2 additions & 2 deletions config/npc/test.exs → config/universe/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ use Mix.Config

prefix = System.get_env("HELIX_DB_PREFIX") || "helix"

config :helix, Helix.NPC.Repo,
config :helix, Helix.Universe.Repo,
pool: Ecto.Adapters.SQL.Sandbox,
database: prefix <> "_test_npc"
database: prefix <> "_test_universe"
2 changes: 1 addition & 1 deletion lib/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ defmodule Helix.Application.DomainsSupervisor do
supervisor(Helix.Hardware.Supervisor, []),
supervisor(Helix.Log.Supervisor, []),
supervisor(Helix.Network.Supervisor, []),
supervisor(Helix.NPC.Supervisor, []),
supervisor(Helix.Universe.Supervisor, []),
supervisor(Helix.Process.Supervisor, []),
supervisor(Helix.Server.Supervisor, []),
supervisor(Helix.Software.Supervisor, [])
Expand Down
46 changes: 29 additions & 17 deletions lib/cache/action/cache.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ defmodule Helix.Cache.Action.Cache do
alias Helix.Cache.Query.Cache, as: CacheQuery

@spec purge_server(Server.idtb) ::
term
:ok
@doc """
Purges the server entry from the cache.
Expand Down Expand Up @@ -76,7 +76,7 @@ defmodule Helix.Cache.Action.Cache do
end

@spec update_server(Server.idtb) ::
term
:ok
@doc """
Updates a server entry.
Expand All @@ -93,7 +93,7 @@ defmodule Helix.Cache.Action.Cache do
end

@spec update_server(Server.idtb, term) ::
term
:ok
defp update_server(server_id, params) do
unless is_nil(params) do
unless is_nil(params.motherboard_id) do
Expand All @@ -109,8 +109,8 @@ defmodule Helix.Cache.Action.Cache do
end
end

@spec update_server_by_motherboard(Motherboard.t | Component.idtb) ::
term
@spec update_server_by_motherboard(Motherboard.idtb) ::
:ok
@doc """
Given a motherboard, update its corresponding server.
Expand All @@ -128,7 +128,7 @@ defmodule Helix.Cache.Action.Cache do
end

@spec update_server_by_storage(Storage.idtb) ::
term
:ok
@doc """
Given a storage, update its corresponding server.
Expand All @@ -147,7 +147,7 @@ defmodule Helix.Cache.Action.Cache do
end

@spec update_storage(Storage.idtb) ::
term
:ok
@doc """
Updates a storage entry from the cache.
Expand All @@ -161,7 +161,7 @@ defmodule Helix.Cache.Action.Cache do
end

@spec purge_storage(Storage.idtb) ::
term
:ok
@doc """
Purges a storage entry.
Expand All @@ -170,8 +170,8 @@ defmodule Helix.Cache.Action.Cache do
def purge_storage(storage_id),
do: CacheInternal.purge(:storage, storage_to_id(storage_id))

@spec update_component(Motherboard.t | Component.idtb) ::
term
@spec update_component(Motherboard.idtb) ::
:ok
@doc """
Updates a component entry on the cache.
Expand All @@ -189,8 +189,8 @@ defmodule Helix.Cache.Action.Cache do
CacheInternal.update(:component, component_id)
end

@spec purge_component(Motherboard.t | Component.idtb) ::
term
@spec purge_component(Motherboard.idtb) ::
:ok
@doc """
Purges a component entry from the cache.
Expand All @@ -201,28 +201,40 @@ defmodule Helix.Cache.Action.Cache do
def purge_component(component_id),
do: CacheInternal.purge(:component, component_to_id(component_id))

@spec update_nip(Network.idtb, IPv4.t) ::
term
@spec update_network(Network.idtb, IPv4.t) ::
:ok
@doc """
Updates the nip entry on the cache.
It will also update the underlying server, even if it doesn't exists.
"""
def update_nip(network_id, ip) do
def update_network(network_id, ip) do
network_id = network_to_id(network_id)
{:ok, server_id} = CacheQuery.from_nip_get_server(network_id, ip)
update_server(server_id)
CacheInternal.update(:network, {network_id, ip})
end

@spec purge_network(Network.idtb, IPv4.t) ::
:ok
@doc """
Purges the nip entry from the cache.
It does not purge/update the server.
"""
def purge_nip(network_id, ip),
def purge_network(network_id, ip),
do: CacheInternal.purge(:network, {network_to_id(network_id), ip})

@spec update_web(Network.idtb, IPv4.t) ::
:ok
def update_web(network, ip),
do: CacheInternal.update(:web, {network_to_id(network), ip})

@spec purge_web(Network.idtb, IPv4.t) ::
:ok
def purge_web(network, ip),
do: CacheInternal.purge(:web, {network_to_id(network), ip})

@spec direct_cache_query(:server | :motherboard | :component | :storage, HELL.PK.t) ::
server_id :: HELL.PK.t
| nil
Expand Down Expand Up @@ -295,7 +307,7 @@ defmodule Helix.Cache.Action.Cache do
def network_to_id(id) when is_binary(id),
do: id

@spec motherboard_to_id(Motherboard.t | Component.idtb) ::
@spec motherboard_to_id(Motherboard.idtb) ::
HELL.PK.t
defp motherboard_to_id(%Motherboard{motherboard_id: id}),
do: component_to_id(id)
Expand Down
70 changes: 55 additions & 15 deletions lib/cache/internal/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defmodule Helix.Cache.Internal.Builder do

alias HELL.IPv4
alias Helix.Entity.Internal.Entity, as: EntityInternal
alias Helix.Entity.Model.Entity
alias Helix.Network.Model.Network
alias Helix.Hardware.Internal.Component, as: ComponentInternal
alias Helix.Hardware.Internal.Motherboard, as: MotherboardInternal
Expand All @@ -25,15 +26,19 @@ defmodule Helix.Cache.Internal.Builder do
alias Helix.Server.Model.Server
alias Helix.Software.Internal.Storage, as: StorageInternal
alias Helix.Software.Model.Storage
alias Helix.Universe.NPC.Internal.NPC, as: NPCInternal
alias Helix.Universe.NPC.Internal.Web, as: NPCWebInternal
alias Helix.Universe.NPC.Model.NPC
alias Helix.Cache.Model.ComponentCache
alias Helix.Cache.Model.NetworkCache
alias Helix.Cache.Model.ServerCache
alias Helix.Cache.Model.StorageCache
alias Helix.Cache.Model.WebCache

@spec by_server(Server.id) ::
{:ok, ServerCache.t}
| {:error, {:server, :notfound}}
| {:error, :unknown}
| {:error, :internal}
def by_server(server_id) do
with \
entity = %{} <- EntityInternal.fetch_by_server(server_id) || :nxserver,
Expand All @@ -56,15 +61,15 @@ defmodule Helix.Cache.Internal.Builder do
{:nxmobo, entity} ->
{:ok, ServerCache.new(server_id, entity.entity_id)}
_ ->
{:error, :unknown}
{:error, :internal}
end
end

@spec by_motherboard(Motherboard.id) ::
{:ok, ServerCache.t}
| {:error, {:server, :notfound}}
| {:error, {:motherboard, :notfound}}
| {:error, :unknown}
| {:error, {:server, :notfound}}
| {:error, :internal}
def by_motherboard(motherboard_id) do
with \
server =
Expand All @@ -74,16 +79,13 @@ defmodule Helix.Cache.Internal.Builder do
else
:nxmobo ->
{:error, {:motherboard, :notfound}}
_ ->
{:error, :unknown}
end
end

# @spec by_nip(Network.id, IPv4.t) ::
# {:ok, NetworkCache.t}
# | {:error, {:nip, :notfound}}
# | {:error, {:server, :notfound}}
# | {:error, :unknown}
@spec by_nip(Network.id, IPv4.t) ::
{:ok, NetworkCache.t}
| {:error, {:nip, :notfound}}
| {:error, {:server, :notfound}}
def by_nip(network_id, ip) do
with \
mobo = %{} <- MotherboardInternal.fetch_by_nip(network_id, ip) || :nxnip,
Expand All @@ -95,8 +97,6 @@ defmodule Helix.Cache.Internal.Builder do
{:error, {:nip, :notfound}}
:nxserver ->
{:error, {:server, :notfound}}
_ ->
{:error, :unknown}
end

end
Expand All @@ -106,7 +106,7 @@ defmodule Helix.Cache.Internal.Builder do
| {:error, {:storage, :notfound}}
| {:error, {:drive, :notfound}}
| {:error, {:drive, :unlinked}}
| {:error, :unknown}
| {:error, :internal}
def by_storage(storage_id) do
with \
storage = %{} <- StorageInternal.fetch(storage_id) || :nxstorage,
Expand All @@ -126,7 +126,7 @@ defmodule Helix.Cache.Internal.Builder do
:unlinked ->
{:error, {:drive, :unlinked}}
_ ->
{:error, :unknown}
{:error, :internal}
end
end

Expand All @@ -148,6 +148,46 @@ defmodule Helix.Cache.Internal.Builder do
end
end

@spec web_by_nip(Network.id, IPv4.t) ::
{:ok, WebCache.t}
| {:error, {:nip, :notfound}}
| {:error, {:server, :notfound}}
| {:error, {:web, :notfound}}
| {:error, :internal}
def web_by_nip(network_id, ip) do
with \
mobo = %{} <- MotherboardInternal.fetch_by_nip(network_id, ip) || :nxnip,
server = %{} <- ServerInternal.fetch_by_motherboard(mobo) || :nxserver,
entity = %{} <- EntityInternal.fetch_by_server(server),
content = %{} <- get_web_content(entity, network_id, ip) || :nxweb
do
{:ok, WebCache.new(network_id, ip, content)}
else
:nxnip ->
{:error, {:nip, :notfound}}
:nxserver ->
{:error, {:server, :notfound}}
:nxweb ->
{:error, {:web, :notfound}}
_ ->
{:error, :internal}
end
end

@spec get_web_content(Entity.t, Network.id, IPv4.t) ::
NPCWebInternal.npc_content
| nil
defp get_web_content(entity, network_id, ip) do
case entity.entity_type do
:npc ->
NPC.ID.cast!(to_string(entity.entity_id))
|> NPCInternal.fetch()
|> NPCWebInternal.generate_content(network_id, ip)
_ ->
%{to: "do"}
end
end

@spec get_storages_from_motherboard(Motherboard.t) ::
[Storage.id]
defp get_storages_from_motherboard(motherboard) do
Expand Down
15 changes: 11 additions & 4 deletions lib/cache/internal/cache.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ defmodule Helix.Cache.Internal.Cache do
alias Helix.Cache.Model.NetworkCache
alias Helix.Cache.Model.ServerCache
alias Helix.Cache.Model.StorageCache
alias Helix.Cache.Model.WebCache
alias Helix.Cache.Internal.Populate, as: PopulateInternal
alias Helix.Cache.State.PurgeQueue, as: StatePurgeQueue
alias Helix.Cache.Repo
Expand All @@ -26,7 +27,8 @@ defmodule Helix.Cache.Internal.Cache do
:server => ServerCache.Query,
:component => ComponentCache.Query,
:network => NetworkCache.Query,
:storage => StorageCache.Query
:storage => StorageCache.Query,
:web => WebCache.Query
}

docp """
Expand All @@ -50,15 +52,18 @@ defmodule Helix.Cache.Internal.Cache do

# Network
:network => {:network, :by_nip, :all},
{:nip, :server} => {:network, :by_nip, :server_id},
{:network, :server} => {:network, :by_nip, :server_id},

# Storage
:storage => {:storage, :by_storage, :all},
{:storage, :server} => {:storage, :by_storage, :server_id},

# Component
:component => {:component, :by_component, :all},
{:component, :motherboard} => {:component, :by_component, :motherboard_id}
{:component, :motherboard} => {:component, :by_component, :motherboard_id},

# Web
{:web, :content} => {:web, :web_by_nip, :content}
}

# @spec lookup(condition, [binary]) ::
Expand Down Expand Up @@ -185,7 +190,9 @@ defmodule Helix.Cache.Internal.Cache do
end

docp """
We've tried to fetch the data but it isn't cached. This may be for two reasons:
We've tried to fetch the data but it isn't cached. This may be for three
reasons:
1) Entry is not on the DB
2) Entry is on the DB but it's expired
3) Entry is on the DB and valid, but marked as purged on the PurgeQueue
Expand Down
Loading

0 comments on commit 02032f6

Please sign in to comment.