Module: Batsd::Dash::GraphHelper

Defined in:
lib/batsd-dash/graph.rb

Instance Method Summary collapse

Instance Method Details

#transform_point_at!(index, values) ⇒ Object

Transform a point at a given index. Works against values array

Parameters:

  • index (Integer)
  • values (Array)

    array of values



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/batsd-dash/graph.rb', line 49

def transform_point_at!(index, values)
  data_pt = values[index]

  # we've already transformed this index (must be zerofill)
  return data_pt unless Hash === data_pt

  pt_time = data_pt['timestamp'].to_i * 1000
  pt_value = data_pt['value'].to_f

  values[index] = [pt_time, pt_value]
end

#values_for_graph(values, opts = {}) ⇒ Array

This method works directly against values. It will tranform all datapoint to an array where the first element is a milisecond timestamp and the second is a float value data point.

Parameters:

  • values (Array)

    array of datapoints

  • opts (Hash) (defaults to: {})

    optional options hash

Returns:

  • (Array)

    modified values array



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/batsd-dash/graph.rb', line 12

def values_for_graph(values, opts = {})
  return values if values.empty?

  values.tap do |pts|
    step = opts[:interval] * 1000
    range = opts[:range]

    # transform the first point
    transform_point_at!(0, values)

    # start from the first timestamp
    time = values.first.first + step
    index = 0

    # loop through values to transform and zerofill
    while index < values.size - 1
      obj = transform_point_at!(index += 1, values)
      next unless opts[:zero_fill]

      if obj.first <= time
        time += step
        next
      end

      # need to insert zerofilled point (if zerofill is enabled)
      values.insert(index, [time, 0])
      time += step
    end
  end
end