Module: SolidObserver::DashboardHelper

Included in:
ApplicationHelper
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

#build_spark_context(series, width, height) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/helpers/solid_observer/dashboard_helper.rb', line 16

def build_spark_context(series, width, height)
  first_point = series.first
  last_point = series.last
  min_time = first_point[:t]

  {
    min_time: min_time,
    time_span: last_point[:t] - min_time,
    max_value: [series.max_by { |point| point[:v] }[:v], 1].max,
    width: width,
    height: height,
    inner_width: width - 2,
    inner_height: height - 2
  }
end

#format_spark_point(point:, context:) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'app/helpers/solid_observer/dashboard_helper.rb', line 32

def format_spark_point(point:, context:)
  time_span = context[:time_span]
  coordinate_x = if time_span.zero?
    context[:width] / 2.0
  else
    ((point[:t] - context[:min_time]).to_f / time_span) * context[:inner_width] + 1
  end

  coordinate_y = context[:height] - 1 - (point[:v].to_f / context[:max_value]) * context[:inner_height]
  format("%.1f,%.1f", coordinate_x, coordinate_y)
end

#range_label(range_key) ⇒ Object



54
55
56
# File 'app/helpers/solid_observer/dashboard_helper.rb', line 54

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
# File 'app/helpers/solid_observer/dashboard_helper.rb', line 9

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

  context = build_spark_context(series, width, height)
  series.map { |point| format_spark_point(point: point, context: context) }.join(" ")
end