Class: ODDB::App

Inherits:
SBSM::DRbServer
  • Object
show all
Includes:
Failsafe
Defined in:
src/util/oddbapp.rb,
test/stub/oddbapp.rb

Constant Summary

AUTOSNAPSHOT =
true
CLEANING_INTERVAL =
5*60
EXPORT_HOUR =
2
UPDATE_HOUR =
9
MEMORY_LIMIT =
20480
RUN_CLEANER =
false
RUN_UPDATER =
false
SESSION =
Session
UNKNOWN_USER =
UnknownUser
UPDATE_INTERVAL =
24*60*60
VALIDATOR =
Validator
YUS_SERVER =
DRb::DRbObject.new(nil, YUS_URI)
RUN_EXPORTER =
false

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Failsafe

#failsafe

Constructor Details

- (App) initialize(opts = {})

A new instance of App



1273
# File 'src/util/oddbapp.rb', line 1273

def initialize opts={}

Instance Attribute Details

- (Object) cleaner (readonly)

Returns the value of attribute cleaner



1272
1273
1274
# File 'src/util/oddbapp.rb', line 1272

def cleaner
  @cleaner
end

- (Object) updater (readonly)

Returns the value of attribute updater



1272
1273
1274
# File 'src/util/oddbapp.rb', line 1272

def updater
  @updater
end

Instance Method Details

- (Object) _admin(src, result, priority = 0)



1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
# File 'src/util/oddbapp.rb', line 1396

def _admin(src, result, priority=0)
  t = Thread.new {
    Thread.current.abort_on_exception = false
    result << failsafe {
      response = instance_eval(src)
      str = response.to_s
      if(str.length > 200)
        response.class
      else
        str
      end
    }.to_s
  }
  t[:source] = src
  t.priority = priority
  @admin_threads.add(t)
  t
end

- (Object) _assign_effective_forms(arg = nil)



1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
# File 'src/util/oddbapp.rb', line 1496

def _assign_effective_forms(arg=nil)
  result = nil
  last = nil
  @system.substances.select { |subs| 
    !subs.has_effective_form? && (arg.nil? || arg.to_s < subs.to_s)
  }.sort_by { |subs| subs.name }.each { |subs|
    puts "Looking for effective form of ->#{subs}<- (#{subs.sequences.size} Sequences)"
    name = subs.to_s
    parts = name.split(/\s/u)
    suggest = if(parts.size == 1)
      subs
    elsif(![nil, '', 'Acidum'].include?(parts.first))
      @system.search_single_substance(parts.first) \
        || @system.search_single_substance(parts.first.gsub(/i$/u, 'um'))
    end
    last = result
    result = nil
    while(result.nil?)
      possibles = [
        "d(elete)", 
        "S(elf)", 
        "n(othing)", 
        "other_name",
      ]
      if(suggest)
        puts "Suggestion:                   ->#{suggest}<-"
        possibles.unshift("s(uggestion)")
      end
      if(last)
        puts "Last:                         ->#{last}<-"
        possibles.unshift("l(ast)")
      end
      print possibles.join(", ")
      print " > "
      $stdout.flush
      answer = $stdin.readline.strip
      puts "you typed:                    ->#{answer}<-"
      case answer
      when ''
        # do nothing
      when 'l'
        result = last
      when 's'
        result = suggest
      when 'S'
        result = subs
      when 'd'
        subs.sequences.each { |seq| 
          seq.delete_active_agent(subs) 
          seq.active_agents.odba_isolated_store
        }
        subs.odba_delete
        break
      when 'n'
        break
      when 'q'
        return
      when /c .+/u
        puts "creating:"
        pointer = Persistence::Pointer.new(:substance)
        puts "pointer: #{pointer}"
        args = { :lt => answer.split(/\s+/u, 2).last.strip }
        argstr = args.collect { |*pair| pair.join(' => ') }.join(', ')
        puts "args: #{argstr}"
        result = @system.update(pointer.creator, args)
        result.effective_form = result
        result.odba_store
        puts "result: #{result}"
      else
        result = @system.substance(answer)
      end
    end
    if(result)
      subs.effective_form = result
      subs.odba_store
    end
  }
  nil
end

- (Object) _migrate_child_to_utf8(child, queue, table, iconv, opts = {})



1765
# File 'src/util/oddbapp.rb', line 1765

def _migrate_child_to_utf8 child, queue, table, iconv, opts={}

- (Object) _migrate_feedbacks(item)



1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
# File 'src/util/oddbapp.rb', line 1670

def _migrate_feedbacks(item)
  item = item.odba_instance
  fbs = item.instance_variable_get('@feedbacks').odba_instance
  case fbs
  when Array
    # already migrated, ignore
  when Hash
    new = fbs.values.select { |fb| 
      fb.is_a?() 
    }.sort_by { |fb| fb.time }.reverse
    fbs.odba_delete
    new.odba_store
    item.instance_variable_set('@feedbacks', new)
    item.odba_store
    new.each { |fb|
      id = fb.odba_id
      fb.instance_variable_set('@oid', id)
      ptr = Persistence::Pointer.new([:feedback, id])
      fb.instance_variable_set('@pointer', ptr)
      @system.feedbacks.store(id, fb)
      fb.instance_variable_set('@item', item)
      fb.odba_store
    }
  when nil
    item.instance_variable_set('@feedbacks', [])
    item.odba_store
  end
end

- (Object) _migrate_obj_to_utf8(obj, queue, table, iconv, opts = {})



1739
# File 'src/util/oddbapp.rb', line 1739

def _migrate_obj_to_utf8 obj, queue, table, iconv, opts={}

- (Object) _migrate_to_utf8(queue, table, iconv, opts = {})



1725
# File 'src/util/oddbapp.rb', line 1725

def _migrate_to_utf8 queue, table, iconv, opts={}

- (Object) accept_orphaned(orphan, pointer, symbol, origin = nil)

prevalence-methods ################################



1295
1296
1297
1298
# File 'src/util/oddbapp.rb', line 1295

def accept_orphaned(orphan, pointer, symbol, origin=nil)
  command = AcceptOrphan.new(orphan, pointer,symbol, origin)
  @system.execute_command(command)
end

- (Object) assign_effective_forms(arg = nil)



1493
1494
1495
# File 'src/util/oddbapp.rb', line 1493

def assign_effective_forms(arg=nil)
  _assign_effective_forms(arg)
end

- (Object) clean



1299
1300
1301
1302
# File 'src/util/oddbapp.rb', line 1299

def clean
  super
  @system.clean_invoices
end

- (Object) create(pointer)



1303
1304
1305
# File 'src/util/oddbapp.rb', line 1303

def create(pointer)
  @system.execute_command(CreateCommand.new(pointer))
end

- (Object) create_commercial_forms



1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
# File 'src/util/oddbapp.rb', line 1306

def create_commercial_forms
  @system.each_package { |pac| 
    if(comform = pac.comform)
      possibilities = [
        comform.strip,
        comform.gsub(/\([^\)]+\)/u, '').strip,
        comform.gsub(/[()]/u, '').strip,
      ].uniq.delete_if { |possibility| possibility.empty? }
      cform = nil
      possibilities.each { |possibility|
        if(cform = CommercialForm.find_by_name(possibility))
          break
        end
      }
      if(cform.nil?)
        args = { :de => possibilities.first, 
          :synonyms => possibilities[1..-1] }
        possibilities.each { |possibility|
          if(form = @system.galenic_form(possibility))
            args = form.descriptions
            args.store(:synonyms, form.synonyms)
            break
          end
        }
        pointer = Persistence::Pointer.new(:commercial_form)
        cform = @system.update(pointer.creator, args)
      end
      pac.commercial_form = cform
      pac.odba_store
    end
  }
end

- (Object) delete(pointer)



1338
1339
1340
# File 'src/util/oddbapp.rb', line 1338

def delete(pointer)
  @system.execute_command(DeleteCommand.new(pointer))
end

- (Object) grant_download(email, filename, price, expires = Time.now+2592000)



1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
# File 'src/util/oddbapp.rb', line 1480

def grant_download(, filename, price, expires=Time.now+2592000)
  ip = Persistence::Pointer.new(:invoice)
  inv = update ip.creator, :yus_name => , :currency => 'EUR'
  itp = inv.pointer + :item
  update itp.creator, :text => filename, :price => price, :time => Time.now,
                      :type => :download, :expiry_time => expires,
                      :duration => (Time.now - expires) / 86400,
                      :vat_rate => 8.0
  inv.payment_received!
  inv.odba_store
  "http://#{SERVER_NAME}/de/gcc/download/invoice/#{inv.oid}/email/#{email}/filename/#{filename}"
end

- (Object) inject_poweruser(email, pass, days)



1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
# File 'src/util/oddbapp.rb', line 1341

def inject_poweruser(, pass, days)
  user_pointer = Persistence::Pointer.new(:poweruser)
  user_data = {
    :unique_email => ,
    :pass_hash    => Digest::MD5.hexdigest(pass),
  }
  invoice_pointer = Persistence::Pointer.new(:invoice)
  time = Time.now
  expiry = InvoiceItem.expiry_time(days, time)
  invoice_data = { :currency => State::PayPal::Checkout::CURRENCY }
  item_data = {
    :duration     => days,
    :expiry_time  => expiry,
    :total_netto  => State::Limit.price(days.to_i),
    :quantity     => days,
    :text         => 'unlimited access',
    :time         => time,
    :type         => :poweruser,
    :vat_rate     => VAT_RATE,
  }
  user = @system.update(user_pointer.creator, user_data, :admin)
  invoice = @system.update(invoice_pointer.creator, invoice_data, :admin)
  item_pointer = invoice.pointer + [:item]
  @system.update(item_pointer.creator, item_data, :admin)
  user.add_invoice(invoice)
  invoice.payment_received!
  invoice.odba_isolated_store
end

- (Object) ipn(notification)



1476
1477
1478
1479
# File 'src/util/oddbapp.rb', line 1476

def ipn(notification)
  Util::Ipn.process notification, self
  nil # don't return the invoice back across drb - it's not defined in yipn
end

- (Object) log_size



1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
# File 'src/util/oddbapp.rb', line 1813

def log_size
  @size_logger = Thread.new {
    time = Time.now
    bytes = 0
    threads = 0
    sessions = 0
    format = "%s %s: sessions: %4i - threads: %4i - memory: %4iMB %s"
    loop {
      begin
        lasttime = time
        time = Time.now
        alarm = time - lasttime > 60 ? '*' : ' '
        lastthreads = threads
        threads = Thread.list.size
        lastbytes = bytes
        bytes = File.read("/proc/#{$$}/stat").split(' ').at(22).to_i
        mbytes = bytes / (2**20)
        if mbytes > MEMORY_LIMIT
          puts "Footprint exceeds #{MEMORY_LIMIT}MB. Exiting."
          Thread.main.raise SystemExit
        end
        lastsessions = sessions
        sessions = @sessions.size
        gc = ''
        gc << 'S' if sessions < lastsessions
        gc << 'T' if threads < lastthreads
        gc << 'M' if bytes < lastbytes
        path = File.expand_path('../../doc/resources/downloads/status',
                                File.dirname(__FILE__))
        lines = File.readlines(path)[0,100] rescue []
        lines.unshift sprintf(format, alarm, 
                              time.strftime('%Y-%m-%d %H:%M:%S'),
                              sessions, threads, mbytes, gc)
        File.open(path, 'w') { |fh|
          fh.puts lines
        }
      rescue StandardError => e
        puts e.class
        puts e.message
        $stdout.flush
      end
    sleep 5
    }
  }
end

- (Object) login(email, pass)



1414
1415
1416
# File 'src/util/oddbapp.rb', line 1414

def (, pass)
  YusUser.new(YUS_SERVER.(, pass, YUS_DOMAIN))
end

- (Object) login_token(email, token)



1417
1418
1419
# File 'src/util/oddbapp.rb', line 1417

def (, token)
  YusUser.new(YUS_SERVER.(, token, YUS_DOMAIN))
end

- (Object) logout(session)



1420
1421
1422
1423
# File 'src/util/oddbapp.rb', line 1420

def logout(session)
  YUS_SERVER.logout(session)
rescue DRb::DRbError, RangeError
end

- (Object) merge_commercial_forms(source, target)



1369
1370
1371
1372
# File 'src/util/oddbapp.rb', line 1369

def merge_commercial_forms(source, target)
  command = MergeCommand.new(source.pointer, target.pointer)
  @system.execute_command(command)
end

- (Object) merge_companies(source_pointer, target_pointer)



1373
1374
1375
1376
# File 'src/util/oddbapp.rb', line 1373

def merge_companies(source_pointer, target_pointer)
  command = MergeCommand.new(source_pointer, target_pointer)
  @system.execute_command(command)
end

- (Object) merge_galenic_forms(source, target)



1377
1378
1379
1380
# File 'src/util/oddbapp.rb', line 1377

def merge_galenic_forms(source, target)
  command = MergeCommand.new(source.pointer, target.pointer)
  @system.execute_command(command)
end

- (Object) merge_indications(source, target)



1381
1382
1383
1384
# File 'src/util/oddbapp.rb', line 1381

def merge_indications(source, target)
  command = MergeCommand.new(source.pointer, target.pointer)
  @system.execute_command(command)
end

- (Object) merge_substances(source_pointer, target_pointer)



1385
1386
1387
1388
# File 'src/util/oddbapp.rb', line 1385

def merge_substances(source_pointer, target_pointer)
  command = MergeCommand.new(source_pointer, target_pointer)
  @system.execute_command(command)
end

- (Object) migrate_feedbacks



1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
# File 'src/util/oddbapp.rb', line 1659

def migrate_feedbacks
  @system.each_package { |pac|
    _migrate_feedbacks(pac)
  }
  @system.each_migel_product { |prd|
    _migrate_feedbacks(prd)
  }
  @system.feedbacks.odba_store
  @system.odba_store
  update_feedback_rss_feed
end

- (Object) migrate_to_utf8



1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
# File 'src/util/oddbapp.rb', line 1707

def migrate_to_utf8
  iconv = ::Iconv.new 'UTF-8//TRANSLIT//IGNORE', 'ISO-8859-1'
  ODBA.cache.retire_age = 5
  ODBA.cache.cleaner_step = 100000
  system = @system.odba_instance
  table = { system.odba_id => true, :serialized => {} }
  table.store :finalizer, proc { |object_id|
    table[:serialized].delete object_id }
  queue = [ system ]
  last_size = 0
  system.instance_variable_set '@config', nil
  while !queue.empty?
    if (queue.size - last_size).abs >= 10000
      puts last_size = queue.size
    end
    _migrate_to_utf8 queue, table, iconv, :all => true
  end
end

- (Object) multilinguify_analysis



1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
# File 'src/util/oddbapp.rb', line 1625

def multilinguify_analysis
  @system.analysis_positions.each { |pos|
    if(descr = pos.description)
      pos.instance_variable_set('@description', nil)
      @system.update(pos.pointer, {:de => descr})
    end
    if(fn = pos.footnote)
      pos.instance_variable_set('@footnote', nil)
      ptr = pos.pointer + :footnote
      @system.update(ptr.creator, {:de => fn})
    end
    if(lt = pos.list_title)
      pos.instance_variable_set('@list_title', nil)
      ptr = pos.pointer + :list_title
      @system.update(ptr.creator, {:de => lt})
    end
    if(tn = pos.taxnote)
      pos.instance_variable_set('@taxnote', nil)
      ptr = pos.pointer + :taxnote
      @system.update(ptr.creator, {:de => tn})
    end
    if(perm = pos.permissions)
      pos.instance_variable_set('@permissions', nil)
      ptr = pos.pointer + :permissions
      @system.update(ptr.creator, {:de => perm})
    end
    if(lim = pos.instance_variable_get('@limitation'))
      pos.instance_variable_set('@limitation', nil)
      ptr = pos.pointer + :limitation_text
      @system.update(ptr.creator, {:de => lim})
    end
    pos.odba_store
  }
end

- (Object) peer_cache(cache)



1424
1425
1426
# File 'src/util/oddbapp.rb', line 1424

def peer_cache cache
  ODBA.peer cache
end

- (Object) replace_fachinfo(iksnr, pointer)



1389
1390
1391
# File 'src/util/oddbapp.rb', line 1389

def replace_fachinfo(iksnr, pointer)
  @system.execute_command(ReplaceFachinfoCommand.new(iksnr, pointer))
end

- (Object) reset



1427
1428
1429
1430
1431
1432
1433
1434
1435
# File 'src/util/oddbapp.rb', line 1427

def reset
  @random_updater.kill if(@random_updater.is_a? Thread)
  if RUN_UPDATER
    @random_updater = run_random_updater
  end
  @mutex.synchronize {
    @sessions.clear
  }
end

- (Object) run_random_updater



1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
# File 'src/util/oddbapp.rb', line 1436

def run_random_updater
  Thread.new {
    Thread.current.abort_on_exception = true
    update_hour = rand(24)
    update_min = rand(60)
    today = (update_hour > Time.now.hour) ? \
      @@today : @@today.next
    loop {
      next_run = Time.local(today.year, today.month, today.day,
        update_hour, update_min)
      puts "next random update will take place at #{next_run}"
      $stdout.flush
      sleep(next_run - Time.now)
      Updater.new(self).run_random
      @system.recount
      GC.start
      today = @@today.next
      update_hour = rand(24)
      update_min = rand(60)
    }
  }
end

- (Object) unpeer_cache(cache)



1458
1459
1460
# File 'src/util/oddbapp.rb', line 1458

def unpeer_cache cache
  ODBA.unpeer cache
end

- (Object) update(pointer, values, origin = nil)



1392
1393
1394
# File 'src/util/oddbapp.rb', line 1392

def update(pointer, values, origin=nil)
  @system.update(pointer, values, origin)
end

- (Object) update_feedback_rss_feed



1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
# File 'src/util/oddbapp.rb', line 1461

def update_feedback_rss_feed
  async {
    begin
    @rss_mutex.synchronize {
      values = @system.sorted_feedbacks
      plg = Plugin.new(self)
      plg.update_rss_feeds('feedback.rss', values, View::Rss::)
    }
    rescue StandardError => e
      puts e.message
      puts e.backtrace
    end
  }
end

- (Object) utf8ify(object, opts = {})



1698
1699
1700
1701
1702
1703
1704
1705
1706
# File 'src/util/oddbapp.rb', line 1698

def utf8ify(object, opts={})
  from = 'ISO-8859-1'
  to = 'UTF-8//TRANSLIT//IGNORE'
  if opts[:reverse]
    from, to = to, from
  end
  iconv = ::Iconv.new to, from
  _migrate_to_utf8([object], {}, iconv)
end

- (Boolean) yus_allowed?(email, action, key = nil)

Returns:

  • (Boolean)


1576
1577
1578
1579
1580
# File 'src/util/oddbapp.rb', line 1576

def yus_allowed?(, action, key=nil)
  YUS_SERVER.autosession(YUS_DOMAIN) { |session|
    session.entity_allowed?(, action, key)
  }
end

- (Object) yus_create_user(email, pass = nil)



1581
1582
1583
1584
1585
1586
1587
# File 'src/util/oddbapp.rb', line 1581

def yus_create_user(, pass=nil)
  YUS_SERVER.autosession(YUS_DOMAIN) { |session|
    session.create_entity(, pass)
  }
  # if there is a password, we can log in
  (, pass) if(pass)
end

- (Object) yus_get_preference(name, key)



1593
1594
1595
1596
1597
1598
1599
# File 'src/util/oddbapp.rb', line 1593

def yus_get_preference(name, key)
  YUS_SERVER.autosession(YUS_DOMAIN) { |session|
    session.get_entity_preference(name, key)
  }
rescue Yus::YusError
  # user not found
end

- (Object) yus_get_preferences(name, keys)



1600
1601
1602
1603
1604
1605
1606
# File 'src/util/oddbapp.rb', line 1600

def yus_get_preferences(name, keys)
  YUS_SERVER.autosession(YUS_DOMAIN) { |session|
    session.get_entity_preferences(name, keys)
  }
rescue Yus::YusError
  {} # return an empty hash
end

- (Object) yus_grant(name, key, item, expires = nil)



1588
1589
1590
1591
1592
# File 'src/util/oddbapp.rb', line 1588

def yus_grant(name, key, item, expires=nil)
  YUS_SERVER.autosession(YUS_DOMAIN) { |session|
    session.grant(name, key, item, expires)
  }
end

- (Object) yus_model(name)



1607
1608
1609
1610
1611
1612
1613
# File 'src/util/oddbapp.rb', line 1607

def yus_model(name)
  if(odba_id = yus_get_preference(name, 'association'))
    ODBA.cache.fetch(odba_id, nil)
  end
rescue Yus::YusError, ODBA::OdbaError
  # association not found
end

- (Object) yus_reset_password(name, token, password)



1614
1615
1616
1617
1618
# File 'src/util/oddbapp.rb', line 1614

def yus_reset_password(name, token, password)
  YUS_SERVER.autosession(YUS_DOMAIN) { |session|
    session.reset_entity_password(name, token, password)
  }
end

- (Object) yus_set_preference(name, key, value, domain = YUS_DOMAIN)



1619
1620
1621
1622
1623
# File 'src/util/oddbapp.rb', line 1619

def yus_set_preference(name, key, value, domain=YUS_DOMAIN)
  YUS_SERVER.autosession(YUS_DOMAIN) { |session|
    session.set_entity_preference(name, key, value, domain)
  }
end