This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Articleがhas_many :commentsな時に、 | |
# Articleの一覧といっしょに、それぞれにcommentsが存在するかどうかの情報がほしい | |
# これだとN+1になる | |
articles = Article.all | |
articles.each do |article| | |
commented = article.comments.exists? | |
end | |
# N+1にはならないけど不要なcommentsが全てロードされてなんかムダっぽい |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use Data::Dumper; | |
my $num = 100; | |
for my $i ( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/ruby | |
require 'time' | |
require 'pp' | |
require 'elasticsearch' | |
def time2index(time); time.utc.strftime('logstash-%Y.%m.%d') end | |
today = Date.today | |
start_time = (today - 1).to_time |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://d.hatena.ne.jp/<USERNAME>/archiveにアクセスて50日ずつのページ数がいくつあるか確認しておく・・・ | |
USERNAME=mikeda | |
PAGES=5 | |
for((i=0;$i<$((50*PAGES));i+=50));do curl -g "http://d.hatena.ne.jp/${USERNAME}/archive?of=${i}" > archive_${i}.html;done | |
cat archive_*.html | grep "archive archive-section" | perl -lne 'm|'${USERNAME}'/(\d{4})\d{4}/| or next;print $1' | sort | uniq -c | |
# 44 2008 | |
# 88 2009 | |
# 26 2010 | |
# 40 2011 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 一定時間で閾値以上の件数があるkeyを検出する | |
# config: | |
# <store> | |
# type bot_detector | |
# tag bot_count | |
# count_interval 300 | |
# count_key uid | |
# threshold 10000 | |
# limit 10 | |
# </store> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# muninのmysql_プラグインで複数のRDSを監視するために、ゴニョる | |
# | |
# 事前手順 | |
# 1. munin.confにvirtual nodeを追加する | |
# [rds;rds-test] | |
# address 127.0.0.1 | |
# use_node_name no | |
# 2. 処理を移譲するためのプラグインを作成しておく | |
# mkdir -p /tmp/munin/plugins/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'aws-sdk' | |
$config_file = File.join(File.dirname(__FILE__), "../../config/aws/aws.yml") | |
$config = YAML.load(File.read($config_file)) | |
AWS.config( | |
access_key_id: $config['access_key_id'], | |
secret_access_key: $config['secret_access_key'], | |
region: $config['region'] | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# EC2のインスタンスを作成 | |
# VPC | |
# インスタンスはEBSタイプ | |
# 追加EBSをcreate & attach & mount | |
# EIPをallocate & associate | |
# インスタンス、EBSボリュームにはNameタグをつける | |
# 使用例 | |
# $ ruby run_instance.rb app01 m1.small 10.0.0.101 30 | |
require 'aws-sdk' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/ruby | |
# アクセスログを1時間ごとに集計。アクセスタイプごとのアクセス数をTSV形式で出力する | |
# usage : analyze_access_log.rb <YYYYMMDD> <access_log>... | |
require 'json' | |
require 'time' | |
require 'pp' | |
ANALYZE_DATE = ARGV.shift # YYYYMMDD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/ruby | |
require 'json' | |
result = {} | |
cur_ks = nil | |
cur_cf = nil | |
ARGF.each_line do |line| | |
line.strip! | |
if line =~ /^Keyspace: (.+)/ |
NewerOlder