Class: Marty::DataGrid
- Inherits:
-
Base
- Object
- ActiveRecord::Base
- Base
- Marty::DataGrid
- Defined in:
- app/models/marty/data_grid.rb,
app/services/marty/data_grid/constraint.rb
Defined Under Namespace
Classes: Constraint, 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, }.freeze
- ARRSEP =
'|'.freeze
- NOT_STRING_START =
'NOT ('.freeze
- NOT_STRING_END =
')'.freeze
- PLV_DT_FMT =
'%Y-%m-%d %H:%M:%S.%N6'
Class Method Summary collapse
- .clear_dtcache ⇒ Object
- .convert_data_type(data_type) ⇒ Object
- .create_from_import(name, import_text, created_dt = nil) ⇒ Object
- .export_keys(inf) ⇒ Object
- .get_struct_attrs ⇒ Object
- .lookup_grid_distinct_entry_h(pt, h, dgh, visited = nil, follow = true, return_grid_data = false, distinct = true) ⇒ Object
- .maybe_get_klass(type) ⇒ Object
- .modify_grid(params, metadata, data) ⇒ Object
-
.parse(pt, grid_text, options) ⇒ Object
parse grid external representation into metadata/data.
- .parse_fvalue(pt, v, type, klass) ⇒ Object
- .parse_keys(pt, keys, type) ⇒ Object
- .parse_nots(_pt, keys) ⇒ Object
- .plpg_lookup_grid_distinct(h_passed, dgh, ret_grid_data = false, distinct = true) ⇒ Object
- .register_rule_handler(handler) ⇒ Object
- .type_to_index(type) ⇒ Object
Instance Method Summary collapse
-
#build_index ⇒ Object
FIXME: should be private.
- #dir_infos(dir) ⇒ Object
- #export ⇒ Object
-
#export=(text) ⇒ Object
FIXME: this is only here to appease Netzke add_in_form.
- #export_array ⇒ Object
- #freeze ⇒ Object
-
#save ⇒ Object
FIXME: hacky – save is just save!.
-
#save! ⇒ Object
FIXME: not sure what’s the right way to perform the save in a transaction – i.e.
- #to_s ⇒ Object
- #update_from_import(name, import_text, created_dt = nil) ⇒ Object
- #update_rules(old, new) ⇒ Object
Methods inherited from Base
get_final_attrs, make_hash, make_openstruct, mcfly_pt
Methods inherited from ActiveRecord::Base
Class Method Details
.clear_dtcache ⇒ Object
181 182 183 |
# File 'app/models/marty/data_grid.rb', line 181 def self.clear_dtcache @@dtcache = {} end |
.convert_data_type(data_type) ⇒ Object
170 171 172 173 174 175 176 177 178 179 |
# File 'app/models/marty/data_grid.rb', line 170 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
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 |
# File 'app/models/marty/data_grid.rb', line 620 def self.create_from_import(name, import_text, created_dt = nil) parsed_result = parse(created_dt, import_text, {}) = parsed_result[:metadata] data = parsed_result[:data] data_type = parsed_result[:data_type] lenient = parsed_result[:lenient] constraint = parsed_result[:constraint] dg = 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.constraint = constraint dg.save! dg end |
.export_keys(inf) ⇒ Object
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'app/models/marty/data_grid.rb', line 327 def self.export_keys(inf) # should unify this with Marty::DataConversion.convert type = inf['type'] nots = inf.fetch('nots', []) klass = type.constantize unless INDEX_MAP[type] keys = 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 keys.each_with_index.map do |v, index| next v unless nots[index] add_not(v) end end |
.get_struct_attrs ⇒ Object
118 119 120 |
# File 'app/models/marty/data_grid.rb', line 118 def self.get_struct_attrs self.struct_attrs ||= super + ['id', 'group_id', 'created_dt', 'name'] end |
.lookup_grid_distinct_entry_h(pt, h, dgh, visited = nil, follow = true, return_grid_data = false, distinct = true) ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'app/models/marty/data_grid.rb', line 274 def self.lookup_grid_distinct_entry_h( pt, h, dgh, visited = nil, follow = true, return_grid_data = false, distinct = true ) # Perform grid lookup, if result is another data_grid, and follow is true, # then perform lookup on the resulting grid. Allows grids to be nested # as multi-grids. If return_grid_data is true, also return the grid # data and metadata # return is a hash for the grid results: # # "result" => <result of running the grid> # "name" => <grid name> # "data" => <grid's data array> # "metadata" => <grid's metadata (array of hashes)> vhash = plpg_lookup_grid_distinct(h, dgh, return_grid_data, distinct) return vhash if vhash['result'].nil? || !dgh['data_type'] c_data_type = Marty::DataGrid.convert_data_type(dgh['data_type']) return vhash if String === c_data_type res = vhash['result'] v = if ::Marty::EnumHelper.pg_enum?(klass: res) c_data_type.find_by_name(res) elsif Marty::DataGrid == c_data_type follow ? Marty::DataGrid.lookup_h(pt, res) : Marty::DataGrid.lookup(pt, res) else Marty::DataConversion.find_row(c_data_type, { 'name' => res }, pt) end return vhash.merge('result' => v) unless Marty::DataGrid == c_data_type && follow visited ||= [] visited << dgh['group_id'] raise "#{self.class} recursion loop detected -- #{visited}" if visited.member?(v['group_id']) lookup_grid_distinct_entry_h( pt, h, v, visited, follow, return_grid_data, distinct) end |
.maybe_get_klass(type) ⇒ Object
472 473 474 475 476 |
# File 'app/models/marty/data_grid.rb', line 472 def self.maybe_get_klass(type) type.constantize unless INDEX_MAP[type] || type == 'float' rescue NameError raise "unknown header type/klass: #{type}" end |
.modify_grid(params, metadata, data) ⇒ Object
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 |
# File 'app/models/marty/data_grid.rb', line 686 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 || dir, keys, type, rs_keep = .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 || ['keys'] = remove_indices(['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
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 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 |
# File 'app/models/marty/data_grid.rb', line 495 def self.parse(pt, grid_text, ) [:headers] ||= false [:col_sep] ||= "\t" pt ||= 'infinity' rows = CSV.new(grid_text, ).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, constraint, *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 constraint = nil if x.first.in?(['v', 'h']) start_md = constraint || data_type || lenient ? 1 : 0 = rows[start_md...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) keys = key && parse_keys(pt, [key], type) nots = key && parse_nots(pt, [key]) res = { 'attr' => attr, 'type' => type, 'dir' => dir, 'keys' => keys, 'nots' => nots, } 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']) inf['nots'] = parse_nots(pt, row[v_infos.count, row.count]) 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']) inf['nots'] = parse_nots(pt, v_key_cols[i]) 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| next v unless v Marty::DataConversion.convert(v, tsym) end end else data = data_rows.map do |r| r[v_infos.count, r.count].map do |v| next v unless v next v if Marty::DataGrid. find_class_instance(pt, c_data_type, v) raise "can't find key '#{v}' for class #{data_type}" end end end { metadata: , data: data, data_type: data_type, lenient: lenient, constraint: constraint, } end |
.parse_fvalue(pt, v, type, klass) ⇒ Object
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 |
# File 'app/models/marty/data_grid.rb', line 423 def self.parse_fvalue(pt, v, type, klass) return unless v v = remove_not(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
478 479 480 481 482 483 484 |
# File 'app/models/marty/data_grid.rb', line 478 def self.parse_keys(pt, keys, type) klass = maybe_get_klass(type) keys.map do |v| parse_fvalue(pt, v, type, klass) end end |
.parse_nots(_pt, keys) ⇒ Object
486 487 488 489 490 491 492 |
# File 'app/models/marty/data_grid.rb', line 486 def self.parse_nots(_pt, keys) keys.map do |v| next false unless v v.starts_with?(NOT_STRING_START) && v.ends_with?(NOT_STRING_END) end end |
.plpg_lookup_grid_distinct(h_passed, dgh, ret_grid_data = false, distinct = true) ⇒ Object
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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'app/models/marty/data_grid.rb', line 187 def self.plpg_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 PLPG 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
133 134 135 |
# File 'app/models/marty/data_grid.rb', line 133 def self.register_rule_handler(handler) (@@rule_handlers ||= []) << handler end |
.type_to_index(type) ⇒ Object
162 163 164 165 166 167 168 |
# File 'app/models/marty/data_grid.rb', line 162 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_index ⇒ Object
FIXME: should be private
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 |
# File 'app/models/marty/data_grid.rb', line 662 def build_index # create indices for the metadata .each do |inf| attr = inf['attr'] type = inf['type'] keys = inf['keys'] nots = inf.fetch('nots', []) # 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.not = nots[index] || false gi.save! end end end |
#dir_infos(dir) ⇒ Object
323 324 325 |
# File 'app/models/marty/data_grid.rb', line 323 def dir_infos(dir) .select { |inf| inf['dir'] == dir } end |
#export ⇒ Object
405 406 407 408 409 410 411 412 413 414 415 416 |
# File 'app/models/marty/data_grid.rb', line 405 def export # return null string when called from Netzke on add_in_form return '' if .nil? && data.nil? , h_key_rows, data_rows = export_array Marty::DataExporter. to_csv( + [[]] + 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
365 366 |
# File 'app/models/marty/data_grid.rb', line 365 def export=(text) end |
#export_array ⇒ Object
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'app/models/marty/data_grid.rb', line 368 def export_array # add data type metadata row if not default lenstr = 'lenient' if lenient typestr = data_type unless [nil, DEFAULT_DATA_TYPE].member?(data_type) len_type = [lenstr, typestr].compact.join(' ') = if (lenient || typestr) && constraint [[len_type, constraint]] elsif lenient || typestr [[len_type]] elsif constraint [['', constraint]] else [] end += .map do |inf| [inf['attr'], inf['type'], inf['dir'], inf['rs_keep'] || ''] end v_infos, h_infos = dir_infos('v'), dir_infos('h') h_key_rows = h_infos.map do |inf| [nil] * v_infos.count + self.class.export_keys(inf) end 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 do |keys, i| keys + (data[i] || []) end [, h_key_rows, data_rows] end |
#freeze ⇒ Object
126 127 128 129 130 131 |
# File 'app/models/marty/data_grid.rb', line 126 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 |
#save ⇒ Object
FIXME: hacky – save is just save!
158 159 160 |
# File 'app/models/marty/data_grid.rb', line 158 def save 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.
144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'app/models/marty/data_grid.rb', line 144 def save! if 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_s ⇒ Object
122 123 124 |
# File 'app/models/marty/data_grid.rb', line 122 def to_s name end |
#update_from_import(name, import_text, created_dt = nil) ⇒ Object
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 |
# File 'app/models/marty/data_grid.rb', line 641 def update_from_import(name, import_text, created_dt = nil) parsed_result = self.class.parse(created_dt, import_text, {}) = parsed_result[:metadata] data = parsed_result[:data] data_type = parsed_result[:data_type] lenient = parsed_result[:lenient] constraint = parsed_result[:constraint] self.name = name self.data = data self.data_type = data_type self.lenient = !!lenient # Otherwise changed will depend on order in hashes self. = unless == self.constraint = constraint self.created_dt = created_dt if created_dt save! end |
#update_rules(old, new) ⇒ Object
137 138 139 |
# File 'app/models/marty/data_grid.rb', line 137 def update_rules(old, new) @@rule_handlers.each { |rh| rh.call(old, new) } end |