-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start working on metric_registry_export
- Loading branch information
1 parent
93009bb
commit b0e2e64
Showing
12 changed files
with
417 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
defmodule Sanbase.Metric.Registry.Sync do | ||
@moduledoc """ | ||
This module is responsible for syncing the metric registry with the external | ||
source of truth. | ||
""" | ||
|
||
alias Sanbase.Metric.Registry | ||
@process_name :metric_registry_sync | ||
|
||
@spec initiate(list(non_neg_integer())) :: :ok | ||
def initiate(metric_registry_ids) when is_list(metric_registry_ids) do | ||
case ongoing_sync?() do | ||
true -> | ||
{:error, "Sync process is already running"} | ||
|
||
false -> | ||
pid = spawn_link(__MODULE__, :sync, [metric_registry_ids]) | ||
Process.register(pid, @process_name) | ||
{:ok, pid} | ||
end | ||
end | ||
|
||
def initiate_sync(metric_registry_ids) do | ||
with :ok <- check_initiate_env(), | ||
{:ok, content} <- get_sync_content(metric_registry_ids), | ||
:ok <- start_sync(content), | ||
:ok <- record_sync(content) do | ||
{:ok, content} | ||
end | ||
end | ||
|
||
def apply_sync(json_content) do | ||
with :ok <- check_apply_env(), | ||
{:ok, list} when is_list(list) <- Jason.decode(json_content) do | ||
for %{} = params <- list do | ||
params = Map.put(params, "sync_status", "synced") | ||
|
||
%{"metric" => metric, "data_type" => data_type, "fixed_parameters" => fixed_parameters} = | ||
params | ||
|
||
with {:ok, metric} <- Registry.by_name(metric, data_type, fixed_parameters), | ||
{:ok, metric} <- Registry.update(metric, params) do | ||
{:ok, metric} | ||
end | ||
|> dbg() | ||
end | ||
end | ||
end | ||
|
||
defp start_sync(content) do | ||
url = get_sync_target_url() | ||
|
||
case Req.post(url, json: content) do | ||
{:ok, _} -> :ok | ||
{:error, _} -> {:error, "Failed to sync"} | ||
end | ||
end | ||
|
||
defp get_sync_target_url() do | ||
secret = System.get_env("METRIC_REGISTRY_SYNC_SECRET") | ||
|
||
case Sanbase.Utils.Config.module_get(Sanbase, :deployment_env) do | ||
"dev" -> "http://localhost:4000/sync_metric_registry/#{secret}" | ||
"stage" -> "http://api.santiment.net/sync_metric_registry/#{secret}" | ||
"prod" -> raise("Cannot initiate sync from PROD") | ||
end | ||
end | ||
|
||
defp get_sync_content([]), do: {:error, "Nothing to sync"} | ||
|
||
defp get_sync_content(metric_registry_ids) do | ||
structs = Sanbase.Metric.Registry.by_ids(metric_registry_ids) | ||
content = generate_content(structs) | ||
{:ok, content} | ||
end | ||
|
||
defp check_initiate_env() do | ||
deployment_env = Sanbase.Utils.Config.module_get(Sanbase, :deployment_env) | ||
database_url = System.get_env("DATABASE_URL") | ||
|
||
# If local, the DATABASE_URL should not be set pointing to stage/prod. | ||
# Only work if the local postgres is used | ||
local? = deployment_env == "env" and is_nil(database_url) | ||
stage? = deployment_env == "stage" | ||
|
||
if local? or stage? do | ||
:ok | ||
else | ||
{:error, "Can only deploy sync from STAGE to PROD"} | ||
end | ||
end | ||
|
||
defp check_apply_env() do | ||
deployment_env = Sanbase.Utils.Config.module_get(Sanbase, :deployment_env) | ||
database_url = System.get_env("DATABASE_URL") | ||
|
||
# If local, the DATABASE_URL should not be set pointing to stage/prod. | ||
# Only work if the local postgres is used | ||
local? = deployment_env == "env" and is_nil(database_url) | ||
prod? = deployment_env == "prod" | ||
|
||
if local? or prod? do | ||
:ok | ||
else | ||
{:error, "Can only apply sync only on PROD"} | ||
end | ||
end | ||
|
||
defp generate_content(structs) do | ||
api_url = SanbaseWeb.Endpoint.api_url() | ||
from_host = URI.parse(api_url).host | ||
|
||
%{ | ||
metric_registry_structs: Jason.encode!(structs), | ||
generated_at: DateTime.utc_now() |> DateTime.to_iso8601(), | ||
from_host: from_host | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule Sanbase.Metric.Registry.SyncServer do | ||
use GenServer | ||
@name :metric_registry_sync_server | ||
def start_link(_opts) do | ||
GenServer.start_link(__MODULE__, name: @name) | ||
end | ||
|
||
def init(_) do | ||
{:ok, %{}} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,57 @@ | ||
defmodule SanbaseWeb.MetricRegistryController do | ||
use SanbaseWeb, :controller | ||
|
||
def sync(conn, %{"secret" => secret} = params) do | ||
case secret == get_sync_secret() do | ||
true -> | ||
# TODO: Remove | ||
IO.inspect(params) | ||
|
||
conn | ||
|> resp(200, "OK") | ||
|> send_resp() | ||
|
||
false -> | ||
conn | ||
|> resp(403, "Unauthorized") | ||
|> send_resp() | ||
end | ||
end | ||
|
||
def export_json(conn, _params) do | ||
conn | ||
|> resp(200, "ok") | ||
|> resp(200, get_metric_registry_json()) | ||
|> send_resp() | ||
end | ||
|
||
defp get_metric_registry_json() do | ||
Sanbase.Metric.Registry.all() | ||
|> Enum.take(1) | ||
|> Enum.map(&transform/1) | ||
|
||
# |> Enum.map(&Jason.encode!/1) | ||
# |> Enum.intersperse("\n") | ||
end | ||
|
||
defp transform(struct) when is_struct(struct) do | ||
struct | ||
|> Map.from_struct() | ||
|> Map.drop([:__meta__, :inserted_at, :updated_at, :change_suggestions]) | ||
|> Map.new(fn | ||
{k, v} when is_list(v) -> | ||
{k, Enum.map(v, &transform/1)} | ||
|
||
{k, v} when is_map(v) -> | ||
{k, transform(v)} | ||
|
||
{k, v} -> | ||
{k, v} | ||
end) | ||
end | ||
|
||
defp transform(data), do: data | ||
|
||
defp get_sync_secret() do | ||
Sanbase.Utils.Config.module_get(__MODULE__, :sync_secret, "no_secret") | ||
end | ||
end |
Oops, something went wrong.