Skip to content

Instantly share code, notes, and snippets.

@deevis
deevis / gist:ba64c05f54d9639f4b3b872e584be2c4
Created February 3, 2021 19:05
Get longest running requests from log file
for t in `grep "Finished:" log/production.log | sed 's/.*]: \(.*\)s/\1/g' | sort -n | tail -n 20`; do grep $t log/production.log; done | sed 's/\(.*\) INFO.*Finished: \(.*\)/\1 \2/g'
@deevis
deevis / Restore from innodb datafiles instructions.txt
Created November 10, 2020 03:35
Restore a rails DB from ibd files in a backup folder but missing schema definition...
# Backstory - I had accidentally deleted /usr/local/mysql/data/mysql.ibd and
# as a result lost all schema information for all dbs...
#
# 1) move the db data folder someplace safe:
mv /usr/local/mysql/data/my_rails_db /backups/my_rails_db
# 2) recreate the db with the rails app and create the same (empty) schema
rake db:create
rake db:migrate
@deevis
deevis / gist:30a4779b08d6068461066a7845368b59
Created August 12, 2020 17:49
Neo4j - fun with NLP in a graph
"the only thing we have to fear is fear itself",
"get busy living or get busy dying",
"those who dare to fail miserably can achieve greatly",
"it is hard to fail , but it is worse never to have tried to succeed",
"love is a serious mental disease",
"remember that the happiest people are not those getting more but those giving more",
"the opposite of love is not hate , it is indifference",
"life is trying things to see if they work",
"the quick brown fox jumped over the lazy dog",
"you will face many defeats in life but , never let yourself be defeated",
$: (Dollar Colon) is basically a shorthand version of $LOAD_PATH. $: contains an array of paths that your script will search through when using require.
$0 (Dollar Zero) contains the name of the ruby program being run. This is typically the script name.
$* (Dollar Splat) is basically shorthand for ARGV. $* contains the command line arguments that were passed to the script.
$? (Dollar Question Mark) returns the exit status of the last child process to finish.
$$ (Dollar Dollar) returns the process number of the program currently being ran.
$~ (Dollar Tilde) contains the MatchData from the previous successful pattern match.
$1, $2, $3, $4 etc represent the content of the previous successful pattern match.
$& (Dollar Ampersand) contains the matched string from the previous successful pattern match.
$+ (Dollar Plus) contains the last match from the previous successful pattern match.
$` (Dollar Backtick) contains the string before the actual matched string of the previous successful pattern match.
@deevis
deevis / gist:7842853971b03f2c7e3aee95169326a9
Created April 10, 2020 15:55
Ping IP addresses referenced by nslookup
for ip in `nslookup 7-review-feature-ch-m7rnev.review.verisys.com | grep -v "#" | grep "Address:" | sed 's/.*: \(.*\)/\1/g'`;do echo $ip; ping -c 3 $ip | grep time; done
@deevis
deevis / rational_dates.rb
Last active March 31, 2020 15:29
Program to display a frequency histogram of rational values determined by month divided by day values over the course of the next year
# Program to display a frequency histogram of rational values
# determined by month divided by day values over the course of the next year
# For example, March 30 is 3/30 = 0.1
require 'active_support/all'
vals = (0...365).map{|x| d = x.days.from_now; [d.strftime("%m/%d"), d.strftime("%m").to_f / d.strftime("%d").to_f]}
# print the results
# vals.each{|v| puts "#{v[0]} #{v[1]}"};nil
counts = Hash.new(0); vals.each{|v| counts[v[1]] += 1}
counts.sort{|a,b| b[1] <=> a[1]}.each{|val, count| puts "#{count} ==> #{val}"};nil
@deevis
deevis / generate_gem_info.sh
Last active March 17, 2021 16:44
Build a list of Gems used by a (bundle-backed) rails project
# Build an informative list of all Gems used by the project - this will take a minute to run
echo "Calling 'gem info' for every gem in the project...patience is a virtue..."
for gem in `bundle list | sed 's/ \* \(.*\) (.*/\1/g' | grep -v " "`
do echo "--------------------------------------------------------------------"
gem info $gem | grep -v "Installed at"
done | tee gem_info.txt
echo "Building License Counts"
echo "#---#---#---#---#---#---#---#---#---#---#---#---#---#---#" | tee -a gem_info.txt
echo " Licenses " | tee -a gem_info.txt
@deevis
deevis / sierpinski.html
Created October 24, 2019 18:20
Draw a Sierpinski triangle using an HTML5 canvas and Javascript
<html>
<body>
<canvas id="myCanvas" width="1500" height="1100" style="border: 1px solid black;"></canvas>
</body>
<script>
var canvas = document.getElementById("myCanvas");
var canvasWidth = canvas.width;
var canvasHeight = canvas.height;
var ctx = canvas.getContext("2d");
var canvasData = ctx.getImageData(0, 0, canvasWidth, canvasHeight);
@deevis
deevis / gist:43bb7828e5a1e2d841792a5f240291bd
Created October 15, 2019 19:51
Find all instance of a code string and when and by whom it was last edited
for f in `find . -iname "*.rb" | xargs grep "Searchflow\.call" | sed 's/\(.*.rb\):.*/\1/g' | uniq`; do echo -e "\n\n------------------- $f -------------------------";git blame $f | grep "Searchflow\.call" | sed 's/.*(\(.*\).*\(201.-..-..\).*\(Searchflow.call(api_alias:.*\), payload.*/\1 \2 \3/g'; done
@deevis
deevis / gist:6b5c0ecf0cdc97c6baf950266fb4673a
Created October 9, 2019 21:47
Rails migrations histogram by year/month
for year in `seq 2017 2019`; do for month in `seq -f "%02g" 1 12`;do echo -n "$year$month";find db/migrate -iname "*.rb" | grep "/$year$month" | wc -l; done; done