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: Find Options - Update components API #552

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 6 additions & 5 deletions libs/application_runner/lib/environment/query_dyn_sup.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ defmodule ApplicationRunner.Environment.QueryDynSup do
DynamicSupervisor.init(strategy: :one_for_one)
end

@spec ensure_child_started(term(), String.t() | nil, map() | nil, map() | nil, map()) ::
@spec ensure_child_started(term(), String.t() | nil, map() | nil, map() | nil, map(), map()) ::
{:ok, pid()} | {:error, term()}
def ensure_child_started(env_id, coll, query_parsed, query_transformed, projection) do
def ensure_child_started(env_id, coll, query_parsed, query_transformed, projection, options \\ %{}) do
Logger.debug(
"#{__MODULE__} ensure query server started for #{inspect([env_id, coll, query_parsed, query_transformed])}"
)

case start_child(env_id, coll, query_parsed, query_transformed, projection) do
case start_child(env_id, coll, query_parsed, query_transformed, projection, options) do
{:ok, pid} ->
Logger.info("ApplicationRunner.Environment.QueryServer started")
{:ok, pid}
Expand All @@ -50,13 +50,14 @@ defmodule ApplicationRunner.Environment.QueryDynSup do
end
end

defp start_child(env_id, coll, query_parsed, query_transformed, projection) do
defp start_child(env_id, coll, query_parsed, query_transformed, projection, options) do
init_value = [
query_parsed: query_parsed,
query_transformed: query_transformed,
coll: coll,
env_id: env_id,
projection: projection
projection: projection,
options: options
]

DynamicSupervisor.start_child(get_full_name(env_id), {QueryServer, init_value})
Expand Down
31 changes: 20 additions & 11 deletions libs/application_runner/lib/environment/query_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ defmodule ApplicationRunner.Environment.QueryServer do
Swarm.join(group, pid)
end

def get_data(env_id, coll, query_parsed, projection) do
def get_data(env_id, coll, query_parsed, projection, options) do
GenServer.call(get_full_name({env_id, coll, query_parsed}), {:get_data, projection})
end

Expand All @@ -64,8 +64,9 @@ defmodule ApplicationRunner.Environment.QueryServer do
{:ok, coll} <- Keyword.fetch(opts, :coll),
{:ok, query_transformed} <- Keyword.fetch(opts, :query_transformed),
{:ok, query_parsed} <- Keyword.fetch(opts, :query_parsed),
{:ok, data} <- fetch_initial_data(env_id, coll, query_transformed),
{:ok, projection} <- Keyword.fetch(opts, :projection) do
{:ok, projection} <- Keyword.fetch(opts, :projection),
{:ok, options} <- Keyword.fetch(opts, :options),
{:ok, data} <- fetch_initial_data(env_id, coll, query_transformed, projection, options) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The initial data should not be fetched with the projections and options since they both should be applied after.
The initial data must stay full to apply the projections and options.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok so this PR will need a lot more work to handle each mongo aggregators by hand.

We will start by just implementing the "skip" and "limit" aggregators and we will add more in the future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. You can create an Epic with the list of the aggregators to manage as we did for the operators

projection_data =
if projection != %{} do
%{projection => projection_data(data, projection)}
Expand All @@ -83,7 +84,8 @@ defmodule ApplicationRunner.Environment.QueryServer do
latest_timestamp: Mongo.timestamp(DateTime.utc_now()),
done_ids: MapSet.new(),
w_pids: MapSet.new(),
projection_data: projection_data
projection_data: projection_data,
options: options
}}
else
:error ->
Expand Down Expand Up @@ -119,20 +121,27 @@ defmodule ApplicationRunner.Environment.QueryServer do
Map.values(map_data)
end

defp fetch_initial_data(_env_id, coll, query_transformed)
defp fetch_initial_data(_env_id, coll, query_transformed, projection, options)
when is_nil(coll) or is_nil(query_transformed) do
Logger.debug("#{__MODULE__} fetch_initial_data with nil query")

{:ok, []}
end

defp fetch_initial_data(env_id, coll, query_transformed) do
defp fetch_initial_data(env_id, coll, query_transformed, projection, options) do
Logger.debug("#{__MODULE__} fetch_initial_data with data: #{inspect([env_id, coll, query_transformed])}")

mongo_opts =
Keyword.merge(
[projection: projection],
Enum.map(options, fn {k, v} -> {String.to_atom(k), v} end)
)

MongoInstance.run_mongo_task(env_id, MongoStorage, :filter_docs, [
env_id,
coll,
query_transformed
query_transformed,
mongo_opts
])
end

Expand Down Expand Up @@ -418,14 +427,14 @@ defmodule ApplicationRunner.Environment.QueryServer do
if projection_change?(projection_data, new_data, k) do
{k, v}
else
group = ViewServer.group_name(env_id, coll, query_parsed, k)
group = ViewServer.group_name(env_id, coll, query_parsed, k, %{})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modification not needed:

Suggested change
group = ViewServer.group_name(env_id, coll, query_parsed, k, %{})
group = ViewServer.group_name(env_id, coll, query_parsed, k)

Swarm.publish(group, {:data_changed, projection_data(new_data, k)})
{k, projection_data(new_data, k)}
end
end)

# Notify ViewServer with no projection.
group = ViewServer.group_name(env_id, coll, query_parsed, %{})
group = ViewServer.group_name(env_id, coll, query_parsed, %{}, %{})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same:

Suggested change
group = ViewServer.group_name(env_id, coll, query_parsed, %{}, %{})
group = ViewServer.group_name(env_id, coll, query_parsed, %{})

Swarm.publish(group, {:data_changed, new_data})

new_projection_data
Expand All @@ -441,11 +450,11 @@ defmodule ApplicationRunner.Environment.QueryServer do
}
) do
Enum.each(Map.keys(projection_data), fn projection_key ->
group = ViewServer.group_name(env_id, old_coll, query_parsed, projection_key)
group = ViewServer.group_name(env_id, old_coll, query_parsed, projection_key, %{})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same:

Suggested change
group = ViewServer.group_name(env_id, old_coll, query_parsed, projection_key, %{})
group = ViewServer.group_name(env_id, old_coll, query_parsed, projection_key)

Swarm.publish(group, {:coll_changed, new_coll})
end)

group = ViewServer.group_name(env_id, old_coll, query_parsed, %{})
group = ViewServer.group_name(env_id, old_coll, query_parsed, %{}, %{})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same:

Suggested change
group = ViewServer.group_name(env_id, old_coll, query_parsed, %{}, %{})
group = ViewServer.group_name(env_id, old_coll, query_parsed, %{})

Swarm.publish(group, {:coll_changed, new_coll})
end

Expand Down
6 changes: 4 additions & 2 deletions libs/application_runner/lib/environment/view_dyn_sup.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ defmodule ApplicationRunner.Environment.ViewDynSup do
query_parsed = view_uid.query_parsed
query_transformed = view_uid.query_transformed
projection = view_uid.projection
options = view_uid.options

Logger.debug("#{__MODULE__} ensure_child_started for #{inspect(%{env_id: env_id, session_id: session_id})}")

Expand All @@ -38,14 +39,15 @@ defmodule ApplicationRunner.Environment.ViewDynSup do
coll,
query_parsed,
query_transformed,
projection
projection,
options
) do
case start_child(env_id, function_name, view_uid) do
{:ok, pid} ->
Logger.info("ApplicationRunner.Environment.ViewServer")

QueryServer.join_group(qs_pid, session_id)
ViewServer.join_group(pid, env_id, coll, query_parsed, projection)
ViewServer.join_group(pid, env_id, coll, query_parsed, projection, options)
QueryServer.monitor(qs_pid, pid)
{:ok, pid}

Expand Down
10 changes: 5 additions & 5 deletions libs/application_runner/lib/environment/view_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ defmodule ApplicationRunner.Environment.ViewServer do

require Logger

def group_name(env_id, coll, query, projection) do
{__MODULE__, env_id, coll, query, projection}
def group_name(env_id, coll, query, projection, options \\ %{}) do
{__MODULE__, env_id, coll, query, projection, options}
end

def join_group(pid, env_id, coll, query, projection) do
group = group_name(env_id, coll, query, projection)
def join_group(pid, env_id, coll, query, projection, options \\ %{}) do
group = group_name(env_id, coll, query, projection, options)
Swarm.join(group, pid)
end

Expand Down Expand Up @@ -46,7 +46,7 @@ defmodule ApplicationRunner.Environment.ViewServer do
%ViewUid{} = view_uid = Keyword.fetch!(opts, :view_uid)

with data <-
QueryServer.get_data(env_id, view_uid.coll, view_uid.query_parsed, view_uid.projection),
QueryServer.get_data(env_id, view_uid.coll, view_uid.query_parsed, view_uid.projection, view_uid.options),
{:ok, view} <-
ApplicationServices.fetch_view(
function_name,
Expand Down
6 changes: 4 additions & 2 deletions libs/application_runner/lib/environment/view_uid.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule ApplicationRunner.Environment.ViewUid do
@moduledoc """
This identify a unique widget for a given environment.
"""
@enforce_keys [:name, :coll, :query_parsed, :query_transformed, :props, :context, :projection]
@enforce_keys [:name, :coll, :query_parsed, :query_transformed, :props, :context, :projection, :options]
defstruct [
:name,
:props,
Expand All @@ -11,6 +11,7 @@ defmodule ApplicationRunner.Environment.ViewUid do
:context,
:coll,
:projection,
:options,
prefix_path: ""
]

Expand All @@ -22,6 +23,7 @@ defmodule ApplicationRunner.Environment.ViewUid do
coll: String.t() | nil,
context: map() | nil,
prefix_path: String.t(),
projection: map()
projection: map(),
options: map()
}
end
13 changes: 8 additions & 5 deletions libs/application_runner/lib/session/route_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ defmodule ApplicationRunner.Session.RouteServer do
props <- Map.get(base_view, "props", %{}),
find <- Map.get(base_view, "find", %{}),
context_projection <- Map.get(base_view, "context"),
{coll, query, projection} <- extract_find(base_view, find),
{coll, query, projection, options} <- extract_find(base_view, find),
{:ok, view_uid} <-
create_view_uid(
session_metadata,
name,
%{coll: coll, query: query, projection: projection},
%{coll: coll, query: query, projection: projection, options: options},
%{"route" => route_params},
props,
session_metadata.context,
Expand All @@ -128,15 +128,16 @@ defmodule ApplicationRunner.Session.RouteServer do
coll = Map.get(find, "coll")
query = Map.get(find, "query", %{})
projection = Map.get(find, "projection", %{})
options = Map.get(find, "options", %{})

if find == %{} && coll_deprecated != nil do
Logger.warning(
"Definition of view #{name} is deprecated since applicationRunner beta 106 check https://docs.lenra.io/components-api/components/view.html."
)

{coll_deprecated, query_deprecated, %{}}
{coll_deprecated, query_deprecated, %{}, %{}}
else
{coll, query, projection}
{coll, query, projection, options}
end
end

Expand Down Expand Up @@ -195,6 +196,7 @@ defmodule ApplicationRunner.Session.RouteServer do
coll = Map.get(find, :coll)
query = Map.get(find, :query)
projection = Map.get(find, :projection)
options = Map.get(find, :options)

mongo_user_id =
case session_metadata.user_id do
Expand Down Expand Up @@ -236,7 +238,8 @@ defmodule ApplicationRunner.Session.RouteServer do
query_transformed: query_transformed,
coll: coll,
context: context,
projection: projection
projection: projection,
options: options
}}
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ defmodule ApplicationRunner.Session.UiBuilders.LenraBuilder do
find = Map.get(component, "find", %{})
context_projection = Map.get(component, "context")

{coll, query, projection} = RouteServer.extract_find(component, find)
{coll, query, projection, options} = RouteServer.extract_find(component, find)

with {:ok, new_view_uid} <-
RouteServer.create_view_uid(
session_metadata,
name,
%{coll: coll, query: query, projection: projection},
%{coll: coll, query: query, projection: projection, options: options},
%{},
props,
view_uid.context,
Expand Down
2 changes: 1 addition & 1 deletion libs/application_runner/priv/components-api
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ defmodule ApplicationRunner.Environment.ViewDynSupTest do
query_transformed: %{},
props: %{},
context: %{},
projection: %{}
projection: %{},
options: %{}
}

assert :undefined != Swarm.whereis_name(Environment.ViewDynSup.get_name(env_id))
Expand Down
Loading