Workdesk to stack research work on Security and/or Organization items
[smr.git] / gui / lib / tasks / smr.rake
blob226cf7f1fc548f511851b1b5094fd6a00fd7291b
1 require 'security_identifiers'
3 namespace :smr do
4   desc "Run quote update cycle. Useful for scheduling, ie with cron."
5   task :update_quotes => :environment do
6       require 'fetch_quote_job'
8       Security.where(:fetch_quote=>true).each do |s|
9           next if s.has_trading_ceased? or s.is_expired?
10           Smr::FetchQuoteJob.perform_later(s)
11       end
12   end
14   desc "Create Securities from ISIN symbols in :file."
15   task :import_symbols, [:file] => :environment do |t, args|
16       symbols, item, found, imported, known, expired, invalid, no_metadata = 0, 0, 0, 0, 0, 0, 0, 0
17       stop_each = rand 1402
19       unless args[:file]
20         puts 'Usage: rake smr:import_symbols[/path/to/file]'
21         exit 1
22       end
24       matches = File.open(args[:file], 'r').read.scan(/[A-Z]{2}[A-Z0-9]{9}\d{1}?/)
25       symbols = matches.count
27       matches.each do |m|
28         puts '%i/%i => %s' % [item, symbols, m]
29         item+=1
31         unless SecurityIdentifiers::ISIN.new(m).valid?
32             puts '  ISIN code invalid, skipping.'; invalid+=1
33             next
34         end
35         # use original string until https://github.com/invisiblelines/security_identifiers/issues/5 is solved
36         isin = m
38         puts "--- sleep #{sleep rand(300) } --- " if item % stop_each == 0
40         if Security.where(:symbol=>isin).count == 0
41             s = Security.new :symbol=>isin
42             if s.fetch_metadata
43                 puts '  found %s' % s; found+=1
44                 if s.is_expired?
45                     puts '  is expired, dropping.'; expired+=1
46                     s.destroy
47                 else
48                     s.description = 'created by smr:import_symbols'
49                     s.save!
50                     imported+=1
51                 end
52             else
53                 puts '  no metadata found, dropping.'; no_metadata+=1
54                 s.destroy
55                 next
56             end
57         else puts '  is known, skipping.'; known+=1 end
58 #Security.where(:symbol=>isin).destroy_all
59       end
61       puts
62       puts 'stats: symbols=%i invalid_symbols=%i found=%i not_found=%i skipped_known=%i skipped_expired=%i imported=%i' % [
63         symbols, invalid, found, no_metadata, known, expired, imported
64       ]
65   end
67   desc 'Create fixtures for testing from data in an existing database. Defaults to development database.  Set RAILS_ENV to override.'
68   task :extract_fixtures => :environment do
69     sql  = 'SELECT * FROM `%s`'
70     skip_tables = ['schema_info']
71     ActiveRecord::Base.establish_connection
72     (ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|
73       i = '000'
74       File.open("#{Rails.root}/test/fixtures/#{table_name}.yml", 'w') do |file|
75         data = ActiveRecord::Base.connection.select_all(sql % table_name)
76         file.write data.inject({}) { |hash, record|
77           hash["#{table_name}_#{i.succ!}"] = record
78           hash
79         }.to_yaml
80       end
81     end
82   end
83 end