eost 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fa9cf7eabe4ac0b063ac96b816560d9f4c07c87dd1c0e110a9993d3eb5bc4534
4
- data.tar.gz: 6cb7ee1faa67d3426fa96da1eeb231eb797328550a7ef66d4654bb64573d16b6
3
+ metadata.gz: b04e7a8105407a676d58e6175f5823ed7338b56cf0ea5898b9d15fbd599413e3
4
+ data.tar.gz: 99a8578ca355c1f14c2c022c704f1e0006775475f334d34364487d638399c96d
5
5
  SHA512:
6
- metadata.gz: c08c2e2ed421b3fef1610e0fd93c35d180d2939afd050b4f20f863d6bbf740ff6142bace94f05b637371cfff7410fe1048cff59818d0600f8ca1a61dfa94f0b5
7
- data.tar.gz: bbfb0df62f2c9af92fd883bf9d3e2485ae80d2b209ec25627949bd6c41dd5725d7b812aa3e1b87481a2c76c265775b0a78d256d434ce0cda7681395fde44aff7
6
+ metadata.gz: 6f0320676e378a2f14ad917e820cad76bc8ff8d79d9ca2db2e47ce371704a090c03090b8295bbe98759f524ad073c77068399564919f372cbb87b5e96f896b77
7
+ data.tar.gz: a7f451fbaff9f14db7f1a1dcd3f7d3cba9beb4e55a3f201679a2433ff5906ccd67633ad652bff2fcab57e67835ec33579413a5e8f5ac52eaf5ebaa0bf2e4c37f
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- eost (0.1.5)
4
+ eost (0.1.6)
5
5
  google-cloud-bigquery
6
6
  roo
7
7
  thor
@@ -1,10 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'thor'
4
- require 'eost/version'
5
4
  require 'eost/bigquery'
6
5
  require 'eost/folhacalculo'
6
+ require 'eost/version'
7
7
 
8
+ # @author Hernani Rodrigues Vaz
8
9
  module Eost
9
10
  ID = `whoami`.chomp
10
11
 
@@ -18,12 +19,13 @@ module Eost
18
19
  option :x, banner: 'EXT', default: '.csv',
19
20
  desc: 'Extensao das folhas calculo'
20
21
  option :e, type: :boolean, default: false,
21
- desc: 'apaga linhas existentes no bigquery'
22
+ desc: 'apaga linha igual no bigquery'
23
+ option :m, type: :boolean, default: false,
24
+ desc: 'apaga linhas existencia multipla no bigquery'
22
25
  # processa csv
23
26
  def load
24
- # opcoes apagar linhas
25
27
  Dir.glob("#{options[:d]}/*#{options[:x]}").sort.each do |f|
26
- Bigquery.new(f, options[:e]).processa
28
+ Bigquery.new(f, { e: options[:e], m: options[:m], i: true }).processa
27
29
  end
28
30
  end
29
31
 
@@ -35,7 +37,7 @@ module Eost
35
37
  # mostra csv
36
38
  def mostra
37
39
  Dir.glob("#{options[:d]}/*#{options[:x]}").sort.each do |f|
38
- Bigquery.new(f).show
40
+ Bigquery.new(f).processa
39
41
  end
40
42
  end
41
43
 
@@ -7,40 +7,43 @@ module Eost
7
7
  DF = '%Y-%m-%d'
8
8
  DI = '%Y-%m-%d %H:%M:%S'
9
9
 
10
- # folhas calculo comuns no bigquery
10
+ # (see Bigquery)
11
11
  class Bigquery
12
- # @return [Roo::Excelx] folha calculo a processar
13
- attr_reader :book
12
+ # @return [Google::Cloud::Bigquery] API bigquery
13
+ attr_reader :apibq
14
+ # @return [Roo::CSV] folha calculo a processar
15
+ attr_reader :folha
16
+ # @return [Hash<Symbol, Boolean>] opcoes trabalho com linhas
17
+ attr_reader :linha
18
+
14
19
  # @return [Array] row folha calculo em processamento
15
20
  attr_reader :row
16
- # @return [Google::Cloud::Bigquery] API bigquery
17
- attr_reader :api
18
21
  # @return [Google::Cloud::Bigquery::QueryJob] job bigquery
19
22
  attr_reader :job
20
23
  # @return (see sql_select)
21
24
  attr_reader :sql
22
- # @return [Boolean] apaga linhas existentes sim/nao?
23
- attr_reader :apaga
24
25
 
25
- # permite processa folhas calculo comuns no bigquery
26
- #
27
26
  # @param [String] csv folha calculo para processar
28
- # @param [Boolean] apaga linhas existentes sim/nao?
29
- # @return [Bigquery] acesso folha calculo & bigquery
30
- def initialize(csv = '', apaga = false)
31
- @book = Roo::CSV.new(csv) if csv.size.positive?
32
- @apaga = apaga
27
+ # @param [Hash<Symbol, Boolean>] ops opcoes trabalho com linhas
28
+ # @option ops [Boolean] :e (false) apaga linha igual?
29
+ # @option ops [Boolean] :m (false) apaga linhas existencia multipla?
30
+ # @option ops [Boolean] :i (false) insere linha nova?
31
+ # @return [Bigquery] acesso folhas calculo bloks.io
32
+ # & correspondente bigquery dataset
33
+ def initialize(csv = '', ops = { e: false, m: false, i: false })
33
34
  # usa env GOOGLE_APPLICATION_CREDENTIALS para obter credentials
34
35
  # @see https://cloud.google.com/bigquery/docs/authentication/getting-started
35
- @api = Google::Cloud::Bigquery.new
36
+ @apibq = Google::Cloud::Bigquery.new
37
+ @folha = Roo::CSV.new(csv) if csv.size.positive?
38
+ @linha = ops
36
39
  end
37
40
 
38
41
  # cria job bigquery & verifica execucao
39
42
  #
40
- # @param [String] sql para executar
43
+ # @param [String] sql a executar
41
44
  # @return [Boolean] job ok?
42
45
  def job_bigquery?(sql)
43
- @job = api.query_job(sql)
46
+ @job = apibq.query_job(sql)
44
47
  @job.wait_until_done!
45
48
  puts @job.error['message'] if @job.failed?
46
49
  @job.failed?
@@ -54,34 +57,38 @@ module Eost
54
57
  job_bigquery?(sql) ? 0 : job.num_dml_affected_rows
55
58
  end
56
59
 
57
- # cria sql job bigquery com resultados
60
+ # pesquisa existencia linha folha calculo no bigquery
58
61
  #
59
- # @return [Array<Hash>] resultado sql
62
+ # @return [Google::Cloud::Bigquery::Data] resultado do sql num array<hash>
60
63
  def sql_select
61
- # se array.count > 1 => nao fazer nada
64
+ # array.count = 0 ==> pode carregar esta linha
65
+ # array.count >= 1 ==> nao carregar esta linha
62
66
  @sql = job_bigquery?('select * ' + sql_where) ? [{}, {}] : job.data
63
67
  end
64
68
 
65
- # @return [String] parte sql para processamento linhas similares
69
+ # @return [String] parte sql para processamento linhas existentes
66
70
  def sql_where
67
- "from coins.eos where blocknumber=#{row[0]}"
71
+ "from hernanirvaz.coins.eos where blocknumber=#{row[0]}"
68
72
  end
69
73
 
70
74
  # @return [Integer] numero linhas inseridas
71
75
  def sql_insert
72
- dml('insert coins.eos(blocknumber,time,contract,' \
76
+ return 1 unless linha[:i]
77
+
78
+ dml('insert hernanirvaz.coins.eos(blocknumber,time,contract,' \
73
79
  'action,acfrom,acto,amount,symbol,memo,data,dias) VALUES(' +
74
- sql_insert1 + sql_insert2)
80
+ str_insert1)
75
81
  end
76
82
 
77
83
  # @return [String] campos insert da linha bigquery
78
- def sql_insert1
79
- "#{row[0]},'#{DateTime.parse(row[1]).strftime(DI)}','#{row[2]}',"
84
+ def str_insert1
85
+ "#{row[0]},'#{DateTime.parse(row[1]).strftime(DI)}','#{row[2]}'," +
86
+ str_insert2
80
87
  end
81
88
 
82
89
  # @return [String] campos insert da linha bigquery
83
- def sql_insert2
84
- "'#{row[3]}','#{row[4]}','#{row[5]}',#{row[6]}," \
90
+ def str_insert2
91
+ "'#{row[3]}','#{row[4]}','#{row[5]}',#{row[6].to_f}," \
85
92
  "'#{row[7]}','#{row[8]}','#{row[9]}',0)"
86
93
  end
87
94
 
@@ -3,80 +3,73 @@
3
3
  module Eost
4
4
  HT = %w[block_num block_time contract action
5
5
  from to amount symbol memo data].freeze
6
- R1 = '%<v3>-12.12s %<v4>s %<v5>-12.12s %<v6>-12.12s'
7
- R2 = '%<v7>10.5f %<v8>-10.10s'
6
+ R1 = '%<v3>-12.12s %<v4>-8.8s %<v5>-12.12s %<v6>-12.12s'
7
+ R2 = '%<v7>10.5f %<v8>-8.8s'
8
8
 
9
- # folhas calculo comuns no bigquery
9
+ # trabalhar com folhas calculo bloks.io & dados no bigquery
10
10
  class Bigquery
11
- # @return [String] linha folha calculo formatada
12
- def row_str
13
- "#{row[0]} #{DateTime.parse(row[1]).strftime(DF)} " + row_r1 + row_r2
14
- end
15
-
16
- # @return [String] linha folha calculo formatada
17
- def row_r1
18
- format(R1, v3: row[2], v4: row[3], v5: row[4], v6: row[5])
19
- end
20
-
21
- # @return [String] linha folha calculo formatada
22
- def row_r2
23
- format(R2, v7: row[6], v8: row[7])
24
- end
25
-
26
11
  # processa linhas folha calculo
27
12
  def processa
28
13
  n = 0
29
- # usada somente a primeira sheet
30
- book.sheet(0).parse(header_search: HT) do |r|
31
- n += 1
32
- puts n == 1 ? "\n" + book.info : processa_row(r)
33
- end
34
- end
35
-
36
- # mostra linhas folha calculo
37
- def show
38
- n = 0
39
- # usada somente a primeira sheet
40
- book.sheet(0).parse(header_search: HT) do |r|
14
+ folha.sheet(0).parse(header_search: HT) do |r|
41
15
  n += 1
42
- puts n == 1 ? "\n" + book.info : show_row(r)
16
+ puts n == 1 ? "\n" + folha.info : processa_row(r)
43
17
  end
44
18
  end
45
19
 
46
20
  # processa linha folha calculo para arquivo
47
21
  #
48
- # @param (see show_row)
49
- # @return [String] linha folha calculo processada
22
+ # @param [Hash] has da linha em processamento
23
+ # @return [String] texto informativo do processamento
50
24
  def processa_row(has)
51
25
  @row = has.values
52
26
  sql_select
53
- if rnaoexiste? then row_str + (sql_insert == 1 ? ' NOVA' : ' ERRO')
54
- elsif rexiste? then row_existente
27
+ if row_naoexiste? then row_str + (sql_insert == 1 ? ' NOVA' : ' ERRO')
28
+ elsif row_existe? then row_existente
29
+ else row_multiplas
55
30
  end
56
31
  end
57
32
 
58
- # obtem linha folha calculo para apresentacao
59
- #
60
- # @param [Hash] has da linha em processamento
61
- # @return (see row_str)
62
- def show_row(has)
63
- @row = has.values
64
- row_str
33
+ # @return [String] linha folha calculo formatada
34
+ def row_str
35
+ "#{row[0]} #{DateTime.parse(row[1]).strftime(DF)} " + row_r1 + row_r2
36
+ end
37
+
38
+ # @return [String] linha folha calculo formatada
39
+ def row_r1
40
+ format(R1, v3: row[2], v4: row[3], v5: row[4], v6: row[5])
41
+ end
42
+
43
+ # @return [String] linha folha calculo formatada
44
+ def row_r2
45
+ format(R2, v7: row[6].to_f, v8: row[7])
65
46
  end
66
47
 
67
48
  # @return [String] linha folha calculo existente
68
49
  def row_existente
69
- d = apaga ? sql_delete : 0
70
- row_str + " EXISTENTE#{d.zero? ? '' : ' APAGADA'}"
50
+ d = linha[:e] ? sql_delete : 0
51
+ row_str + ' EXISTENTE' + str_apagadas(d)
52
+ end
53
+
54
+ # @return [String] linha folha calculo existencia multipla
55
+ def row_multiplas
56
+ d = linha[:m] ? sql_delete : 0
57
+ row_str + ' MULTIPLAS ' + sql.count.to_s + str_apagadas(d)
58
+ end
59
+
60
+ # @param [Integer] numero linhas apagadas
61
+ # @return [String] texto formatado linhas apagadas
62
+ def str_apagadas(num)
63
+ num.positive? ? ' & ' + num.to_s + ' APAGADA(S) ' : ' '
71
64
  end
72
65
 
73
66
  # @return [Boolean] linha folha calculo nao existe no bigquery?
74
- def rnaoexiste?
67
+ def row_naoexiste?
75
68
  sql.count.zero?
76
69
  end
77
70
 
78
71
  # @return [Boolean] linha folha calculo existe no bigquery?
79
- def rexiste?
72
+ def row_existe?
80
73
  sql.count == 1
81
74
  end
82
75
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Eost
4
- VERSION = '0.1.5'
4
+ VERSION = '0.1.6'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eost
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hernâni Rodrigues Vaz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-04-16 00:00:00.000000000 Z
11
+ date: 2020-04-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler