Class: Bct::Bigquery
- Inherits:
-
Object
- Object
- Bct::Bigquery
- Defined in:
- lib/bct/bigquery1.rb,
lib/bct/bigquery2.rb
Overview
classe para processar etherscan/greymass & bigquery
Instance Attribute Summary collapse
-
#api ⇒ Google::Cloud::Bigquery
readonly
API bigquery.
-
#job ⇒ Google::Cloud::Bigquery::QueryJob
readonly
Job bigquery.
-
#ops ⇒ Thor::CoreExt::HashWithIndifferentAccess
readonly
Opcoes trabalho.
-
#sqr ⇒ Google::Cloud::Bigquery::Data
readonly
Resultado do SQL.
Instance Method Summary collapse
-
#apies ⇒ Etherscan
API etherscan.
-
#apigm ⇒ Greymass
API greymass.
-
#dml(cmd) ⇒ Integer
cria Data Manipulation Language (DML) job bigquery.
-
#eost_ins ⇒ String
Comando insert SQL formatado eos.
-
#eost_val1(htx) ⇒ String
Valores formatados para insert eos (parte1).
-
#eost_val2(htx, act) ⇒ String
Valores formatados para insert eos (parte2).
-
#ethk_ins ⇒ String
Comando insert SQL formatado ethk (trx token).
-
#ethk_val1(hkx) ⇒ String
Valores formatados ethk (trx token parte1).
-
#ethk_val2(hkx) ⇒ String
Valores formatados ethk (trx token parte2).
-
#ethk_val3(hkx) ⇒ String
Valores formatados ethk (trx token parte3).
-
#etht_ins ⇒ String
Comando insert SQL formatado etht (trx normais).
-
#etht_val1(htx) ⇒ String
Valores formatados etht (trx normais parte1).
-
#etht_val2(htx) ⇒ String
Valores formatados etht (trx normais parte2).
-
#etht_val3(htx) ⇒ String
Valores formatados etht (trx normais parte3).
-
#initialize(pop) ⇒ Bigquery
constructor
API bigquery & API etherscan.
-
#job?(cmd) ⇒ Boolean
cria job bigquery & verifica execucao.
-
#mostra_tudo ⇒ Object
mostra resumos e transacoes etherscan & greymass.
-
#processa_eth ⇒ Object
insere transacoes novas nas tabelas etht (trx normais), ethk (trx token).
-
#processa_tudo ⇒ Object
insere transacoes novas nas tabelas todas etht, ethk, eos.
-
#sql(cmd, red = []) ⇒ Google::Cloud::Bigquery::Data
cria Structured Query Language (SQL) job bigquery.
Constructor Details
#initialize(pop) ⇒ Bigquery
Returns API bigquery & API etherscan.
26 27 28 29 30 31 |
# File 'lib/bct/bigquery1.rb', line 26 def initialize(pop) # usa env GOOGLE_APPLICATION_CREDENTIALS para obter credentials # @see https://cloud.google.com/bigquery/docs/authentication/getting-started @api = Google::Cloud::Bigquery.new @ops = pop end |
Instance Attribute Details
#api ⇒ Google::Cloud::Bigquery (readonly)
Returns API bigquery.
13 14 15 |
# File 'lib/bct/bigquery1.rb', line 13 def api @api end |
#job ⇒ Google::Cloud::Bigquery::QueryJob (readonly)
Returns job bigquery.
15 16 17 |
# File 'lib/bct/bigquery1.rb', line 15 def job @job end |
#ops ⇒ Thor::CoreExt::HashWithIndifferentAccess (readonly)
Returns opcoes trabalho.
17 18 19 |
# File 'lib/bct/bigquery1.rb', line 17 def ops @ops end |
#sqr ⇒ Google::Cloud::Bigquery::Data (readonly)
Returns resultado do SQL.
19 20 21 |
# File 'lib/bct/bigquery1.rb', line 19 def sqr @sqr end |
Instance Method Details
#apies ⇒ Etherscan
Returns API etherscan.
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/bct/bigquery1.rb', line 34 def apies @apies ||= Etherscan.new( { wb: sql("select * from #{BD}.walletEth order by 2"), nt: sql("select itx,iax from #{BD}.ethtx"), nk: sql("select itx,iax from #{BD}.ethkx") }, ops ) end |
#apigm ⇒ Greymass
Returns API greymass.
46 47 48 49 50 51 52 53 54 |
# File 'lib/bct/bigquery1.rb', line 46 def apigm @apigm ||= Greymass.new( { wb: sql("select * from #{BD}.walletEos order by 2"), nt: sql("select itx,iax from #{BD}.eostx") }, ops ) end |
#dml(cmd) ⇒ Integer
cria Data Manipulation Language (DML) job bigquery
98 99 100 |
# File 'lib/bct/bigquery1.rb', line 98 def dml(cmd) job?(cmd) ? 0 : job.num_dml_affected_rows end |
#eost_ins ⇒ String
Returns comando insert SQL formatado eos.
90 91 92 93 |
# File 'lib/bct/bigquery2.rb', line 90 def eost_ins "insert #{BD}.eos(gseq,aseq,bnum,time,contract,action,acfrom,acto,iax,amount,moeda,memo,dias" \ ") VALUES#{apigm.novax.map { |e| eost_val1(e) }.join(',')}" end |
#eost_val1(htx) ⇒ String
Returns valores formatados para insert eos (parte1).
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/bct/bigquery2.rb', line 97 def eost_val1(htx) a = htx[:action_trace][:act] "(#{htx[:global_action_seq]}," \ "#{htx[:account_action_seq]}," \ "#{htx[:block_num]}," \ "DATETIME(TIMESTAMP('#{htx[:block_time]}'))," \ "'#{a[:account]}'," \ "'#{a[:name]}'," \ "#{eost_val2(htx, a)}" end |
#eost_val2(htx, act) ⇒ String
Returns valores formatados para insert eos (parte2).
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/bct/bigquery2.rb', line 110 def eost_val2(htx, act) d = act[:data] q = d[:quantity].to_s s = d[:memo].inspect "'#{d[:from]}'," \ "'#{d[:to]}'," \ "'#{htx[:iax]}'," \ "#{q.to_d},'#{q[/[[:upper:]]+/]}'," \ "nullif('#{s.gsub(/['"]/, '')}','nil')," \ "#{ops[:h][String(htx[:itx])] || 0})" end |
#ethk_ins ⇒ String
Returns comando insert SQL formatado ethk (trx token).
50 51 52 53 54 |
# File 'lib/bct/bigquery2.rb', line 50 def ethk_ins "insert #{BD}.ethk(blocknumber,timestamp,txhash,nonce,blockhash,transactionindex,axfrom,axto,iax," \ 'value,tokenname,tokensymbol,tokendecimal,gas,gasprice,gasused,input,contractaddress,dias' \ ") VALUES#{apies.novkx.map { |e| ethk_val1(e) }.join(',')}" end |
#ethk_val1(hkx) ⇒ String
Returns valores formatados ethk (trx token parte1).
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/bct/bigquery2.rb', line 57 def ethk_val1(hkx) "(#{Integer(hkx[:blockNumber])}," \ "#{Integer(hkx[:timeStamp])}," \ "'#{hkx[:hash]}'," \ "#{Integer(hkx[:nonce])}," \ "'#{hkx[:blockHash]}'," \ "#{Integer(hkx[:transactionIndex])}," \ "'#{hkx[:from]}'," \ "'#{hkx[:to]}'," \ "'#{hkx[:iax]}'," \ "#{ethk_val2(hkx)}" end |
#ethk_val2(hkx) ⇒ String
Returns valores formatados ethk (trx token parte2).
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/bct/bigquery2.rb', line 71 def ethk_val2(hkx) "cast('#{hkx[:value]}' as numeric)," \ "'#{hkx[:tokenName]}'," \ "'#{hkx[:tokenSymbol]}'," \ "#{Integer(hkx[:tokenDecimal])}," \ "cast('#{hkx[:gas]}' as numeric)," \ "cast('#{hkx[:gasPrice]}' as numeric)," \ "cast('#{hkx[:gasUsed]}' as numeric)," \ "#{ethk_val3(hkx)}" end |
#ethk_val3(hkx) ⇒ String
Returns valores formatados ethk (trx token parte3).
83 84 85 86 87 |
# File 'lib/bct/bigquery2.rb', line 83 def ethk_val3(hkx) "#{hkx[:input].length.zero? ? 'null' : "'#{hkx[:input]}'"}," \ "#{hkx[:contractAddress].length.zero? ? 'null' : "'#{hkx[:contractAddress]}'"}," \ "#{Integer(ops[:h][hkx[:blockNumber]] || 0)})" end |
#etht_ins ⇒ String
Returns comando insert SQL formatado etht (trx normais).
11 12 13 14 15 |
# File 'lib/bct/bigquery2.rb', line 11 def etht_ins "insert #{BD}.etht(blocknumber,timestamp,txhash,nonce,blockhash,transactionindex,axfrom,axto,iax," \ 'value,gas,gasprice,gasused,iserror,txreceipt_status,input,contractaddress,dias' \ ") VALUES#{apies.novtx.map { |e| etht_val1(e) }.join(',')}" end |
#etht_val1(htx) ⇒ String
Returns valores formatados etht (trx normais parte1).
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/bct/bigquery2.rb', line 18 def etht_val1(htx) "(#{Integer(htx[:blockNumber])}," \ "#{Integer(htx[:timeStamp])}," \ "'#{htx[:hash]}'," \ "#{Integer(htx[:nonce])}," \ "'#{htx[:blockHash]}'," \ "#{Integer(htx[:transactionIndex])}," \ "'#{htx[:from]}'," \ "'#{htx[:to]}'," \ "'#{htx[:iax]}'," \ "#{etht_val2(htx)}" end |
#etht_val2(htx) ⇒ String
Returns valores formatados etht (trx normais parte2).
32 33 34 35 36 37 38 39 40 |
# File 'lib/bct/bigquery2.rb', line 32 def etht_val2(htx) "cast('#{htx[:value]}' as numeric)," \ "cast('#{htx[:gas]}' as numeric)," \ "cast('#{htx[:gasPrice]}' as numeric)," \ "cast('#{htx[:gasUsed]}' as numeric)," \ "#{Integer(htx[:isError])}," \ "#{htx[:txreceipt_status].length.zero? ? 'null' : htx[:txreceipt_status]}," \ "#{etht_val3(htx)}" end |
#etht_val3(htx) ⇒ String
Returns valores formatados etht (trx normais parte3).
43 44 45 46 47 |
# File 'lib/bct/bigquery2.rb', line 43 def etht_val3(htx) "#{htx[:input].length.zero? ? 'null' : "'#{htx[:input]}'"}," \ "#{htx[:contractAddress].length.zero? ? 'null' : "'#{htx[:contractAddress]}'"}," \ "#{Integer(ops[:h][htx[:blockNumber]] || 0)})" end |
#job?(cmd) ⇒ Boolean
cria job bigquery & verifica execucao
78 79 80 81 82 83 |
# File 'lib/bct/bigquery1.rb', line 78 def job?(cmd) @job = api.query_job(cmd) @job.wait_until_done! puts(@job.error['message']) if @job.failed? @job.failed? end |
#mostra_tudo ⇒ Object
mostra resumos e transacoes etherscan & greymass
57 58 59 60 |
# File 'lib/bct/bigquery1.rb', line 57 def mostra_tudo apies.mostra_resumo apigm.mostra_resumo end |
#processa_eth ⇒ Object
insere transacoes novas nas tabelas etht (trx normais), ethk (trx token)
69 70 71 72 |
# File 'lib/bct/bigquery1.rb', line 69 def processa_eth puts(format("%<n>2i LINHAS INSERIDAS #{BD}.etht", n: apies.novtx.count.positive? ? dml(etht_ins) : 0)) puts(format("%<n>2i LINHAS INSERIDAS #{BD}.ethk", n: apies.novkx.count.positive? ? dml(ethk_ins) : 0)) end |
#processa_tudo ⇒ Object
insere transacoes novas nas tabelas todas etht, ethk, eos
63 64 65 66 |
# File 'lib/bct/bigquery1.rb', line 63 def processa_tudo processa_eth puts(format("%<n>2i LINHAS INSERIDAS #{BD}.eos ", n: apigm.novax.count.positive? ? dml(eost_ins) : 0)) end |
#sql(cmd, red = []) ⇒ Google::Cloud::Bigquery::Data
cria Structured Query Language (SQL) job bigquery
90 91 92 |
# File 'lib/bct/bigquery1.rb', line 90 def sql(cmd, red = []) @sqr = job?(cmd) ? red : job.data end |