Class: Tiler::Widgets::LineChartQuery

Inherits:
Query::Base show all
Defined in:
lib/tiler/widgets/line_chart.rb

Constant Summary collapse

VALID_BUCKETS =
%w[1h 1d 1w].freeze
DEFAULT_BUCKET =
"1d".freeze
DEFAULT_AGG =
"sum".freeze

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



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tiler/widgets/line_chart.rb', line 11

def call
  series = parse_series(config["series"])
  bucket = VALID_BUCKETS.include?(config["bucket"]) ? config["bucket"] : DEFAULT_BUCKET

  return empty_result if series.empty?

  buckets = time_buckets(bucket)
  labels  = buckets.map { |b| format_label(b, bucket) }

  datasets = series.each_with_index.map do |s, i|
    # Per-series color wins over palette/color override, which wins over
    # the default chart palette. Lets you pin a specific series (e.g.
    # "errors must always be red") regardless of where it sits.
    color = s[:color] || chart_colors[i % chart_colors.size]
    data  = buckets.map { |b| aggregate_for_bucket(b, bucket, s[:column], s[:agg]) }
    { label: s[:label], data: data, borderColor: color,
      backgroundColor: "#{color}33", tension: 0.3, fill: false, spanGaps: true }
  end

  { labels: labels, datasets: datasets, options: {} }
end