Skip to content

Instantly share code, notes, and snippets.

View mmasashi's full-sized avatar

Masashi mmasashi

View GitHub Profile
@mmasashi
mmasashi / dump_mysql_proc_info.sh
Created May 27, 2015 22:38
Dump mysql process info and innodb status every 30 seconds
#!/bin/bash
MYSQL_HOST=""
MYSQL_USER=""
MYSQL_PASSWORD=""
MYSQL_DB=""
CMD_INTERVAL=30 # 30sec
dump_info() {
echo "==== $(date)"
# decode_buf_timestamp('522bedf5152a281c') => 2015-10-23 05:27:49 UTC
def decode_buf_timestamp(str)
Time.at((str.to_i(16) >> 12) / (1000 * 1000)).utc
end
git ls-files | xargs -n1 git --no-pager blame -f -w|grep <git name> |wc -l
require 'msgpack'
require 'json'
fpath = ARGV[0]
io = if fpath
File.open(fpath)
else
$stdin
end
u = MessagePack::Unpacker.new(io)
@mmasashi
mmasashi / change_tz.rb
Created April 29, 2015 23:59
How to chnage the timezone of ruby Time object
require 'tzinfo'
require 'date'
TIMEZONES = {
'us-east-1' => 'US/Eastern',
'us-west-1' => 'US/Pacific',
'us-west-2' => 'US/Pacific',
'eu-west-1' => 'GB',
'ap-southeast-1' => 'Singapore',
'ap-northeast-1' => 'Japan',
@mmasashi
mmasashi / encode_for_redshift.rb
Created April 28, 2015 23:45
Encode string for redshift
class RedshiftString
# Redshift supports UTF-8 but it enforces stricter rule than other
# implementations such as MySQL or Ruby. This method returns a
# Redshift-safe string from the given string.
def self.encode(string, options = {})
result = string.encoding == Encoding::UTF_8 ? string.encode(Encoding::UTF_16, options).encode(Encoding::UTF_8) : string.encode(Encoding::UTF_8, options)
result.each_char.collect{|c|
# Per Redshift document
# http://docs.aws.amazon.com/redshift/latest/dg/multi-byte-character-load-errors.html
if c >= "\uFDD0" && c <= "\uFDEF" || c == "\uFFFE" || c == "\uFFFF"
@mmasashi
mmasashi / load_submodule_models.rb
Created April 22, 2015 23:27
Load Rails submodule models with different database.
# rails_root/config/initializers/load_submodule_models.rb
RAILS_MODEL_LIST = %w(
Account
Plan
User
UserPreference
)
def apply_db_change(class_name)
@mmasashi
mmasashi / detect_boost_ver.cpp
Created April 17, 2015 22:00
How to detect boost lib version
#include <boost/version.hpp>
#include <iostream>
int main()
{
std::cout << BOOST_LIB_VERSION << std::endl;
return 0;
}
@mmasashi
mmasashi / append_load_path.rb
Created April 3, 2015 20:48
Append a new load path to the head of $LOAD_PATH if not exists.
def append_load_path_if_not_exist(new_lib_path)
absolute_path = File.realpath(new_lib_path)
$LOAD_PATH.unshift absolute_path unless $LOAD_PATH.include? absolute_path
end
another_lib_dir = File.realpath('../../lib', __FILE__)
append_load_path_if_not_exist(another_lib_dir)
@mmasashi
mmasashi / num_commits_per_day.sql
Created December 2, 2014 23:32
Number of commits per day (Amazon Redshift)
select trunc(startwork), count(distinct xid) from stl_commit_stats where xid > 0 group by trunc(startwork) order by 1;