Skip to content

Commit

Permalink
Add Metadata request (#6)
Browse files Browse the repository at this point in the history
* Provide api to request tag metadata

Why:
In order to make this information visible to the applications using scrub

How:
add new public api and corresponding queries in the session.ex application

* Fix return tuple from from read_metadata to includ :ok and data

* Add template data filter

Why:
Scrub.session appends template data to the metadata map. This data contains information about templates for structured types
not needed currently

How:
Filtering items whose structure map key is not set to :atomic

* Remove incorrectly uncommented code

* Increase Version Number

* Including mix format changes
  • Loading branch information
aguirrem authored Jun 4, 2020
1 parent c4d4998 commit 3f05231
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
25 changes: 25 additions & 0 deletions lib/scrub.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ defmodule Scrub do
Scrub.Session.close(session)
end

def read_metadata(session) do
case Session.get_tags_metadata(session) do
{:ok, metadata} ->
{:ok, filter_template_data(metadata)}

error ->
error
end
end

def read_tag(session, tag) when is_binary(tag) do
case Session.get_tag_metadata(session, tag) do
{:ok, tag} ->
Expand Down Expand Up @@ -81,6 +91,21 @@ defmodule Scrub do
end
end

def filter_template_data(tags) do
tags
|> Enum.reject(fn item ->
is_structure_type(item)
end)
end

def is_structure_type(%{structure: :atomic}) do
false
end

def is_structure_type(_x) do
true
end

# def read_tag(host, tag) when is_binary(host) do
# open_conn(host)
# |> read_tag(tag)
Expand Down
31 changes: 30 additions & 1 deletion lib/scrub/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ defmodule Scrub.Session do
DBConnection.start_link(__MODULE__, opts)
end

def get_tags_metadata(session) do
case DBConnection.execute(session, %Query{query: :get_all_tags_metadata}, <<>>) do
{:ok, _query, {:error, err}} ->
{:error, err}

{:ok, _query, result} ->
{:ok, result}

{:error, _} = err ->
err
end
end

def get_tag_metadata(session, tag) do
case DBConnection.execute(session, %Query{query: :get_tag_metadata}, tag) do
{:ok, _query, {:error, err}} ->
Expand Down Expand Up @@ -229,6 +242,16 @@ defmodule Scrub.Session do
end
end

@impl true
def handle_execute(
%Query{query: :get_all_tags_metadata} = query,
_data,
_opts,
%{tag_metadata: reply} = state
) do
{:ok, query, reply, state}
end

@impl true
def handle_execute(
%Query{query: :get_tag_metadata} = query,
Expand Down Expand Up @@ -477,7 +500,13 @@ defmodule Scrub.Session do
end

def encode(%Query{query: tag}, data, _s)
when tag in [:send_rr_data, :close, :get_tag_metadata, :send_unit_data] do
when tag in [
:send_rr_data,
:close,
:get_tag_metadata,
:get_all_tags_metadata,
:send_unit_data
] do
data
end

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Scrub.MixProject do
def project do
[
app: :scrub,
version: "0.1.2",
version: "0.1.3",
elixir: "~> 1.8",
start_permanent: Mix.env() == :prod,
deps: deps()
Expand Down

0 comments on commit 3f05231

Please sign in to comment.