Class: Railscope::Storage::Database

Inherits:
Base
  • Object
show all
Defined in:
lib/railscope/storage/database.rb

Instance Method Summary collapse

Methods inherited from Base

#find!

Instance Method Details

#all(filters: {}, page: 1, per_page: 25, displayable_only: true) ⇒ Object



29
30
31
32
33
# File 'lib/railscope/storage/database.rb', line 29

def all(filters: {}, page: 1, per_page: 25, displayable_only: true)
  scope = build_scope(filters, displayable_only)
  records = scope.recent.limit(per_page).offset((page - 1) * per_page)
  records.map { |r| EntryData.from_active_record(r) }
end

#count(filters: {}, displayable_only: true) ⇒ Object



35
36
37
# File 'lib/railscope/storage/database.rb', line 35

def count(filters: {}, displayable_only: true)
  build_scope(filters, displayable_only).count
end

#destroy_all! ⇒ Object



53
54
55
# File 'lib/railscope/storage/database.rb', line 53

def destroy_all!
  Entry.delete_all
end

#destroy_expired! ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/railscope/storage/database.rb', line 57

def destroy_expired!
  total_deleted = 0
  batch_size = 1000

  loop do
    expired_ids = Entry.expired.limit(batch_size).pluck(:uuid)
    break if expired_ids.empty?

    deleted = Entry.where(uuid: expired_ids).delete_all
    total_deleted += deleted
    break if deleted < batch_size
  end

  total_deleted
end

#family_count(family_hash) ⇒ Object



49
50
51
# File 'lib/railscope/storage/database.rb', line 49

def family_count(family_hash)
  Entry.for_family(family_hash).count
end

#find(uuid) ⇒ Object



22
23
24
25
26
27
# File 'lib/railscope/storage/database.rb', line 22

def find(uuid)
  record = Entry.find_by(uuid: uuid)
  return nil unless record

  EntryData.from_active_record(record)
end

#for_batch(batch_id) ⇒ Object



39
40
41
42
# File 'lib/railscope/storage/database.rb', line 39

def for_batch(batch_id)
  records = Entry.for_batch(batch_id).order(:occurred_at)
  records.map { |r| EntryData.from_active_record(r) }
end

#for_family(family_hash, page: 1, per_page: 25) ⇒ Object



44
45
46
47
# File 'lib/railscope/storage/database.rb', line 44

def for_family(family_hash, page: 1, per_page: 25)
  records = Entry.for_family(family_hash).recent.limit(per_page).offset((page - 1) * per_page)
  records.map { |r| EntryData.from_active_record(r) }
end

#ready? ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
# File 'lib/railscope/storage/database.rb', line 73

def ready?
  Entry.table_exists?
rescue StandardError
  false
end

#update_by_batch(batch_id:, entry_type:, payload_updates:) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/railscope/storage/database.rb', line 13

def update_by_batch(batch_id:, entry_type:, payload_updates:)
  entry = Entry.where(batch_id: batch_id, entry_type: entry_type).order(created_at: :desc).first
  return nil unless entry

  entry.payload = entry.payload.merge(payload_updates)
  entry.save!
  EntryData.from_active_record(entry)
end

#write(attributes) ⇒ Object



6
7
8
9
10
11
# File 'lib/railscope/storage/database.rb', line 6

def write(attributes)
  # Remove uuid if present - let the database generate it
  attrs = attributes.except(:uuid, :created_at, :updated_at)
  record = Entry.create!(attrs)
  EntryData.from_active_record(record)
end