Skip to content

Instantly share code, notes, and snippets.

@snobutaka
Last active July 6, 2017 23:24
Show Gist options
  • Save snobutaka/9c13aade79829ee3397895598eb4721e to your computer and use it in GitHub Desktop.
Save snobutaka/9c13aade79829ee3397895598eb4721e to your computer and use it in GitHub Desktop.
misc tool to edit csv columns
# coding: utf-8
require 'csv'
class Contractors
HEAD = BasicObject.new
class << HEAD
def calc(values)
values[0]
end
end
SUM = BasicObject.new
class << SUM
def calc(values)
values.map{|v| v.to_i}.sum.to_s
end
end
end
class CSVColumn
def initialize(pattern, calculator)
@pattern = pattern
@calculator = calculator
end
def matches?(name)
@pattern === name
end
def calc(values)
@calculator.calc(values)
end
def inspect
@pattern
end
end
class CSVContractor
def initialize(output_columns)
@output_columns = output_columns
@indices_of_columns = Hash.new { |hash, key| hash[key] = [] }
end
def decide_column_indices(headers)
# ヘッダ行を読んで各カラムのインデックスを決める
headers.each_with_index do |header, index|
@output_columns.each do |column|
if column.matches?(header)
@indices_of_columns[column].push(index)
break
end
end
end
end
def process_single_line(line)
# 与えられた 1 行を処理して標準出力する
values = Hash.new { |hash, key| hash[key] = [] }
line.each_with_index do |val, index|
@output_columns.each do |column|
if @indices_of_columns[column].include?(index)
values[column].push(val)
break
end
end
end
puts @output_columns.map{ |col| col.calc(values[col])}.join(",")
end
def process(path2csv)
# CSV を処理して標準出力する
csv = CSV.read(path2csv)
decide_column_indices(csv.first)
csv.drop(1).each do |line|
process_single_line(line)
end
end
end
Archive Server CPU Web Server CPU Solr CPU PostgreSQL1 CPU PostgreSQL2 CPU PostgreSQL3 CPU Archive Server Mem Web Server Mem Solr Mem PostgreSQL1 Mem PostgreSQL2 Mem PostgreSQL3 Mem
1 2 3 4 5 6 7 8 9 10 11 12
require './csv_contractor.rb'
output_columns = [
as_cpu = CSVColumn.new("Archive Server CPU", Contractors::HEAD),
as_mem = CSVColumn.new("Archive Server Mem", Contractors::HEAD),
web_cpu = CSVColumn.new("Web Server CPU", Contractors::HEAD),
web_mem = CSVColumn.new("Web Server Mem", Contractors::HEAD),
solr_cpu = CSVColumn.new("Solr CPU", Contractors::HEAD),
solr_mem = CSVColumn.new("Solr Mem", Contractors::HEAD),
pgsql_cpu = CSVColumn.new(/PostgreSQL\d* CPU/, Contractors::SUM),
pgsql_mem = CSVColumn.new(/PostgreSQL\d* Mem/, Contractors::SUM)
]
csv_contractor = CSVContractor.new(output_columns)
csv_contractor.process("./test.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment