Time Wasters

Because you can’t be productive all the time


Transfer files to FTP via Rails console

First require Net::FTP so the functions will be available

1
require 'net/ftp'

Create FTP connection, with a custom port

1
2
3
4
ftp = Net::FTP.new
ftp.connect('url', port) 
ftp.login('username', 'password')
ftp.passive = true

Move to the folder you want to upload the file into

1
ftp.chdir('folder/')

Get URL of paperclip file

1
url = 'public'+paperclip.photo.url.gsub(/\?.*/, '')

Upload to server

1
ftp.putbinaryfile(url)

Close connection

1
ftp.quit()

Permalink » No comments

Fastest way to search an object

Same deal as my previous post, I want to pin down the fastest method to use to search the parameter values of an object in Javascript using https://jsperf.com.  I found 4 different ways to receive the same result, this isn’t an exhaustive list but features some of the most common methods used for searching objects.

Between multiple tests findIndex and find did the best, but find was faster more often then findIndex.

obj_search

 

 

Permalink » No comments

Exporting data to CSV in rails console

I’ve had a similar post to this in the past where I exported an entire table to CSV, but what if you want to export only a subset of that data? Well, that’s what this post is for :)

  1. Start with a query that narrows things down as much as possible
  2. Then loop through those results to apply any additional filtering you need
  3. Finally generate the CSV file with a header
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
date = Date.new(2016,12,15)
l = Table.where("created_at > ? AND column != 'variable'", date)
 
ActiveRecord::Base.logger = nil
results = []
l.each do |i|
 u = User.where(:email => i.email)
 if u.present?
   if Table.where(:email => u[0].email).blank?
     results << {:email=>i.email,:member=>'y'}
   end
 else
   results << {:email=>i.email,:member=>'n'}
 end
end;0
 
 
require 'csv'
 
file = "#{Rails.root}/public/data.csv"
 
CSV.open( file, 'w' ) do |writer|
 writer << results.first.map { |a,v| a }
 results.each do |s|
 writer << s.map { |a,v| v }
 end
end
 
 

Permalink » No comments

simple-zoom

Just finished pushing a very simple zoom app in GitHub, I couldn’t find a nice script that would allow the image to take up the full size of it’s parent element when zoomed in, and would handle updating the image dynamically.  So I wrote one for myself.

Get the files on GitHub.

See a preview:

Permalink » No comments

Monitoring & Understanding Server Load

I have a site whose performance could stand some improvement.  I want to be able to make sure that any changes I make are actually having an impact, so I’m going to start with setting up some monitoring tools. I found a very nice and free monitoring application called Load Average. After I got that all set I did a bit of research so I can fully understand and make the most out of the numbers I see in there.

CPU LOAD

First off, I needed to get a handle on how CPU Load is measured. If you run “cat /proc/loadavg” on a Linux server you will get a string similar to this:

1
0.02 0.03 0.00 1/437 21084

The first three numbers are the average CPU Load over 1 minutes, 5 minutes, and 15 minutes.

The lower the numbers the better, and the highest it should be is equal to the number of cores you have.  With one core it would be bad if it went over 1, but if you had 8 cores then a value of 1 wouldn’t be a problem, but 8.5 would.  To find out how many cores you have you can run “nproc.”

MEMORY USAGE

Check how much memory you’re using by running “free -m.” You’ll get a readout like this:

1
2
3
4
             total       used       free     shared    buffers     cached
Mem:         11909      10785       1124          1        234       9372
-/+ buffers/cache:       1178      10731
Swap:            0          0          0

The number you want to watch out for here is the used buffer/cache, here it’s “1178.”  That’s the memory that’s being used by the applications currently running on your server.  That number should be less then the total memory + swap memory, here’s that’s “11909.”

 

References:

http://stackoverflow.com/questions/11987495/linux-proc-loadavg
http://blog.scoutapp.com/articles/2009/07/31/understanding-load-averages
http://serverfault.com/questions/67759/how-to-understand-the-memory-usage-and-load-average-in-linux-server
http://www.cyberciti.biz/faq/linux-get-number-of-cpus-core-command/

Permalink » No comments