Class: Tiler::Widgets::NumberWithDeltaQuery

Inherits:
Query::Base
  • Object
show all
Defined in:
lib/tiler/widgets/number_with_delta.rb

Constant Summary collapse

SPARK_BUCKETS =
7

Constants inherited from Query::Base

Query::Base::SAFE_COLUMN_RE

Instance Attribute Summary

Attributes inherited from Query::Base

#config, #panel, #source

Instance Method Summary collapse

Methods inherited from Query::Base

#initialize

Constructor Details

This class inherits a constructor from Tiler::Query::Base

Instance Method Details

#callObject



9
10
11
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
# File 'lib/tiler/widgets/number_with_delta.rb', line 9

def call
  col   = config["value_column"]
  agg   = config["aggregation"] || "count"
  prev  = previous_window_seconds

  current  = aggregate(base_scope, col, agg)
  previous = if prev && time_window_start && source
    scope = apply_filters(
      source.data_records.where(recorded_at: (time_window_start - prev)...time_window_start)
    )
    aggregate(scope, col, agg)
  end

  delta = (current.to_f - previous.to_f) if previous
  pct   = if previous.to_f.nonzero?
    ((current.to_f - previous.to_f) / previous.to_f * 100).round(1)
  end

  {
    value:       current,
    previous:    previous,
    delta:       delta,
    pct:         pct,
    label:       panel.title,
    aggregation: agg,
    time_window: config["time_window"],
    direction:   (delta.to_f.positive? ? :up : (delta.to_f.negative? ? :down : :flat)),
    spark:       sparkline_points(col, agg),
    color:       sanitize_color(config["color"])
  }
end