Module: PgInsights::InsightsHelper

Defined in:
app/helpers/pg_insights/insights_helper.rb

Instance Method Summary collapse

Instance Method Details

#cell_value_class(cell) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/helpers/pg_insights/insights_helper.rb', line 88

def cell_value_class(cell)
  if cell.nil?
    "null-value"
  elsif cell.to_s.strip.empty?
    "empty-value"
  elsif cell.is_a?(Numeric) || (cell.is_a?(String) && cell.match?(/^\d+(\.\d+)?$/))
    "numeric-value"
  elsif cell.to_s.length > 50
    "long-text"
  else
    "text-value"
  end
end

#filter_buttons_dataObject



79
80
81
82
83
84
85
86
# File 'app/helpers/pg_insights/insights_helper.rb', line 79

def filter_buttons_data
  [
    { category: "all", label: "All", active: true },
    { category: "database", label: "Database", active: false },
    { category: "business", label: "Business", active: false },
    { category: "saved", label: "Saved", active: false }
  ]
end

#format_cell_value(cell) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
# File 'app/helpers/pg_insights/insights_helper.rb', line 102

def format_cell_value(cell)
  if cell.nil?
    "NULL"
  elsif cell.to_s.strip.empty?
    "empty"
  elsif cell.to_s.length > 100
    truncate(cell.to_s, length: 100)
  else
    cell.to_s
  end
end

#query_category_badge_class(category) ⇒ Object



53
54
55
# File 'app/helpers/pg_insights/insights_helper.rb', line 53

def query_category_badge_class(category)
  "query-category-badge #{category.to_s.downcase}"
end

#query_example_button_data(query) ⇒ Object



71
72
73
74
75
76
77
# File 'app/helpers/pg_insights/insights_helper.rb', line 71

def query_example_button_data(query)
  {
    query_id: query["id"],
    category: query["category"],
    title: query["description"]
  }
end

#query_info_textObject



114
115
116
117
118
# File 'app/helpers/pg_insights/insights_helper.rb', line 114

def query_info_text
  execute_timeout = format_timeout(PgInsights.query_execution_timeout)
  analyze_timeout = format_timeout(PgInsights.query_analysis_timeout)
  "SELECT only • #{execute_timeout} exec • #{analyze_timeout} analyze • 1k row limit"
end

#render_chart(result) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/helpers/pg_insights/insights_helper.rb', line 15

def render_chart(result)
  chart_data = prepare_chart_data(result)
  return "" unless chart_data[:chartData].present?

  # Use bar chart as default with ChartKick
  begin
    bar_chart(
      chart_data[:chartData],
      height: "350px",
      colors: [ "#00979D", "#00838a", "#00767a" ],
      responsive: true
    )
  rescue => e
    (:div,
                "Chart data format error: #{e.message}",
                style: "text-align: center; padding: 40px; color: #64748b;"
    )
  end
end

#render_stats(result) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/helpers/pg_insights/insights_helper.rb', line 35

def render_stats(result)
  stats = calculate_stats(result)

  (:div) do
    (:div, class: "stats-section") do
      (:h4, "Summary Statistics") +
        (:div, class: "stats-grid") do
          stats.map do |stat|
            (:div, class: "stat-card") do
              (:div, stat[:value], class: "stat-value") +
                (:div, stat[:label], class: "stat-label")
            end
          end.join.html_safe
        end
    end
  end
end

#should_show_chart?(result) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
6
7
8
9
10
11
12
13
# File 'app/helpers/pg_insights/insights_helper.rb', line 3

def should_show_chart?(result)
  return false unless result&.rows&.any?
  return false if result.columns.size < 2 || result.columns.size > 4

  # Check if we have at least one numeric column
  has_numeric = result.rows.any? do |row|
    row.any? { |cell| cell.is_a?(Numeric) || (cell.is_a?(String) && cell.match?(/^\d+(\.\d+)?$/)) }
  end

  has_numeric && result.rows.size <= 50 # Limit for better chart readability
end

#sql_placeholder_textObject



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/helpers/pg_insights/insights_helper.rb', line 57

def sql_placeholder_text
  "    -- Enter your SQL query here\n\n    -- Example: Database table sizes (great for charts!)\n    SELECT tablename,\n           pg_total_relation_size(schemaname||'.'||tablename) as size_bytes\n    FROM pg_tables\n    WHERE schemaname = 'public'\n    ORDER BY pg_total_relation_size(schemaname||'.'||tablename) DESC\n    LIMIT 10;\n  SQL\nend\n".strip