1 2 3 4 5 | source 'https://rubygems.org' gem 'sinatra' gem 'ibm_db' gem 'googlecharts' gem 'rack' |
1 | bundle install |
1 2 3 4 5 6 7 8 9 | require 'rubygems' require 'sinatra' require 'ibm_db' require 'json' require 'gchart' Tilt.register Tilt::ERBTemplate, 'html.erb' #Set template engine as erb for Sinatra get '/' do erb :index end |
1 2 3 | <html> <h1>Hello Sinatra</h1> </html> |
1 2 | require './bluaccl' run Sinatra::Application |
1 | $ rackup |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #Parse VCAP_SERVICES to get BluAcceleration Service credentials if(ENV['VCAP_SERVICES']) # we are running inside PaaS, access database details from VCAP_Services $db = JSON.parse(ENV['VCAP_SERVICES'])["BLUAcceleration-10.5.3 Rev. A Build 0.1"] $credentials = $db.first["credentials"] $host = $credentials["host"] $username = $credentials["username"] $password = $credentials["password"] $database = "SAMPLEDB" #use $credentials["database"] to point to your Database from VCAP_SERVICES. # In this example we are going against the BLUAcceleration SAMPLEDB database $port = $credentials["port"] else # we are running local, provide local DB credentials $host = "localhost" $username = "bludbuser" $password = "password" $database = "SAMPLEDB" $port = 50000 end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | require 'ibm_db' def getDataFromDW #Connect to database using parsed credentials conn = IBM_DB.connect "DATABASE=#{$database};HOSTNAME=#{$host}ORT=#{$port}ROTOCOL=TCPIP;UID=#{$username}WD=#{$password};", '', '' #Run the analytic SQL stmt = IBM_DB.exec conn, $profitAnalysisSQL data = {} while(res = IBM_DB.fetch_assoc stmt) if data.has_key?(res['PRODUCT']) data[res['PRODUCT']][res['YEAR']] = res['PROFIT'] else profit = {} profit[res['YEAR']] = res['PROFIT'] data[res['PRODUCT']] = profit end end IBM_DB.close conn return data end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | def renderBarGraph data array2011 = [] #Array group that contains profits for Brands respectively for year 2011 array2012 = [] array2013 = [] productNames = [] #Render a Bar chart that shows profits of each Product Brand in comparison year-to-year data.each do |product,profitHash| productNames << product profitHash.each do |year, profit| if(year == 2011) array2011 << profit elsif (year == 2012) array2012 << profit else array2013 << profit end if(profit > max) max = profit end end end #Render the Bar chart using the gchart library and return the img html tag for display Gchart.bar( :title => "rofit by Product Brand", :data => [array2011, array2012, array2013], :background => 'efefef', :chart_background => 'CCCCCC', :bar_colors => '0000DD,00AA00,EE00EE', :stacked => false, :size => '600x400', :bar_width_and_spacing => '15,0,30', :legend => ['2011', '2012','2013'], :axis_with_labels => 'x,y', :axis_labels => [productNames.join('|'), [0,(max/2).to_f,max.to_f].join('|')], #:format => 'file', :filename => 'custom_filename.png') #To save to a file :format => 'image_tag',:alt => "rofit by brand img") #To be rendered as an image on web page end |
1 2 | Set the cf target and then deploy your application to Bluemix with the db2rubybuildpack. $ cf push bluaccl -b https://github.com/ibmdb/db2rubybuildpack |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |