Class: PgHero::HomeController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/pg_hero/home_controller.rb

Instance Method Summary collapse

Instance Method Details

#enable_query_statsObject



84
85
86
87
88
89
90
91
# File 'app/controllers/pg_hero/home_controller.rb', line 84

def enable_query_stats
  begin
    PgHero.enable_query_stats
    redirect_to :back, notice: "Query stats enabled"
  rescue ActiveRecord::StatementInvalid => e
    redirect_to :back, alert: "The database user does not have permission to enable query stats"
  end
end

#explainObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/pg_hero/home_controller.rb', line 52

def explain
  @title = "Explain"
  @query = params[:query]
  # TODO use get + token instead of post so users can share links
  # need to prevent CSRF and DoS
  if request.post? and @query
    begin
      @explanation = PgHero.explain(@query)
    rescue ActiveRecord::StatementInvalid => e
      @error = e.message
    end
  end
end

#indexObject



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/pg_hero/home_controller.rb', line 11

def index
  @title = "Status"
  @slow_queries = PgHero.slow_queries
  @long_running_queries = PgHero.long_running_queries
  @index_hit_rate = PgHero.index_hit_rate
  @table_hit_rate = PgHero.table_hit_rate
  @missing_indexes = PgHero.missing_indexes
  @unused_indexes = PgHero.unused_indexes
  @good_cache_rate = @table_hit_rate >= 0.99 && @index_hit_rate >= 0.99
  @query_stats_available = PgHero.query_stats_available?
  @total_connections = PgHero.total_connections
  @good_total_connections = @total_connections < PgHero.total_connections_threshold
end

#indexesObject



25
26
27
28
# File 'app/controllers/pg_hero/home_controller.rb', line 25

def indexes
  @title = "Indexes"
  @index_usage = PgHero.index_usage
end

#killObject



71
72
73
74
75
76
77
# File 'app/controllers/pg_hero/home_controller.rb', line 71

def kill
  if PgHero.kill(params[:pid])
    redirect_to root_path, notice: "Query killed"
  else
    redirect_to :back, notice: "Query no longer running"
  end
end

#kill_allObject



79
80
81
82
# File 'app/controllers/pg_hero/home_controller.rb', line 79

def kill_all
  PgHero.kill_all
  redirect_to :back, notice: "Connections killed"
end

#queriesObject



36
37
38
39
# File 'app/controllers/pg_hero/home_controller.rb', line 36

def queries
  @title = "Live Queries"
  @running_queries = PgHero.running_queries
end

#query_statsObject



41
42
43
44
# File 'app/controllers/pg_hero/home_controller.rb', line 41

def query_stats
  @title = "Queries"
  @query_stats = PgHero.query_stats
end

#reset_query_statsObject



93
94
95
96
97
98
99
100
# File 'app/controllers/pg_hero/home_controller.rb', line 93

def reset_query_stats
  begin
    PgHero.reset_query_stats
    redirect_to :back, notice: "Query stats reset"
  rescue ActiveRecord::StatementInvalid => e
    redirect_to :back, alert: "The database user does not have permission to reset query stats"
  end
end

#spaceObject



30
31
32
33
34
# File 'app/controllers/pg_hero/home_controller.rb', line 30

def space
  @title = "Space"
  @database_size = PgHero.database_size
  @relation_sizes = PgHero.relation_sizes
end

#system_statsObject



46
47
48
49
50
# File 'app/controllers/pg_hero/home_controller.rb', line 46

def system_stats
  @title = "System Stats"
  @cpu_usage = PgHero.cpu_usage.map{|k, v| [k, v.round] }
  @connection_stats = PgHero.connection_stats
end

#tuneObject



66
67
68
69
# File 'app/controllers/pg_hero/home_controller.rb', line 66

def tune
  @title = "Tune"
  @settings = PgHero.settings
end