Upgrade to Pro — share decks privately, control downloads, hide ads and more …

雑につくるKPIツールのススメ

TAKAHASHI Kazunari
March 28, 2015
430

 雑につくるKPIツールのススメ

TAKAHASHI Kazunari

March 28, 2015
Tweet

Transcript

  1. ࢲ͸…. • @1syo • Rails Programmer • Yokohama.rb • 2012

    Ruby kaja award winner • MinatoRubyKaig01 organizer • TDDBC Yokohama TA
  2. #! /bin/sh! host=localhost! database=db! user=dbuser! password=password! ! from_date=`date --date '1

    days ago' +\%Y-\%m-\%d`! to_date=`date +\%Y-\%m-\%d`! ! [email protected]! [email protected]! subject=counter! bound=`date +"%Y%m%d%H%M%S"`! filename="filename_$from_date.csv"! ! sql="SELECT * FROM users WHERE created_at BETWEEN $from_date AND $to_date"! result=`mysql -u$user -p$password -h$host $database - e"$sql" | sed 's/\t/","/g;s/^/"/;s/$/"/;s/\r\n//g'`!
  3. /usr/sbin/sendmail -t -i <<__MAIL__! From: "$from" ! To: "$to"! Subject:

    "$subject"! MIME-Version: 1.0! Content-Type: multipart/mixed; boundary=\"---------"$bound"\"! ! -----------"$bound"! Content-Type: Text/Plain; charset=ISO-2022-JP! Content-Transfer-Encoding: 7bit! ! Now: "$from_date"! ! --! `hostname`! "$from"! ! -----------"$bound"! Content-Type: application/octet-stream! name="$filename"! Content-Transfer-Encoding: base64! Content-Disposition: attachment;! filename="$filename"! ! `echo -e "$result" | base64`! ! -----------"$bound"--! __MAIL__
  4. module KPI! module Summary! class SalesReport! def client! @client ||=

    Mysql2::Client.new(! host: Settings.kpi.database.host,! database: Settings.kpi.database.xxxxxxx,! username: config['username'],! password: config['password']! )! end! ! ! ### தུ ####! def collect! sql = <<-__SQL__! select *! from (#লུ...) salse_reports! where placed_at >= '#{start_at}'! and placed_at < '#{end_at}'! order by placed_at! __SQL__! client.query(sql)! end! ! def rows! @rows ||= collect.map { |row| row.values }! end! ! def save! spreadsheet = KPI::GoogleDrive::Collection.new.spreadsheet(@start_at)! sheet = spreadsheet.worksheet(:sales_report, rows)! sheet.clear! sheet.save! end! end! end! end