Skip to content

Instantly share code, notes, and snippets.

View phil-monroe's full-sized avatar

Phil Monroe phil-monroe

View GitHub Profile
@phil-monroe
phil-monroe / deps2csv.rb
Created January 14, 2014 19:45
Extract maven dependencies to a csv
data = `mvn dependency:tree`
matches = data.split(?\n).map{|l| l.match(/^\[INFO\][\s\|\+\-\\]+(.*)\:(?:jar|war)\:(.*)\:.*$/) }.compact
proj = Dir.pwd.split(?/).last
deps = matches.map{ |m| [ "#{m[1]}:#{m[2]}", m[1].split(?:).first, m[1].split(?:).last, m[2] ] }.uniq.sort
require 'csv'
CSV.open("../#{proj}-dependencies.csv", "w") do |csv|
csv << ["full name", "group id", "artifact id", "version"]
@phil-monroe
phil-monroe / fib.rb
Last active January 3, 2016 06:29
Fibonacci Sequence Generator with Dynamic Programming
require 'benchmark'
def fib(n)
raise ArgumentError, "needs to be positive! n: #{n}" if n < 0
Array.new(n, 1).tap do |seq|
(2..seq.length-1).each do |idx|
seq[idx] = seq[idx-1] + seq[idx-2]
end if n > 2
end
end
@phil-monroe
phil-monroe / hadoop-logs.sh
Last active December 23, 2015 23:09
Shell function to fetch logs after aggregation
hadoop-logs(){
hdfs dfs -cat "/var/log/hadoop-yarn/apps/$USER/logs/$1/*" | less
}
package com.identified.helpers;
import org.apache.avro.Schema;
import org.apache.avro.io.*;
import org.apache.avro.specific.SpecificDatumReader;
import org.apache.avro.specific.SpecificDatumWriter;
import org.apache.avro.specific.SpecificRecordBase;
import java.io.ByteArrayOutputStream;
@phil-monroe
phil-monroe / faye_debugger.html
Created June 19, 2013 23:42
Super simple Faye Client for debugging purposes Replace url's on line 7 and 18 to the proper endpoints
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Faye demo</title>
<script src="http://localhost:3000/assets/faye-browser.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
</head>
@phil-monroe
phil-monroe / publish_batch_worker_completed.rb
Created June 19, 2013 23:33
Sidekiq Middleware to publish to Faye for real time batch status updates
module Sidekiq
module Middleware
module Server
class PublishBatchWorkerCompleted
def call(worker, job, queue)
yield
ensure
begin
unless worker.bid.nil?
@phil-monroe
phil-monroe / 01-Faye Rails.md
Last active March 14, 2016 12:35
Embed scalable Faye into Rails app hosted with Puma, Thin, etc.

This is just a jotting of notes on how to embed Faye into a single Rails process. Makes it nice to do simple real time things without the need for a separate Faye server/process.

Also uses Faye Redis to work across load balanced Rails apps.

You also need to copy the compiled javascript into vendor/assets/javascripts and include into application.js manifest.

Ignore the numbers in the file names... just used to add order to the Gist.

This uses the faye/faye Github repo at edc5b42f6560d31eae61caf00f6765a90e1818d1 since I wanted to use with the Puma rack server and that is only available in the master branch (until Faye 1.0)

@phil-monroe
phil-monroe / pg_top.rb
Created June 18, 2013 18:58
Simple ruby script to view Postgres stats
require 'yaml'
require 'terminal-table'
loop do
# Get Running Processes
rows = %w(pid client_addr state waiting query_start query)
stats = `psql identified_production -t -c 'select #{rows.join(', ')} from pg_stat_activity order by state DESC'`
stats = stats.split("\n")
stats.map! { |s| s.split('|').map(&:strip) }
@phil-monroe
phil-monroe / recompile_gems.sh
Created April 30, 2013 21:35
Recompiles all gems that have native extensions
#!/bin/bash
bundle exec ruby -e <<RUBY
require 'rubygems/commands/uninstall_command'
require 'bundler/cli'
Gem.loaded_specs.select{|k,v| !v.extensions.empty?}.keys.map do |gem|
Gem::Commands::UninstallCommand.new
.handle_options ['-x', '-I', gem]
.execute rescue nil
@phil-monroe
phil-monroe / normalize.rb
Created April 25, 2013 18:03
Normalize a query from to just lowercase letters and single spaces
def query
@query ||= params[:query].to_s.tap do |q|
q.strip!
q.downcase!
q.gsub!(/[^a-z\s]/, '')
q.gsub!(/\s+/, ' ')
end
end