Class: Tiler::Widgets::TableQuery

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

Constant Summary collapse

DEFAULT_LIMIT =
20
DEFAULT_AGG =
"last".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



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/table.rb', line 10

def call
  cols     = parse_columns(config["columns"])
  group_by = config["group_by"]
  limit    = (config["limit"] || DEFAULT_LIMIT).to_i.clamp(1, 500)

  return empty_result(cols) if cols.empty? || group_by.blank?
  return empty_result(cols) unless safe_col?(group_by)

  groups = distinct_values(group_by)
  rows = groups.map do |g|
    scope = base_scope.where("json_extract(payload, ?) = ?", "$.#{group_by}", g.to_s)
    [ g ] + cols.map { |c| cell_for(scope, c) }
  end

  # Sort by the first numeric column (descending) for leaderboard feel;
  # falls back to label order if no numeric column was declared.
  first_num = cols.index { |c| c[:num] }
  rows = if first_num
    rows.sort_by { |r| -r[first_num + 1].to_f }
  else
    rows.sort_by { |r| r[0].to_s }
  end

  {
    columns: [ { label: group_by.humanize, num: false } ] + cols,
    rows:    rows.first(limit),
    total:   groups.size,
    limit:   limit
  }
end