# frozen_string_literal: true require('google/cloud/bigquery') require('bigdecimal/util') # @author Hernani Rodrigues Vaz module Bct BD = 'hernanirvaz.coins' # (see Bigquery) class Bigquery # @return [Google::Cloud::Bigquery] API bigquery attr_reader :api # @return [Google::Cloud::Bigquery::QueryJob] job bigquery attr_reader :job # @return [Thor::CoreExt::HashWithIndifferentAccess] opcoes trabalho attr_reader :ops # @return (see sql) attr_reader :sqr # @param [Thor::CoreExt::HashWithIndifferentAccess] pop opcoes trabalho # @option pop [Hash] :h ({}) configuracao ajuste reposicionamento temporal # @option pop [Boolean] :v (false) mostra transacoes normais & token? # @option pop [Boolean] :t (false) mostra transacoes todas ou somente novas? # @return [Bigquery] API bigquery & API etherscan 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 # @return [Etherscan] API etherscan 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 # @return [Greymass] API greymass def apigm @apigm ||= Greymass.new( { wb: sql("select * from #{BD}.walletEos order by 2"), nt: sql("select itx,iax from #{BD}.eostx") }, ops ) end # mostra resumos e transacoes etherscan & greymass def mostra_tudo apies.mostra_resumo apigm.mostra_resumo end # insere transacoes novas nas tabelas todas etht, ethk, eos def processa_tudo processa_eth puts(format("%2i LINHAS INSERIDAS #{BD}.eos ", n: apigm.novax.count.positive? ? dml(eost_ins) : 0)) end # insere transacoes novas nas tabelas etht (trx normais), ethk (trx token) def processa_eth puts(format("%2i LINHAS INSERIDAS #{BD}.etht", n: apies.novtx.count.positive? ? dml(etht_ins) : 0)) puts(format("%2i LINHAS INSERIDAS #{BD}.ethk", n: apies.novkx.count.positive? ? dml(ethk_ins) : 0)) end # cria job bigquery & verifica execucao # # @param cmd (see sql) # @return [Boolean] job ok? def job?(cmd) @job = api.query_job(cmd) @job.wait_until_done! puts(@job.error['message']) if @job.failed? @job.failed? end # cria Structured Query Language (SQL) job bigquery # # @param [String] cmd comando SQL a executar # @param [String] red resultado quando SQL tem erro # @return [Google::Cloud::Bigquery::Data] resultado do SQL def sql(cmd, red = []) @sqr = job?(cmd) ? red : job.data end # cria Data Manipulation Language (DML) job bigquery # # @param cmd (see sql) # @return [Integer] numero linhas afetadas def dml(cmd) job?(cmd) ? 0 : job.num_dml_affected_rows end end end