Class: Coupler::Models::Resource

Inherits:
Sequel::Model
  • Object
show all
Includes:
CommonModel, Jobify
Defined in:
lib/coupler/models/resource.rb

Constant Summary collapse

LIMIT =
10000

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Jobify

included

Methods included from CommonModel

#after_save, #before_create, #before_update, included, #save!, #touch!

Instance Attribute Details

#resource_typeObject

Returns the value of attribute resource_type.



23
24
25
# File 'lib/coupler/models/resource.rb', line 23

def resource_type
  @resource_type
end

Class Method Details

.count_by_projectObject



25
26
27
# File 'lib/coupler/models/resource.rb', line 25

def self.count_by_project
  dataset.naked.group_and_count(:project_id).to_hash(:project_id, :count)
end

Instance Method Details

#activate!Object

Activate resource that was pending until import was completed



168
169
170
171
172
# File 'lib/coupler/models/resource.rb', line 168

def activate!
  set_primary_key
  create_fields
  update(:status => "ok")
end

#final_database(&block) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/coupler/models/resource.rb', line 90

def final_database(&block)
  if transformations_dataset.count == 0
    source_database(&block)
  else
    project.local_database(&block)
  end
end

#final_dataset(&block) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/coupler/models/resource.rb', line 98

def final_dataset(&block)
  if transformations_dataset.count == 0
    source_dataset(&block)
  else
    local_dataset(&block)
  end
end

#import=(*args) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/coupler/models/resource.rb', line 29

def import=(*args)
  result = super
  if new?
    self.table_name = "import_#{import.id}"
  end
  result
end

#local_datasetObject



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/coupler/models/resource.rb', line 78

def local_dataset
  if block_given?
    project.local_database do |database|
      ds = database[:"resource_#{id}"]
      yield ds
    end
  else
    database = project.local_database
    database[:"resource_#{id}"]
  end
end

#name=(*args) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/coupler/models/resource.rb', line 37

def name=(*args)
  result = super
  if new?
    self.slug ||= name.downcase.gsub(/\s+/, "_")
  end
  result
end

#preview_transformation(transformation) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/coupler/models/resource.rb', line 148

def preview_transformation(transformation)
  result = []
  _iterate_over_source_and_transform(50) { |r| result << r }
  result.each_index do |i|
    begin
      after = transformation.transform(result[i].dup)
      result[i] = { :before => result[i], :after => after }
    rescue Exception => e # yes, I know rescuing Exception is "bad"
      return e
    end
  end
  fields = result[0][:before].keys | result[0][:after].keys
  { :fields => fields, :data => result }
end

#primary_key_symObject



163
164
165
# File 'lib/coupler/models/resource.rb', line 163

def primary_key_sym
  primary_key_name.to_sym
end

#scenariosObject



106
107
108
# File 'lib/coupler/models/resource.rb', line 106

def scenarios
  Scenario.filter(["resource_1_id = ? OR resource_2_id = ?", id, id]).all
end

#source_database(&block) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/coupler/models/resource.rb', line 45

def source_database(&block)
  if import
    project.local_database(&block)
  else
    connection.database(&block)
  end
end

#source_datasetObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/coupler/models/resource.rb', line 53

def source_dataset
  if block_given?
    source_database do |database|
      columns = fields_dataset.filter(:is_selected => true, :is_generated => false).collect { |f| f.name.to_sym }
      yield database[table_name.to_sym].select(*columns.collect(&:to_sym))
    end
  else
    database = source_database
    columns = fields_dataset.filter(:is_selected => true, :is_generated => false).collect { |f| f.name.to_sym }
    database[table_name.to_sym].select(*columns.collect(&:to_sym))
  end
end

#source_dataset_countObject



66
67
68
69
70
# File 'lib/coupler/models/resource.rb', line 66

def source_dataset_count
  count = nil
  source_dataset { |ds| count = ds.count }
  count
end

#source_schemaObject



72
73
74
75
76
# File 'lib/coupler/models/resource.rb', line 72

def source_schema
  schema = nil
  source_database { |db| schema = db.schema(table_name.to_sym) }
  schema
end

#transform!(&progress) ⇒ Object



137
138
139
140
141
142
143
144
145
146
# File 'lib/coupler/models/resource.rb', line 137

def transform!(&progress)
  t_ids = transformation_ids.join(",")
  create_local_table!
  _transform(&progress)
  self.update({
    :status => 'ok',
    :transformed_at => Time.now,
    :transformed_with => t_ids
  })
end

#transformations_updated!Object



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
# File 'lib/coupler/models/resource.rb', line 110

def transformations_updated!
  last_updated_at = transformed_at
  transformation_ids = []

  fields_dataset.update(:local_db_type => nil, :local_type => nil)
  transformations_dataset.order(:position).each do |transformation|
    transformation_ids << transformation.id
    if last_updated_at.nil? || transformation.updated_at > last_updated_at
      last_updated_at = transformation.updated_at
    end
    if transformation.source_field_id == transformation.result_field_id
      source_field = transformation.source_field
      changes = transformation.field_changes[source_field.id]
      source_field.update({
        :local_db_type => changes[:db_type] || source_field[:db_type],
        :local_type    => changes[:type]    || source_field[:type]
      })
    end
  end

  if transformed_with.to_s != transformation_ids.join(",") || (last_updated_at && last_updated_at > transformed_at)
    update(:status => "out_of_date")
  else
    update(:status => "ok")
  end
end