Class: Marty::DataGrid

Inherits:
Base show all
Defined in:
app/models/marty/data_grid.rb

Defined Under Namespace

Classes: DataGridValidator

Constant Summary collapse

DEFAULT_DATA_TYPE =

If data_type is nil, assume float

"float"
INDEX_MAP =
{
  "numrange"  => Marty::GridIndexNumrange,
  "int4range" => Marty::GridIndexInt4range,
  "integer"   => Marty::GridIndexInteger,
  "string"    => Marty::GridIndexString,
  "boolean"   => Marty::GridIndexBoolean,
}
ARRSEP =
'|'
PLV_DT_FMT =
"%Y-%m-%d %H:%M:%S.%N6"

Constants inherited from Base

Base::COUNT_SIG, Base::DISTINCT_SIG, Base::FIRST_SIG, Base::GROUP_SIG, Base::JOINS_SIG, Base::LAST_SIG, Base::LIMIT_SIG, Base::MCFLY_PT_SIG, Base::NOT_SIG, Base::ORDER_SIG, Base::PLUCK_SIG, Base::SELECT_SIG, Base::WHERE_SIG

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

mcfly_pt

Methods inherited from ActiveRecord::Base

joins, old_joins

Class Method Details

.clear_dtcacheObject



163
164
165
# File 'app/models/marty/data_grid.rb', line 163

def self.clear_dtcache
  @@dtcache = {}
end

.convert_data_type(data_type) ⇒ Object



152
153
154
155
156
157
158
159
160
161
# File 'app/models/marty/data_grid.rb', line 152

def self.convert_data_type(data_type)
  # given data_type, convert it to class and or known data type --
  # returns nil if data_type is invalid

  return DEFAULT_DATA_TYPE if data_type.nil?
  return data_type if
    Marty::DataConversion::DATABASE_TYPES.member?(data_type.to_sym)

  data_type.constantize rescue nil
end

.create_from_import(name, import_text, created_dt = nil) ⇒ Object



571
572
573
574
575
576
577
578
579
580
581
582
# File 'app/models/marty/data_grid.rb', line 571

def self.create_from_import(name, import_text, created_dt=nil)
  , data, data_type, lenient = parse(created_dt, import_text, {})
  dg            = self.new
  dg.name       = name
  dg.data       = data
  dg.data_type  = data_type
  dg.lenient    = !!lenient
  dg.   = 
  dg.created_dt = created_dt if created_dt
  dg.save!
  dg
end

.export_keys(inf) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'app/models/marty/data_grid.rb', line 316

def self.export_keys(inf)
  # should unify this with Marty::DataConversion.convert

  type = inf["type"]
  klass = type.constantize unless INDEX_MAP[type]

  inf["keys"].map do
    |v|

    case type
    when "numrange", "int4range"
      Marty::Util.pg_range_to_human(v)
    when "boolean"
      v.to_s
    when "string", "integer"
      v.map(&:to_s).join(ARRSEP) if v
    else
      # assume it's an AR class
      v.each do |k|
        begin
          # check to see if class instance actually exists
          Marty::DataGrid.
            find_class_instance('infinity', klass, k) || raise(NoMethodError)
        rescue NoMethodError
          raise "instance #{k} of #{type} not found"
        end
      end if v
      v.join(ARRSEP) if v
    end
  end
end

.lookup_h(pt, name, fields = nil) ⇒ Object



90
91
92
93
94
# File 'app/models/marty/data_grid.rb', line 90

def self.lookup_h(pt, name, fields = nil)
  fields ||= %w(id group_id created_dt metadata data_type)
  dga = get_all(pt).where(name: name).pluck(*fields).first
  dga && Hash[fields.zip(dga)]
end

.maybe_get_klass(type) ⇒ Object



441
442
443
444
445
446
447
# File 'app/models/marty/data_grid.rb', line 441

def self.maybe_get_klass(type)
  begin
    type.constantize unless INDEX_MAP[type] || type == "float"
  rescue NameError
    raise "unknown header type/klass: #{type}"
  end
end

.modify_grid(params, metadata, data) ⇒ Object



622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
# File 'app/models/marty/data_grid.rb', line 622

def self.modify_grid(params, , data)
  removes = ["h", "v"].each_with_object({}) {|dir, hash| hash[dir] = Set.new}

  , data_copy = .deep_dup, data.deep_dup

  .each do |meta|
    dir, keys, type, rs_keep = meta.values_at(
                       "dir", "keys", "type", "rs_keep")
    next unless rs_keep

    if type == "numrange" || type == "int4range"
      modop, modvalparm = parse_bounds(rs_keep)
      modval = params[modvalparm]
      if modval
        prune_a, rewrite_a = compute_numeric_mods(keys, modop, modval)
        removes[dir].merge(prune_a)
        rewrite_a.each { |(ind, value)| keys[ind] = value }
      end
    else
      modval = params[rs_keep]
      if modval
        prune_a, rewrite_a = compute_set_mods(keys, modval)
        removes[dir].merge(prune_a)
        rewrite_a.each { |(ind, value)| keys[ind] = value }
      end
    end
  end

  removes.reject! { |dir, set| set.empty? }

  removes.each do
    |dir, set|
    .select { |m| m["dir"] == dir }.each do |meta|
      meta["keys"] = remove_indices(meta["keys"], removes[dir])
    end
  end

  data_copy = remove_indices(data_copy, removes["v"]) if removes["v"]

  data_copy.each_index do |index|
    data_copy[index] = remove_indices(data_copy[index], removes["h"])
  end if removes["h"]

  [data_copy, ]
end

.parse(pt, grid_text, options) ⇒ Object

parse grid external representation into metadata/data



458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# File 'app/models/marty/data_grid.rb', line 458

def self.parse(pt, grid_text, options)
  options[:headers] ||= false
  options[:col_sep] ||= "\t"

  pt ||= 'infinity'

  rows = CSV.new(grid_text, options).to_a
  blank_index = rows.find_index {|x| x.all?(&:nil?)}

  raise "must have a blank row separating metadata" unless
    blank_index

  raise "can't import grid with trailing blank column" if
    rows.map { |r| r.last.nil? }.all?

  raise "last row can't be blank" if rows[-1].all?(&:nil?)

  data_type, lenient = nil, false

  # check if there's a data_type definition
  dt, *x = rows[0]
  if dt && x.all?(&:nil?)
    dts = dt.split
    raise "bad data type '#{dt}'" if dts.count > 2

    lenient = dts.delete "lenient"
    data_type = dts.first
  end

   = rows[(data_type || lenient ? 1 : 0)...blank_index].map do
    |attr, type, dir, rs_keep, key|

    raise "metadata elements must include attr/type/dir" unless
      attr && type && dir
    raise "bad dir #{dir}" unless ["h", "v"].member? dir
    raise "unknown metadata type #{type}" unless
      Marty::DataGrid.type_to_index(type)

    res = {
      "attr" => attr,
      "type" => type,
      "dir"  => dir,
      "keys" => key && parse_keys(pt, [key], type),
    }
    res["rs_keep"] = rs_keep if rs_keep
    res
  end

  v_infos = .select {|inf| inf["dir"] == "v"}
  h_infos = .select {|inf| inf["dir"] == "h"}

  # keys+data start right after blank_index
  data_index = blank_index+1

  # process horizontal key rows
  h_infos.each_with_index do
    |inf, i|

    row = rows[data_index+i]

    raise "horiz. key row #{data_index+i} must include nil starting cells" if
      row[0, v_infos.count].any?

    inf["keys"] = parse_keys(pt, row[v_infos.count, row.count], inf["type"])
  end

  raise "horiz. info keys length mismatch!" unless
    h_infos.map {|inf| inf["keys"].length}.uniq.count <= 1

  data_rows = rows[data_index+h_infos.count, rows.count]

  # process vertical key columns
  v_key_cols = data_rows.map {|r| r[0, v_infos.count]}.transpose

  v_infos.each_with_index do |inf, i|
    inf["keys"] = parse_keys(pt, v_key_cols[i], inf["type"])
  end

  raise "vert. info keys length mismatch!" unless
    v_infos.map {|inf| inf["keys"].length}.uniq.count <= 1

  c_data_type = Marty::DataGrid.convert_data_type(data_type)

  raise "bad data type #{data_type}" unless c_data_type

  # based on data type, decide to check using convert or instance
  # lookup.  FIXME: DRY.
  if String === c_data_type
    tsym = c_data_type.to_sym

    data = data_rows.map do
      |r|
      r[v_infos.count, r.count].map do
        |v|
        Marty::DataConversion.convert(v, tsym) if v
      end
    end
  else
    data = data_rows.map do
      |r|
      r[v_infos.count, r.count].map do
        |v|
        next v if !v || Marty::DataGrid.
                       find_class_instance(pt, c_data_type, v)

        raise "can't find key '#{v}' for class #{data_type}"
      end
    end
  end

  [, data, data_type, lenient]
end

.parse_fvalue(pt, v, type, klass) ⇒ Object



394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'app/models/marty/data_grid.rb', line 394

def self.parse_fvalue(pt, v, type, klass)
  return unless v

  case type
  when "numrange", "int4range"
    Marty::Util.human_to_pg_range(v)
  when "integer"
    v.split(ARRSEP).map do |val|
      Integer(val) rescue raise "invalid integer: #{val}"
    end.uniq.sort
  when "float"
    v.split(ARRSEP).map do |val|
      Float(val) rescue raise "invalid float: #{val}"
    end.uniq.sort
  when "string"
    res = v.split(ARRSEP).uniq.sort
    raise "leading/trailing spaces in elements not allowed" if
      res.any? {|x| x != x.strip}
    raise "0-length string not allowed" if res.any?(&:empty?)
    res
  when "boolean"
    case v.downcase
    when "true", "t"
      true
    when "false", "f"
      false
    else
      raise "bad boolean #{v}"
    end
  else
    # AR class
    # FIXME: won't work if the obj identifier (name) has ARRSEP
    res = v.split(ARRSEP).uniq
    res.each do
      |k|
      begin
        # check to see if class instance actually exists
        Marty::DataGrid.
          find_class_instance(pt, klass, k) || raise(NoMethodError)
      rescue NoMethodError
        raise "instance #{k} of #{type} not found"
      end
    end
    res
  end
end

.parse_keys(pt, keys, type) ⇒ Object



449
450
451
452
453
454
455
# File 'app/models/marty/data_grid.rb', line 449

def self.parse_keys(pt, keys, type)
  klass = maybe_get_klass(type)
  keys.map do
    |v|
    parse_fvalue(pt, v, type, klass)
  end
end

.plv_lookup_grid_distinct(h_passed, dgh, ret_grid_data = false, distinct = true) ⇒ Object



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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'app/models/marty/data_grid.rb', line 169

def self.plv_lookup_grid_distinct(h_passed, dgh, ret_grid_data=false,
                                  distinct=true)
  cd = dgh["created_dt"]
  @@dtcache ||= {}
  @@dtcache[cd] ||= cd.strftime(PLV_DT_FMT)
  row_info = {
    "id"         => dgh["id"],
    "group_id"   => dgh["group_id"],
    "created_dt" => @@dtcache[cd]
  }

  h = dgh["metadata"].each_with_object({}) do |m, h|
    attr = m["attr"]
    inc = h_passed.fetch(attr, :__nf__)
    next if inc == :__nf__
    val = (defined? inc.name) ? inc.name : inc
    h[attr] = val.is_a?(String) ?
                ActiveRecord::Base.connection.quote(val)[1..-2] : val
  end

  fn     = "lookup_grid_distinct"
  hjson  = "'#{h.to_json}'::JSONB"
  rijson = "'#{row_info.to_json}'::JSONB"
  params = "#{hjson}, #{rijson}, #{ret_grid_data}, #{distinct}"
  sql    = "SELECT #{fn}(#{params})"
  raw    = ActiveRecord::Base.connection.execute(sql)[0][fn]
  res    = JSON.parse(raw)

  if res["error"]
    msg = res["error"]
    parms, sqls, ress, dg = res["error_extra"].values_at(
                         "params", "sql", "results", "dg")

    raise "DG #{name}: Error in PLV8 call: #{msg}\n"\
          "params: #{parms}\n"\
          "sqls: #{sqls}\n"\
          "results: #{ress}\n"\
          "dg: #{dg}\n"\
          "ri: #{row_info}" if res["error"]
  end

  if ret_grid_data
    dg = find(dgh["id"])
    md, mmd = modify_grid(h_passed, dg., dg.data)
    res["data"] = md
    res["metadata"] = mmd
  end
  res
end

.register_rule_handler(handler) ⇒ Object



117
118
119
# File 'app/models/marty/data_grid.rb', line 117

def self.register_rule_handler(handler)
  (@@rule_handlers ||= []) << handler
end

.type_to_index(type) ⇒ Object



145
146
147
148
149
150
# File 'app/models/marty/data_grid.rb', line 145

def self.type_to_index(type)
  # map given header type to an index class -- uses string index
  # for ruby classes.
  return INDEX_MAP[type] if INDEX_MAP[type]
  INDEX_MAP["string"] if (type.constantize rescue nil)
end

Instance Method Details

#build_indexObject

FIXME: should be private



598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
# File 'app/models/marty/data_grid.rb', line 598

def build_index
  # create indices for the metadata
  .each do
    |inf|

    attr, type, keys = inf["attr"], inf["type"], inf["keys"]

    # find index class
    idx_class = Marty::DataGrid.type_to_index(type)

    keys.each_with_index do
      |k, index|

      gi              = idx_class.new
      gi.attr         = attr
      gi.key          = k
      gi.created_dt   = created_dt
      gi.data_grid_id = group_id
      gi.index        = index
      gi.save!
    end
  end
end

#dir_infos(dir) ⇒ Object



312
313
314
# File 'app/models/marty/data_grid.rb', line 312

def dir_infos(dir)
  .select {|inf| inf["dir"] == dir}
end

#exportObject



379
380
381
382
383
384
385
386
387
388
389
390
# File 'app/models/marty/data_grid.rb', line 379

def export
  # return null string when called from Netzke on add_in_form
  return "" if .nil? && data.nil?

  meta_rows, h_key_rows, data_rows = export_array

  Marty::DataExporter.
    to_csv(meta_rows + [[]] + h_key_rows + data_rows,
           "col_sep" => "\t",
           ).
    gsub(/\"\"/, '') # remove "" to beautify output
end

#export=(text) ⇒ Object

FIXME: this is only here to appease Netzke add_in_form



349
350
# File 'app/models/marty/data_grid.rb', line 349

def export=(text)
end

#export_arrayObject



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'app/models/marty/data_grid.rb', line 352

def export_array
  # add data type metadata row if not default
  dt_row = lenient ? ["lenient"] : []
  dt_row << data_type unless [nil, DEFAULT_DATA_TYPE].member?(data_type)

  meta_rows = dt_row.empty? ? [] : [[dt_row.join(' ')]]

  meta_rows += .map { |inf|
    [inf["attr"], inf["type"], inf["dir"], inf["rs_keep"] || ""]
  }

  v_infos, h_infos = dir_infos("v"), dir_infos("h")

  h_key_rows = h_infos.map { |inf|
    [nil]*v_infos.count + self.class.export_keys(inf)
  }

  transposed_v_keys = v_infos.empty? ? [[]] :
    v_infos.map {|inf| self.class.export_keys(inf)}.transpose

  data_rows = transposed_v_keys.each_with_index.map { |keys, i|
    keys + (self.data[i] || [])
  }

  [meta_rows, h_key_rows, data_rows]
end

#freezeObject



110
111
112
113
114
115
# File 'app/models/marty/data_grid.rb', line 110

def freeze
  # FIXME: mcfly lookups freeze their results in order to protect
  # the cache.  That doesn't interact correctly with lazy_load which
  # modifies the attr hash at runtime.
  self
end

#lookup_grid_distinct_entry(pt, h, visited = nil, follow = true, return_grid_data = false) ⇒ Object

DEPRECATED: use lookup_grid_distinct_entry_h instead



300
301
302
303
304
305
306
307
308
# File 'app/models/marty/data_grid.rb', line 300

def lookup_grid_distinct_entry(pt, h, visited=nil, follow=true,
                               return_grid_data=false)
  warn "DEPRECATED: instance method lookup_grid_distinct_entry. "\
       "Use class method lookup_grid_distinct_entry_h instead"
  dgh = self.attributes.slice("id","group_id","created_dt",
                            "metadata", "data_type")
  self.class.lookup_grid_distinct_entry_h(pt, h, dgh, visited, follow,
                                          return_grid_data)
end

#saveObject

FIXME: hacky – save is just save!



141
142
143
# File 'app/models/marty/data_grid.rb', line 141

def save
  self.save!
end

#save!Object

FIXME: not sure what’s the right way to perform the save in a transaction – i.e. together with build_index. before_save would be OK, but then save inside it would cause an infinite loop.



127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/models/marty/data_grid.rb', line 127

def save!
  if self.changed?
    transaction do
      nc, nw, n = [name_changed?, name_was, name]
      res = super
      update_rules(nw, n) if nc && nw.present?
      reload
      build_index
      res
    end
  end
end

#to_sObject



106
107
108
# File 'app/models/marty/data_grid.rb', line 106

def to_s
  name
end

#update_from_import(name, import_text, created_dt = nil) ⇒ Object



584
585
586
587
588
589
590
591
592
593
594
595
# File 'app/models/marty/data_grid.rb', line 584

def update_from_import(name, import_text, created_dt=nil)
  , data, data_type, lenient =
                             self.class.parse(created_dt, import_text, {})

  self.name       = name
  self.data       = data
  self.data_type  = data_type
  self.lenient    = !!lenient
  self.   =  unless self. ==  # Otherwise changed will depend on order in hashes
  self.created_dt = created_dt if created_dt
  save!
end

#update_rules(old, new) ⇒ Object



120
121
122
# File 'app/models/marty/data_grid.rb', line 120

def update_rules(old, new)
  @@rule_handlers.each { |rh| rh.call(old, new) }
end