Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: refactors and reworks the LogEventLive component and viewer #2109

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/logflare/logs/log_events.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ defmodule Logflare.Logs.LogEvents do
def fetch_event_by_id(source_token, id, opts)
when is_list(opts) and is_atom(source_token) and is_binary(id) do
partitions_range = Keyword.get(opts, :partitions_range, [])
source = Sources.get_by_and_preload(token: source_token)
source = Sources.Cache.get_by_and_preload(token: source_token)
bq_table_id = source.bq_table_id
bq_project_id = source.user.bigquery_project_id || GCPConfig.default_project_id()
%{bigquery_dataset_id: dataset_id} = GenUtils.get_bq_user_info(source.token)
Expand Down
19 changes: 19 additions & 0 deletions lib/logflare/sources.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ defmodule Logflare.Sources do
alias Logflare.Source.BigQuery.SchemaBuilder
alias Logflare.SourceSchemas
alias Logflare.User
alias Logflare.Billing.Plan
alias Logflare.Backends

require Logger
Expand Down Expand Up @@ -394,4 +395,22 @@ defmodule Logflare.Sources do
_ -> false
end
end

@doc """
Formats a source TTL to the specified unit
"""
@spec source_ttl_to_days(Source.t(), Plan.t()) :: integer()
def source_ttl_to_days(%Source{bigquery_table_ttl: ttl}, _plan)
when ttl >= 0 and ttl != nil do
round(ttl)
end

# fallback to plan value or default init value
# use min to avoid misrepresenting what user should see, in cases where actual is more than plan.
def source_ttl_to_days(_source, %Plan{limit_source_ttl: ttl}) do
min(
round(GenUtils.default_table_ttl_days()),
round(ttl / :timer.hours(24))
)
end
end
23 changes: 23 additions & 0 deletions lib/logflare/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,29 @@ defmodule Logflare.Utils do

def stringify_keys(not_a_map), do: not_a_map

@doc """
Stringifies a term.

### Example
iex> stringify(:my_atom)
"my_atom"
iex> stringify(1.1)
"1.1"
iex> stringify(122)
"122"
iex> stringify("something")
"something"
iex> stringify(%{})
"%{}"
iex> stringify([])
"[]"
"""
def stringify(v) when is_atom(v), do: Atom.to_string(v)
def stringify(v) when is_binary(v), do: v
def stringify(v) when is_float(v), do: Float.to_string(v)
def stringify(v) when is_integer(v), do: Integer.to_string(v)
def stringify(v), do: inspect(v)

@doc """
Sets the default ecto changeset field value if not set

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule LogflareWeb.LogEventLive.Show do
defmodule LogflareWeb.LogEventLive do
@moduledoc """
Handles all user interactions with the source logs search
"""
Expand All @@ -13,7 +13,9 @@ defmodule LogflareWeb.LogEventLive.Show do
require Logger

def mount(%{"source_id" => source_id} = params, session, socket) do
source = Sources.get_source_for_lv_param(source_id)
source_id = String.to_integer(source_id)
ts = Map.get(params, "timestamp")
source = Sources.get(source_id)
token = source.token

team_user =
Expand All @@ -30,25 +32,23 @@ defmodule LogflareWeb.LogEventLive.Show do
nil
end

{path, log_id} = cache_key = params_to_cache_key(params)

socket =
socket
|> assign(:source, source)
|> assign(:user, user)
|> assign(:team_user, team_user)
|> assign(:path, path)
|> assign(:log_event, nil)
|> assign(:origin, params["origin"])
|> assign(:id_param, log_id)

le = LogEvents.Cache.get!(token, cache_key)

socket =
if le do
assign(socket, :log_event, le)
else
socket
|> assign(:log_event_id, params["uuid"])
|> case do
socket when ts != nil ->
{:ok, dt, _} = DateTime.from_iso8601(ts)
assign(socket, :timestamp, dt)

socket ->
socket
end
|> assign(:log_event, LogEvents.Cache.get!(token, {"uuid", params["uuid"]}))

{:ok, socket}
end
Expand All @@ -57,12 +57,7 @@ defmodule LogflareWeb.LogEventLive.Show do
LogflareWeb.LogView.render("log_event.html", assigns)
end

@spec params_to_cache_key(map()) :: {String.t(), String.t()}
defp params_to_cache_key(%{"uuid" => id}) do
{"uuid", id}
end

defp params_to_cache_key(%{"path" => path, "value" => value}) do
{path, value}
def handle_info({:put_flash, type, msg}, socket) do
{:noreply, socket |> put_flash(type, msg)}
end
end
Loading
Loading