Module: ScribeDb
- Defined in:
- lib/scribelite/models/forward.rb,
lib/scribelite.rb,
lib/scribelite/schema.rb,
lib/scribelite/importer.rb,
lib/scribelite/models/tx.rb,
lib/scribelite/models/scribe.rb
Overview
forward references
require first to resolve circular references
Defined Under Namespace
Modules: Model Classes: CreateDb
Constant Summary collapse
- Models =
note: convenience alias for Model lets you use include ScribeDb::Models
Model
Class Method Summary collapse
- ._import_ethscription(rec) ⇒ Object
- .auto_migrate! ⇒ Object
- .connect(config = {}) ⇒ Object
- .create ⇒ Object
- .create_all ⇒ Object
-
.import_ethscriptions(page: 1, per_page: 50, sort_order: 'asc') ⇒ Object
note: by default - sort asc(ending) - oldest first (0,1,2,3, .etc.).
-
.open(database = './scribe.db') ⇒ Object
convenience helper for sqlite only.
- .setup_in_memory_db ⇒ Object
- .sync_facet_txs ⇒ Object (also: sync_facet_txns)
Class Method Details
._import_ethscription(rec) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/scribelite/importer.rb', line 75 def self._import_ethscription( rec ) txid = rec['transaction_hash'] block = rec['block_number'] idx = rec['transaction_index'] from = rec['creator'] to = rec['initial_owner'] ## todo - double check if daylight saving time (dst) breaks timestamp == utc identity/conversion? ## 2001-02-03T04:05:06+07:00 ## 2016-05-29T22:28:15.000Z ## check if %z can handle .000Z ?? ## DateTime.strptime( '2016-05-29T22:28:15.000Z', '%Y-%m-%dT%H:%M:%S%z' ) ## pp rec['creation_timestamp'] date = DateTime.strptime( rec['creation_timestamp'], '%Y-%m-%dT%H:%M:%S.000Z' ) num = rec['ethscription_number'] content_type = rec['mimetype'] data = rec['content_uri'] sha = rec['sha'] duplicate = rec['esip6'] flagged = rec['image_removed_by_request_of_rights_holder'] ### check - if flagged ## content_type always set to text/plain ## and data to data:, ??? ## (re)set to nil/null - why? why not? if flagged data = nil content_type = nil end tx_attribs = { id: txid, block: block, idx: idx, date: date, from: from, to: to, data: data, ## note: for now saved as utf8 string (not hex!!!!) } scribe_attribs = { id: txid, num: num, content_type: content_type, duplicate: duplicate, flagged: flagged, sha: sha, bytes: data ? data.length : nil ## auto-add/calc bytes (content length) } if num.nil? puts "!! skipping unconfirmed / unassigned inscribe - no. num" return end scribe = Scribe.find_by( id: txid ) if scribe ## skip for now - found id db else # puts "scribe_attribs:" # pp scribe_attribs Scribe.create!( **scribe_attribs ) end tx = Tx.find_by( id: txid ) if tx ## skip for now - found id db else # puts "tx_attribs:" # pp tx_attribs Tx.create!( **tx_attribs ) end end |
.auto_migrate! ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/scribelite.rb', line 55 def self.auto_migrate! ### todo/fix: ## check props table and versions!!!!! # first time? - auto-run db migratation, that is, create db tables unless LogDb::Model::Log.table_exists? LogDb.create # add logs table end unless ConfDb::Model::Prop.table_exists? ConfDb.create # add props table end unless ScribeDb::Model::Scribe.table_exists? ScribeDb.create end end |
.connect(config = {}) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/scribelite.rb', line 84 def self.connect( config={} ) if config.empty? puts "ENV['DATBASE_URL'] - >#{ENV['DATABASE_URL']}<" ### change default to ./scribe.db ?? why? why not? db = URI.parse( ENV['DATABASE_URL'] || 'sqlite3:///scribe.db' ) if db.scheme == 'postgres' config = { adapter: 'postgresql', host: db.host, port: db.port, username: db.user, password: db.password, database: db.path[1..-1], encoding: 'utf8' } else # assume sqlite3 config = { adapter: db.scheme, # sqlite3 database: db.path[1..-1] # scribe.db (NB: cut off leading /, thus 1..-1) } end end puts "Connecting to db using settings: " pp config ActiveRecord::Base.establish_connection( config ) # ActiveRecord::Base.logger = Logger.new( STDOUT ) end |
.create ⇒ Object
43 44 45 46 47 |
# File 'lib/scribelite.rb', line 43 def self.create CreateDb.new.up ConfDb::Model::Prop.create!( key: 'db.schema.scribe.version', value: Scribelite::VERSION ) end |
.create_all ⇒ Object
49 50 51 52 53 |
# File 'lib/scribelite.rb', line 49 def self.create_all LogDb.create # add logs table ConfDb.create # add props table ScribeDb.create end |
.import_ethscriptions(page: 1, per_page: 50, sort_order: 'asc') ⇒ Object
note: by default - sort asc(ending) - oldest first (0,1,2,3, .etc.)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/scribelite/importer.rb', line 8 def self.import_ethscriptions( page: 1, per_page: 50, sort_order: 'asc' ) net = Ethscribe.config.client recs = net.ethscriptions( page: page, per_page: per_page, sort_order: sort_order ) puts " #{recs.size} record(s)" recs.each_with_index do |rec,i| puts "==> page #{page}/#{i+1} - #{rec['transaction_hash']}..." _import_ethscription( rec ) end recs.size ## return number of records fetched for now end |
.open(database = './scribe.db') ⇒ Object
convenience helper for sqlite only
74 75 76 77 78 79 80 |
# File 'lib/scribelite.rb', line 74 def self.open( database='./scribe.db' ) ## convenience helper for sqlite only connect( adapter: 'sqlite3', database: database ) ## build schema if database new/empty auto_migrate! end |
.setup_in_memory_db ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/scribelite.rb', line 117 def self.setup_in_memory_db # Database Setup & Config ActiveRecord::Base.logger = Logger.new( STDOUT ) ## ActiveRecord::Base.colorize_logging = false - no longer exists - check new api/config setting? self.connect( adapter: 'sqlite3', database: ':memory:' ) ## build schema ScribeDb.create_all end |
.sync_facet_txs ⇒ Object Also known as: sync_facet_txns
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/scribelite/importer.rb', line 27 def self.sync_facet_txs net = Ethscribe.config.client ## add block argument - why? why not? ## get count by block e.g. ## our_count = Scribe.joins(:tx).where( 'block < ?', new_block_number).count count = Scribe.count block = if count > 0 last_scribe = Scribe.where( num: Scribe.maximum(:num) ).first last_scribe.tx.block + 1 else 0 end loop do res = net.newer_facet_txs( block, max: 2500, count: count ) break if res['blocks'].size == 0 first_block = res['blocks'][0]['block_number'] last_block = res['blocks'][-1]['block_number'] puts "==> #{res['blocks'].size} block(s) - #{first_block} to #{last_block}..." puts " total_future_ethscriptions: #{res['total_future_ethscriptions']}" batch_count = 0 res['blocks'].each do |block_rec| block_rec['ethscriptions'].each do |rec| _import_ethscription( rec ) count += 1 batch_count += 1 end end puts " #{batch_count} record(s) added" block = last_block + 1 break if res['total_future_ethscriptions'] == 0 end end |