Skip to content

Commit

Permalink
Refactor component to pass options down to the vega spec
Browse files Browse the repository at this point in the history
  • Loading branch information
simonprev committed Sep 20, 2024
1 parent 125aef7 commit f659386
Show file tree
Hide file tree
Showing 19 changed files with 126 additions and 74 deletions.
13 changes: 8 additions & 5 deletions lib/telemetry_ui/metrics/average.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ defmodule TelemetryUI.Metrics.Average do
}

def to_image(metric, extension, assigns) do
spec = Components.Stat.spec(metric, assigns, @options)
spec = VegaLite.Export.to_json(spec)
TelemetryUI.VegaLiteToImage.export(spec, extension)
assigns = TelemetryUI.Metrics.merge_assigns_options(assigns, @options)

metric
|> Components.Stat.spec(assigns)
|> VegaLite.Export.to_json()
|> TelemetryUI.VegaLiteToImage.export(extension)
end

def to_html(metric, assigns) do
assigns = %{assigns | options: @options}
assigns = TelemetryUI.Metrics.merge_assigns_options(assigns, @options)

metric
|> Components.Stat.spec(assigns, @options)
|> Components.Stat.spec(assigns)
|> TelemetryUI.Web.VegaLite.draw(metric, assigns)
end
end
Expand Down
13 changes: 8 additions & 5 deletions lib/telemetry_ui/metrics/average_list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@ defmodule TelemetryUI.Metrics.AverageList do
}

def to_image(metric, extension, assigns) do
spec = Components.StatList.spec(metric, assigns, @options)
spec = VegaLite.Export.to_json(spec)
TelemetryUI.VegaLiteToImage.export(spec, extension)
assigns = TelemetryUI.Metrics.merge_assigns_options(assigns, @options)

metric
|> Components.StatList.spec(assigns)
|> VegaLite.Export.to_json()
|> TelemetryUI.VegaLiteToImage.export(extension)
end

def to_html(metric, assigns) do
assigns = %{assigns | options: @options}
assigns = TelemetryUI.Metrics.merge_assigns_options(assigns, @options)

metric
|> Components.StatList.spec(assigns, @options)
|> Components.StatList.spec(assigns)
|> TelemetryUI.Web.VegaLite.draw(metric, assigns)
end
end
Expand Down
13 changes: 8 additions & 5 deletions lib/telemetry_ui/metrics/average_over_time.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@ defmodule TelemetryUI.Metrics.AverageOverTime do
}

def to_image(metric, extension, assigns) do
spec = Components.TimeSeries.spec(metric, assigns, @options)
spec = VegaLite.Export.to_json(spec)
TelemetryUI.VegaLiteToImage.export(spec, extension)
assigns = TelemetryUI.Metrics.merge_assigns_options(assigns, @options)

metric
|> Components.TimeSeries.spec(assigns)
|> VegaLite.Export.to_json()
|> TelemetryUI.VegaLiteToImage.export(extension)
end

def to_html(metric, assigns) do
assigns = %{assigns | options: @options}
assigns = TelemetryUI.Metrics.merge_assigns_options(assigns, @options)

metric
|> Components.TimeSeries.spec(assigns, @options)
|> Components.TimeSeries.spec(assigns)
|> TelemetryUI.Web.VegaLite.draw(metric, assigns)
end
end
Expand Down
13 changes: 8 additions & 5 deletions lib/telemetry_ui/metrics/count_list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@ defmodule TelemetryUI.Metrics.CountList do
}

def to_image(metric, extension, assigns) do
spec = Components.StatList.spec(metric, assigns, @options)
spec = VegaLite.Export.to_json(spec)
TelemetryUI.VegaLiteToImage.export(spec, extension)
assigns = TelemetryUI.Metrics.merge_assigns_options(assigns, @options)

metric
|> Components.StatList.spec(assigns)
|> VegaLite.Export.to_json()
|> TelemetryUI.VegaLiteToImage.export(extension)
end

def to_html(metric, assigns) do
assigns = %{assigns | options: @options}
assigns = TelemetryUI.Metrics.merge_assigns_options(assigns, @options)

metric
|> Components.StatList.spec(assigns, @options)
|> Components.StatList.spec(assigns)
|> TelemetryUI.Web.VegaLite.draw(metric, assigns)
end
end
Expand Down
13 changes: 8 additions & 5 deletions lib/telemetry_ui/metrics/count_over_time.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@ defmodule TelemetryUI.Metrics.CountOverTime do
}

def to_image(metric, extension, assigns) do
spec = Components.TimeSeries.spec(metric, assigns, @options)
spec = VegaLite.Export.to_json(spec)
TelemetryUI.VegaLiteToImage.export(spec, extension)
assigns = TelemetryUI.Metrics.merge_assigns_options(assigns, @options)

metric
|> Components.TimeSeries.spec(assigns)
|> VegaLite.Export.to_json()
|> TelemetryUI.VegaLiteToImage.export(extension)
end

def to_html(metric, assigns) do
assigns = %{assigns | options: @options}
assigns = TelemetryUI.Metrics.merge_assigns_options(assigns, @options)

metric
|> Components.TimeSeries.spec(assigns, @options)
|> Components.TimeSeries.spec(assigns)
|> TelemetryUI.Web.VegaLite.draw(metric, assigns)
end
end
Expand Down
13 changes: 8 additions & 5 deletions lib/telemetry_ui/metrics/counter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@ defmodule TelemetryUI.Metrics.Counter do
}

def to_image(metric, extension, assigns) do
spec = Components.Stat.spec(metric, assigns, @options)
spec = VegaLite.Export.to_json(spec)
TelemetryUI.VegaLiteToImage.export(spec, extension)
assigns = TelemetryUI.Metrics.merge_assigns_options(assigns, @options)

metric
|> Components.Stat.spec(assigns)
|> VegaLite.Export.to_json()
|> TelemetryUI.VegaLiteToImage.export(extension)
end

def to_html(metric, assigns) do
assigns = %{assigns | options: @options}
assigns = TelemetryUI.Metrics.merge_assigns_options(assigns, @options)

metric
|> Components.Stat.spec(assigns, @options)
|> Components.Stat.spec(assigns)
|> TelemetryUI.Web.VegaLite.draw(metric, assigns)
end
end
Expand Down
13 changes: 8 additions & 5 deletions lib/telemetry_ui/metrics/distribution.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@ defmodule TelemetryUI.Metrics.Distribution do
}

def to_image(metric, extension, assigns) do
spec = Components.Buckets.spec(metric, assigns, @options)
spec = VegaLite.Export.to_json(spec)
TelemetryUI.VegaLiteToImage.export(spec, extension)
assigns = TelemetryUI.Metrics.merge_assigns_options(assigns, @options)

metric
|> Components.Buckets.spec(assigns)
|> VegaLite.Export.to_json()
|> TelemetryUI.VegaLiteToImage.export(extension)
end

def to_html(metric, assigns) do
assigns = %{assigns | options: @options}
assigns = TelemetryUI.Metrics.merge_assigns_options(assigns, @options)

metric
|> Components.Buckets.spec(assigns, @options)
|> Components.Buckets.spec(assigns)
|> TelemetryUI.Web.VegaLite.draw(metric, assigns)
end
end
Expand Down
13 changes: 8 additions & 5 deletions lib/telemetry_ui/metrics/last_value.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@ defmodule TelemetryUI.Metrics.LastValue do
}

def to_image(metric, extension, assigns) do
spec = Components.Stat.spec(metric, assigns, @options)
spec = VegaLite.Export.to_json(spec)
TelemetryUI.VegaLiteToImage.export(spec, extension)
assigns = TelemetryUI.Metrics.merge_assigns_options(assigns, @options)

metric
|> Components.Stat.spec(assigns)
|> VegaLite.Export.to_json()
|> TelemetryUI.VegaLiteToImage.export(extension)
end

def to_html(metric, assigns) do
assigns = %{assigns | options: @options}
assigns = TelemetryUI.Metrics.merge_assigns_options(assigns, @options)

metric
|> Components.Stat.spec(assigns, @options)
|> Components.Stat.spec(assigns)
|> TelemetryUI.Web.VegaLite.draw(metric, assigns)
end
end
Expand Down
13 changes: 8 additions & 5 deletions lib/telemetry_ui/metrics/median.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ defmodule TelemetryUI.Metrics.Median do
}

def to_image(metric, extension, assigns) do
spec = Components.Stat.spec(metric, assigns, @options)
spec = VegaLite.Export.to_json(spec)
TelemetryUI.VegaLiteToImage.export(spec, extension)
assigns = TelemetryUI.Metrics.merge_assigns_options(assigns, @options)

metric
|> Components.Stat.spec(assigns)
|> VegaLite.Export.to_json()
|> TelemetryUI.VegaLiteToImage.export(extension)
end

def to_html(metric, assigns) do
assigns = %{assigns | options: @options}
assigns = TelemetryUI.Metrics.merge_assigns_options(assigns, @options)

metric
|> Components.Stat.spec(assigns, @options)
|> Components.Stat.spec(assigns)
|> TelemetryUI.Web.VegaLite.draw(metric, assigns)
end
end
Expand Down
13 changes: 8 additions & 5 deletions lib/telemetry_ui/metrics/median_over_time.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ defmodule TelemetryUI.Metrics.MedianOverTime do
}

def to_image(metric, extension, assigns) do
spec = Components.TimeSeries.spec(metric, assigns, @options)
spec = VegaLite.Export.to_json(spec)
TelemetryUI.VegaLiteToImage.export(spec, extension)
assigns = TelemetryUI.Metrics.merge_assigns_options(assigns, @options)

metric
|> Components.TimeSeries.spec(assigns)
|> VegaLite.Export.to_json()
|> TelemetryUI.VegaLiteToImage.export(extension)
end

def to_html(metric, assigns) do
assigns = %{assigns | options: @options}
assigns = TelemetryUI.Metrics.merge_assigns_options(assigns, @options)

metric
|> Components.TimeSeries.spec(assigns, @options)
|> Components.TimeSeries.spec(assigns)
|> TelemetryUI.Web.VegaLite.draw(metric, assigns)
end
end
Expand Down
9 changes: 8 additions & 1 deletion lib/telemetry_ui/metrics/metrics.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,23 @@ defmodule TelemetryUI.Metrics do

defmacro __using__(_) do
quote do
defstruct id: nil, title: nil, telemetry_metric: nil, data: nil, ui_options: [], unit: nil, tags: [], data_resolver: nil
defstruct id: nil, title: nil, telemetry_metric: nil, data: nil, options: %{}, ui_options: [], unit: nil, tags: [], data_resolver: nil
end
end

def merge_assigns_options(assigns, options) do
options = Map.merge(options, Map.get(assigns, :options) || %{})
%{assigns | options: options}
end

for {metric_name, metric_struct} <- @telemetry_metrics do
def unquote(metric_name)(:data, options) do
{ui_options, options} = Keyword.pop(options, :ui_options, [])

struct!(unquote(metric_struct),
id: id(options[:description]),
title: options[:description],
options: options[:options],
unit: options[:unit],
ui_options: ui_options,
tags: Keyword.get(options, :tags, []),
Expand All @@ -57,6 +63,7 @@ defmodule TelemetryUI.Metrics do
id: id(metric),
title: metric.description || Event.cast_event_name(metric),
unit: unit,
options: options[:options],
ui_options: ui_options,
tags: metric.tags,
data_resolver: &{:async, fn -> TelemetryUI.metric_data(&1, metric, &2) end},
Expand Down
13 changes: 8 additions & 5 deletions lib/telemetry_ui/metrics/sum.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@ defmodule TelemetryUI.Metrics.Sum do
}

def to_image(metric, extension, assigns) do
spec = Components.Stat.spec(metric, assigns, @options)
spec = VegaLite.Export.to_json(spec)
TelemetryUI.VegaLiteToImage.export(spec, extension)
assigns = TelemetryUI.Metrics.merge_assigns_options(assigns, @options)

metric
|> Components.Stat.spec(assigns)
|> VegaLite.Export.to_json()
|> TelemetryUI.VegaLiteToImage.export(extension)
end

def to_html(metric, assigns) do
assigns = %{assigns | options: @options}
assigns = TelemetryUI.Metrics.merge_assigns_options(assigns, @options)

metric
|> Components.Stat.spec(assigns, @options)
|> Components.Stat.spec(assigns)
|> TelemetryUI.Web.VegaLite.draw(metric, assigns)
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/telemetry_ui/web/components/buckets.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ defmodule TelemetryUI.Web.Components.Buckets do

alias VegaLite, as: Vl

def spec(metric, assigns, options) do
def spec(metric, assigns) do
options = assigns.options
unit = to_unit(metric.unit)

tooltip = [
Expand Down Expand Up @@ -33,7 +34,7 @@ defmodule TelemetryUI.Web.Components.Buckets do
Vl.layers(spec, [title(metric, y: -20), buckets_chart])
end

def encode_offset_tags_color(spec, tags, assigns) do
defp encode_offset_tags_color(spec, tags, assigns) do
bar_options = [
align: "center",
baseline: "line-bottom",
Expand Down
6 changes: 4 additions & 2 deletions lib/telemetry_ui/web/components/stat.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ defmodule TelemetryUI.Web.Components.Stat do
alias TelemetryUI.Web.Components.CompareAggregate
alias VegaLite, as: Vl

def spec(%{tags: []} = metric, assigns, options) do
def spec(%{tags: []} = metric, assigns) do
options = assigns.options
time_unit = fetch_time_unit(assigns.filters.from, assigns.filters.to)
unit = to_unit(metric.unit)
chart_offset = 80
Expand Down Expand Up @@ -68,7 +69,8 @@ defmodule TelemetryUI.Web.Components.Stat do
])
end

def spec(metric, assigns, options) do
def spec(metric, assigns) do
options = assigns.options
chart_offset = 80

assigns
Expand Down
7 changes: 4 additions & 3 deletions lib/telemetry_ui/web/components/stat_list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ defmodule TelemetryUI.Web.Components.StatList do

alias VegaLite, as: Vl

def spec(%{tags: []} = metric, assigns, options) do
TelemetryUI.Web.Components.Stat.spec(metric, assigns, options)
def spec(%{tags: []} = metric, assigns) do
TelemetryUI.Web.Components.Stat.spec(metric, assigns)
end

def spec(metric, assigns, options) do
def spec(metric, assigns) do
options = assigns.options
chart_offset = 80

assigns
Expand Down
6 changes: 4 additions & 2 deletions lib/telemetry_ui/web/components/time_series.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ defmodule TelemetryUI.Web.Components.TimeSeries do
alias TelemetryUI.Web.Components.CompareAggregate
alias VegaLite, as: Vl

def spec(%{tags: []} = metric, assigns, options) do
def spec(%{tags: []} = metric, assigns) do
options = assigns.options
to = DateTime.to_unix(DateTime.add(assigns.filters.to, 60, :second), :millisecond)
from = DateTime.to_unix(assigns.filters.from, :millisecond)
time_unit = fetch_time_unit(assigns.filters.from, assigns.filters.to)
Expand Down Expand Up @@ -45,7 +46,8 @@ defmodule TelemetryUI.Web.Components.TimeSeries do
Vl.layers(spec, [title(metric, y: -24), compare_aggregate_text_spec(options, metric), aggregate_text_spec(options, unit), summary_chart])
end

def spec(metric, assigns, options) do
def spec(metric, assigns) do
options = assigns.options
to = DateTime.to_unix(DateTime.add(assigns.filters.to, 60, :second), :millisecond)
from = DateTime.to_unix(assigns.filters.from, :millisecond)
time_unit = fetch_time_unit(assigns.filters.from, assigns.filters.to)
Expand Down
Loading

0 comments on commit f659386

Please sign in to comment.