Class: Bct::Bigquery

Inherits:
Object
  • Object
show all
Defined in:
lib/bct/bigquery1.rb,
lib/bct/bigquery2.rb

Overview

classe para processar etherscan/greymass & bigquery

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pop) ⇒ Bigquery

Returns API bigquery & API etherscan.

Parameters:

  • pop (Thor::CoreExt::HashWithIndifferentAccess)

    opcoes trabalho

Options Hash (pop):

  • :h (Hash) — default: {}

    configuracao ajuste reposicionamento temporal

  • :v (Boolean) — default: false

    mostra transacoes normais & token?

  • :t (Boolean) — default: false

    mostra transacoes todas ou somente novas?



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

#apiGoogle::Cloud::Bigquery (readonly)

Returns API bigquery.

Returns:

  • (Google::Cloud::Bigquery)

    API bigquery



13
14
15
# File 'lib/bct/bigquery1.rb', line 13

def api
  @api
end

#jobGoogle::Cloud::Bigquery::QueryJob (readonly)

Returns job bigquery.

Returns:

  • (Google::Cloud::Bigquery::QueryJob)

    job bigquery



15
16
17
# File 'lib/bct/bigquery1.rb', line 15

def job
  @job
end

#opsThor::CoreExt::HashWithIndifferentAccess (readonly)

Returns opcoes trabalho.

Returns:

  • (Thor::CoreExt::HashWithIndifferentAccess)

    opcoes trabalho



17
18
19
# File 'lib/bct/bigquery1.rb', line 17

def ops
  @ops
end

#sqrGoogle::Cloud::Bigquery::Data (readonly)

Returns resultado do SQL.

Returns:

  • (Google::Cloud::Bigquery::Data)

    resultado do SQL



19
20
21
# File 'lib/bct/bigquery1.rb', line 19

def sqr
  @sqr
end

Instance Method Details

#apiesEtherscan

Returns API etherscan.

Returns:



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

#apigmGreymass

Returns API greymass.

Returns:



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

Parameters:

  • cmd (String)

    comando SQL a executar

Returns:

  • (Integer)

    numero linhas afetadas



98
99
100
# File 'lib/bct/bigquery1.rb', line 98

def dml(cmd)
  job?(cmd) ? 0 : job.num_dml_affected_rows
end

#eost_insString

Returns comando insert SQL formatado eos.

Returns:

  • (String)

    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).

Parameters:

  • htx (Hash)

    transacao ligadas a uma carteira - sem elementos irrelevantes

Returns:

  • (String)

    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).

Parameters:

  • htx (Hash)

    transacao ligadas a uma carteira - sem elementos irrelevantes

Returns:

  • (String)

    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_insString

Returns comando insert SQL formatado ethk (trx token).

Returns:

  • (String)

    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).

Returns:

  • (String)

    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).

Returns:

  • (String)

    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).

Returns:

  • (String)

    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_insString

Returns comando insert SQL formatado etht (trx normais).

Returns:

  • (String)

    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).

Returns:

  • (String)

    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).

Returns:

  • (String)

    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).

Returns:

  • (String)

    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

Parameters:

  • cmd (String)

    comando SQL a executar

Returns:

  • (Boolean)

    job ok?



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_tudoObject

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_ethObject

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_tudoObject

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

Parameters:

  • cmd (String)

    comando SQL a executar

  • red (String) (defaults to: [])

    resultado quando SQL tem erro

Returns:

  • (Google::Cloud::Bigquery::Data)

    resultado do SQL



90
91
92
# File 'lib/bct/bigquery1.rb', line 90

def sql(cmd, red = [])
  @sqr = job?(cmd) ? red : job.data
end