Skip to content

Commit

Permalink
chore: add more discrepancy handling logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziinc committed Dec 14, 2023
1 parent 88e22b9 commit a1b8e99
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
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: ttl} = source, _plan) when ttl >= 0 and ttl != nil do
ttl
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

# 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
round(ttl / :timer.hours(24))
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

0 comments on commit a1b8e99

Please sign in to comment.