Module: SolidObserver::DashboardHelper

Defined in:
app/helpers/solid_observer/dashboard_helper.rb

Constant Summary collapse

SVG_W =
120
SVG_H =
32
RANGE_LABELS =
{
  "15m" => "in last 15m",
  "30m" => "in last 30m",
  "1h" => "in last hour",
  "7h" => "in last 7h",
  "1d" => "in last day",
  "7d" => "in last 7d",
  "14d" => "in last 14d"
}.freeze

Instance Method Summary collapse

Instance Method Details

#range_label(range_key) ⇒ Object



35
36
37
# File 'app/helpers/solid_observer/dashboard_helper.rb', line 35

def range_label(range_key)
  RANGE_LABELS.fetch(range_key, "in selected range")
end

#spark_points(series, width: SVG_W, height: SVG_H) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/helpers/solid_observer/dashboard_helper.rb', line 9

def spark_points(series, width: SVG_W, height: SVG_H)
  return "" if series.blank?

  t_min = series.first[:t]
  t_max = series.last[:t]
  v_max = [series.max_by { |point| point[:v] }[:v], 1].max
  time_span = t_max - t_min

  series.map { |point|
    val = point[:v]
    point_x = time_span.zero? ? width / 2.0 : ((point[:t] - t_min).to_f / time_span) * (width - 2) + 1
    point_y = height - 1 - (val.to_f / v_max) * (height - 2)
    format("%.1f,%.1f", point_x, point_y)
  }.join(" ")
end