PgFlowDashboard.Live.LiveHelpers (PgFlow v0.2.0)

Copy Markdown View Source

Shared hooks and utilities for PgFlowDashboard LiveViews.

Provides common functionality for configuration access and real-time subscriptions.

Summary

Functions

Determines view mode based on count and threshold.

Formats a duration in milliseconds for display.

Formats a percentage for display, rounded to one decimal place.

Formats a datetime relative to now.

Formats a timestamp for display in the configured time zone.

Assigns dashboard configuration for LiveViews.

Splits a list fetched with limit+1 into results and has_more flag.

Schedules a refresh timer for polling updates.

Returns a short form of a UUID for display.

Returns CSS classes for a status badge.

Returns the color for a status in hex format (for SVG).

Subscribes to updates for a specific run.

Subscribes to PgFlow PubSub topics for real-time updates.

Unsubscribes from a specific run's updates.

Functions

determine_view_mode(count, threshold \\ 12)

Determines view mode based on count and threshold.

Returns :card for small datasets (≤ threshold) and :list for larger ones. Default threshold is 12 (fits nicely in a 3-column grid).

Examples

iex> determine_view_mode(5)
:card

iex> determine_view_mode(15)
:list

iex> determine_view_mode(15, 20)
:card

format_duration(ms)

Formats a duration in milliseconds for display.

Formats in a compact style suitable for dashboards: "50ms", "1.5s", "2.3m", "1.2h".

format_percent(percent)

Formats a percentage for display, rounded to one decimal place.

The dashboard's percentage columns are numeric, which Postgrex decodes to Decimal. Phoenix.HTML.Safe has no implementation for that struct, so interpolating one straight into a template raises Protocol.UndefinedError — every such value has to reach the markup through a formatter.

format_relative_time(dt)

Formats a datetime relative to now.

Returns strings like "in 5 minutes", "in 2 hours", "3 minutes ago".

format_timestamp(dt, time_zone)

Formats a timestamp for display in the configured time zone.

on_mount(session, socket)

Assigns dashboard configuration for LiveViews.

This function should be called in the on_mount callback:

def on_mount(:default, _params, session, socket) do
  PgFlowDashboard.Live.LiveHelpers.on_mount(session, socket)
end

paginate_results(items, page_size)

Splits a list fetched with limit+1 into results and has_more flag.

When fetching paginated data, request page_size + 1 items. This function splits the result to determine if there are more items available.

Examples

iex> paginate_results([1, 2, 3], 2)
{[1, 2], true}

iex> paginate_results([1, 2], 2)
{[1, 2], false}

schedule_refresh(socket)

Schedules a refresh timer for polling updates.

short_id(id)

Returns a short form of a UUID for display.

status_classes(status)

Returns CSS classes for a status badge.

status_color(status)

Returns the color for a status in hex format (for SVG).

subscribe_to_run(socket, run_id)

Subscribes to updates for a specific run.

subscribe_to_updates(socket)

Subscribes to PgFlow PubSub topics for real-time updates.

unsubscribe_from_run(socket, run_id)

Unsubscribes from a specific run's updates.