Class: PaperTrailViewer::DataSource::Bigquery

Inherits:
Base
  • Object
show all
Defined in:
lib/paper_trail_viewer/data_source/bigquery.rb

Defined Under Namespace

Classes: Adapter

Instance Method Summary collapse

Methods inherited from Base

#call

Constructor Details

#initialize(project_id:, credentials:, table:) ⇒ Bigquery

Returns a new instance of Bigquery.



3
4
5
6
7
8
9
10
11
# File 'lib/paper_trail_viewer/data_source/bigquery.rb', line 3

def initialize(project_id:, credentials:, table:)
  require 'google/cloud/bigquery'

  @bigquery = Google::Cloud::Bigquery.new(
    project_id:  project_id,
    credentials: credentials,
  )
  @table = table
end

Instance Method Details

#perform_query(q) ⇒ Object

Parameters:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/paper_trail_viewer/data_source/bigquery.rb', line 14

def perform_query(q)
  # https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax
  bigquery_result = @bigquery.query(<<~SQL, max: q.per_page)
    SELECT   *
    FROM     `#{@table}`
    # Ignore blank versions that only touch updated_at or untracked fields.
    WHERE    object_changes != ''
    #{"AND   item_type = '#{q.item_type}'"        if q.item_type.present?}
    #{"AND   item_id = #{q.item_id}"              if q.item_id.present?}
    #{"AND   event = '#{q.event}'"                if q.event.present?}
    #{"AND   object_changes LIKE '%#{q.filter}%'" if q.filter.present?}
    ORDER BY created_at DESC, id DESC
    # Paginate via OFFSET.
    # LIMIT must be greater than `max:` or result#next? is always false.
    LIMIT    #{q.per_page + 1} OFFSET #{(q.page - 1) * q.per_page}
  SQL

  Adapter.new(bigquery_result)
end