add metadata endpoint

This commit is contained in:
Tao Bror Bojlén 2019-08-09 19:07:02 +03:00
parent 509924ed52
commit 82153b283b
No known key found for this signature in database
GPG key ID: C6EC7AAB905F9E6F
6 changed files with 35 additions and 2 deletions

View file

@ -108,6 +108,14 @@ defmodule Backend.Api do
end end
end end
def list_instance_types() do
Instance
|> select([i], i.type)
|> where([i], not is_nil(i.type))
|> distinct(true)
|> Repo.all()
end
def search_instances(query, filters, from \\ 0) do def search_instances(query, filters, from \\ 0) do
page_size = 50 page_size = 50

View file

@ -79,7 +79,6 @@ defmodule Backend.Util do
@spec get_last_crawl(String.t()) :: Crawl.t() | nil @spec get_last_crawl(String.t()) :: Crawl.t() | nil
def get_last_crawl(domain) do def get_last_crawl(domain) do
Crawl Crawl
|> select([c], c)
|> where([c], c.instance_domain == ^domain) |> where([c], c.instance_domain == ^domain)
|> order_by(desc: :id) |> order_by(desc: :id)
|> limit(1) |> limit(1)

View file

@ -0,0 +1,11 @@
defmodule BackendWeb.MetadataController do
use BackendWeb, :controller
alias Backend.Api
action_fallback BackendWeb.FallbackController
def index(conn, _params) do
instance_types = Api.list_instance_types()
render(conn, "index.json", instance_types: instance_types)
end
end

View file

@ -8,6 +8,7 @@ defmodule BackendWeb.Router do
scope "/api", BackendWeb do scope "/api", BackendWeb do
pipe_through(:api) pipe_through(:api)
resources("/metadata", MetadataController, only: [:index])
resources("/instances", InstanceController, only: [:show]) resources("/instances", InstanceController, only: [:show])
resources("/graph", GraphController, only: [:index, :show]) resources("/graph", GraphController, only: [:index, :show])
resources("/search", SearchController, only: [:index]) resources("/search", SearchController, only: [:index])

View file

@ -26,7 +26,12 @@ defmodule BackendWeb.InstanceView do
} }
true -> true ->
last_updated = max_datetime(crawl.inserted_at, instance.updated_at) last_updated =
if crawl == nil do
instance.updated_at
else
max_datetime(crawl.inserted_at, instance.updated_at)
end
filtered_peers = filtered_peers =
instance.peers instance.peers

View file

@ -0,0 +1,9 @@
defmodule BackendWeb.MetadataView do
use BackendWeb, :view
def render("index.json", %{instance_types: instance_types}) do
%{
instanceTypes: instance_types
}
end
end