Class: DataDuck::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/dataduck/table.rb

Direct Known Subclasses

IntegrationTable

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.actionsObject

Returns the value of attribute actions.



8
9
10
# File 'lib/dataduck/table.rb', line 8

def actions
  @actions
end

.output_schemaObject

Returns the value of attribute output_schema.



7
8
9
# File 'lib/dataduck/table.rb', line 7

def output_schema
  @output_schema
end

.sourcesObject

Returns the value of attribute sources.



6
7
8
# File 'lib/dataduck/table.rb', line 6

def sources
  @sources
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



11
12
13
# File 'lib/dataduck/table.rb', line 11

def data
  @data
end

#errorsObject

Returns the value of attribute errors.



12
13
14
# File 'lib/dataduck/table.rb', line 12

def errors
  @errors
end

Class Method Details

.output(schema) ⇒ Object



42
43
44
45
# File 'lib/dataduck/table.rb', line 42

def self.output(schema)
  self.output_schema ||= {}
  self.output_schema.merge!(schema)
end

.source(source_name, source_table_or_query = nil, source_columns = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dataduck/table.rb', line 26

def self.source(source_name, source_table_or_query = nil, source_columns = nil)
  self.sources ||= []

  source_spec = {}
  if source_table_or_query.respond_to?(:to_s) && source_table_or_query.to_s.downcase.include?('select ')
    source_spec = {query: source_table_or_query}
  elsif source_columns.nil? && source_table_or_query.respond_to?(:each)
    source_spec = {columns: source_table_or_query, table_name: DataDuck::Util.camelcase_to_underscore(self.name)}
  else
    source_spec = {columns: source_columns, table_name: source_table_or_query.to_s}
  end

  source_spec[:source] = DataDuck::Source.source(source_name)
  self.sources << source_spec
end

.transforms(transformation_name) ⇒ Object



14
15
16
17
# File 'lib/dataduck/table.rb', line 14

def self.transforms(transformation_name)
  self.actions ||= []
  self.actions << [:transform, transformation_name]
end

.validates(validation_name) ⇒ Object



20
21
22
23
# File 'lib/dataduck/table.rb', line 20

def self.validates(validation_name)
  self.actions ||= []
  self.actions << [:validate, validation_name]
end

Instance Method Details

#actionsObject



47
48
49
50
51
52
53
54
55
56
# File 'lib/dataduck/table.rb', line 47

def actions
  my_actions = []
  for_class = self.class
  while for_class < Table
    my_actions.concat(for_class.actions || [])
    for_class = for_class.superclass
  end

  my_actions
end

#batch_sizeObject



183
184
185
# File 'lib/dataduck/table.rb', line 183

def batch_size
  nil
end

#building_nameObject



197
198
199
# File 'lib/dataduck/table.rb', line 197

def building_name
  self.should_fully_reload? ? self.staging_name : self.name
end

#check_table_valid!Object



58
59
60
61
62
63
# File 'lib/dataduck/table.rb', line 58

def check_table_valid!
  if !self.batch_size.nil?
    raise Exception.new("Table #{ self.name }'s batch_size must be > 0") unless self.batch_size > 0
    raise Exception.new("Table #{ self.name } has batch_size defined but no extract_by_column") if self.extract_by_column.nil?
  end
end

#distribution_keyObject



65
66
67
68
69
70
71
# File 'lib/dataduck/table.rb', line 65

def distribution_key
  if self.output_column_names.include?("id")
    "id"
  else
    nil
  end
end

#etl!(destinations, options = {}) ⇒ Object



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
# File 'lib/dataduck/table.rb', line 73

def etl!(destinations, options = {})
  if destinations.length != 1
    raise ArgumentError.new("DataDuck can only etl to one destination at a time for now.")
  end

  if options[:dates].nil?
    options[:dates] = [Date.today]
  end

  self.check_table_valid!

  destination = destinations.first

  if self.should_fully_reload?
    destination.drop_staging_table!(self)
  end

  batch_number = 0
  while batch_number < 1_000
    batch_number += 1
    self.extract!(destination, options)
    self.transform!
    self.load!(destination)

    if self.batch_size.nil?
      break
    else
      if self.batch_size == self.data.length
        DataDuck::Logs.info "Finished batch #{ batch_number }, continuing with the next batch"
      else
        DataDuck::Logs.info "Finished batch #{ batch_number } (last batch)"
        break
      end
    end
  end

  self.data = []

  if self.should_fully_reload?
    destination.finish_fully_reloading_table!(self)
  end
end

#extract!(destination = nil, options = {}) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/dataduck/table.rb', line 116

def extract!(destination = nil, options = {})
  DataDuck::Logs.info "Extracting table #{ self.name }"

  self.errors ||= []
  self.data = []
  self.class.sources.each do |source_spec|
    source = source_spec[:source]
    my_query = self.extract_query(source_spec, destination)
    results = source.query(my_query)
    self.data.concat(results)
  end
  self.data
end

#extract_by_clause(value) ⇒ Object



152
153
154
155
156
157
158
# File 'lib/dataduck/table.rb', line 152

def extract_by_clause(value)
  if value
    "WHERE #{ self.extract_by_column } >= '#{ value }'"
  else
    ""
  end
end

#extract_by_columnObject



187
188
189
190
191
# File 'lib/dataduck/table.rb', line 187

def extract_by_column
  return 'updated_at' if self.output_column_names.include?("updated_at")

  nil
end

#extract_query(source_spec, destination = nil) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/dataduck/table.rb', line 130

def extract_query(source_spec, destination = nil)
  escape_char = source_spec[:source].escape_char

  base_query = source_spec.has_key?(:query) ? source_spec[:query] :
     "SELECT #{ escape_char }#{ source_spec[:columns].sort.join(escape_char + ',' + escape_char) }#{ escape_char } FROM #{ source_spec[:table_name] }"

  extract_part = ""
  limit_part = self.limit_clause

  if self.extract_by_column
    if destination.table_names.include?(self.building_name)
      extract_by_column_without_table = self.extract_by_column.include?(".") ? self.extract_by_column.split(".").last : self.extract_by_column
      extract_by_value = destination.query("SELECT MAX(#{ extract_by_column_without_table }) AS val FROM #{ self.building_name }").first
      extract_by_value = extract_by_value.nil? ? nil : extract_by_value[:val]

      extract_part = self.extract_by_clause(extract_by_value)
    end
  end

  [base_query, extract_part, limit_part].join(' ').strip
end

#include_with_all?Boolean



172
173
174
# File 'lib/dataduck/table.rb', line 172

def include_with_all?
  true
end

#indexesObject



176
177
178
179
180
181
# File 'lib/dataduck/table.rb', line 176

def indexes
  which_columns = []
  which_columns << "id" if self.output_column_names.include?("id")
  which_columns << "created_at" if self.output_column_names.include?("created_at")
  which_columns
end

#limit_clauseObject



160
161
162
163
164
165
166
# File 'lib/dataduck/table.rb', line 160

def limit_clause
  if self.extract_by_column && self.batch_size
    "ORDER BY #{ self.extract_by_column } LIMIT #{ self.batch_size }"
  else
    ""
  end
end

#load!(destination) ⇒ Object



168
169
170
# File 'lib/dataduck/table.rb', line 168

def load!(destination)
  destination.load_table!(self)
end

#nameObject



251
252
253
254
255
256
257
258
# File 'lib/dataduck/table.rb', line 251

def name
  fixed_name = DataDuck::Util.camelcase_to_underscore(self.class.name)
  if fixed_name.start_with?("data_duck/")
    fixed_name = fixed_name.split("/").last
  end

  self.prefix + fixed_name
end

#output_column_namesObject



209
210
211
# File 'lib/dataduck/table.rb', line 209

def output_column_names
  self.output_schema.keys.sort.map(&:to_s)
end

#output_schemaObject



205
206
207
# File 'lib/dataduck/table.rb', line 205

def output_schema
  self.class.output_schema || self.class.superclass.output_schema || {}
end

#prefixObject



260
261
262
# File 'lib/dataduck/table.rb', line 260

def prefix
  ""
end

#recreate!(destination) ⇒ Object



213
214
215
# File 'lib/dataduck/table.rb', line 213

def recreate!(destination)
  destination.recreate_table!(self)
end

#should_fully_reload?Boolean



193
194
195
# File 'lib/dataduck/table.rb', line 193

def should_fully_reload?
  false # Set to true if you want to fully reload a table with each ETL
end

#showObject



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/dataduck/table.rb', line 217

def show
  puts "Table #{ self.name }"
  self.class.sources.each do |source_spec|
    puts "\nSources from #{ source_spec[:table_name] || source_spec[:query] } on #{ source_spec[:source].name }"
    source_spec[:columns].each do |col_name|
      puts "  #{ col_name }"
    end
  end

  puts "\nOutputs "
  num_separators = self.output_schema.keys.map { |key| key.length }.max
  self.output_schema.each_pair do |name, datatype|
    puts "  #{ name }#{ ' ' * (num_separators + 2 - name.length) }#{ datatype }"
  end
end

#staging_nameObject



201
202
203
# File 'lib/dataduck/table.rb', line 201

def staging_name
  "zz_dataduck_#{ self.name }"
end

#transform!Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/dataduck/table.rb', line 233

def transform!
  DataDuck::Logs.info "Transforming table #{ self.name }"

  self.errors ||= []
  self.actions.each do |action|
    action_type = action[0]
    action_method_name = action[1]
    if action_type == :transform
      self.data.map! { |row| self.public_send(action_method_name, row) }
    elsif action_type == :validate
      self.data.each do |row|
        error = self.public_send(action_method_name, row)
        self.errors << error if !error.blank?
      end
    end
  end
end