Class: Marty::DataGrid
- Inherits:
-
Base
- Object
- ActiveRecord::Base
- Base
- Marty::DataGrid
- 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 ActiveRecord::Base
ActiveRecord::Base::COUNT_SIG, ActiveRecord::Base::DISTINCT_SIG, ActiveRecord::Base::FIND_BY_SIG, ActiveRecord::Base::FIRST_SIG, ActiveRecord::Base::GROUP_SIG, ActiveRecord::Base::JOINS_SIG, ActiveRecord::Base::LAST_SIG, ActiveRecord::Base::LIMIT_SIG, ActiveRecord::Base::MCFLY_PT_SIG, ActiveRecord::Base::NOT_SIG, ActiveRecord::Base::ORDER_SIG, ActiveRecord::Base::PLUCK_SIG, ActiveRecord::Base::SELECT_SIG, ActiveRecord::Base::WHERE_SIG
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
- .plv_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
171 172 173 |
# File 'app/models/marty/data_grid.rb', line 171 def self.clear_dtcache @@dtcache = {} end |
.convert_data_type(data_type) ⇒ Object
160 161 162 163 164 165 166 167 168 169 |
# File 'app/models/marty/data_grid.rb', line 160 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
574 575 576 577 578 579 580 581 582 583 584 585 |
# File 'app/models/marty/data_grid.rb', line 574 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
315 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 |
# File 'app/models/marty/data_grid.rb', line 315 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 |
.get_struct_attrs ⇒ Object
110 111 112 |
# File 'app/models/marty/data_grid.rb', line 110 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
263 264 265 266 267 268 269 270 271 272 273 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 |
# File 'app/models/marty/data_grid.rb', line 263 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 = plv_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 = case when Marty::PgEnum === res c_data_type.find_by_name(res) when 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
444 445 446 447 448 449 450 |
# File 'app/models/marty/data_grid.rb', line 444 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
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 667 668 669 |
# File 'app/models/marty/data_grid.rb', line 625 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
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 570 571 572 |
# File 'app/models/marty/data_grid.rb', line 461 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, *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
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 440 441 442 |
# File 'app/models/marty/data_grid.rb', line 397 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
452 453 454 455 456 457 458 |
# File 'app/models/marty/data_grid.rb', line 452 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
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 218 219 220 221 222 223 224 225 |
# File 'app/models/marty/data_grid.rb', line 177 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
125 126 127 |
# File 'app/models/marty/data_grid.rb', line 125 def self.register_rule_handler(handler) (@@rule_handlers ||= []) << handler end |
.type_to_index(type) ⇒ Object
153 154 155 156 157 158 |
# File 'app/models/marty/data_grid.rb', line 153 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
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 |
# File 'app/models/marty/data_grid.rb', line 601 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
311 312 313 |
# File 'app/models/marty/data_grid.rb', line 311 def dir_infos(dir) .select {|inf| inf["dir"] == dir} end |
#export ⇒ Object
378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'app/models/marty/data_grid.rb', line 378 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
348 349 |
# File 'app/models/marty/data_grid.rb', line 348 def export=(text) end |
#export_array ⇒ Object
351 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 |
# File 'app/models/marty/data_grid.rb', line 351 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) = dt_row.empty? ? [] : [[dt_row.join(' ')]] += .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] || []) } [, h_key_rows, data_rows] end |
#freeze ⇒ Object
118 119 120 121 122 123 |
# File 'app/models/marty/data_grid.rb', line 118 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!
149 150 151 |
# File 'app/models/marty/data_grid.rb', line 149 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.
135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'app/models/marty/data_grid.rb', line 135 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_s ⇒ Object
114 115 116 |
# File 'app/models/marty/data_grid.rb', line 114 def to_s name end |
#update_from_import(name, import_text, created_dt = nil) ⇒ Object
587 588 589 590 591 592 593 594 595 596 597 598 |
# File 'app/models/marty/data_grid.rb', line 587 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
128 129 130 |
# File 'app/models/marty/data_grid.rb', line 128 def update_rules(old, new) @@rule_handlers.each { |rh| rh.call(old, new) } end |