Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
208 lines (170 sloc) 5.99 KB
require 'rubygems'
require 'active_record'
require 'yaml'
require 'logger'
require 'csv'
require 'builder'
require 'sinatra'
require 'json'
ROOT = File.dirname(__FILE__)
configure :production, :test do
services = JSON.parse(ENV['VCAP_SERVICES'])
puts services.inspect
mysql_key = services.keys.select { |svc| svc =~ /mysql/i }.first
mysql = services[mysql_key].first['credentials']
@dbconfig = {:adapter => 'mysql2', :database => mysql['name'], :host => mysql['hostname'], :port => mysql['port'],
:username => mysql['user'], :password => mysql['password']}
#@@client = Mysql2::Client.new mysql_conf
end
configure :development do
@dbconfig = YAML::load(File.open('config/database.yml'))
end
configure do
ActiveRecord::Base.establish_connection(@dbconfig)
#ActiveRecord::Base.logger = Logger.new(STDERR)
ActiveRecord::Migration.verbose = true
ActiveRecord::Migrator.migrate("db/migrate")
Dir.mkdir(File.join(ROOT, "public", 'masterexport')) unless File.directory? File.join(ROOT, "public", 'masterexport')
end
class ActiveRecordClearer
def initialize(app)
@app = app
end
def call(env)
response = @app.call(env)
ActiveRecord::Base.clear_active_connections!
response
end
end
use ActiveRecordClearer
class MasterTicket < ActiveRecord::Base
attr_accessible :id, :package, :category, :license, :target, :resolution, :summary
end
class UseTicket < ActiveRecord::Base
attr_accessible :id, :assignee, :category, :status, :resolution, :summary
end
get '/' do
@masters = []
@uses = []
erb :index
end
post '/search' do
params.inspect
@masters = MasterTicket.where("summary like ? ", '%' +params['search'] + '%').order('id').all
@masters.inspect
@uses = UseTicket.where("summary like ? ", '%' +params['search'] + '%').order('id').all
@uses.inspect
erb :master
end
get "/upload" do
erb :upload
end
delete "/master" do
ActiveRecord::Base.connection.execute("TRUNCATE TABLE master_tickets")
status 200
end
post "/master/upload" do
ActiveRecord::Base.connection.execute("TRUNCATE TABLE master_tickets")
i = 0
CSV.foreach(params['csvfile'][:tempfile], :quote_char => '"', :col_sep =>',', :row_sep =>:auto) do |row|
if row[0].to_i > 0
MasterTicket.new(:id=>row[0],:category => row[1], :target => row[2], :license => row[3], :resolution => row[4], :summary =>row[5]).save
i = i+1
end
end
return "The file was successfully uploaded! #{i} records"
end
get "/upload_use" do
erb :upload_use
end
post "/upload_use" do
ActiveRecord::Base.connection.execute("TRUNCATE TABLE use_tickets")
i = 0
CSV.foreach(params['csvfile'][:tempfile], :quote_char => '"', :col_sep =>',', :row_sep =>:auto) do |row|
if row[0].to_i > 0
UseTicket.new(:id=>row[0],:assignee => row[1], :status => row[2], :resolution => row[3], :summary =>row[4], :category => row[5]).save
i = i+1
end
end
return "The file was successfully uploaded! #{i} records"
end
get "/parselocal" do
start = Time.now
ActiveRecord::Base.connection.execute("TRUNCATE TABLE master_tickets")
i = 0
CSV.foreach("master.csv", :quote_char => '"', :col_sep =>',', :row_sep =>:auto) do |row|
#csv_data[row[3].strip] = row
if row[0].to_i > 0
MasterTicket.new(:id=>row[0],:category => row[1], :target => row[2], :license => row[3], :resolution => row[4], :summary =>row[5]).save
i = i+1
else
STDERR.puts "Incorrect row in CSV:" + row.inspect
end
end
STDERR.puts "Processing time:" + (Time.now - start).to_s
end
get "/upload_masterfile" do
res = ""
@files = Dir.foreach(File.join(ROOT, "public", 'masterexport')).sort
#@files.sort!
erb :upload_masterfile
end
post "/upload_masterfile" do
i = 0
type = params['type']
file = File.new(File.join(ROOT, "public", 'masterexport', Time.now.strftime("%Y-%m-%d %H %M %S") + "_" + type + ".xml"), "w")
#xml output
xml = Builder::XmlMarkup.new( :indent => 2, :target => file )
xml.instruct! :xml, :encoding => "UTF-8"
if type == 'master'
#parse master tickets
xml.packages do |ps|
CSV.foreach(params['csvfile'][:tempfile], :quote_char => '"', :col_sep =>',', :row_sep =>:auto) do |row|
if row[0]!= "Package Name"
ps.package do |p|
p.name row[0]
p.version row[1] unless row[1].empty?
p.license_name row[2] unless row[2].empty?
p.encryption (row[3] == "yes").to_s unless row[3].empty?
p.is_modified (row[4] == "yes").to_s unless row[4].empty?
p.description row[5] unless row[5].empty?
p.oss_project row[6] unless row[6].empty?
p.source_url row[7] unless row[7].empty?
p.dist_type row[8] unless row[8].empty?
end
end
i = i+1
end
end
else
#parse use requests
xml.use_requests do |ps|
CSV.foreach(params['csvfile'][:tempfile], :quote_char => '"', :col_sep =>',', :row_sep =>:auto) do |row|
if row[0]!= "Package Name"
ps.use_request do |p|
p.package_name row[0]
p.package_version row[1] unless row[1].empty?
p.package_category row[2] unless row[2].empty?
p.interaction row[3] unless row[3].empty?
p.description row[4] unless row[4].empty?
p.modified (row[5] == "TRUE").to_s unless row[5].empty?
end
end
i = i+1
end
end
end
#puts xml.to_s
file.close
redirect to('/upload_masterfile')
end
helpers do
def cycle
%w{even odd}[@_cycle = ((@_cycle || -1) + 1) % 2]
end
CYCLE = %w{even odd}
def cycle_fully_sick
CYCLE[@_cycle = ((@_cycle || -1) + 1) % 2]
end
end
ActiveRecord::Base.connection.close