Class: Profiler::Storage::SqliteStore

Inherits:
BaseStore
  • Object
show all
Defined in:
lib/profiler/storage/sqlite_store.rb

Instance Method Summary collapse

Methods inherited from BaseStore

#exists?

Constructor Details

#initialize(options = {}) ⇒ SqliteStore

Returns a new instance of SqliteStore.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/profiler/storage/sqlite_store.rb', line 12

def initialize(options = {})
  require "sqlite3"

  db_path = options[:database] || default_db_path
  blob_path = options[:blob_path] || default_blob_path

  FileUtils.mkdir_p(File.dirname(db_path))

  @db = SQLite3::Database.new(db_path.to_s)
  @db.results_as_hash = true
  @db.busy_timeout = 5000
  @db.execute("PRAGMA journal_mode=WAL")
  @db.execute("PRAGMA synchronous=NORMAL")

  @blob_store = BlobStore.new(blob_path.to_s)

  migrate!
end

Instance Method Details

#cleanup(older_than: 24 * 60 * 60) ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/profiler/storage/sqlite_store.rb', line 131

def cleanup(older_than: 24 * 60 * 60)
  cutoff = (Time.now - older_than).utc.iso8601
  tokens = @db.execute(
    "SELECT token FROM profiler_profiles WHERE started_at < :cutoff", cutoff: cutoff
  ).map { |r| r["token"] }
  @db.execute("DELETE FROM profiler_profiles WHERE started_at < :cutoff", cutoff: cutoff)
  tokens.each { |token| @blob_store.delete(token) }
end

#clear(type: nil) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/profiler/storage/sqlite_store.rb', line 118

def clear(type: nil)
  if type.nil?
    tokens = @db.execute("SELECT token FROM profiler_profiles").map { |r| r["token"] }
    @db.execute("DELETE FROM profiler_profiles")
  else
    tokens = @db.execute(
      "SELECT token FROM profiler_profiles WHERE profile_type = :type", type: type.to_s
    ).map { |r| r["token"] }
    @db.execute("DELETE FROM profiler_profiles WHERE profile_type = :type", type: type.to_s)
  end
  tokens.each { |token| @blob_store.delete(token) }
end

#delete(token) ⇒ Object



113
114
115
116
# File 'lib/profiler/storage/sqlite_store.rb', line 113

def delete(token)
  @db.execute("DELETE FROM profiler_profiles WHERE token = :token", token: token)
  @blob_store.delete(token)
end

#find_by_parent(parent_token) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/profiler/storage/sqlite_store.rb', line 105

def find_by_parent(parent_token)
  rows = @db.execute(
    "SELECT * FROM profiler_profiles WHERE parent_token = :parent_token ORDER BY started_at ASC",
    parent_token: parent_token
  )
  rows.map { |row| row_to_profile(row) }.compact
end

#list(limit: 50, offset: 0) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/profiler/storage/sqlite_store.rb', line 97

def list(limit: 50, offset: 0)
  rows = @db.execute(
    "SELECT * FROM profiler_profiles ORDER BY started_at DESC LIMIT :limit OFFSET :offset",
    limit: limit, offset: offset
  )
  rows.map { |row| row_to_profile(row, load_blobs: false) }.compact
end

#load(token) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/profiler/storage/sqlite_store.rb', line 85

def load(token)
  row = @db.get_first_row(
    "SELECT * FROM profiler_profiles WHERE token = :token", token: token
  )
  return nil unless row

  row_to_profile(row)
rescue => e
  warn "SqliteStore: failed to load profile #{token}: #{e.message}"
  nil
end

#save(token, profile) ⇒ Object



31
32
33
34
35
36
37
38
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
76
77
78
79
80
81
82
83
# File 'lib/profiler/storage/sqlite_store.rb', line 31

def save(token, profile)
  data = profile.to_h

  collectors_meta = {}
  (data[:collectors_data] || {}).each do |collector_name, collector_data|
    next unless collector_data.is_a?(Hash)

    if collector_name.to_s == "http"
      requests = collector_data["requests"]
      if requests
        stripped = save_http_response_bodies(token, requests)
        collectors_meta[collector_name] = collector_data.merge("requests" => stripped)
      else
        collectors_meta[collector_name] = collector_data
      end
    else
      collectors_meta[collector_name] = collector_data
    end
  end

  @db.execute(
    <<~SQL,
      INSERT OR REPLACE INTO profiler_profiles (
        token, profile_type, gem_version, path, method, status, duration, memory,
        started_at, finished_at, parent_token, is_ajax,
        tabs, params, headers, response_headers, collectors_meta
      ) VALUES (
        :token, :profile_type, :gem_version, :path, :method, :status, :duration, :memory,
        :started_at, :finished_at, :parent_token, :is_ajax,
        :tabs, :params, :headers, :response_headers, :collectors_meta
      )
    SQL
    token:            token,
    profile_type:     data[:profile_type] || "http",
    gem_version:      data[:gem_version],
    path:             data[:path],
    method:           data[:method],
    status:           data[:status],
    duration:         data[:duration],
    memory:           data[:memory],
    started_at:       data[:started_at],
    finished_at:      data[:finished_at],
    parent_token:     data[:parent_token],
    is_ajax:          data[:is_ajax] ? 1 : 0,
    tabs:             JSON.generate(data[:tabs] || []),
    params:           JSON.generate(data[:params] || {}),
    headers:          JSON.generate(data[:headers] || {}),
    response_headers: JSON.generate(data[:response_headers] || {}),
    collectors_meta:  JSON.generate(collectors_meta)
  )

  token
end