Class: PgReports::ReportDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_reports/report_definition.rb

Overview

Parses YAML report definitions and generates Report objects

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(yaml_path) ⇒ ReportDefinition

Returns a new instance of ReportDefinition.



8
9
10
11
# File 'lib/pg_reports/report_definition.rb', line 8

def initialize(yaml_path)
  @config = YAML.load_file(yaml_path)["report"]
  @yaml_path = yaml_path
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/pg_reports/report_definition.rb', line 6

def config
  @config
end

Instance Method Details

#filter_parametersObject

Extract filter parameters for UI



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/pg_reports/report_definition.rb', line 119

def filter_parameters
  params = {}

  # Parameters from parameters section
  config["parameters"]&.each do |name, param_config|
    params[name] = {
      type: param_config["type"],
      default: param_config["default"],
      description: I18n.t("pg_reports.parameters.#{name}.description", default: param_config["description"]),
      label: I18n.t("pg_reports.parameters.#{name}.label", default: name.to_s.titleize)
    }
  end

  # Add threshold parameters from filters (config-based)
  config["filters"]&.each do |filter|
    if filter["value"]["source"] == "config"
      config_key = filter["value"]["key"]
      field_name = filter["field"]

      params["#{field_name}_threshold"] = {
        type: filter["cast"] || "integer",
        default: PgReports.config.public_send(config_key),
        description: I18n.t("pg_reports.parameters.threshold_description", field: field_name),
        label: I18n.t("pg_reports.parameters.threshold_label", field: field_name.titleize),
        current_config: PgReports.config.public_send(config_key),
        is_threshold: true
      }
    end
  end

  params
end

#generate_report(**params) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pg_reports/report_definition.rb', line 13

def generate_report(**params)
  # 1. Execute SQL
  data = execute_sql(**params)

  # 2. Apply filters
  data = apply_filters(data, params)

  # 3. Apply enrichment
  data = apply_enrichment(data) if enrichment?

  # 4. Apply limit
  limit = params[:limit] || default_limit
  data = data.first(limit) if limit && data.respond_to?(:first)

  # 5. Create Report
  Report.new(
    title: interpolate_title(params),
    data: data,
    columns: config["columns"]
  )
end

#problem_explanationsObject

Extract problem explanations mapping from YAML



153
154
155
# File 'lib/pg_reports/report_definition.rb', line 153

def problem_explanations
  config["problem_explanations"] || {}
end