Skip to content

Commit

Permalink
Add tests, format duration better.
Browse files Browse the repository at this point in the history
  • Loading branch information
tpendragon committed Nov 27, 2024
1 parent ea9abb2 commit 3e76b78
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
28 changes: 25 additions & 3 deletions lib/dpul_collections/indexing_pipeline/dashboard_page.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ defmodule DpulCollections.IndexingPipeline.DashboardPage do
rows_name="metrics"
>
<:col field={:updated_at} sortable={:desc} />
<:col field={:duration} header="Duration (s)" />
<:col :let={record} field={:duration} header="Duration (hh:mm:ss)">
<%= to_hh_mm_ss(record.duration) %>
</:col>
<:col field={:records_acked} header="Record Count" />
<:col :let={record} field={:per_second} header="Records per Second">
<%= per_second(record) %>
Expand All @@ -73,7 +75,9 @@ defmodule DpulCollections.IndexingPipeline.DashboardPage do
rows_name="metrics"
>
<:col field={:updated_at} sortable={:desc} />
<:col field={:duration} header="Duration (s)" />
<:col :let={record} field={:duration} header="Duration (hh:mm:ss)">
<%= to_hh_mm_ss(record.duration) %>
</:col>
<:col field={:records_acked} header="Record Count" />
<:col :let={record} field={:per_second} header="Records per Second">
<%= per_second(record) %>
Expand All @@ -88,7 +92,9 @@ defmodule DpulCollections.IndexingPipeline.DashboardPage do
rows_name="metrics"
>
<:col field={:updated_at} sortable={:desc} />
<:col field={:duration} header="Duration (s)" />
<:col :let={record} field={:duration} header="Duration (hh:mm:ss)">
<%= to_hh_mm_ss(record.duration) %>
</:col>
<:col field={:records_acked} header="Record Count" />
<:col :let={record} field={:per_second} header="Records per Second">
<%= per_second(record) %>
Expand All @@ -104,4 +110,20 @@ defmodule DpulCollections.IndexingPipeline.DashboardPage do
defp per_second(%{duration: duration, records_acked: records_acked}) do
records_acked / duration
end

# Pulled from
# https://nickjanetakis.com/blog/formatting-seconds-into-hh-mm-ss-with-elixir-and-python
# and modified to be consistently hh:mm:ss
defp to_hh_mm_ss(0), do: "00:00:00"

defp to_hh_mm_ss(seconds) do
units = [3600, 60, 1]
# Returns a list of how many hours, minutes, and seconds there are, reducing
# the total seconds by that amount if it's greater than 1.
t =
Enum.map_reduce(units, seconds, fn unit, val -> {div(val, unit), rem(val, unit)} end)
|> elem(0)

Enum.map_join(t, ":", fn x -> x |> Integer.to_string() |> String.pad_leading(2, "0") end)
end
end
48 changes: 48 additions & 0 deletions test/dpul_collections/indexing_pipeline/dashboard_page_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
defmodule DpuLCollections.IndexingPipeline.DashboardPageTest do
alias DpulCollections.IndexingPipeline.Figgy.IndexingProducerSource
alias DpulCollections.IndexingPipeline.Figgy.TransformationProducerSource
alias DpulCollections.IndexingPipeline.Figgy.HydrationProducerSource
alias DpulCollections.IndexingPipeline.Metrics
use DpulCollectionsWeb.ConnCase
import Phoenix.LiveViewTest
@endpoint DpulCollectionsWeb.Endpoint

test "GET /dev/dashboard/index_metrics", %{conn: conn} do
Metrics.create_index_metric(%{
type: HydrationProducerSource.processor_marker_key(),
measurement_type: "full_index",
duration: 0,
records_acked: 20
})

Metrics.create_index_metric(%{
type: TransformationProducerSource.processor_marker_key(),
measurement_type: "full_index",
duration: 10,
records_acked: 20
})

Metrics.create_index_metric(%{
type: IndexingProducerSource.processor_marker_key(),
measurement_type: "full_index",
duration: 200,
records_acked: 60
})

{:ok, view, html} =
conn
|> put_req_header("authorization", "Basic " <> Base.encode64("admin:test"))
|> get(~p"/dev/dashboard/index_metrics")
|> live

assert html =~ "Hydration Metric Times"
assert html =~ "Transformation Metric Times"
assert html =~ "Indexing Metric Times"
assert has_element?(view, "td.hydration-table-per_second", "20")
assert has_element?(view, "td.hydration-table-duration", "00:00:00")
assert has_element?(view, "td.transformation-table-per_second", "2")
assert has_element?(view, "td.transformation-table-duration", "00:00:10")
assert has_element?(view, "td.indexing-table-per_second", "0.3")
assert has_element?(view, "td.indexing-table-duration", "00:03:20")
end
end

0 comments on commit 3e76b78

Please sign in to comment.