Skip to content

Commit

Permalink
optimize source filter query
Browse files Browse the repository at this point in the history
  • Loading branch information
yujonglee committed Oct 10, 2024
1 parent 4f17386 commit 51c67b4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
5 changes: 5 additions & 0 deletions core/lib/canary/sources/source.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ defmodule Canary.Sources.Source do
actions do
defaults [:read, update: [:state, :last_fetched_at, :name, :overview, :config]]

read :find_with_project_public_key do
argument :project_public_key, :string, allow_nil?: false
filter expr(project.public_key == ^arg(:project_public_key))
end

create :create do
primary? true

Expand Down
24 changes: 9 additions & 15 deletions core/lib/canary_web/operations_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ defmodule CanaryWeb.OperationsController do
use CanaryWeb, :controller
require Ash.Query

plug :find_project when action in [:search, :ask]
plug :find_sources when action in [:search, :ask]

defp find_project(conn, _opts) do
defp find_sources(conn, _opts) do
err_msg = "no client found with the given key"

with {:ok, token} <- get_token_from_header(conn),
{:ok, project} <-
Canary.Accounts.Project
|> Ash.Query.filter(public_key == ^token)
|> Ash.Query.build(load: [:sources])
|> Ash.read_one() do
conn |> assign(:current_project, project)
{:ok, sources} <-
Canary.Sources.Source
|> Ash.Query.for_read(:find_with_project_public_key, %{project_public_key: token})
|> Ash.read() do
conn |> assign(:sources, sources)
else
_ -> conn |> send_resp(401, err_msg) |> halt()
end
Expand All @@ -27,12 +26,7 @@ defmodule CanaryWeb.OperationsController do
end

def search(conn, %{"query" => %{"text" => query, "tags" => tags}}) do
case Canary.Searcher.run(
conn.assigns.current_project.sources,
query,
tags: tags,
cache: cache?()
) do
case Canary.Searcher.run(conn.assigns.sources, query, tags: tags, cache: cache?()) do
{:ok, matches} ->
data = %{
matches: matches,
Expand Down Expand Up @@ -65,7 +59,7 @@ defmodule CanaryWeb.OperationsController do

Task.start_link(fn ->
Canary.Interactions.Responder.run(
conn.assigns.current_project.sources,
conn.assigns.sources,
query,
fn data -> send(here, data) end,
tags: tags,
Expand Down

0 comments on commit 51c67b4

Please sign in to comment.