Sinatra Oracle

Sinatra – Oracle

oracle installation for local

http://www.oracle.com/technetwork/database/database-technologies/express-edition/overview/index.html

 

Setting up sinatra with oracle database, this has eaten up my time so just thought will give a clear idea of settings.

Gemfile — add these to your gem file

# Database requires
gem ‘ruby-plsql’
gem “sinatra-activerecord”
gem ‘ruby-oci8’
gem “activerecord”
gem ‘activerecord-oracle_enhanced-adapter’

gem ‘standalone_migrations’

———————————————————

database.yml

development:
adapter: oracle_enhanced
database: data_base_name
username: user_name
password: password
host: hostname
port: 1521

Note: oracle database would be by default XE,oracle default user name would be ‘system’, host kindly check for your system listener oracle and use the same(usually it will be your system name)

 

—————————————————————

create a file in config named environment.rb

#The environment variable DATABASE_URL should be in the following format:
# =>database://{user}:{password}@{host}:{port}/path

require ‘active_record’
require ‘activerecord-oracle_enhanced-adapter’
require ‘active_record/connection_adapters/oracle_enhanced_adapter’

configure :development do

DB_CONFIG = YAML::load(File.open(‘config/database.yml’))

db = URI.parse(“jdbc:oracle://#{DB_CONFIG[‘username’]}:#{DB_CONFIG[‘password’]}@#{DB_CONFIG[‘host’]}:#{DB_CONFIG[‘port’]}/#{DB_CONFIG[‘database’]}”)
ActiveRecord::Base.establish_connection(:adapter => “oracle_enhanced”, :database => “//#{DB_CONFIG[‘host’]}/{DB_CONFIG[‘database’]}”, :username => “#{DB_CONFIG[‘username’]}”,:password => “#{DB_CONFIG[‘password’]}”)
end

—————————————————————————

Rakefile – helpful for migrations

add belowlines

require ‘active_record’
require ‘activerecord-oracle_enhanced-adapter’
require ‘sinatra/activerecord’
require ‘sinatra/activerecord/rake

—————————————————————————-

app.rb — add the following

require ‘./config/environments’

—————————————————————————-

create a file for migrations and models

and run the

rake db:migrate

and further learning would be easier to understand go through the reference.

——————————————————————————

References

http://www.oracle.com/technetwork/testcontent/rubyrails-085107.html

https://github.com/rsim/oracle-enhanced#without-rails-and-bundler

 

Foot-Traffic Analysis – Problem with Solution

Foot-Traffic Analysis

The world’s most prestigious art gallery in the world needs your help! Management wants to figure out how many people visit each room in the gallery, and for how long: this is to help improve the quality of the overall gallery in the future.

Your goal is to write a program that takes a formatted log file that describes the overall gallery’s foot-traffic on a minute-to-minute basis. From this data you must compute the average time spent in each room, and how many visitors there were in each room.

Input Description

You will be first given an integer N which represents the following N-number of lines of text. Each line represents either a visitor entering or leaving a room: it starts with an integer, representing a visitor’s unique identifier. Next on this line is another integer, representing the room index. Note that there are at most 100 rooms, starting at index 0, and at most 1,024 visitors, starting at index 0. Next is a single character, either ‘I’ (for “In”) for this visitor entering the room, or ‘O’ (for “out”) for the visitor leaving the room. Finally, at the end of this line, there is a time-stamp integer: it is an integer representing the minute the event occurred during the day. This integer will range from 0 to 1439 (inclusive). All of these elements are space-delimited.

You may assume that all input is logically well-formed: for each person entering a room, he or she will always leave it at some point in the future. A visitor will only be in one room at a time.

Note that the order of events in the log are not sorted in any way; it shouldn’t matter, as you can solve this problem without sorting given data. Your output (see details below) must besorted by room index, ascending.

Output Description

For each room that had log data associated with it, print the room index (starting at 0), then print the average length of time visitors have stayed as an integer (round down), andthen finally print the total number of visitors in the room. All of this should be on the same line and be space delimited; you may optionally include labels on this text, like in our sample output 1.

Sample Inputs & Outputs

Sample Input 1

4

0 0 I 540

1 0 I 540

0 0 O 560

1 0 O 560

Sample Output 1

Room 0, 20 minute average visit, 2 visitor(s) total

Sample Input 2

36

0 11 I 347

1 13 I 307

2 15 I 334

3 6 I 334

4 9 I 334

5 2 I 334

6 2 I 334

7 11 I 334

8 1 I 334

0 11 O 376

1 13 O 321

2 15 O 389

3 6 O 412

4 9 O 418

5 2 O 414

6 2 O 349

7 11 O 418

8 1 O 418

0 12 I 437

1 28 I 343

2 32 I 408

3 15 I 458

4 18 I 424

5 26 I 442

6 7 I 435

7 19 I 456

8 19 I 450

0 12 O 455

1 28 O 374

2 32 O 495

3 15 O 462

4 18 O 500

5 26 O 479

6 7 O 493

7 19 O 471

8 19 O 458

Sample Output 2

Room 1, 85 minute average visit, 1 visitor total

Room 2, 48 minute average visit, 2 visitors total

Room 6, 79 minute average visit, 1 visitor total

Room 7, 59 minute average visit, 1 visitor total

Room 9, 85 minute average visit, 1 visitor total

Room 11, 57 minute average visit, 2 visitors total

Room 12, 19 minute average visit, 1 visitor total

Room 13, 15 minute average visit, 1 visitor total

Room 15, 30 minute average visit, 2 visitors total

Room 18, 77 minute average visit, 1 visitor total

Room 19, 12 minute average visit, 2 visitors total

Room 26, 38 minute average visit, 1 visitor total

Room 28, 32 minute average visit, 1 visitor total

Room 32, 88 minute average visit, 1 visitor total

=====================================================================

Solution

class Base
attr_accessor :id
def initialize(arg)
@id = arg
end
def self.all
ObjectSpace.each_object(self).to_a
end
class <<self
[:id].each do |method_name|
define_method “find_or_create_by_#{method_name}” do |arg|
obj = self.all.find{|ob| ob.send(method_name).to_s == arg.to_s}
obj.nil? ? self.new(arg) : obj
end
end
end
end

class Visitor < Base
end

class Room < Base
end

class Period < Base
attr_accessor :visitor, :room, :time_in, :status, :time_out
def initialize(v,r,st,t_in)
@visitor = v.to_i
@room = r.to_i
@time_in = t_in.to_i
@status = st
end
def self.exit_period(v,r,st,t_out)
period = Period.all.find{ |p| p.visitor == v.to_i && p.room == r.to_i}
period.time_out = t_out.to_i
period.status = st
end
def self.job(inp)
inp = inp.split(” “)
visitor = Visitor.find_or_create_by_id(inp[0])
room = Room.find_or_create_by_id(inp[1])
case inp[2]
when “I”
Period.new(visitor.id,room.id,inp[2],inp[3])
when “O”
Period.exit_period(visitor.id,room.id,inp[2],inp[3])
else
puts “wrong input”
end
end

end

inp_cmd = “entry”
until inp_cmd == “exit”
inp_cmd = gets.chop
Period.job(inp_cmd) unless inp_cmd == “exit”
end

Room.all.sort_by{ |r| r.id.to_i}.each do |room|
p room.inspect
period = Period.all.find_all{ |p| p.room == room.id.to_i}
total_time = 0
period.each do |p|
time = p.time_out.to_i – p.time_in.to_i
total_time = total_time + time
end
p avg = total_time/period.count
end

Nginx – Unicorn setup for rails appliaction

————install nginx————
sudo apt-get install nginx
————start start————-
sudo service nginx start

others commands:
sudo service nginx restart
sudo service nginx stop

————install unicorn————
* add unicorn to your gem file

gem ‘unicorn’
bundle

————-setup unicorn————-

**1 add file to your application in config/unicorn.rb

cmd: vim config/unicorn.rb

**2 copy the folling template to the same file
https://github.com/defunkt/unicorn/blob/master/examples/unicorn.conf.rb

**3 edit the following changes to the same file
*1 working_directory – change the working directory to your application from root path
eg: working_directory “/home/vagrant/code/application”

*2 listen – change the listen socket to your application socket
eg: listen “/tmp/unicorn.application_name.sock”, :backlog => 64

*3 listen port – change the listening port to the port which you have open eg 8080
eg: listen 8080, :tcp_nopush => true

*4 pid – change the pid to your application pid
eg: pid “/home/vagrant/code/application_name/tmp/pids/unicorn.pid”

*5 stderr_path, stdout_path – change the log files
stderr_path “/home/vagrant/code/application_name/log/unicorn.log”
stdout_path “/home/vagrant/code/application_name/log/unicorn.log”

—————setup nginx—————–
note: change the application name to your app name

**1 open the nginx config file in vim
cmd: sudo vim /etc/nginx/sites-available/default

**2 add the following code to the file above server

upstream billing{
server unix:/tmp/unicorn.application_name.sock fail_timeout=0;
}
**3

vagrant – chef

————-vagrant 1.6.5———————
wget https://dl.bintray.com/mitchellh/vagrant/vagrant_1.6.5_x86_64.deb
sudo dpkg -i vagrant_1.6.5_x86_64.deb

—————vagrant tutorial—————
https://docs.vagrantup.com

http://www.vagrantbox.es/
—————-chef solo———————–
wget https://opscode-omnibus-packages.s3.amazonaws.com/ubuntu/12.04/x86_64/chefdk_0.3.4-1_amd64.deb
sudo dpkg -i chefdk_0.3.4-1_amd64.deb

make your cookbooks under

chef-repo/cookbooks/

—————chef tutorial——————
https://www.getchef.com/download-chef-client/
http://learn.getchef.com/ubuntu/make-your-recipe-more-manageable/

http://leopard.in.ua/2013/02/17/chef-server-getting-started-part-1/

————–chef market———————
https://supermarket.getchef.com/cookbooks?utf8=%E2%9C%93&q=mysql

Vagrant

Vagrant

Vagrant is a tool for building and distributing development environments.

Development environments managed by Vagrant can run on local virtualized platforms such as VirtualBox or VMware, in the cloud via AWS or OpenStack, or in containers such as with Docker or raw LXC.

Vagrant provides the framework and configuration format to create and manage complete portable development environments. These development environments can live on your computer or in the cloud, and are portable between Windows, Mac OS X, and Linux.

http://railscasts.com/episodes/292-virtual-machines-with-vagrant?autoplay=true

—————————————

Setup

sudo apt-get install virtualbox

sudo apt-get install vagrant

vagrant init

————-

goto vagrant file name change

config.vm.box = “stockvm”

—————-

vagrant up

vagrant ssh

Your done with the vagrant creation.

MySQL and PostgreSQL Database Setup

Setting up your production database is pretty easy. Make sure to keep in mind that you should use a different password for your production databases.

Depending on what database you want to use, follow the steps related to the database:

Installing MySQL

All you need to do in order to install MySQL is to run the following command:

sudo apt-get install mysql-server mysql-client libmysqlclient-dev

You can use the root user and password set during installation for your database or you can add a new user to MySQL.

Installing PostgreSQL

Postgres 9.3 is available in the Ubuntu repositories and we can install it like so:

sudo apt-get install postgresql postgresql-contrib libpq-dev

Next we need to setup our postgres user:

sudo su - postgres
createuser --pwprompt
exit

The password you type in here will be the one to put in your my_app/current/config/database.ymllater when you deploy your app for the first time.