Change name of syscall to find_points_via_group

pull/1164/head
Rick Carlino 2020-02-28 16:30:02 -06:00
parent e06a53040f
commit afec9d13bd
9 changed files with 41 additions and 16 deletions

View File

@ -76,13 +76,13 @@ defmodule FarmbotCeleryScript.Compiler.Sequence do
group_ast.args[:every_point_type]
# lookup all point_groups related to this value
case FarmbotCeleryScript.SysCalls.get_point_group(point_group_arg) do
case FarmbotCeleryScript.SysCalls.find_points_via_group(point_group_arg) do
{:error, reason} ->
quote location: :keep, do: Macro.escape({:error, unquote(reason)})
%{name: group_name} = point_group ->
total = Enum.count(point_group.point_ids)
# Map over all the points returned by `get_point_group/1`
# Map over all the points returned by `find_points_via_group/1`
{body, _} =
Enum.reduce(point_group.point_ids, {[], 1}, fn point_id,
{acc, index} ->

View File

@ -92,12 +92,12 @@ defmodule FarmbotCeleryScript.SysCalls do
@callback eval_assertion(comment :: String.t(), expression :: String.t()) ::
true | false | error()
@callback get_point_group(String.t() | resource_id) :: %{
@callback find_points_via_group(String.t() | resource_id) :: %{
required(:point_ids) => [resource_id]
}
def get_point_group(sys_calls \\ @sys_calls, point_group_id) do
point_group_or_error(sys_calls, :get_point_group, [point_group_id])
def find_points_via_group(sys_calls \\ @sys_calls, point_group_id) do
point_group_or_error(sys_calls, :find_points_via_group, [point_group_id])
end
def format_lhs(sys_calls \\ @sys_calls, lhs)

View File

@ -99,7 +99,7 @@ defmodule FarmbotCeleryScript.SysCalls.Stubs do
do: error(:point, [point_type, resource_id])
@impl true
def get_point_group(id_or_type), do: error(:get_point_group, [id_or_type])
def find_points_via_group(id), do: error(:find_points_via_group, [id])
@impl true
def power_off(), do: error(:power_off, [])

View File

@ -10,7 +10,7 @@ defmodule FarmbotCeleryScript.CompilerGroupsTest do
test "compilation of point_group in parameter application" do
fake_point_ids = [4, 5, 6, 7]
expect(Stubs, :get_point_group, fn _ ->
expect(Stubs, :find_points_via_group, fn _ ->
%{name: "woosh", point_ids: fake_point_ids}
end)

View File

@ -29,21 +29,21 @@ defmodule FarmbotCeleryScript.SysCallsTest do
end
test "point groups failure" do
expect(Stubs, :get_point_group, 1, fn _id ->
expect(Stubs, :find_points_via_group, 1, fn _id ->
:whatever
end)
boom = fn -> SysCalls.get_point_group(Stubs, :something_else) end
boom = fn -> SysCalls.find_points_via_group(Stubs, :something_else) end
assert_raise FarmbotCeleryScript.RuntimeError, boom
end
test "point groups success" do
expect(Stubs, :get_point_group, 1, fn _id ->
expect(Stubs, :find_points_via_group, 1, fn _id ->
%{point_ids: [1, 2, 3]}
end)
pg = %{point_ids: [1, 2, 3]}
result = SysCalls.get_point_group(Stubs, 456)
result = SysCalls.find_points_via_group(Stubs, 456)
assert result == pg
end

View File

@ -25,7 +25,8 @@ defmodule FarmbotCore.Asset do
Sequence,
Sensor,
SensorReading,
Tool
Tool,
CriteriaRetriever
}
alias FarmbotCore.AssetSupervisor
@ -314,6 +315,23 @@ defmodule FarmbotCore.Asset do
end
end
def find_points_via_group(params) do
case Repo.get_by(PointGroup, params) do
%{id: _id, sort_type: sort_by} = point_group ->
sorted = CriteriaRetriever.run(point_group)
|> sort_points(sort_by || "xy_ascending")
|> Enum.map(&Map.fetch!(&1, :id))
%{ point_ids: sorted }
other ->
# Swallow all other errors
a = inspect(params)
b = inspect(other)
Logger.debug("Unexpected point group #{a} #{b}")
nil
end
end
def new_point_group!(params) do
%PointGroup{}
|> PointGroup.changeset(params)

View File

@ -42,7 +42,7 @@ defmodule FarmbotCore.Asset.CriteriaRetriever do
meta = "meta."
meta_len = String.length(meta)
pg.criteria["string_eq"]
(pg.criteria["string_eq"] || %{})
|> Map.to_list()
|> Enum.filter(fn {k, _v} ->
String.starts_with?(k, meta)
@ -108,10 +108,12 @@ defmodule FarmbotCore.Asset.CriteriaRetriever do
end
defp stage_1_day_field({pg, accum}) do
days = pg.criteria["day"]["days_ago"] || 0
day_criteria = pg.criteria["day"] || %{}
days = day_criteria["days_ago"] || 0
op = day_criteria["op"] || "<"
time = Timex.shift(Timex.now(), days: -1 * days)
{ pg, accum ++ [{"created_at", pg.criteria["day"]["op"] || "<", time}] }
{ pg, accum ++ [{"created_at", op, time}] }
end
defp stage_2({lhs, "IN", rhs}, results) do

View File

@ -130,8 +130,12 @@ defmodule FarmbotOS.SysCalls do
@impl true
defdelegate point(kind, id), to: PointLookup
# TODO(RICK) This may not be used anymore by CSRT.
# @impl true
# defdelegate get_point_group(type_or_id), to: PointLookup
@impl true
defdelegate get_point_group(type_or_id), to: PointLookup
defdelegate find_points_via_group(id), to: Asset
@impl true
defdelegate get_toolslot_for_tool(id), to: PointLookup

View File

@ -20,6 +20,7 @@ defmodule FarmbotOS.SysCalls.PointLookup do
end
end
# TODO(Rick) This can be removed. Not used by CSRT.
def get_point_group(type) when is_binary(type) do
Logger.debug("Looking up points by type: #{type}")
points = Asset.get_all_points_by_type(type)