Class: PgInsights::TimelineController

Inherits:
ApplicationController show all
Defined in:
app/controllers/pg_insights/timeline_controller.rb

Instance Method Summary collapse

Instance Method Details

#compareObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/controllers/pg_insights/timeline_controller.rb', line 39

def compare
  @date1 = Date.parse(params[:date1]) rescue nil
  @date2 = Date.parse(params[:date2]) rescue nil

  unless @date1 && @date2
    flash[:error] = "Invalid dates provided"
    redirect_to timeline_path
    return
  end

  @snapshot1 = HealthCheckResult.find_snapshot_by_date(@date1)
  @snapshot2 = HealthCheckResult.find_snapshot_by_date(@date2)

  unless @snapshot1 && @snapshot2
    flash[:error] = "Could not find snapshots for the selected dates"
    redirect_to timeline_path
    return
  end

              @parameter_changes = HealthCheckResult.compare_snapshots(@snapshot1, @snapshot2)
  performance_metrics = compare_performance_metrics(@snapshot1, @snapshot2)
  @performance_comparison = {
    metrics: performance_metrics.transform_values do |data|
      data.merge(difference: data[:change])
    end
  }
   = (@snapshot1, @snapshot2)
  @configuration_comparison = @parameter_changes

  @comparison_data = {
    snapshot1: @snapshot1,
    snapshot2: @snapshot2,
    parameters: @parameter_changes,
    performance: @performance_comparison,
    metadata: 
  }
end

#exportObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/pg_insights/timeline_controller.rb', line 77

def export
  format = params[:format]&.downcase || "csv"
  days = params[:days]&.to_i || 30

  snapshots = HealthCheckResult.snapshots(days + 30)

  case format
  when "csv"
    send_data generate_csv_export(snapshots),
              filename: "pg_insights_timeline_#{Date.current}.csv",
              type: "text/csv",
              disposition: "attachment"
  when "json"
    send_data generate_json_export(snapshots),
              filename: "pg_insights_timeline_#{Date.current}.json",
              type: "application/json",
              disposition: "attachment"
  else
    flash[:error] = "Unsupported export format. Use 'csv' or 'json'."
    redirect_to timeline_path
  end
end

#indexObject



7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/controllers/pg_insights/timeline_controller.rb', line 7

def index
  unless PgInsights.snapshots_available?
    flash[:notice] = "Database snapshots are not enabled. Configure PgInsights.enable_snapshots = true to use timeline features."
    redirect_to health_path
    return
  end

  @snapshots = HealthCheckResult.snapshots(90)
  @parameter_changes = HealthCheckResult.detect_parameter_changes_since(30)
  @timeline_data = HealthCheckResult.timeline_data(30, @parameter_changes)
  @stats = calculate_timeline_stats(@snapshots)
end

#refreshObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/controllers/pg_insights/timeline_controller.rb', line 100

def refresh
  if PgInsights.snapshots_available? && PgInsights.background_jobs_available?
    if PgInsights::DatabaseSnapshotJob.perform_later
      render json: { message: "Snapshot collection started" }
    else
      render json: { error: "Failed to start snapshot collection" }, status: 422
    end
  else
    begin
      HealthCheckService.execute_and_cache_check("database_snapshot")
      render json: { message: "Snapshot collected successfully" }
    rescue => e
      render json: { error: "Snapshot collection failed: #{e.message}" }, status: 422
    end
  end
end

#showObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/pg_insights/timeline_controller.rb', line 20

def show
  @snapshot = HealthCheckResult.find(params[:id])

  unless @snapshot.check_type == "database_snapshot"
    flash[:error] = "Invalid snapshot"
    redirect_to timeline_path
    return
  end

  @previous_snapshot = HealthCheckResult.snapshots
                                       .where("executed_at < ?", @snapshot.executed_at)
                                       .first

  if @previous_snapshot
    @parameter_changes = HealthCheckResult.compare_snapshots(@previous_snapshot, @snapshot)
    @performance_comparison = compare_performance_metrics(@previous_snapshot, @snapshot)
  end
end

#statusObject



117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/controllers/pg_insights/timeline_controller.rb', line 117

def status
  snapshot_status = {
    enabled: PgInsights.snapshots_available?,
    frequency: PgInsights.snapshot_frequency,
    retention_days: PgInsights.snapshot_retention_days,
    latest_snapshot: HealthCheckResult.latest_snapshot&.executed_at,
    total_snapshots: HealthCheckResult.snapshots.count,
    configuration_valid: PgInsights::DatabaseSnapshotJob.validate_configuration
  }

  render json: snapshot_status
end