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: ttl dashboard display #1893

Merged
merged 2 commits into from
Dec 14, 2023
Merged
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
8 changes: 8 additions & 0 deletions lib/logflare/google/bigquery/gen_utils/gen_utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ defmodule Logflare.Google.BigQuery.GenUtils do

@table_ttl 604_800_000
@default_dataset_location "US"

@doc """
Returns the default TTL used (in days) for initializing the table.
"""
def default_table_ttl_days() do
@table_ttl / :timer.hours(24)
end

defp env_project_id, do: Application.get_env(:logflare, Logflare.Google)[:project_id]

defp env_default_table_name_append,
Expand Down
15 changes: 11 additions & 4 deletions lib/logflare_web/views/source_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule LogflareWeb.SourceView do
alias LogflareWeb.Router.Helpers, as: Routes
alias Logflare.Billing.Plan
alias Logflare.Source
alias Logflare.Google.BigQuery.GenUtils
use LogflareWeb, :view

def log_url(route) do
Expand All @@ -24,11 +25,17 @@ defmodule LogflareWeb.SourceView do
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: nil} = source, %Plan{} = plan) do
source_ttl_to_days(%{source | bigquery_table_ttl: plan.limit_source_ttl}, :day)
def source_ttl_to_days(%Source{bigquery_table_ttl: ttl}, _plan)
when ttl >= 0 and ttl != nil do
round(ttl)
end

def source_ttl_to_days(%Source{bigquery_table_ttl: ttl}, _plan) do
round(ttl / :timer.hours(24))
# 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
13 changes: 13 additions & 0 deletions test/logflare_web/controllers/source_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ defmodule LogflareWeb.SourceControllerTest do
assert html =~ "ttl: 3 day"
end

test "renders default plan ttl correctly", %{conn: conn, source: source} do
conn
|> get(Routes.source_path(conn, :dashboard))
|> html_response(200) =~ "ttl: 3 day"
end

test "renders default bigquery ttl correctly", %{conn: conn, source: source} do
conn
|> get(Routes.source_path(conn, :dashboard))
|> html_response(200) =~ "ttl: 3 day"
end


test "show source", %{conn: conn, source: source} do
html =
conn
Expand Down
Loading