Class: Calicli::CLI::App

Inherits:
Thor
  • Object
show all
Defined in:
lib/calicli/cli.rb

Instance Method Summary collapse

Instance Method Details

#blocksObject



41
42
43
44
45
46
47
48
49
# File 'lib/calicli/cli.rb', line 41

def blocks
  puts "Blocks: #{Block.count}"
  Block.find_each do |b|
    puts "#{b.iID_BLOQUE} => #{b.sDESCRIPCION}"
    b.areas.each do |a|
      puts "        => #{a.sDESCRIPCION} : area=#{a.dAREA}, dist=#{a.dDISTANCIA}"
    end
  end
end

#clean_storageObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/calicli/cli.rb', line 19

def clean_storage
  say "This is a destructive action. Type CLEAN to confirm your action"
  clean = ask("Type CLEAN: ")
  clean = clean.upcase
  if clean.eql?("CLEAN")
    QuickStore.store.delete(:harvest_id)
    QuickStore.store.delete(:run_number)
    say "Done", :green
  else
    say "No action.", :red
  end
end

#harvestObject



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
# File 'lib/calicli/cli.rb', line 81

def harvest
  date = options[:date]
  batch_size = options[:batch_size]
  last_processed = QuickStore.store.get(:harvest_id)
  run_number = options[:run] || QuickStore.store.get(:run_number) || 1
  by_run = options[:by_run]
  puts "date=#{date}, by_run=#{by_run}, run_number=#{run_number}, last_processed=#{last_processed}".green
  #{date: date, number: number, runner: runners.sample, cable: cable, arrive_at: arrive_at}
  arel = Harvest
  if date.present?
    date = Time.parse(date).utc
    puts "date=#{date.beginning_of_day} .... #{date.end_of_day}"
    arel = arel.where(dtFECHA: date.beginning_of_day..date.end_of_day)
  else last_processed
    arel  = arel.where("iID_ENTREGA > ?", last_processed)
  end
  
  if by_run
    arel = arel.where(iCARRERA: run_number)
  end
  
  data = []
  last_id = nil
  last_run = nil
  arel.each do |h|
    
    x = {date: h.dtFECHA.beginning_of_day, arrive_at: h.dtFECHA, number: h.iCARRERA, runner: h.user.sNOMBRE, cable: h.area.sDESCRIPCION,  color: h.ribbon.sCOLOR.downcase, weight: h.dPESO, ref: h.iID_ENTREGA}
    e = h.evaluations.first
    if e
      x[:evaluation] = {}
      x[:evaluation][e.property.sDESCRIPCION_CORTA] = e.sDATO
    end
    data << x
    last_id = h.iID_ENTREGA
    last_run = h.iCARRERA
  end
  
  
  if data.any?
    begin
      url = Calicli.config.erp_api_base + "/data"
      resp = RestClient.post url, {data: data, data_type: "harvest"}.to_json, {content_type: :json, accept: :json, 'x-auth-token' => Calicli.config.erp_api_token}
      if options[:store]
        QuickStore.store.set(:harvest_id, last_id)
        QuickStore.store.set(:run_number, (last_run + 1))
      end
      puts "OK".green
      puts "last_id=#{last_id}, run_number=#{last_run}".green
    rescue => e
      puts "Error: #{e.message}".red
    end
  else
    puts "No new data"
  end
  
  
end

#infoObject



9
10
11
12
13
14
15
16
# File 'lib/calicli/cli.rb', line 9

def info
  puts "CaliCli #{Calicli::VERSION}"

  harvest_id = QuickStore.store.get(:harvest_id)
  run_number =QuickStore.store.get(:run_number)
  puts "Storage: harvest_id=#{harvest_id}, run_number=#{run_number}"
  puts "Connection: harvest_count=#{Harvest.count}"
end

#ribbonsObject



33
34
35
36
37
38
# File 'lib/calicli/cli.rb', line 33

def ribbons
  puts "Ribbons: #{Ribbon.count}"
  Ribbon.all.each do |r|
    puts "#{r.iID_CINTA} => #{r.sCOLOR}"
  end        
end

#sample_harvestObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/calicli/cli.rb', line 53

def sample_harvest
  harvests = Harvest.where(iCARRERA: 1).limit(30)
  harvests.each do |h|
    puts "Entrega: #{h.iID_ENTREGA}"
    puts "Viaje: #{h.iCARRERA}"
    puts "Carrero: (#{h.user.iID_USUARIO}) #{h.user.sNOMBRE}"
    puts "Capataz: (#{h.boss.iID_USUARIO}) #{h.boss.sNOMBRE}"
    puts "Cable: (#{h.iID_AREA}) #{h.area.sDESCRIPCION}"
    puts "Fecha: #{h.dtFECHA}"
    puts "Cinta: (#{h.ribbon.iID_CINTA}) #{h.ribbon.sCOLOR}"
    puts "Peso: #{h.dPESO} kg"
    if h.evaluations.any?
      puts "Evaluaciones:"
      puts "================="
      h.evaluations.each do |e|
        puts "  #{e.property.sDESCRIPCION_CORTA}: #{e.sDATO}"
      end
    end
    puts "~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+\n"
  end
end