Class: Quill

Inherits:
Object
  • Object
show all
Defined in:
lib/quill-sql.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Quill

Returns a new instance of Quill.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/quill-sql.rb', line 41

def initialize(options = {})
  validate_options(options)    
  @base_url = options[:metadata_server_url] || HOST
  @config = { headers: { 'Authorization' => "Bearer #{options[:private_key]}" } }
  
  credentials = options[:database_config]
  if options[:database_connection_string]
    credentials = DatabaseHelper.get_database_credentials(
      options[:database_type],
      options[:database_connection_string]
    )
  end

  @target_connection = CachedConnection.new(
    options[:database_type],
    credentials,
    options[:cache] || {}
  )
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



39
40
41
# File 'lib/quill-sql.rb', line 39

def base_url
  @base_url
end

#configObject (readonly)

Returns the value of attribute config.



39
40
41
# File 'lib/quill-sql.rb', line 39

def config
  @config
end

#target_connectionObject (readonly)

Returns the value of attribute target_connection.



39
40
41
# File 'lib/quill-sql.rb', line 39

def target_connection
  @target_connection
end

Instance Method Details

#query(params = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/quill-sql.rb', line 61

def query(params = {})
  validate_query_params(params)

  tenants = params[:tenants]
  flags = params[:flags]
   = params[:metadata]
   = .transform_keys(&:to_sym)
  filters = params[:filters]

  # TODO: Add support for multiple tenants
  if tenants&.any?
    @target_connection.tenant_ids = TenantUtils.extract_tenant_ids(tenants)
  end

   = {}

  unless [:task]
    return { error: "Missing task.", status: "error", data: {} }
  end

  tenant_flags = nil

  begin
    # If the task requires flags to be synthesized from tenants
    if FLAG_TASKS.include?([:task]) &&
       tenants&.first != ALL_TENANTS &&
       tenants&.first != SINGLE_TENANT &&
       ([:task] != 'filter-options' ||
        ![:reportId])
       
      response = post_quill('tenant-mapped-flags', {
        reportId: [:reportId] || [:dashboardItemId],
        dashboardName: [:name],
        tenants: tenants,
        flags: flags
      })

      return {
        status: "error",
        error: response[:error],
        data: response[:metadata] || {}
      } if response[:error]

      flag_query_results = run_queries(response[:queries], @target_connection.database_type)
      tenant_flags = response[:metadata][:queryOrder].map.with_index do |tenant_field, index|
        {
          tenant_field: tenant_field,
          flags: Set.new(
            flag_query_results[:queryResults][index][:rows].map do |row|
              row['quill_flag']
            end
          ).to_a
        }
      end
    elsif tenants&.first == SINGLE_TENANT && flags
      if flags.length && flags[0].is_a?(Hash)
        tenant_flags = [{ tenant_field: SINGLE_TENANT, flags: flags }]
      else
        tenant_flags = flags.map do |flag|
          { tenant_field: SINGLE_TENANT, flags: flag }
        end
      end
    end

    pre_query_results = if [:preQueries]
                          run_queries(
                            [:preQueries],
                            @target_connection.database_type,
                            [:databaseType],
                            [:runQueryConfig]
                          )
                        else
                          {}
                        end

    if .dig(:runQueryConfig, "overridePost")
      return {
        data: { queryResults: pre_query_results },
        status: "success"
      }
    end

    response = post_quill([:task], {
      **,
      sdk_filters: filters&.map { |filter| FilterUtils.convert_custom_filter(filter) },
      **pre_query_results,
      tenants: tenants,
      flags: tenant_flags,
      viewQuery: [:preQueries]&.first
    })

    return {
      status: "error",
      error: response[:error],
      data: response[:metadata] || {}
    } if response[:error]

     = response[:metadata] if response[:metadata]

    results = run_queries(
      response[:queries],
      @target_connection.database_type,
      [:database_type],
      .dig(:runQueryConfig)
    )
    if results[:mappedArray] && .dig(:runQueryConfig, :arrayToMap)
      array_to_map = .dig(:runQueryConfig, :arrayToMap)
      results[:mappedArray].each_with_index do |array, index|
        target_item = .dig(array_to_map[:arrayName].to_sym, index)
        if target_item
          target_item[array_to_map[:field]] = array
        end
      end
      results.delete(:mappedArray)
    end

    if results[:queryResults]&.size == 1
      query_results = results[:queryResults].first
      [:rows] = query_results[:rows] if query_results[:rows]
      [:fields] = query_results[:fields] if query_results[:fields]
    end

    {
      data: ,
      queries: results,
      status: "success"
    }
  rescue StandardError => e
    if [:task] == "update-view"
      post_quill("set-broken-view", {
        table: [:name],
        clientId: [:clientId],
        error: e.message
      })
    end

    {
      status: "error",
      error: e.message,
      data:  || {}
    }
  end
end