Skip to content

Commit

Permalink
Merge pull request #1721 from Logflare/feat/manual-alerts-trigger
Browse files Browse the repository at this point in the history
feat: manual trigger of alerts
  • Loading branch information
Ziinc authored Oct 5, 2023
2 parents 97502be + 27e4ba5 commit f490a0b
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/logflare/alerting.ex
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ defmodule Logflare.Alerting do
:ok
else
{:ok, []} ->
:ok
{:error, :no_results}

other ->
other
Expand Down
4 changes: 4 additions & 0 deletions lib/logflare_web/live/alerts/actions/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
</code>
</div>

<div>
<button class="btn btn-secondary" phx-click="manual-trigger">Manual trigger</button>
</div>

<div>
<ul class="list-group tw-text-sm">
<li class="list-group-item">
Expand Down
20 changes: 20 additions & 0 deletions lib/logflare_web/live/alerts/alerts_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,26 @@ defmodule LogflareWeb.AlertsLive do
end
end

def handle_event(
"manual-trigger",
_params,
%{assigns: %{alert: %_{} = alert}} = socket
) do
with :ok <- Alerting.run_alert(alert) do
{:noreply,
socket
|> put_flash(:info, "Alert has been triggered. Notifications sent!")}
else
{:error, :no_results} ->
{:noreply,
socket
|> put_flash(
:error,
"Alert has been triggered. No results from query, notifications not sent!"
)}
end
end

defp refresh(%{assigns: assigns} = socket) do
alerts = Alerting.list_alert_queries(assigns.user)

Expand Down
17 changes: 17 additions & 0 deletions test/logflare/alerting_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,23 @@ defmodule Logflare.AlertingTest do

assert :ok = Alerting.run_alert(alert_query)
end

test "run_alert_query/1 does not send notifications if no results", %{user: user} do
alert_query = insert(:alert, user: user)

GoogleApi.BigQuery.V2.Api.Jobs
|> expect(:bigquery_jobs_query, 1, fn _conn, _proj_id, _opts ->
{:ok, TestUtils.gen_bq_response([])}
end)

Logflare.Backends.Adaptor.WebhookAdaptor.Client
|> reject(:send, 2)

Logflare.Backends.Adaptor.SlackAdaptor.Client
|> reject(:send, 2)

assert {:error, :no_results} = Alerting.run_alert(alert_query)
end
end

describe "citrine integration" do
Expand Down
47 changes: 47 additions & 0 deletions test/logflare_web/live_views/alerts_live_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule LogflareWeb.AlertsLiveTest do
use LogflareWeb.ConnCase
import Phoenix.LiveViewTest
alias Logflare.Backends.Adaptor.WebhookAdaptor
alias Logflare.Backends.Adaptor.SlackAdaptor

@update_attrs %{
name: "some updated name",
Expand Down Expand Up @@ -142,4 +144,49 @@ defmodule LogflareWeb.AlertsLiveTest do
|> has_element?()
end
end

describe "running alerts" do
setup [:create_alert_query]

setup do
# mock goth behaviour
Goth
|> stub(:fetch, fn _mod -> {:ok, %Goth.Token{token: "auth-token"}} end)

WebhookAdaptor.Client
|> stub(:send, fn _, _ -> %Tesla.Env{} end)

SlackAdaptor.Client
|> stub(:send, fn _, _ -> %Tesla.Env{} end)

:ok
end

test "manual alert trigger - notification sent", %{conn: conn, alert_query: alert_query} do
GoogleApi.BigQuery.V2.Api.Jobs
|> expect(:bigquery_jobs_query, 1, fn _conn, _proj_id, _opts ->
{:ok, TestUtils.gen_bq_response([%{"testing" => "results-123"}])}
end)

{:ok, view, _html} = live(conn, Routes.alerts_path(conn, :show, alert_query))

assert view
|> element("button", "Manual trigger")
|> render_click() =~ "Alert has been triggered. Notifications sent!"
end

test "manual alert trigger - notification not sent", %{conn: conn, alert_query: alert_query} do
GoogleApi.BigQuery.V2.Api.Jobs
|> expect(:bigquery_jobs_query, 1, fn _conn, _proj_id, _opts ->
{:ok, TestUtils.gen_bq_response([])}
end)

{:ok, view, _html} = live(conn, Routes.alerts_path(conn, :show, alert_query))

assert view
|> element("button", "Manual trigger")
|> render_click() =~
"Alert has been triggered. No results from query, notifications not sent!"
end
end
end

0 comments on commit f490a0b

Please sign in to comment.